Skip to content

Commit ebc4176

Browse files
ebiggersaxboe
authored andcommitted
blk-crypto: add basic hardware-wrapped key support
To prevent keys from being compromised if an attacker acquires read access to kernel memory, some inline encryption hardware can accept keys which are wrapped by a per-boot hardware-internal key. This avoids needing to keep the raw keys in kernel memory, without limiting the number of keys that can be used. Such hardware also supports deriving a "software secret" for cryptographic tasks that can't be handled by inline encryption; this is needed for fscrypt to work properly. To support this hardware, allow struct blk_crypto_key to represent a hardware-wrapped key as an alternative to a raw key, and make drivers set flags in struct blk_crypto_profile to indicate which types of keys they support. Also add the ->derive_sw_secret() low-level operation, which drivers supporting wrapped keys must implement. For more information, see the detailed documentation which this patch adds to Documentation/block/inline-encryption.rst. Signed-off-by: Eric Biggers <[email protected]> Tested-by: Bartosz Golaszewski <[email protected]> # sm8650 Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent a64dcfb commit ebc4176

File tree

14 files changed

+417
-38
lines changed

14 files changed

+417
-38
lines changed

Documentation/block/inline-encryption.rst

Lines changed: 215 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ Basic design
7777
============
7878

7979
We introduce ``struct blk_crypto_key`` to represent an inline encryption key and
80-
how it will be used. This includes the actual bytes of the key; the size of the
81-
key; the algorithm and data unit size the key will be used with; and the number
82-
of bytes needed to represent the maximum data unit number the key will be used
83-
with.
80+
how it will be used. This includes the type of the key (raw or
81+
hardware-wrapped); the actual bytes of the key; the size of the key; the
82+
algorithm and data unit size the key will be used with; and the number of bytes
83+
needed to represent the maximum data unit number the key will be used with.
8484

