xref: /openbmc/linux/Documentation/block/inline-encryption.rst (revision 54b259f68de59920714588cb52c4a262cb712f98)
1*54b259f6SSatya Tangirala.. SPDX-License-Identifier: GPL-2.0
2*54b259f6SSatya Tangirala
3*54b259f6SSatya Tangirala=================
4*54b259f6SSatya TangiralaInline Encryption
5*54b259f6SSatya Tangirala=================
6*54b259f6SSatya Tangirala
7*54b259f6SSatya TangiralaBackground
8*54b259f6SSatya Tangirala==========
9*54b259f6SSatya Tangirala
10*54b259f6SSatya TangiralaInline encryption hardware sits logically between memory and the disk, and can
11*54b259f6SSatya Tangiralaen/decrypt data as it goes in/out of the disk. Inline encryption hardware has a
12*54b259f6SSatya Tangiralafixed number of "keyslots" - slots into which encryption contexts (i.e. the
13*54b259f6SSatya Tangiralaencryption key, encryption algorithm, data unit size) can be programmed by the
14*54b259f6SSatya Tangiralakernel at any time. Each request sent to the disk can be tagged with the index
15*54b259f6SSatya Tangiralaof a keyslot (and also a data unit number to act as an encryption tweak), and
16*54b259f6SSatya Tangiralathe inline encryption hardware will en/decrypt the data in the request with the
17*54b259f6SSatya Tangiralaencryption context programmed into that keyslot. This is very different from
18*54b259f6SSatya Tangiralafull disk encryption solutions like self encrypting drives/TCG OPAL/ATA
19*54b259f6SSatya TangiralaSecurity standards, since with inline encryption, any block on disk could be
20*54b259f6SSatya Tangiralaencrypted with any encryption context the kernel chooses.
21*54b259f6SSatya Tangirala
22*54b259f6SSatya Tangirala
23*54b259f6SSatya TangiralaObjective
24*54b259f6SSatya Tangirala=========
25*54b259f6SSatya Tangirala
26*54b259f6SSatya TangiralaWe want to support inline encryption (IE) in the kernel.
27*54b259f6SSatya TangiralaTo allow for testing, we also want a crypto API fallback when actual
28*54b259f6SSatya TangiralaIE hardware is absent. We also want IE to work with layered devices
29*54b259f6SSatya Tangiralalike dm and loopback (i.e. we want to be able to use the IE hardware
30*54b259f6SSatya Tangiralaof the underlying devices if present, or else fall back to crypto API
31*54b259f6SSatya Tangiralaen/decryption).
32*54b259f6SSatya Tangirala
33*54b259f6SSatya Tangirala
34*54b259f6SSatya TangiralaConstraints and notes
35*54b259f6SSatya Tangirala=====================
36*54b259f6SSatya Tangirala
37*54b259f6SSatya Tangirala- IE hardware has a limited number of "keyslots" that can be programmed
38*54b259f6SSatya Tangirala  with an encryption context (key, algorithm, data unit size, etc.) at any time.
39*54b259f6SSatya Tangirala  One can specify a keyslot in a data request made to the device, and the
40*54b259f6SSatya Tangirala  device will en/decrypt the data using the encryption context programmed into
41*54b259f6SSatya Tangirala  that specified keyslot. When possible, we want to make multiple requests with
42*54b259f6SSatya Tangirala  the same encryption context share the same keyslot.
43*54b259f6SSatya Tangirala
44*54b259f6SSatya Tangirala- We need a way for upper layers like filesystems to specify an encryption
45*54b259f6SSatya Tangirala  context to use for en/decrypting a struct bio, and a device driver (like UFS)
46*54b259f6SSatya Tangirala  needs to be able to use that encryption context when it processes the bio.
47*54b259f6SSatya Tangirala
48*54b259f6SSatya Tangirala- We need a way for device drivers to expose their inline encryption
49*54b259f6SSatya Tangirala  capabilities in a unified way to the upper layers.
50*54b259f6SSatya Tangirala
51*54b259f6SSatya Tangirala
52*54b259f6SSatya TangiralaDesign
53*54b259f6SSatya Tangirala======
54*54b259f6SSatya Tangirala
55*54b259f6SSatya TangiralaWe add a :c:type:`struct bio_crypt_ctx` to :c:type:`struct bio` that can
56*54b259f6SSatya Tangiralarepresent an encryption context, because we need to be able to pass this
57*54b259f6SSatya Tangiralaencryption context from the upper layers (like the fs layer) to the
58*54b259f6SSatya Tangiraladevice driver to act upon.
59*54b259f6SSatya Tangirala
60*54b259f6SSatya TangiralaWhile IE hardware works on the notion of keyslots, the FS layer has no
61*54b259f6SSatya Tangiralaknowledge of keyslots - it simply wants to specify an encryption context to
62*54b259f6SSatya Tangiralause while en/decrypting a bio.
63*54b259f6SSatya Tangirala
64*54b259f6SSatya TangiralaWe introduce a keyslot manager (KSM) that handles the translation from
65*54b259f6SSatya Tangiralaencryption contexts specified by the FS to keyslots on the IE hardware.
66*54b259f6SSatya TangiralaThis KSM also serves as the way IE hardware can expose its capabilities to
67*54b259f6SSatya Tangiralaupper layers. The generic mode of operation is: each device driver that wants
68*54b259f6SSatya Tangiralato support IE will construct a KSM and set it up in its struct request_queue.
69*54b259f6SSatya TangiralaUpper layers that want to use IE on this device can then use this KSM in
70*54b259f6SSatya Tangiralathe device's struct request_queue to translate an encryption context into
71*54b259f6SSatya Tangiralaa keyslot. The presence of the KSM in the request queue shall be used to mean
72*54b259f6SSatya Tangiralathat the device supports IE.
73*54b259f6SSatya Tangirala
74*54b259f6SSatya TangiralaThe KSM uses refcounts to track which keyslots are idle (either they have no
75*54b259f6SSatya Tangiralaencryption context programmed, or there are no in-flight struct bios
76*54b259f6SSatya Tangiralareferencing that keyslot). When a new encryption context needs a keyslot, it
77*54b259f6SSatya Tangiralatries to find a keyslot that has already been programmed with the same
78*54b259f6SSatya Tangiralaencryption context, and if there is no such keyslot, it evicts the least
79*54b259f6SSatya Tangiralarecently used idle keyslot and programs the new encryption context into that
80*54b259f6SSatya Tangiralaone. If no idle keyslots are available, then the caller will sleep until there
81*54b259f6SSatya Tangiralais at least one.
82*54b259f6SSatya Tangirala
83*54b259f6SSatya Tangirala
84*54b259f6SSatya Tangiralablk-mq changes, other block layer changes and blk-crypto-fallback
85*54b259f6SSatya Tangirala=================================================================
86*54b259f6SSatya Tangirala
87*54b259f6SSatya TangiralaWe add a pointer to a ``bi_crypt_context`` and ``keyslot`` to
88*54b259f6SSatya Tangirala:c:type:`struct request`. These will be referred to as the ``crypto fields``
89*54b259f6SSatya Tangiralafor the request. This ``keyslot`` is the keyslot into which the
90*54b259f6SSatya Tangirala``bi_crypt_context`` has been programmed in the KSM of the ``request_queue``
91*54b259f6SSatya Tangiralathat this request is being sent to.
92*54b259f6SSatya Tangirala
93*54b259f6SSatya TangiralaWe introduce ``block/blk-crypto-fallback.c``, which allows upper layers to remain
94*54b259f6SSatya Tangiralablissfully unaware of whether or not real inline encryption hardware is present
95*54b259f6SSatya Tangiralaunderneath. When a bio is submitted with a target ``request_queue`` that doesn't
96*54b259f6SSatya Tangiralasupport the encryption context specified with the bio, the block layer will
97*54b259f6SSatya Tangiralaen/decrypt the bio with the blk-crypto-fallback.
98*54b259f6SSatya Tangirala
99*54b259f6SSatya TangiralaIf the bio is a ``WRITE`` bio, a bounce bio is allocated, and the data in the bio
100*54b259f6SSatya Tangiralais encrypted stored in the bounce bio - blk-mq will then proceed to process the
101*54b259f6SSatya Tangiralabounce bio as if it were not encrypted at all (except when blk-integrity is
102*54b259f6SSatya Tangiralaconcerned). ``blk-crypto-fallback`` sets the bounce bio's ``bi_end_io`` to an
103*54b259f6SSatya Tangiralainternal function that cleans up the bounce bio and ends the original bio.
104*54b259f6SSatya Tangirala
105*54b259f6SSatya TangiralaIf the bio is a ``READ`` bio, the bio's ``bi_end_io`` (and also ``bi_private``)
106*54b259f6SSatya Tangiralais saved and overwritten by ``blk-crypto-fallback`` to
107*54b259f6SSatya Tangirala``bio_crypto_fallback_decrypt_bio``.  The bio's ``bi_crypt_context`` is also
108*54b259f6SSatya Tangiralaoverwritten with ``NULL``, so that to the rest of the stack, the bio looks
109*54b259f6SSatya Tangiralaas if it was a regular bio that never had an encryption context specified.
110*54b259f6SSatya Tangirala``bio_crypto_fallback_decrypt_bio`` will decrypt the bio, restore the original
111*54b259f6SSatya Tangirala``bi_end_io`` (and also ``bi_private``) and end the bio again.
112*54b259f6SSatya Tangirala
113*54b259f6SSatya TangiralaRegardless of whether real inline encryption hardware is used or the
114*54b259f6SSatya Tangiralablk-crypto-fallback is used, the ciphertext written to disk (and hence the
115*54b259f6SSatya Tangiralaon-disk format of data) will be the same (assuming the hardware's implementation
116*54b259f6SSatya Tangiralaof the algorithm being used adheres to spec and functions correctly).
117*54b259f6SSatya Tangirala
118*54b259f6SSatya TangiralaIf a ``request queue``'s inline encryption hardware claimed to support the
119*54b259f6SSatya Tangiralaencryption context specified with a bio, then it will not be handled by the
120*54b259f6SSatya Tangirala``blk-crypto-fallback``. We will eventually reach a point in blk-mq when a
121*54b259f6SSatya Tangirala:c:type:`struct request` needs to be allocated for that bio. At that point,
122*54b259f6SSatya Tangiralablk-mq tries to program the encryption context into the ``request_queue``'s
123*54b259f6SSatya Tangiralakeyslot_manager, and obtain a keyslot, which it stores in its newly added
124*54b259f6SSatya Tangirala``keyslot`` field. This keyslot is released when the request is completed.
125*54b259f6SSatya Tangirala
126*54b259f6SSatya TangiralaWhen the first bio is added to a request, ``blk_crypto_rq_bio_prep`` is called,
127*54b259f6SSatya Tangiralawhich sets the request's ``crypt_ctx`` to a copy of the bio's
128*54b259f6SSatya Tangirala``bi_crypt_context``. bio_crypt_do_front_merge is called whenever a subsequent
129*54b259f6SSatya Tangiralabio is merged to the front of the request, which updates the ``crypt_ctx`` of
130*54b259f6SSatya Tangiralathe 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*54b259f6SSatya Tangiralabio in its bio-list (blk-mq needs to be careful to maintain this invariant
132*54b259f6SSatya Tangiraladuring bio and request merges).
133*54b259f6SSatya Tangirala
134*54b259f6SSatya TangiralaTo make it possible for inline encryption to work with request queue based
135*54b259f6SSatya Tangiralalayered devices, when a request is cloned, its ``crypto fields`` are cloned as
136*54b259f6SSatya Tangiralawell. When the cloned request is submitted, blk-mq programs the
137*54b259f6SSatya Tangirala``bi_crypt_context`` of the request into the clone's request_queue's keyslot
138*54b259f6SSatya Tangiralamanager, and stores the returned keyslot in the clone's ``keyslot``.
139*54b259f6SSatya Tangirala
140*54b259f6SSatya Tangirala
141*54b259f6SSatya TangiralaAPI presented to users of the block layer
142*54b259f6SSatya Tangirala=========================================
143*54b259f6SSatya Tangirala
144*54b259f6SSatya Tangirala``struct blk_crypto_key`` represents a crypto key (the raw key, size of the
145*54b259f6SSatya Tangiralakey, the crypto algorithm to use, the data unit size to use, and the number of
146*54b259f6SSatya Tangiralabytes required to represent data unit numbers that will be specified with the
147*54b259f6SSatya Tangirala``bi_crypt_context``).
148*54b259f6SSatya Tangirala
149*54b259f6SSatya Tangirala``blk_crypto_init_key`` allows upper layers to initialize such a
150*54b259f6SSatya Tangirala``blk_crypto_key``.
151*54b259f6SSatya Tangirala
152*54b259f6SSatya Tangirala``bio_crypt_set_ctx`` should be called on any bio that a user of
153*54b259f6SSatya Tangiralathe block layer wants en/decrypted via inline encryption (or the
154*54b259f6SSatya Tangiralablk-crypto-fallback, if hardware support isn't available for the desired
155*54b259f6SSatya Tangiralacrypto configuration). This function takes the ``blk_crypto_key`` and the
156*54b259f6SSatya Tangiraladata unit number (DUN) to use when en/decrypting the bio.
157*54b259f6SSatya Tangirala
158*54b259f6SSatya Tangirala``blk_crypto_config_supported`` allows upper layers to query whether or not the
159*54b259f6SSatya Tangiralaan encryption context passed to request queue can be handled by blk-crypto
160*54b259f6SSatya Tangirala(either by real inline encryption hardware, or by the blk-crypto-fallback).
161*54b259f6SSatya TangiralaThis is useful e.g. when blk-crypto-fallback is disabled, and the upper layer
162*54b259f6SSatya Tangiralawants to use an algorithm that may not supported by hardware - this function
163*54b259f6SSatya Tangiralalets the upper layer know ahead of time that the algorithm isn't supported,
164*54b259f6SSatya Tangiralaand the upper layer can fallback to something else if appropriate.
165*54b259f6SSatya Tangirala
166*54b259f6SSatya Tangirala``blk_crypto_start_using_key`` - Upper layers must call this function on
167*54b259f6SSatya Tangirala``blk_crypto_key`` and a ``request_queue`` before using the key with any bio
168*54b259f6SSatya Tangiralaheaded for that ``request_queue``. This function ensures that either the
169*54b259f6SSatya Tangiralahardware supports the key's crypto settings, or the crypto API fallback has
170*54b259f6SSatya Tangiralatransforms for the needed mode allocated and ready to go. Note that this
171*54b259f6SSatya Tangiralafunction may allocate an ``skcipher``, and must not be called from the data
172*54b259f6SSatya Tangiralapath, since allocating ``skciphers`` from the data path can deadlock.
173*54b259f6SSatya Tangirala
174*54b259f6SSatya Tangirala``blk_crypto_evict_key`` *must* be called by upper layers before a
175*54b259f6SSatya Tangirala``blk_crypto_key`` is freed. Further, it *must* only be called only once
176*54b259f6SSatya Tangiralathere are no more in-flight requests that use that ``blk_crypto_key``.
177*54b259f6SSatya Tangirala``blk_crypto_evict_key`` will ensure that a key is removed from any keyslots in
178*54b259f6SSatya Tangiralainline encryption hardware that the key might have been programmed into (or the blk-crypto-fallback).
179*54b259f6SSatya Tangirala
180*54b259f6SSatya TangiralaAPI presented to device drivers
181*54b259f6SSatya Tangirala===============================
182*54b259f6SSatya Tangirala
183*54b259f6SSatya TangiralaA :c:type:``struct blk_keyslot_manager`` should be set up by device drivers in
184*54b259f6SSatya Tangiralathe ``request_queue`` of the device. The device driver needs to call
185*54b259f6SSatya Tangirala``blk_ksm_init`` on the ``blk_keyslot_manager``, which specifying the number of
186*54b259f6SSatya Tangiralakeyslots supported by the hardware.
187*54b259f6SSatya Tangirala
188*54b259f6SSatya TangiralaThe device driver also needs to tell the KSM how to actually manipulate the
189*54b259f6SSatya TangiralaIE hardware in the device to do things like programming the crypto key into
190*54b259f6SSatya Tangiralathe IE hardware into a particular keyslot. All this is achieved through the
191*54b259f6SSatya Tangirala:c:type:`struct blk_ksm_ll_ops` field in the KSM that the device driver
192*54b259f6SSatya Tangiralamust fill up after initing the ``blk_keyslot_manager``.
193*54b259f6SSatya Tangirala
194*54b259f6SSatya TangiralaThe KSM also handles runtime power management for the device when applicable
195*54b259f6SSatya Tangirala(e.g. when it wants to program a crypto key into the IE hardware, the device
196*54b259f6SSatya Tangiralamust be runtime powered on) - so the device driver must also set the ``dev``
197*54b259f6SSatya Tangiralafield in the ksm to point to the `struct device` for the KSM to use for runtime
198*54b259f6SSatya Tangiralapower management.
199*54b259f6SSatya Tangirala
200*54b259f6SSatya Tangirala``blk_ksm_reprogram_all_keys`` can be called by device drivers if the device
201*54b259f6SSatya Tangiralaneeds each and every of its keyslots to be reprogrammed with the key it
202*54b259f6SSatya Tangirala"should have" at the point in time when the function is called. This is useful
203*54b259f6SSatya Tangiralae.g. if a device loses all its keys on runtime power down/up.
204*54b259f6SSatya Tangirala
205*54b259f6SSatya Tangirala``blk_ksm_destroy`` should be called to free up all resources used by a keyslot
206*54b259f6SSatya Tangiralamanager upon ``blk_ksm_init``, once the ``blk_keyslot_manager`` is no longer
207*54b259f6SSatya Tangiralaneeded.
208*54b259f6SSatya Tangirala
209*54b259f6SSatya Tangirala
210*54b259f6SSatya TangiralaLayered Devices
211*54b259f6SSatya Tangirala===============
212*54b259f6SSatya Tangirala
213*54b259f6SSatya TangiralaRequest queue based layered devices like dm-rq that wish to support IE need to
214*54b259f6SSatya Tangiralacreate their own keyslot manager for their request queue, and expose whatever
215*54b259f6SSatya Tangiralafunctionality they choose. When a layered device wants to pass a clone of that
216*54b259f6SSatya Tangiralarequest to another ``request_queue``, blk-crypto will initialize and prepare the
217*54b259f6SSatya Tangiralaclone as necessary - see ``blk_crypto_insert_cloned_request`` in
218*54b259f6SSatya Tangirala``blk-crypto.c``.
219*54b259f6SSatya Tangirala
220*54b259f6SSatya Tangirala
221*54b259f6SSatya TangiralaFuture Optimizations for layered devices
222*54b259f6SSatya Tangirala========================================
223*54b259f6SSatya Tangirala
224*54b259f6SSatya TangiralaCreating a keyslot manager for a layered device uses up memory for each
225*54b259f6SSatya Tangiralakeyslot, and in general, a layered device merely passes the request on to a
226*54b259f6SSatya Tangirala"child" device, so the keyslots in the layered device itself are completely
227*54b259f6SSatya Tangiralaunused, and don't need any refcounting or keyslot programming. We can instead
228*54b259f6SSatya Tangiraladefine a new type of KSM; the "passthrough KSM", that layered devices can use
229*54b259f6SSatya Tangiralato advertise an unlimited number of keyslots, and support for any encryption
230*54b259f6SSatya Tangiralaalgorithms they choose, while not actually using any memory for each keyslot.
231*54b259f6SSatya TangiralaAnother use case for the "passthrough KSM" is for IE devices that do not have a
232*54b259f6SSatya Tangiralalimited number of keyslots.
233*54b259f6SSatya Tangirala
234*54b259f6SSatya Tangirala
235*54b259f6SSatya TangiralaInteraction between inline encryption and blk integrity
236*54b259f6SSatya Tangirala=======================================================
237*54b259f6SSatya Tangirala
238*54b259f6SSatya TangiralaAt the time of this patch, there is no real hardware that supports both these
239*54b259f6SSatya Tangiralafeatures. However, these features do interact with each other, and it's not
240*54b259f6SSatya Tangiralacompletely trivial to make them both work together properly. In particular,
241*54b259f6SSatya Tangiralawhen a WRITE bio wants to use inline encryption on a device that supports both
242*54b259f6SSatya Tangiralafeatures, the bio will have an encryption context specified, after which
243*54b259f6SSatya Tangiralaits integrity information is calculated (using the plaintext data, since
244*54b259f6SSatya Tangiralathe encryption will happen while data is being written), and the data and
245*54b259f6SSatya Tangiralaintegrity info is sent to the device. Obviously, the integrity info must be
246*54b259f6SSatya Tangiralaverified before the data is encrypted. After the data is encrypted, the device
247*54b259f6SSatya Tangiralamust not store the integrity info that it received with the plaintext data
248*54b259f6SSatya Tangiralasince that might reveal information about the plaintext data. As such, it must
249*54b259f6SSatya Tangiralare-generate the integrity info from the ciphertext data and store that on disk
250*54b259f6SSatya Tangiralainstead. Another issue with storing the integrity info of the plaintext data is
251*54b259f6SSatya Tangiralathat it changes the on disk format depending on whether hardware inline
252*54b259f6SSatya Tangiralaencryption support is present or the kernel crypto API fallback is used (since
253*54b259f6SSatya Tangiralaif the fallback is used, the device will receive the integrity info of the
254*54b259f6SSatya Tangiralaciphertext, not that of the plaintext).
255*54b259f6SSatya Tangirala
256*54b259f6SSatya TangiralaBecause there isn't any real hardware yet, it seems prudent to assume that
257*54b259f6SSatya Tangiralahardware implementations might not implement both features together correctly,
258*54b259f6SSatya Tangiralaand disallow the combination for now. Whenever a device supports integrity, the
259*54b259f6SSatya Tangiralakernel will pretend that the device does not support hardware inline encryption
260*54b259f6SSatya Tangirala(by essentially setting the keyslot manager in the request_queue of the device
261*54b259f6SSatya Tangiralato NULL). When the crypto API fallback is enabled, this means that all bios with
262*54b259f6SSatya Tangiralaand encryption context will use the fallback, and IO will complete as usual.
263*54b259f6SSatya TangiralaWhen the fallback is disabled, a bio with an encryption context will be failed.
264