1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun================= 4*4882a593SmuzhiyunInline Encryption 5*4882a593Smuzhiyun================= 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunBackground 8*4882a593Smuzhiyun========== 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunInline encryption hardware sits logically between memory and the disk, and can 11*4882a593Smuzhiyunen/decrypt data as it goes in/out of the disk. Inline encryption hardware has a 12*4882a593Smuzhiyunfixed number of "keyslots" - slots into which encryption contexts (i.e. the 13*4882a593Smuzhiyunencryption key, encryption algorithm, data unit size) can be programmed by the 14*4882a593Smuzhiyunkernel at any time. Each request sent to the disk can be tagged with the index 15*4882a593Smuzhiyunof a keyslot (and also a data unit number to act as an encryption tweak), and 16*4882a593Smuzhiyunthe inline encryption hardware will en/decrypt the data in the request with the 17*4882a593Smuzhiyunencryption context programmed into that keyslot. This is very different from 18*4882a593Smuzhiyunfull disk encryption solutions like self encrypting drives/TCG OPAL/ATA 19*4882a593SmuzhiyunSecurity standards, since with inline encryption, any block on disk could be 20*4882a593Smuzhiyunencrypted with any encryption context the kernel chooses. 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunObjective 24*4882a593Smuzhiyun========= 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunWe want to support inline encryption (IE) in the kernel. 27*4882a593SmuzhiyunTo allow for testing, we also want a crypto API fallback when actual 28*4882a593SmuzhiyunIE hardware is absent. We also want IE to work with layered devices 29*4882a593Smuzhiyunlike dm and loopback (i.e. we want to be able to use the IE hardware 30*4882a593Smuzhiyunof the underlying devices if present, or else fall back to crypto API 31*4882a593Smuzhiyunen/decryption). 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunConstraints and notes 35*4882a593Smuzhiyun===================== 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun- IE hardware has a limited number of "keyslots" that can be programmed 38*4882a593Smuzhiyun with an encryption context (key, algorithm, data unit size, etc.) at any time. 39*4882a593Smuzhiyun One can specify a keyslot in a data request made to the device, and the 40*4882a593Smuzhiyun device will en/decrypt the data using the encryption context programmed into 41*4882a593Smuzhiyun that specified keyslot. When possible, we want to make multiple requests with 42*4882a593Smuzhiyun the same encryption context share the same keyslot. 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun- We need a way for upper layers like filesystems to specify an encryption 45*4882a593Smuzhiyun context to use for en/decrypting a struct bio, and a device driver (like UFS) 46*4882a593Smuzhiyun needs to be able to use that encryption context when it processes the bio. 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun- We need a way for device drivers to expose their inline encryption 49*4882a593Smuzhiyun capabilities in a unified way to the upper layers. 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunDesign 53*4882a593Smuzhiyun====== 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunWe add a struct bio_crypt_ctx to struct bio that can 56*4882a593Smuzhiyunrepresent an encryption context, because we need to be able to pass this 57*4882a593Smuzhiyunencryption context from the upper layers (like the fs layer) to the 58*4882a593Smuzhiyundevice driver to act upon. 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunWhile IE hardware works on the notion of keyslots, the FS layer has no 61*4882a593Smuzhiyunknowledge of keyslots - it simply wants to specify an encryption context to 62*4882a593Smuzhiyunuse while en/decrypting a bio. 63*4882a593Smuzhiyun 64*4882a593SmuzhiyunWe introduce a keyslot manager (KSM) that handles the translation from 65*4882a593Smuzhiyunencryption contexts specified by the FS to keyslots on the IE hardware. 66*4882a593SmuzhiyunThis KSM also serves as the way IE hardware can expose its capabilities to 67*4882a593Smuzhiyunupper layers. The generic mode of operation is: each device driver that wants 68*4882a593Smuzhiyunto support IE will construct a KSM and set it up in its struct request_queue. 69*4882a593SmuzhiyunUpper layers that want to use IE on this device can then use this KSM in 70*4882a593Smuzhiyunthe device's struct request_queue to translate an encryption context into 71*4882a593Smuzhiyuna keyslot. The presence of the KSM in the request queue shall be used to mean 72*4882a593Smuzhiyunthat the device supports IE. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunThe KSM uses refcounts to track which keyslots are idle (either they have no 75*4882a593Smuzhiyunencryption context programmed, or there are no in-flight struct bios 76*4882a593Smuzhiyunreferencing that keyslot). When a new encryption context needs a keyslot, it 77*4882a593Smuzhiyuntries to find a keyslot that has already been programmed with the same 78*4882a593Smuzhiyunencryption context, and if there is no such keyslot, it evicts the least 79*4882a593Smuzhiyunrecently used idle keyslot and programs the new encryption context into that 80*4882a593Smuzhiyunone. If no idle keyslots are available, then the caller will sleep until there 81*4882a593Smuzhiyunis at least one. 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun 84*4882a593Smuzhiyunblk-mq changes, other block layer changes and blk-crypto-fallback 85*4882a593Smuzhiyun================================================================= 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunWe add a pointer to a ``bi_crypt_context`` and ``keyslot`` to 88*4882a593Smuzhiyunstruct request. These will be referred to as the ``crypto fields`` 89*4882a593Smuzhiyunfor the request. This ``keyslot`` is the keyslot into which the 90*4882a593Smuzhiyun``bi_crypt_context`` has been programmed in the KSM of the ``request_queue`` 91*4882a593Smuzhiyunthat this request is being sent to. 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunWe introduce ``block/blk-crypto-fallback.c``, which allows upper layers to remain 94*4882a593Smuzhiyunblissfully unaware of whether or not real inline encryption hardware is present 95*4882a593Smuzhiyununderneath. When a bio is submitted with a target ``request_queue`` that doesn't 96*4882a593Smuzhiyunsupport the encryption context specified with the bio, the block layer will 97*4882a593Smuzhiyunen/decrypt the bio with the blk-crypto-fallback. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunIf the bio is a ``WRITE`` bio, a bounce bio is allocated, and the data in the bio 100*4882a593Smuzhiyunis encrypted stored in the bounce bio - blk-mq will then proceed to process the 101*4882a593Smuzhiyunbounce bio as if it were not encrypted at all (except when blk-integrity is 102*4882a593Smuzhiyunconcerned). ``blk-crypto-fallback`` sets the bounce bio's ``bi_end_io`` to an 103*4882a593Smuzhiyuninternal function that cleans up the bounce bio and ends the original bio. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunIf the bio is a ``READ`` bio, the bio's ``bi_end_io`` (and also ``bi_private``) 106*4882a593Smuzhiyunis saved and overwritten by ``blk-crypto-fallback`` to 107*4882a593Smuzhiyun``bio_crypto_fallback_decrypt_bio``. The bio's ``bi_crypt_context`` is also 108*4882a593Smuzhiyunoverwritten with ``NULL``, so that to the rest of the stack, the bio looks 109*4882a593Smuzhiyunas if it was a regular bio that never had an encryption context specified. 110*4882a593Smuzhiyun``bio_crypto_fallback_decrypt_bio`` will decrypt the bio, restore the original 111*4882a593Smuzhiyun``bi_end_io`` (and also ``bi_private``) and end the bio again. 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunRegardless of whether real inline encryption hardware is used or the 114*4882a593Smuzhiyunblk-crypto-fallback is used, the ciphertext written to disk (and hence the 115*4882a593Smuzhiyunon-disk format of data) will be the same (assuming the hardware's implementation 116*4882a593Smuzhiyunof the algorithm being used adheres to spec and functions correctly). 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunIf a ``request queue``'s inline encryption hardware claimed to support the 119*4882a593Smuzhiyunencryption context specified with a bio, then it will not be handled by the 120*4882a593Smuzhiyun``blk-crypto-fallback``. We will eventually reach a point in blk-mq when a 121*4882a593Smuzhiyunstruct request needs to be allocated for that bio. At that point, 122*4882a593Smuzhiyunblk-mq tries to program the encryption context into the ``request_queue``'s 123*4882a593Smuzhiyunkeyslot_manager, and obtain a keyslot, which it stores in its newly added 124*4882a593Smuzhiyun``keyslot`` field. This keyslot is released when the request is completed. 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunWhen the first bio is added to a request, ``blk_crypto_rq_bio_prep`` is called, 127*4882a593Smuzhiyunwhich sets the request's ``crypt_ctx`` to a copy of the bio's 128*4882a593Smuzhiyun``bi_crypt_context``. bio_crypt_do_front_merge is called whenever a subsequent 129*4882a593Smuzhiyunbio is merged to the front of the request, which updates the ``crypt_ctx`` of 130*4882a593Smuzhiyunthe request so that it matches the newly merged bio's ``bi_crypt_context``. In particular, the request keeps a copy of the ``bi_crypt_context`` of the first 131*4882a593Smuzhiyunbio in its bio-list (blk-mq needs to be careful to maintain this invariant 132*4882a593Smuzhiyunduring bio and request merges). 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunTo make it possible for inline encryption to work with request queue based 135*4882a593Smuzhiyunlayered devices, when a request is cloned, its ``crypto fields`` are cloned as 136*4882a593Smuzhiyunwell. When the cloned request is submitted, blk-mq programs the 137*4882a593Smuzhiyun``bi_crypt_context`` of the request into the clone's request_queue's keyslot 138*4882a593Smuzhiyunmanager, and stores the returned keyslot in the clone's ``keyslot``. 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunAPI presented to users of the block layer 142*4882a593Smuzhiyun========================================= 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun``struct blk_crypto_key`` represents a crypto key (the raw key, size of the 145*4882a593Smuzhiyunkey, the crypto algorithm to use, the data unit size to use, and the number of 146*4882a593Smuzhiyunbytes required to represent data unit numbers that will be specified with the 147*4882a593Smuzhiyun``bi_crypt_context``). 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun``blk_crypto_init_key`` allows upper layers to initialize such a 150*4882a593Smuzhiyun``blk_crypto_key``. 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun``bio_crypt_set_ctx`` should be called on any bio that a user of 153*4882a593Smuzhiyunthe block layer wants en/decrypted via inline encryption (or the 154*4882a593Smuzhiyunblk-crypto-fallback, if hardware support isn't available for the desired 155*4882a593Smuzhiyuncrypto configuration). This function takes the ``blk_crypto_key`` and the 156*4882a593Smuzhiyundata unit number (DUN) to use when en/decrypting the bio. 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun``blk_crypto_config_supported`` allows upper layers to query whether or not the 159*4882a593Smuzhiyunan encryption context passed to request queue can be handled by blk-crypto 160*4882a593Smuzhiyun(either by real inline encryption hardware, or by the blk-crypto-fallback). 161*4882a593SmuzhiyunThis is useful e.g. when blk-crypto-fallback is disabled, and the upper layer 162*4882a593Smuzhiyunwants to use an algorithm that may not supported by hardware - this function 163*4882a593Smuzhiyunlets the upper layer know ahead of time that the algorithm isn't supported, 164*4882a593Smuzhiyunand the upper layer can fallback to something else if appropriate. 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun``blk_crypto_start_using_key`` - Upper layers must call this function on 167*4882a593Smuzhiyun``blk_crypto_key`` and a ``request_queue`` before using the key with any bio 168*4882a593Smuzhiyunheaded for that ``request_queue``. This function ensures that either the 169*4882a593Smuzhiyunhardware supports the key's crypto settings, or the crypto API fallback has 170*4882a593Smuzhiyuntransforms for the needed mode allocated and ready to go. Note that this 171*4882a593Smuzhiyunfunction may allocate an ``skcipher``, and must not be called from the data 172*4882a593Smuzhiyunpath, since allocating ``skciphers`` from the data path can deadlock. 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun``blk_crypto_evict_key`` *must* be called by upper layers before a 175*4882a593Smuzhiyun``blk_crypto_key`` is freed. Further, it *must* only be called only once 176*4882a593Smuzhiyunthere are no more in-flight requests that use that ``blk_crypto_key``. 177*4882a593Smuzhiyun``blk_crypto_evict_key`` will ensure that a key is removed from any keyslots in 178*4882a593Smuzhiyuninline encryption hardware that the key might have been programmed into (or the blk-crypto-fallback). 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunAPI presented to device drivers 181*4882a593Smuzhiyun=============================== 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunA :c:type:``struct blk_keyslot_manager`` should be set up by device drivers in 184*4882a593Smuzhiyunthe ``request_queue`` of the device. The device driver needs to call 185*4882a593Smuzhiyun``blk_ksm_init`` (or its resource-managed variant ``devm_blk_ksm_init``) on the 186*4882a593Smuzhiyun``blk_keyslot_manager``, while specifying the number of keyslots supported by 187*4882a593Smuzhiyunthe hardware. 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunThe device driver also needs to tell the KSM how to actually manipulate the 190*4882a593SmuzhiyunIE hardware in the device to do things like programming the crypto key into 191*4882a593Smuzhiyunthe IE hardware into a particular keyslot. All this is achieved through the 192*4882a593Smuzhiyunstruct blk_ksm_ll_ops field in the KSM that the device driver 193*4882a593Smuzhiyunmust fill up after initing the ``blk_keyslot_manager``. 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunThe KSM also handles runtime power management for the device when applicable 196*4882a593Smuzhiyun(e.g. when it wants to program a crypto key into the IE hardware, the device 197*4882a593Smuzhiyunmust be runtime powered on) - so the device driver must also set the ``dev`` 198*4882a593Smuzhiyunfield in the ksm to point to the `struct device` for the KSM to use for runtime 199*4882a593Smuzhiyunpower management. 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun``blk_ksm_reprogram_all_keys`` can be called by device drivers if the device 202*4882a593Smuzhiyunneeds each and every of its keyslots to be reprogrammed with the key it 203*4882a593Smuzhiyun"should have" at the point in time when the function is called. This is useful 204*4882a593Smuzhiyune.g. if a device loses all its keys on runtime power down/up. 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunIf the driver used ``blk_ksm_init`` instead of ``devm_blk_ksm_init``, then 207*4882a593Smuzhiyun``blk_ksm_destroy`` should be called to free up all resources used by a 208*4882a593Smuzhiyun``blk_keyslot_manager`` once it is no longer needed. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunLayered Devices 211*4882a593Smuzhiyun=============== 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunRequest queue based layered devices like dm-rq that wish to support IE need to 214*4882a593Smuzhiyuncreate their own keyslot manager for their request queue, and expose whatever 215*4882a593Smuzhiyunfunctionality they choose. When a layered device wants to pass a clone of that 216*4882a593Smuzhiyunrequest to another ``request_queue``, blk-crypto will initialize and prepare the 217*4882a593Smuzhiyunclone as necessary - see ``blk_crypto_insert_cloned_request`` in 218*4882a593Smuzhiyun``blk-crypto.c``. 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun 221*4882a593SmuzhiyunFuture Optimizations for layered devices 222*4882a593Smuzhiyun======================================== 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunCreating a keyslot manager for a layered device uses up memory for each 225*4882a593Smuzhiyunkeyslot, and in general, a layered device merely passes the request on to a 226*4882a593Smuzhiyun"child" device, so the keyslots in the layered device itself are completely 227*4882a593Smuzhiyununused, and don't need any refcounting or keyslot programming. We can instead 228*4882a593Smuzhiyundefine a new type of KSM; the "passthrough KSM", that layered devices can use 229*4882a593Smuzhiyunto advertise an unlimited number of keyslots, and support for any encryption 230*4882a593Smuzhiyunalgorithms they choose, while not actually using any memory for each keyslot. 231*4882a593SmuzhiyunAnother use case for the "passthrough KSM" is for IE devices that do not have a 232*4882a593Smuzhiyunlimited number of keyslots. 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunInteraction between inline encryption and blk integrity 236*4882a593Smuzhiyun======================================================= 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunAt the time of this patch, there is no real hardware that supports both these 239*4882a593Smuzhiyunfeatures. However, these features do interact with each other, and it's not 240*4882a593Smuzhiyuncompletely trivial to make them both work together properly. In particular, 241*4882a593Smuzhiyunwhen a WRITE bio wants to use inline encryption on a device that supports both 242*4882a593Smuzhiyunfeatures, the bio will have an encryption context specified, after which 243*4882a593Smuzhiyunits integrity information is calculated (using the plaintext data, since 244*4882a593Smuzhiyunthe encryption will happen while data is being written), and the data and 245*4882a593Smuzhiyunintegrity info is sent to the device. Obviously, the integrity info must be 246*4882a593Smuzhiyunverified before the data is encrypted. After the data is encrypted, the device 247*4882a593Smuzhiyunmust not store the integrity info that it received with the plaintext data 248*4882a593Smuzhiyunsince that might reveal information about the plaintext data. As such, it must 249*4882a593Smuzhiyunre-generate the integrity info from the ciphertext data and store that on disk 250*4882a593Smuzhiyuninstead. Another issue with storing the integrity info of the plaintext data is 251*4882a593Smuzhiyunthat it changes the on disk format depending on whether hardware inline 252*4882a593Smuzhiyunencryption support is present or the kernel crypto API fallback is used (since 253*4882a593Smuzhiyunif the fallback is used, the device will receive the integrity info of the 254*4882a593Smuzhiyunciphertext, not that of the plaintext). 255*4882a593Smuzhiyun 256*4882a593SmuzhiyunBecause there isn't any real hardware yet, it seems prudent to assume that 257*4882a593Smuzhiyunhardware implementations might not implement both features together correctly, 258*4882a593Smuzhiyunand disallow the combination for now. Whenever a device supports integrity, the 259*4882a593Smuzhiyunkernel will pretend that the device does not support hardware inline encryption 260*4882a593Smuzhiyun(by essentially setting the keyslot manager in the request_queue of the device 261*4882a593Smuzhiyunto NULL). When the crypto API fallback is enabled, this means that all bios with 262*4882a593Smuzhiyunand encryption context will use the fallback, and IO will complete as usual. 263*4882a593SmuzhiyunWhen the fallback is disabled, a bio with an encryption context will be failed. 264