8585
We introduce ``struct bio_crypt_ctx`` to represent an encryption context. It
8686
contains a data unit number and a pointer to a blk_crypto_key. We add pointers
@@ -301,3 +301,214 @@ kernel will pretend that the device does not support hardware inline encryption
301301
When the crypto API fallback is enabled, this means that all bios with and
302302
encryption context will use the fallback, and IO will complete as usual. When
303303
the fallback is disabled, a bio with an encryption context will be failed.
304+
305+
.. _hardware_wrapped_keys:
306+
307+
Hardware-wrapped keys
308+
=====================
309+
310+
Motivation and threat model
311+
---------------------------
312+
313+
Linux storage encryption (dm-crypt, fscrypt, eCryptfs, etc.) traditionally
314+
relies on the raw encryption key(s) being present in kernel memory so that the
315+
encryption can be performed. This traditionally isn't seen as a problem because
316+
the key(s) won't be present during an offline attack, which is the main type of
317+
attack that storage encryption is intended to protect from.
318+
319+
However, there is an increasing desire to also protect users' data from other
320+
types of attacks (to the extent possible), including:
321+
322+
- Cold boot attacks, where an attacker with physical access to a system suddenly
323+
powers it off, then immediately dumps the system memory to extract recently
324+
in-use encryption keys, then uses these keys to decrypt user data on-disk.
325+
326+
- Online attacks where the attacker is able to read kernel memory without fully
327+
compromising the system, followed by an offline attack where any extracted
328+
keys can be used to decrypt user data on-disk. An example of such an online
329+
attack would be if the attacker is able to run some code on the system that
330+
exploits a Meltdown-like vulnerability but is unable to escalate privileges.
331+
332+
- Online attacks where the attacker fully compromises the system, but their data
333+
exfiltration is significantly time-limited and/or bandwidth-limited, so in
334+
order to completely exfiltrate the data they need to extract the encryption
335+
keys to use in a later offline attack.
336+
337+
Hardware-wrapped keys are a feature of inline encryption hardware that is
338+
designed to protect users' data from the above attacks (to the extent possible),
339+
without introducing limitations such as a maximum number of keys.
340+
341+
Note that it is impossible to **fully** protect users' data from these attacks.
342+
Even in the attacks where the attacker "just" gets read access to kernel memory,
343+
they can still extract any user data that is present in memory, including
344+
plaintext pagecache pages of encrypted files. The focus here is just on
345+
protecting the encryption keys, as those instantly give access to **all** user
346+
data in any following offline attack, rather than just some of it (where which
347+
data is included in that "some" might not be controlled by the attacker).
348+
349+
Solution overview
350+
-----------------
351+
352+
Inline encryption hardware typically has "keyslots" into which software can
353+
program keys for the hardware to use; the contents of keyslots typically can't
354+
be read back by software. As such, the above security goals could be achieved
355+
if the kernel simply erased its copy of the key(s) after programming them into
356+
keyslot(s) and thereafter only referred to them via keyslot number.
357+
358+
However, that naive approach runs into a couple problems:
359+
360+
- It limits the number of unlocked keys to the number of keyslots, which
361+
typically is a small number. In cases where there is only one encryption key
362+
system-wide (e.g., a full-disk encryption key), that can be tolerable.
363+
However, in general there can be many logged-in users with many different
364+
keys, and/or many running applications with application-specific encrypted
365+
storage areas. This is especially true if file-based encryption (e.g.
366+
fscrypt) is being used.
367+
368+
- Inline crypto engines typically lose the contents of their keyslots if the
369+
storage controller (usually UFS or eMMC) is reset. Resetting the storage
370+
controller is a standard error recovery procedure that is executed if certain
371+
types of storage errors occur, and such errors can occur at any time.
372+
Therefore, when inline crypto is being used, the operating system must always
373+
be ready to reprogram the keyslots without user intervention.
374+
375+
Thus, it is important for the kernel to still have a way to "remind" the
376+
hardware about a key, without actually having the raw key itself.
377+
378+
Somewhat less importantly, it is also desirable that the raw keys are never
379+
visible to software at all, even while being initially unlocked. This would
380+
ensure that a read-only compromise of system memory will never allow a key to be
381+
extracted to be used off-system, even if it occurs when a key is being unlocked.
382+
383+
To solve all these problems, some vendors of inline encryption hardware have
384+
made their hardware support *hardware-wrapped keys*. Hardware-wrapped keys
385+
are encrypted keys that can only be unwrapped (decrypted) and used by hardware
386+
-- either by the inline encryption hardware itself, or by a dedicated hardware
387+
block that can directly provision keys to the inline encryption hardware.
388+
389+
(We refer to them as "hardware-wrapped keys" rather than simply "wrapped keys"
390+
to add some clarity in cases where there could be other types of wrapped keys,
391+
such as in file-based encryption. Key wrapping is a commonly used technique.)
392+
393+
The key which wraps (encrypts) hardware-wrapped keys is a hardware-internal key
394+
that is never exposed to software; it is either a persistent key (a "long-term
395+
wrapping key") or a per-boot key (an "ephemeral wrapping key"). The long-term
396+
wrapped form of the key is what is initially unlocked, but it is erased from
397+
memory as soon as it is converted into an ephemerally-wrapped key. In-use
398+
hardware-wrapped keys are always ephemerally-wrapped, not long-term wrapped.
399+
400+
As inline encryption hardware can only be used to encrypt/decrypt data on-disk,
401+
the hardware also includes a level of indirection; it doesn't use the unwrapped
402+
key directly for inline encryption, but rather derives both an inline encryption
403+
key and a "software secret" from it. Software can use the "software secret" for
404+
tasks that can't use the inline encryption hardware, such as filenames
405+
encryption. The software secret is not protected from memory compromise.
406+
407+
Key hierarchy
408+
-------------
409+
410+
Here is the key hierarchy for a hardware-wrapped key::
411+
412+
Hardware-wrapped key
413+
|
414+
|
415+
<Hardware KDF>
416+
|
417+
-----------------------------
418+
| |
419+
Inline encryption key Software secret
420+
421+
The components are:
422+
423+
- *Hardware-wrapped key*: a key for the hardware's KDF (Key Derivation
424+
Function), in ephemerally-wrapped form. The key wrapping algorithm is a
425+
hardware implementation detail that doesn't impact kernel operation, but a
426+
strong authenticated encryption algorithm such as AES-256-GCM is recommended.
427+
428+
- *Hardware KDF*: a KDF (Key Derivation Function) which the hardware uses to
429+
derive subkeys after unwrapping the wrapped key. The hardware's choice of KDF
430+
doesn't impact kernel operation, but it does need to be known for testing
431+
purposes, and it's also assumed to have at least a 256-bit security strength.
432+
All known hardware uses the SP800-108 KDF in Counter Mode with AES-256-CMAC,
433+
with a particular choice of labels and contexts; new hardware should use this
434+
already-vetted KDF.
435+
436+
- *Inline encryption key*: a derived key which the hardware directly provisions
437+
to a keyslot of the inline encryption hardware, without exposing it to
438+
software. In all known hardware, this will always be an AES-256-XTS key.
439+
However, in principle other encryption algorithms could be supported too.
440+
Hardware must derive distinct subkeys for each supported encryption algorithm.
441+
442+
- *Software secret*: a derived key which the hardware returns to software so
443+
that software can use it for cryptographic tasks that can't use inline
444+
encryption. This value is cryptographically isolated from the inline
445+
encryption key, i.e. knowing one doesn't reveal the other. (The KDF ensures
446+
this.) Currently, the software secret is always 32 bytes and thus is suitable
447+
for cryptographic applications that require up to a 256-bit security strength.
448+
Some use cases (e.g. full-disk encryption) won't require the software secret.
449+
450+
Example: in the case of fscrypt, the fscrypt master key (the key that protects a
451+
particular set of encrypted directories) is made hardware-wrapped. The inline
452+
encryption key is used as the file contents encryption key, while the software
453+
secret (rather than the master key directly) is used to key fscrypt's KDF
454+
(HKDF-SHA512) to derive other subkeys such as filenames encryption keys.
455+
456+
Note that currently this design assumes a single inline encryption key per
457+
hardware-wrapped key, without any further key derivation. Thus, in the case of
458+
fscrypt, currently hardware-wrapped keys are only compatible with the "inline
459+
encryption optimized" settings, which use one file contents encryption key per
460+
encryption policy rather than one per file. This design could be extended to
461+
make the hardware derive per-file keys using per-file nonces passed down the
462+
storage stack, and in fact some hardware already supports this; future work is
463+
planned to remove this limitation by adding the corresponding kernel support.
464+
465+
Kernel support
466+
--------------
467+
468+
The inline encryption support of the kernel's block layer ("blk-crypto") has
469+
been extended to support hardware-wrapped keys as an alternative to raw keys,
470+
when hardware support is available. This works in the following way:
471+
472+
- A ``key_types_supported`` field is added to the crypto capabilities in
473+
``struct blk_crypto_profile``. This allows device drivers to declare that
474+
they support raw keys, hardware-wrapped keys, or both.
475+
476+
- ``struct blk_crypto_key`` can now contain a hardware-wrapped key as an
477+
alternative to a raw key; a ``key_type`` field is added to
478+
``struct blk_crypto_config`` to distinguish between the different key types.
479+
This allows users of blk-crypto to en/decrypt data using a hardware-wrapped
480+
key in a way very similar to using a raw key.
481+
482+
- A new method ``blk_crypto_ll_ops::derive_sw_secret`` is added. Device drivers
483+
that support hardware-wrapped keys must implement this method. Users of
484+
blk-crypto can call ``blk_crypto_derive_sw_secret()`` to access this method.
485+
486+
- The programming and eviction of hardware-wrapped keys happens via
487+
``blk_crypto_ll_ops::keyslot_program`` and
488+
``blk_crypto_ll_ops::keyslot_evict``, just like it does for raw keys. If a
489+
driver supports hardware-wrapped keys, then it must handle hardware-wrapped
490+
keys being passed to these methods.
491+
492+
blk-crypto-fallback doesn't support hardware-wrapped keys. Therefore,
493+
hardware-wrapped keys can only be used with actual inline encryption hardware.
494+
495+
Testability
496+
-----------
497+
498+
Both the hardware KDF and the inline encryption itself are well-defined
499+
algorithms that don't depend on any secrets other than the unwrapped key.
500+
Therefore, if the unwrapped key is known to software, these algorithms can be
501+
reproduced in software in order to verify the ciphertext that is written to disk
502+
by the inline encryption hardware.
503+
504+
However, the unwrapped key will only be known to software for testing if the
505+
"import" functionality is used. Proper testing is not possible in the
506+
"generate" case where the hardware generates the key itself. The correct
507+
operation of the "generate" mode thus relies on the security and correctness of
508+
the hardware RNG and its use to generate the key, as well as the testing of the
509+
"import" mode as that should cover all parts other than the key generation.
510+
511+
For an example of a test that verifies the ciphertext written to disk in the
512+
"import" mode, see the fscrypt hardware-wrapped key tests in xfstests, or
513+
`Android's vts_kernel_encryption_test
514+
<https://android.googlesource.com/platform/test/vts-testcase/kernel/+/refs/heads/main/encryption/>`_.

