154b259f6SSatya Tangirala.. SPDX-License-Identifier: GPL-2.0 254b259f6SSatya Tangirala 3abb861faSEric Biggers.. _inline_encryption: 4abb861faSEric Biggers 554b259f6SSatya Tangirala================= 654b259f6SSatya TangiralaInline Encryption 754b259f6SSatya Tangirala================= 854b259f6SSatya Tangirala 954b259f6SSatya TangiralaBackground 1054b259f6SSatya Tangirala========== 1154b259f6SSatya Tangirala 128e9f666aSEric BiggersInline encryption hardware sits logically between memory and disk, and can 138e9f666aSEric Biggersen/decrypt data as it goes in/out of the disk. For each I/O request, software 148e9f666aSEric Biggerscan control exactly how the inline encryption hardware will en/decrypt the data 158e9f666aSEric Biggersin terms of key, algorithm, data unit size (the granularity of en/decryption), 168e9f666aSEric Biggersand data unit number (a value that determines the initialization vector(s)). 1754b259f6SSatya Tangirala 188e9f666aSEric BiggersSome inline encryption hardware accepts all encryption parameters including raw 198e9f666aSEric Biggerskeys directly in low-level I/O requests. However, most inline encryption 208e9f666aSEric Biggershardware instead has a fixed number of "keyslots" and requires that the key, 218e9f666aSEric Biggersalgorithm, and data unit size first be programmed into a keyslot. Each 228e9f666aSEric Biggerslow-level I/O request then just contains a keyslot index and data unit number. 238e9f666aSEric Biggers 248e9f666aSEric BiggersNote that inline encryption hardware is very different from traditional crypto 258e9f666aSEric Biggersaccelerators, which are supported through the kernel crypto API. Traditional 268e9f666aSEric Biggerscrypto accelerators operate on memory regions, whereas inline encryption 278e9f666aSEric Biggershardware operates on I/O requests. Thus, inline encryption hardware needs to be 288e9f666aSEric Biggersmanaged by the block layer, not the kernel crypto API. 298e9f666aSEric Biggers 308e9f666aSEric BiggersInline encryption hardware is also very different from "self-encrypting drives", 318e9f666aSEric Biggerssuch as those based on the TCG Opal or ATA Security standards. Self-encrypting 328e9f666aSEric Biggersdrives don't provide fine-grained control of encryption and provide no way to 338e9f666aSEric Biggersverify the correctness of the resulting ciphertext. Inline encryption hardware 348e9f666aSEric Biggersprovides fine-grained control of encryption, including the choice of key and 358e9f666aSEric Biggersinitialization vector for each sector, and can be tested for correctness. 3654b259f6SSatya Tangirala 3754b259f6SSatya TangiralaObjective 3854b259f6SSatya Tangirala========= 3954b259f6SSatya Tangirala 408e9f666aSEric BiggersWe want to support inline encryption in the kernel. To make testing easier, we 418e9f666aSEric Biggersalso want support for falling back to the kernel crypto API when actual inline 428e9f666aSEric Biggersencryption hardware is absent. We also want inline encryption to work with 438e9f666aSEric Biggerslayered devices like device-mapper and loopback (i.e. we want to be able to use 448e9f666aSEric Biggersthe inline encryption hardware of the underlying devices if present, or else 458e9f666aSEric Biggersfall back to crypto API en/decryption). 4654b259f6SSatya Tangirala 4754b259f6SSatya TangiralaConstraints and notes 4854b259f6SSatya Tangirala===================== 4954b259f6SSatya Tangirala 508e9f666aSEric Biggers- We need a way for upper layers (e.g. filesystems) to specify an encryption 518e9f666aSEric Biggers context to use for en/decrypting a bio, and device drivers (e.g. UFSHCD) need 528e9f666aSEric Biggers to be able to use that encryption context when they process the request. 538e9f666aSEric Biggers Encryption contexts also introduce constraints on bio merging; the block layer 548e9f666aSEric Biggers needs to be aware of these constraints. 5554b259f6SSatya Tangirala 568e9f666aSEric Biggers- Different inline encryption hardware has different supported algorithms, 578e9f666aSEric Biggers supported data unit sizes, maximum data unit numbers, etc. We call these 588e9f666aSEric Biggers properties the "crypto capabilities". We need a way for device drivers to 598e9f666aSEric Biggers advertise crypto capabilities to upper layers in a generic way. 6054b259f6SSatya Tangirala 618e9f666aSEric Biggers- Inline encryption hardware usually (but not always) requires that keys be 628e9f666aSEric Biggers programmed into keyslots before being used. Since programming keyslots may be 638e9f666aSEric Biggers slow and there may not be very many keyslots, we shouldn't just program the 648e9f666aSEric Biggers key for every I/O request, but rather keep track of which keys are in the 658e9f666aSEric Biggers keyslots and reuse an already-programmed keyslot when possible. 6654b259f6SSatya Tangirala 678e9f666aSEric Biggers- Upper layers typically define a specific end-of-life for crypto keys, e.g. 688e9f666aSEric Biggers when an encrypted directory is locked or when a crypto mapping is torn down. 698e9f666aSEric Biggers At these times, keys are wiped from memory. We must provide a way for upper 708e9f666aSEric Biggers layers to also evict keys from any keyslots they are present in. 7154b259f6SSatya Tangirala 728e9f666aSEric Biggers- When possible, device-mapper devices must be able to pass through the inline 738e9f666aSEric Biggers encryption support of their underlying devices. However, it doesn't make 748e9f666aSEric Biggers sense for device-mapper devices to have keyslots themselves. 7554b259f6SSatya Tangirala 768e9f666aSEric BiggersBasic design 778e9f666aSEric Biggers============ 7854b259f6SSatya Tangirala 798e9f666aSEric BiggersWe introduce ``struct blk_crypto_key`` to represent an inline encryption key and 808e9f666aSEric Biggershow it will be used. This includes the actual bytes of the key; the size of the 818e9f666aSEric Biggerskey; the algorithm and data unit size the key will be used with; and the number 828e9f666aSEric Biggersof bytes needed to represent the maximum data unit number the key will be used 838e9f666aSEric Biggerswith. 8454b259f6SSatya Tangirala 858e9f666aSEric BiggersWe introduce ``struct bio_crypt_ctx`` to represent an encryption context. It 868e9f666aSEric Biggerscontains a data unit number and a pointer to a blk_crypto_key. We add pointers 878e9f666aSEric Biggersto a bio_crypt_ctx to ``struct bio`` and ``struct request``; this allows users 888e9f666aSEric Biggersof the block layer (e.g. filesystems) to provide an encryption context when 898e9f666aSEric Biggerscreating a bio and have it be passed down the stack for processing by the block 908e9f666aSEric Biggerslayer and device drivers. Note that the encryption context doesn't explicitly 918e9f666aSEric Biggerssay whether to encrypt or decrypt, as that is implicit from the direction of the 928e9f666aSEric Biggersbio; WRITE means encrypt, and READ means decrypt. 9354b259f6SSatya Tangirala 948e9f666aSEric BiggersWe also introduce ``struct blk_crypto_profile`` to contain all generic inline 958e9f666aSEric Biggersencryption-related state for a particular inline encryption device. The 968e9f666aSEric Biggersblk_crypto_profile serves as the way that drivers for inline encryption hardware 978e9f666aSEric Biggersadvertise their crypto capabilities and provide certain functions (e.g., 988e9f666aSEric Biggersfunctions to program and evict keys) to upper layers. Each device driver that 998e9f666aSEric Biggerswants to support inline encryption will construct a blk_crypto_profile, then 1008e9f666aSEric Biggersassociate it with the disk's request_queue. 10154b259f6SSatya Tangirala 1028e9f666aSEric BiggersThe blk_crypto_profile also manages the hardware's keyslots, when applicable. 1038e9f666aSEric BiggersThis happens in the block layer, so that users of the block layer can just 1048e9f666aSEric Biggersspecify encryption contexts and don't need to know about keyslots at all, nor do 1058e9f666aSEric Biggersdevice drivers need to care about most details of keyslot management. 10654b259f6SSatya Tangirala 1078e9f666aSEric BiggersSpecifically, for each keyslot, the block layer (via the blk_crypto_profile) 1088e9f666aSEric Biggerskeeps track of which blk_crypto_key that keyslot contains (if any), and how many 1098e9f666aSEric Biggersin-flight I/O requests are using it. When the block layer creates a 1108e9f666aSEric Biggers``struct request`` for a bio that has an encryption context, it grabs a keyslot 1118e9f666aSEric Biggersthat already contains the key if possible. Otherwise it waits for an idle 1128e9f666aSEric Biggerskeyslot (a keyslot that isn't in-use by any I/O), then programs the key into the 1138e9f666aSEric Biggersleast-recently-used idle keyslot using the function the device driver provided. 1148e9f666aSEric BiggersIn both cases, the resulting keyslot is stored in the ``crypt_keyslot`` field of 1158e9f666aSEric Biggersthe request, where it is then accessible to device drivers and is released after 1168e9f666aSEric Biggersthe request completes. 11754b259f6SSatya Tangirala 1188e9f666aSEric Biggers``struct request`` also contains a pointer to the original bio_crypt_ctx. 1198e9f666aSEric BiggersRequests can be built from multiple bios, and the block layer must take the 1208e9f666aSEric Biggersencryption context into account when trying to merge bios and requests. For two 1218e9f666aSEric Biggersbios/requests to be merged, they must have compatible encryption contexts: both 1228e9f666aSEric Biggersunencrypted, or both encrypted with the same key and contiguous data unit 1238e9f666aSEric Biggersnumbers. Only the encryption context for the first bio in a request is 1248e9f666aSEric Biggersretained, since the remaining bios have been verified to be merge-compatible 1258e9f666aSEric Biggerswith the first bio. 12654b259f6SSatya Tangirala 1278e9f666aSEric BiggersTo make it possible for inline encryption to work with request_queue based 1288e9f666aSEric Biggerslayered devices, when a request is cloned, its encryption context is cloned as 1298e9f666aSEric Biggerswell. When the cloned request is submitted, it is then processed as usual; this 1308e9f666aSEric Biggersincludes getting a keyslot from the clone's target device if needed. 13154b259f6SSatya Tangirala 1328e9f666aSEric Biggersblk-crypto-fallback 1338e9f666aSEric Biggers=================== 13454b259f6SSatya Tangirala 1358e9f666aSEric BiggersIt is desirable for the inline encryption support of upper layers (e.g. 1368e9f666aSEric Biggersfilesystems) to be testable without real inline encryption hardware, and 1378e9f666aSEric Biggerslikewise for the block layer's keyslot management logic. It is also desirable 1388e9f666aSEric Biggersto allow upper layers to just always use inline encryption rather than have to 1398e9f666aSEric Biggersimplement encryption in multiple ways. 14054b259f6SSatya Tangirala 1418e9f666aSEric BiggersTherefore, we also introduce *blk-crypto-fallback*, which is an implementation 1428e9f666aSEric Biggersof inline encryption using the kernel crypto API. blk-crypto-fallback is built 1438e9f666aSEric Biggersinto the block layer, so it works on any block device without any special setup. 1448e9f666aSEric BiggersEssentially, when a bio with an encryption context is submitted to a 145fce3caeaSChristoph Hellwigblock_device that doesn't support that encryption context, the block layer will 1468e9f666aSEric Biggershandle en/decryption of the bio using blk-crypto-fallback. 1478e9f666aSEric Biggers 1488e9f666aSEric BiggersFor encryption, the data cannot be encrypted in-place, as callers usually rely 1498e9f666aSEric Biggerson it being unmodified. Instead, blk-crypto-fallback allocates bounce pages, 1508e9f666aSEric Biggersfills a new bio with those bounce pages, encrypts the data into those bounce 1518e9f666aSEric Biggerspages, and submits that "bounce" bio. When the bounce bio completes, 1528e9f666aSEric Biggersblk-crypto-fallback completes the original bio. If the original bio is too 1538e9f666aSEric Biggerslarge, multiple bounce bios may be required; see the code for details. 1548e9f666aSEric Biggers 1558e9f666aSEric BiggersFor decryption, blk-crypto-fallback "wraps" the bio's completion callback 1568e9f666aSEric Biggers(``bi_complete``) and private data (``bi_private``) with its own, unsets the 1578e9f666aSEric Biggersbio's encryption context, then submits the bio. If the read completes 1588e9f666aSEric Biggerssuccessfully, blk-crypto-fallback restores the bio's original completion 1598e9f666aSEric Biggerscallback and private data, then decrypts the bio's data in-place using the 1608e9f666aSEric Biggerskernel crypto API. Decryption happens from a workqueue, as it may sleep. 1618e9f666aSEric BiggersAfterwards, blk-crypto-fallback completes the bio. 1628e9f666aSEric Biggers 1638e9f666aSEric BiggersIn both cases, the bios that blk-crypto-fallback submits no longer have an 1648e9f666aSEric Biggersencryption context. Therefore, lower layers only see standard unencrypted I/O. 1658e9f666aSEric Biggers 1668e9f666aSEric Biggersblk-crypto-fallback also defines its own blk_crypto_profile and has its own 1678e9f666aSEric Biggers"keyslots"; its keyslots contain ``struct crypto_skcipher`` objects. The reason 1688e9f666aSEric Biggersfor this is twofold. First, it allows the keyslot management logic to be tested 1698e9f666aSEric Biggerswithout actual inline encryption hardware. Second, similar to actual inline 1708e9f666aSEric Biggersencryption hardware, the crypto API doesn't accept keys directly in requests but 1718e9f666aSEric Biggersrather requires that keys be set ahead of time, and setting keys can be 1728e9f666aSEric Biggersexpensive; moreover, allocating a crypto_skcipher can't happen on the I/O path 1738e9f666aSEric Biggersat all due to the locks it takes. Therefore, the concept of keyslots still 1748e9f666aSEric Biggersmakes sense for blk-crypto-fallback. 1758e9f666aSEric Biggers 1768e9f666aSEric BiggersNote that regardless of whether real inline encryption hardware or 17754b259f6SSatya Tangiralablk-crypto-fallback is used, the ciphertext written to disk (and hence the 1788e9f666aSEric Biggerson-disk format of data) will be the same (assuming that both the inline 1798e9f666aSEric Biggersencryption hardware's implementation and the kernel crypto API's implementation 1808e9f666aSEric Biggersof the algorithm being used adhere to spec and function correctly). 18154b259f6SSatya Tangirala 1828e9f666aSEric Biggersblk-crypto-fallback is optional and is controlled by the 1838e9f666aSEric Biggers``CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK`` kernel configuration option. 18454b259f6SSatya Tangirala 18554b259f6SSatya TangiralaAPI presented to users of the block layer 18654b259f6SSatya Tangirala========================================= 18754b259f6SSatya Tangirala 1888e9f666aSEric Biggers``blk_crypto_config_supported()`` allows users to check ahead of time whether 1898e9f666aSEric Biggersinline encryption with particular crypto settings will work on a particular 190fce3caeaSChristoph Hellwigblock_device -- either via hardware or via blk-crypto-fallback. This function 1918e9f666aSEric Biggerstakes in a ``struct blk_crypto_config`` which is like blk_crypto_key, but omits 1928e9f666aSEric Biggersthe actual bytes of the key and instead just contains the algorithm, data unit 1938e9f666aSEric Biggerssize, etc. This function can be useful if blk-crypto-fallback is disabled. 19454b259f6SSatya Tangirala 1958e9f666aSEric Biggers``blk_crypto_init_key()`` allows users to initialize a blk_crypto_key. 19654b259f6SSatya Tangirala 1978e9f666aSEric BiggersUsers must call ``blk_crypto_start_using_key()`` before actually starting to use 198fce3caeaSChristoph Hellwiga blk_crypto_key on a block_device (even if ``blk_crypto_config_supported()`` 1998e9f666aSEric Biggerswas called earlier). This is needed to initialize blk-crypto-fallback if it 2008e9f666aSEric Biggerswill be needed. This must not be called from the data path, as this may have to 2018e9f666aSEric Biggersallocate resources, which may deadlock in that case. 20254b259f6SSatya Tangirala 2038e9f666aSEric BiggersNext, to attach an encryption context to a bio, users should call 2048e9f666aSEric Biggers``bio_crypt_set_ctx()``. This function allocates a bio_crypt_ctx and attaches 2058e9f666aSEric Biggersit to a bio, given the blk_crypto_key and the data unit number that will be used 2068e9f666aSEric Biggersfor en/decryption. Users don't need to worry about freeing the bio_crypt_ctx 2078e9f666aSEric Biggerslater, as that happens automatically when the bio is freed or reset. 20854b259f6SSatya Tangirala 2098e9f666aSEric BiggersFinally, when done using inline encryption with a blk_crypto_key on a 210fce3caeaSChristoph Hellwigblock_device, users must call ``blk_crypto_evict_key()``. This ensures that 2118e9f666aSEric Biggersthe key is evicted from all keyslots it may be programmed into and unlinked from 2128e9f666aSEric Biggersany kernel data structures it may be linked into. 21354b259f6SSatya Tangirala 2148e9f666aSEric BiggersIn summary, for users of the block layer, the lifecycle of a blk_crypto_key is 2158e9f666aSEric Biggersas follows: 2168e9f666aSEric Biggers 2178e9f666aSEric Biggers1. ``blk_crypto_config_supported()`` (optional) 2188e9f666aSEric Biggers2. ``blk_crypto_init_key()`` 2198e9f666aSEric Biggers3. ``blk_crypto_start_using_key()`` 2208e9f666aSEric Biggers4. ``bio_crypt_set_ctx()`` (potentially many times) 2218e9f666aSEric Biggers5. ``blk_crypto_evict_key()`` (after all I/O has completed) 2228e9f666aSEric Biggers6. Zeroize the blk_crypto_key (this has no dedicated function) 2238e9f666aSEric Biggers 224fce3caeaSChristoph HellwigIf a blk_crypto_key is being used on multiple block_devices, then 2258e9f666aSEric Biggers``blk_crypto_config_supported()`` (if used), ``blk_crypto_start_using_key()``, 226fce3caeaSChristoph Hellwigand ``blk_crypto_evict_key()`` must be called on each block_device. 22754b259f6SSatya Tangirala 22854b259f6SSatya TangiralaAPI presented to device drivers 22954b259f6SSatya Tangirala=============================== 23054b259f6SSatya Tangirala 2318e9f666aSEric BiggersA device driver that wants to support inline encryption must set up a 2328e9f666aSEric Biggersblk_crypto_profile in the request_queue of its device. To do this, it first 2338e9f666aSEric Biggersmust call ``blk_crypto_profile_init()`` (or its resource-managed variant 2348e9f666aSEric Biggers``devm_blk_crypto_profile_init()``), providing the number of keyslots. 23554b259f6SSatya Tangirala 2368e9f666aSEric BiggersNext, it must advertise its crypto capabilities by setting fields in the 2378e9f666aSEric Biggersblk_crypto_profile, e.g. ``modes_supported`` and ``max_dun_bytes_supported``. 23854b259f6SSatya Tangirala 2398e9f666aSEric BiggersIt then must set function pointers in the ``ll_ops`` field of the 2408e9f666aSEric Biggersblk_crypto_profile to tell upper layers how to control the inline encryption 2418e9f666aSEric Biggershardware, e.g. how to program and evict keyslots. Most drivers will need to 2428e9f666aSEric Biggersimplement ``keyslot_program`` and ``keyslot_evict``. For details, see the 2438e9f666aSEric Biggerscomments for ``struct blk_crypto_ll_ops``. 24454b259f6SSatya Tangirala 2458e9f666aSEric BiggersOnce the driver registers a blk_crypto_profile with a request_queue, I/O 2468e9f666aSEric Biggersrequests the driver receives via that queue may have an encryption context. All 2478e9f666aSEric Biggersencryption contexts will be compatible with the crypto capabilities declared in 2488e9f666aSEric Biggersthe blk_crypto_profile, so drivers don't need to worry about handling 2498e9f666aSEric Biggersunsupported requests. Also, if a nonzero number of keyslots was declared in the 2508e9f666aSEric Biggersblk_crypto_profile, then all I/O requests that have an encryption context will 2518e9f666aSEric Biggersalso have a keyslot which was already programmed with the appropriate key. 25254b259f6SSatya Tangirala 2538e9f666aSEric BiggersIf the driver implements runtime suspend and its blk_crypto_ll_ops don't work 2548e9f666aSEric Biggerswhile the device is runtime-suspended, then the driver must also set the ``dev`` 2558e9f666aSEric Biggersfield of the blk_crypto_profile to point to the ``struct device`` that will be 2568e9f666aSEric Biggersresumed before any of the low-level operations are called. 2578e9f666aSEric Biggers 2588e9f666aSEric BiggersIf there are situations where the inline encryption hardware loses the contents 2598e9f666aSEric Biggersof its keyslots, e.g. device resets, the driver must handle reprogramming the 2608e9f666aSEric Biggerskeyslots. To do this, the driver may call ``blk_crypto_reprogram_all_keys()``. 2618e9f666aSEric Biggers 2628e9f666aSEric BiggersFinally, if the driver used ``blk_crypto_profile_init()`` instead of 2638e9f666aSEric Biggers``devm_blk_crypto_profile_init()``, then it is responsible for calling 2648e9f666aSEric Biggers``blk_crypto_profile_destroy()`` when the crypto profile is no longer needed. 26554b259f6SSatya Tangirala 26654b259f6SSatya TangiralaLayered Devices 26754b259f6SSatya Tangirala=============== 26854b259f6SSatya Tangirala 2698e9f666aSEric BiggersRequest queue based layered devices like dm-rq that wish to support inline 2708e9f666aSEric Biggersencryption need to create their own blk_crypto_profile for their request_queue, 2718e9f666aSEric Biggersand expose whatever functionality they choose. When a layered device wants to 2728e9f666aSEric Biggerspass a clone of that request to another request_queue, blk-crypto will 273*435c0e99SEric Biggersinitialize and prepare the clone as necessary. 27454b259f6SSatya Tangirala 27554b259f6SSatya TangiralaInteraction between inline encryption and blk integrity 27654b259f6SSatya Tangirala======================================================= 27754b259f6SSatya Tangirala 27854b259f6SSatya TangiralaAt the time of this patch, there is no real hardware that supports both these 27954b259f6SSatya Tangiralafeatures. However, these features do interact with each other, and it's not 28054b259f6SSatya Tangiralacompletely trivial to make them both work together properly. In particular, 28154b259f6SSatya Tangiralawhen a WRITE bio wants to use inline encryption on a device that supports both 28254b259f6SSatya Tangiralafeatures, the bio will have an encryption context specified, after which 28354b259f6SSatya Tangiralaits integrity information is calculated (using the plaintext data, since 28454b259f6SSatya Tangiralathe encryption will happen while data is being written), and the data and 28554b259f6SSatya Tangiralaintegrity info is sent to the device. Obviously, the integrity info must be 28654b259f6SSatya Tangiralaverified before the data is encrypted. After the data is encrypted, the device 28754b259f6SSatya Tangiralamust not store the integrity info that it received with the plaintext data 28854b259f6SSatya Tangiralasince that might reveal information about the plaintext data. As such, it must 28954b259f6SSatya Tangiralare-generate the integrity info from the ciphertext data and store that on disk 29054b259f6SSatya Tangiralainstead. Another issue with storing the integrity info of the plaintext data is 29154b259f6SSatya Tangiralathat it changes the on disk format depending on whether hardware inline 29254b259f6SSatya Tangiralaencryption support is present or the kernel crypto API fallback is used (since 29354b259f6SSatya Tangiralaif the fallback is used, the device will receive the integrity info of the 29454b259f6SSatya Tangiralaciphertext, not that of the plaintext). 29554b259f6SSatya Tangirala 29654b259f6SSatya TangiralaBecause there isn't any real hardware yet, it seems prudent to assume that 29754b259f6SSatya Tangiralahardware implementations might not implement both features together correctly, 29854b259f6SSatya Tangiralaand disallow the combination for now. Whenever a device supports integrity, the 29954b259f6SSatya Tangiralakernel will pretend that the device does not support hardware inline encryption 3008e9f666aSEric Biggers(by setting the blk_crypto_profile in the request_queue of the device to NULL). 3018e9f666aSEric BiggersWhen the crypto API fallback is enabled, this means that all bios with and 3028e9f666aSEric Biggersencryption context will use the fallback, and IO will complete as usual. When 3038e9f666aSEric Biggersthe fallback is disabled, a bio with an encryption context will be failed. 304