block/blk-crypto-fallback.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static struct bio_set crypto_bio_split;
8787
* This is the key we set when evicting a keyslot. This *should* be the all 0's
8888
* key, but AES-XTS rejects that key, so we use some random bytes instead.
8989
*/
90-
static u8 blank_key[BLK_CRYPTO_MAX_KEY_SIZE];
90+
static u8 blank_key[BLK_CRYPTO_MAX_RAW_KEY_SIZE];
9191

9292
static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
9393
{
@@ -119,7 +119,7 @@ blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
119119
blk_crypto_fallback_evict_keyslot(slot);
120120

121121
slotp->crypto_mode = crypto_mode;
122-
err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], key->raw,
122+
err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], key->bytes,
123123
key->size);
124124
if (err) {
125125
blk_crypto_fallback_evict_keyslot(slot);
@@ -539,7 +539,7 @@ static int blk_crypto_fallback_init(void)
539539
if (blk_crypto_fallback_inited)
540540
return 0;
541541

542-
get_random_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
542+
get_random_bytes(blank_key, sizeof(blank_key));
543543

544544
err = bioset_init(&crypto_bio_split, 64, 0, 0);
545545
if (err)
@@ -561,6 +561,7 @@ static int blk_crypto_fallback_init(void)
561561

562562
blk_crypto_fallback_profile->ll_ops = blk_crypto_fallback_ll_ops;
563563
blk_crypto_fallback_profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
564+
blk_crypto_fallback_profile->key_types_supported = BLK_CRYPTO_KEY_TYPE_RAW;
564565

565566
/* All blk-crypto modes have a crypto API fallback. */
566567
for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++)

block/blk-crypto-internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct blk_crypto_mode {
1414
const char *name; /* name of this mode, shown in sysfs */
1515
const char *cipher_str; /* crypto API name (for fallback case) */
1616
unsigned int keysize; /* key size in bytes */
17+
unsigned int security_strength; /* security strength in bytes */
1718
unsigned int ivsize; /* iv size in bytes */
1819
};
1920

block/blk-crypto-profile.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
352352
return false;
353353
if (profile->max_dun_bytes_supported < cfg->dun_bytes)
354354
return false;
355+
if (!(profile->key_types_supported & cfg->key_type))
356+
return false;
355357
return true;
356358
}
357359

@@ -462,6 +464,44 @@ bool blk_crypto_register(struct blk_crypto_profile *profile,
462464
}
463465
EXPORT_SYMBOL_GPL(blk_crypto_register);
464466

467+
/**
468+
* blk_crypto_derive_sw_secret() - Derive software secret from wrapped key
469+
* @bdev: a block device that supports hardware-wrapped keys
470+
* @eph_key: a hardware-wrapped key in ephemerally-wrapped form
471+
* @eph_key_size: size of @eph_key in bytes
472+
* @sw_secret: (output) the software secret
473+
*
474+
* Given a hardware-wrapped key in ephemerally-wrapped form (the same form that
475+
* it is used for I/O), ask the hardware to derive the secret which software can
476+
* use for cryptographic tasks other than inline encryption. This secret is
477+
* guaranteed to be cryptographically isolated from the inline encryption key,
478+
* i.e. derived with a different KDF context.
479+
*
480+
* Return: 0 on success, -EOPNOTSUPP if the block device doesn't support
481+
* hardware-wrapped keys, -EBADMSG if the key isn't a valid
482+
* ephemerally-wrapped key, or another -errno code.
483+
*/
484+
int blk_crypto_derive_sw_secret(struct block_device *bdev,
485+
const u8 *eph_key, size_t eph_key_size,
486+
u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])
487+
{
488+
struct blk_crypto_profile *profile =
489+
bdev_get_queue(bdev)->crypto_profile;
490+
int err;
491+
492+
if (!profile)
493+
return -EOPNOTSUPP;
494+
if (!(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
495+
return -EOPNOTSUPP;
496+
if (!profile->ll_ops.derive_sw_secret)
497+
return -EOPNOTSUPP;
498+
blk_crypto_hw_enter(profile);
499+
err = profile->ll_ops.derive_sw_secret(profile, eph_key, eph_key_size,
500+
sw_secret);
501+
blk_crypto_hw_exit(profile);
502+
return err;
503+
}
504+
465505
/**
466506
* blk_crypto_intersect_capabilities() - restrict supported crypto capabilities
467507
* by child device
@@ -485,10 +525,12 @@ void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
485525
child->max_dun_bytes_supported);
486526
for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++)
487527
parent->modes_supported[i] &= child->modes_supported[i];
528+
parent->key_types_supported &= child->key_types_supported;
488529
} else {
489530
parent->max_dun_bytes_supported = 0;
490531
memset(parent->modes_supported, 0,
491532
sizeof(parent->modes_supported));
533+
parent->key_types_supported = 0;
492534
}
493535
}
494536
EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities);
@@ -521,6 +563,9 @@ bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
521563
target->max_dun_bytes_supported)
522564
return false;
523565

566+
if (reference->key_types_supported & ~target->key_types_supported)
567+
return false;
568+
524569
return true;
525570
}
526571
EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities);
@@ -555,5 +600,6 @@ void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
555600
sizeof(dst->modes_supported));
556601

557602
dst->max_dun_bytes_supported = src->max_dun_bytes_supported;
603+
dst->key_types_supported = src->key_types_supported;
558604
}
559605
EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);

0 commit comments

Comments
 (0)