x86: Support Key Locker
Subsystems (7)
v5 cover x86: Support Key Locker
Changes from v4 [1]:
* Drop CBC mode support (PATCH10). (Eric Biggers)
* Update the changelog (PATCH8). (Rafael Wysocki)
A couple of other things outside of these patches are still in progress:
* Support DM-crypt/cryptsetup for Key Locker usage (Andy Lutomirski) [2].
* Understand decryption under-performance (Eric Biggers and Milan Broz) [3][4].
This feature's usage for the threat model can be found in the previous cover letter [1]. This version is based on 5.16.
Thanks,
Chang
[1] V4: https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
Chang S. Bae (12):
Documentation/x86: Document Key Locker
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/asm: Add a wrapper function for the LOADIWKEY instruction x86/msr-index: Add MSRs for Key Locker internal wrapping key x86/keylocker: Define Key Locker CPUID leaf x86/cpu/keylocker: Load an internal wrapping key at boot-time x86/PM/keylocker: Restore internal wrapping key on resume from ACPI
S3/4
x86/cpu: Add a configuration and command line option for Key Locker
crypto: x86/aes - Prepare for a new AES implementation
crypto: x86/aes-kl - Support AES algorithm using Key Locker
instructions
crypto: x86/aes-kl - Support XTS mode
.../admin-guide/kernel-parameters.txt | 2 + Documentation/x86/index.rst | 1 + Documentation/x86/keylocker.rst | 98 +++ arch/x86/Kconfig | 3 + arch/x86/crypto/Makefile | 5 +- arch/x86/crypto/aes-intel_asm.S | 26 + arch/x86/crypto/aes-intel_glue.c | 125 ++++ arch/x86/crypto/aes-intel_glue.h | 48 ++ arch/x86/crypto/aeskl-intel_asm.S | 633 ++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 216 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +- arch/x86/crypto/aesni-intel_glue.c | 239 ++----- arch/x86/crypto/aesni-intel_glue.h | 17 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/keylocker.h | 45 ++ arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/special_insns.h | 32 + arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 21 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/keylocker.c | 199 ++++++ arch/x86/kernel/smpboot.c | 2 + arch/x86/lib/x86-opcode-map.txt | 11 +- arch/x86/power/cpu.c | 2 + crypto/Kconfig | 36 + tools/arch/x86/lib/x86-opcode-map.txt | 11 +- 28 files changed, 1633 insertions(+), 216 deletions(-) create mode 100644 Documentation/x86/keylocker.rst create mode 100644 arch/x86/crypto/aes-intel_asm.S create mode 100644 arch/x86/crypto/aes-intel_glue.c create mode 100644 arch/x86/crypto/aes-intel_glue.h create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h create mode 100644 arch/x86/include/asm/keylocker.h create mode 100644 arch/x86/kernel/keylocker.c
base-commit: df0cc57e057f18e44dac8e6c18aba47ab53202f9 --
2.17.1
PATCH v5 1/12 Documentation/x86: Document Key Locker
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
Changes from RFC v2:
* Add as a new patch.
--- Documentation/x86/index.rst | 1 + Documentation/x86/keylocker.rst | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Documentation/x86/keylocker.rst
Documentation/x86/index.rst +1 -0
@@ -38,3 +38,4 @@ x86-specific Documentation features elf_auxvec xstate + keylocker
Documentation/x86/keylocker.rst new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v5 01/12] Documentation/x86: Document Key Locker
Quoted 27 lines · Chang S. Bae
+==============
Documentation/x86/keylocker.rst new file +26
-0+x86 Key Locker +============== + +Introduction +============ + +Key Locker is a CPU feature feature to reduce key exfiltration +opportunities while maintaining a programming interface similar to AES-NI. +It converts the AES key into an encoded form, called the 'key handle'. The +key handle is a wrapped version of the clear-text key where the wrapping +key has limited exposure. Once converted, all subsequent data encryption +using new AES instructions (AES-KL) uses this key handle, reducing the +exposure of private key material in memory. + +Internal Wrapping Key (IWKey) +============================= + +The CPU-internal wrapping key is an entity in a software-invisible CPU +state. On every system boot, a new key is loaded. So the key handle that +was encoded by the old wrapping key is no longer usable on system shutdown +or reboot. + +And the key may be lost on the following exceptional situation upon wakeup: + +IWKey Restore Failure +---------------------
Documentation/x86/keylocker.rst line 39
Quoted 10 lines
Documentation/x86/keylocker.rst new file +10
-0+ +The CPU state is volatile with the ACPI S3/4 sleep states. When the system +supports those states, the key has to be backed up so that it is restored +on wake up. The kernel saves the key in non-volatile media. + +The event of an IWKey restore failure upon resume from suspend, all +established key handles become invalid. In flight dm-crypt operations +receive error results from pending operations. In the likely scenario that +dm-crypt is hosting the root filesystem the recovery is identical to if a +storage controller failed to resume from suspend, reboot. If the volume
"suspend and reboot"?
Quoted 49 lines
Documentation/x86/keylocker.rst new file +49
-0+impacted by an IWKey restore failure is a data-volume then it is possible +that I/O errors on that volume do not bring down the rest of the system. +However, a reboot is still required because the kernel will have +soft-disabled Key Locker. Upon the failure, the crypto library code will +return -ENODEV on every AES-KL function call. The Key Locker implementation +only loads a new IWKey at initial boot, not any time after like resume from +suspend. + +Use Case and Non-use Cases +========================== + +Bare metal disk encryption is the only intended use case. + +Userspace usage is not supported because there is no ABI provided to +communicate and coordinate wrapping-key restore failure to userspace. For +now, key restore failures are only coordinated with kernel users. But the +kernel can not prevent userspace from using the feature's AES instructions +('AES-KL') when the feature has been enabled. So, the lack of userspace +support is only documented, not actively enforced. + +Key Locker is not expected to be advertised to guest VMs and the kernel +implementation ignores it even if the VMM enumerates the capability. The +expectation is that a guest VM wants private IWKey state, but the +architecture does not provide that. An emulation of that capability, by +caching per VM IWKeys in memory, defeats the purpose of Key Locker. The +backup / restore facility is also not performant enough to be suitable for +guest VM context switches. + +AES Instruction Set +=================== + +The feature accompanies a new AES instruction set. This instruction set is +analogous to AES-NI. A set of AES-NI instructions can be mapped to an +AES-KL instruction. For example, AESENC128KL is responsible for ten rounds +of transformation, which is equivalent to nine times AESENC and one +AESENCLAST in AES-NI. + +But they have some notable differences: + +* AES-KL provides a secure data transformation using an encrypted key. + +* If an invalid key handle is provided, e.g. a corrupted one or a handle + restriction failure, the instruction fails with setting RFLAGS.ZF. The + crypto library implementation includes the flag check to return an error + code. Note that the flag is also set when the internal wrapping key is + changed because of missing backup. + +* AES-KL implements support for 128-bit and 256-bit keys, but there is no + AES-KL instruction to process an 192-bit key. But there is no AES-KL
Documentation/x86/keylocker.rst line 98
Quoted 10 lines
Documentation/x86/keylocker.rst new file +10
-0+ instruction to process a 192-bit key. The AES-KL cipher implementation + logs a warning message with a 192-bit key and then falls back to AES-NI. + So, this 192-bit key-size limitation is only documented, not enforced. It + means the key will remain in clear-text in memory. This is to meet Linux + crypto-cipher expectation that each implementation must support all the + AES-compliant key sizes. + +* Some AES-KL hardware implementation may have noticeable performance + overhead when compared with AES-NI instructions. +
The rest is LGTM.
Signature · 1 line
An old man doll... just what I always wanted! - Clara
PATCH v5 2/12 x86/cpufeature: Enumerate Key Locker feature
In reply to Chang S. BaeKey Locker is a CPU feature to minimize exposure of clear-text key material. An encoded form, called 'key handle', is referenced for data encryption or decryption instead of accessing the clear text key.
A wrapping key loaded in the CPU's software-inaccessible state is used to transform a user key into a key handle.
It supports Advanced Encryption Standard (AES) cipher algorithm with new SIMD instruction set like its predecessor (AES-NI). So a new AES implementation will follow in the kernel's crypto library.
Here add it to enumerate the hardware capability, but it will not be shown in /proc/cpuinfo as userspace usage is not supported.
Make the feature depend on XMM2 as it comes with AES SIMD instructions.
Add X86_FEATURE_KEYLOCKER to the disabled features mask. It will be enabled under a new config option.
---
Changes from RFC v2:
* Do not publish the feature flag to userspace.
* Update the changelog.
Changes from RFC v1:
* Updated the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -361,6 +361,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_KEYLOCKER (16*32+23)/* "" Key Locker */ #define X86_FEATURE_BUS_LOCK_DETECT (16*32+24)/* Bus Lock detect */ #define X86_FEATURE_CLDEMOTE (16*32+25)/* CLDEMOTE instruction */ #define X86_FEATURE_MOVDIRI (16*32+27)/* MOVDIRI instruction */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -44,6 +44,12 @@# define DISABLE_OSPKE (1<<(X86_FEATURE_OSPKE & 31))#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */ +#ifdef CONFIG_X86_KEYLOCKER +# define DISABLE_KEYLOCKER 0 +#else +# define DISABLE_KEYLOCKER (1<<(X86_FEATURE_KEYLOCKER & 31)) +#endif /* CONFIG_X86_KEYLOCKER */ +#ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0#else
@@ -85,7 +91,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_KEYLOCKER)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK19 0
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -124,6 +124,8 @@#define X86_CR4_PCIDE _BITUL(X86_CR4_PCIDE_BIT)#define X86_CR4_OSXSAVE_BIT 18/* enable xsave and xrestore */ #define X86_CR4_OSXSAVE _BITUL(X86_CR4_OSXSAVE_BIT) +#define X86_CR4_KEYLOCKER_BIT 19/* enable Key Locker */ +#define X86_CR4_KEYLOCKER _BITUL(X86_CR4_KEYLOCKER_BIT)#define X86_CR4_SMEP_BIT 20/* enable SMEP support */ #define X86_CR4_SMEP _BITUL(X86_CR4_SMEP_BIT)#define X86_CR4_SMAP_BIT 21/* enable SMAP support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -78,6 +78,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, + { X86_FEATURE_KEYLOCKER, X86_FEATURE_XMM2 }, {} };
Signature · 1 line
2.17.1
PATCH v5 3/12 x86/insn: Add Key Locker instructions to the opcode map
In reply to Chang S. BaeAdd the following Key Locker instructions to the opcode map:
LOADIWKEY:
Load an CPU-internal wrapping key.
ENCODEKEY128:
Wrap a 128-bit AES key to a key handle.
ENCODEKEY256:
Wrap a 256-bit AES key to a key handle.
AESENC128KL:
Encrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESENC256KL:
Encrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESDEC128KL:
Decrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESDEC256KL:
Decrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESENCWIDE128KL:
Encrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESENCWIDE256KL:
Encrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
AESDECWIDE128KL:
Decrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESDECWIDE256KL:
Decrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
Details of these instructions can be found in Intel Software Developer's Manual.
---
Changes from RFC v1:
* Separated out the LOADIWKEY addition in a new patch.
* Included AES instructions to avoid warning messages when the AES Key Locker module is built.
--- arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- tools/arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -794,11 +794,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -808,6 +809,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
tools/arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -794,11 +794,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -808,6 +809,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
Signature · 1 line
2.17.1
PATCH v5 4/12 x86/asm: Add a wrapper function for the LOADIWKEY instruction
In reply to Chang S. BaeKey Locker introduces a CPU-internal wrapping key to encode a user key to a key handle. Then a key handle is referenced instead of the plain text key.
The new instruction loads an internal wrapping key in the software-inaccessible CPU state. It operates only in kernel mode.
Define struct iwkey to pass the key value.
The kernel will use this function to load a new key at boot time when the feature is enabled.
---
Changes from RFC v2:
* Separate out the code as a new patch.
* Improve the usability with the new struct as an argument. (Dan Williams)
Note, Dan wondered if:
WARN_ON(!irq_fpu_usable());
would be appropriate in the load_xmm_iwkey() function.
--- arch/x86/include/asm/keylocker.h | 25 ++++++++++++++++++++++ arch/x86/include/asm/special_insns.h | 32 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/keylocker.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_KEYLOCKER_H +#define _ASM_KEYLOCKER_H + +#ifndef __ASSEMBLY__ + +#include <asm/fpu/types.h> + +/** +* struct iwkey - A temporary internal wrapping key storage. +* @integrity_key: A 128-bit key to check that key handles have not +* been tampered with. +* @encryption_key: A 256-bit encryption key used in +* wrapping/unwrapping a clear text key. +* +* This storage should be flushed immediately after loaded. +*/ +struct iwkey { +struct reg_128_bit integrity_key; +struct reg_128_bit encryption_key[2 ]; +}; + +#endif /*__ASSEMBLY__ */ +#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +32 -0
@@ -9,6 +9,7 @@#include <asm/processor-flags.h> #include <linux/irqflags.h> #include <linux/jump_label.h> +#include <asm/keylocker.h> /* * The compiler should not reorder volatile asm statements with respect to each
@@ -294,6 +295,37 @@ static inline int enqcmds(void __iomem *dst, const void *src)return 0 ; } +/** +* load_xmm_iwkey - Load a CPU-internal wrapping key +* @key: A struct iwkey pointer. +* +* Load @key to XMMs then do LOADIWKEY. After this, flush XMM +* registers. Caller is responsible for kernel_cpu_begin(). +*/ +static inline void load_xmm_iwkey (struct iwkey *key) +{ +struct reg_128_bit zeros = {0 }; + + asmvolatile ("movdqu %0, %%xmm0; movdqu %1, %%xmm1; movdqu %2, %%xmm2;" + ::"m" (key->integrity_key),"m" (key->encryption_key[0 ]), +"m" (key->encryption_key[1 ])); + +/* +* LOADIWKEY %xmm1,%xmm2 +* +* EAX and XMM0 are implicit operands. Load a key value +* from XMM0-2 to a software-invisible CPU state. With zero +* in EAX, CPU does not do hardware randomization and the key +* backup is allowed. +* +* This instruction is supported by binutils >= 2.36. +*/ + asmvolatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1" ::"a" (0 )); + + asmvolatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;" + ::"m" (zeros)); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
Signature · 1 line
2.17.1
PATCH v5 5/12 x86/msr-index: Add MSRs for Key Locker internal wrapping key
In reply to Chang S. BaeThe CPU state that contains the internal wrapping key is in the same power domain as the cache. So any sleep state that would invalidate the cache (like S3) also invalidates the state of the wrapping key.
But, since the state is inaccessible to software, it needs a special mechanism to save and restore the key during deep sleep.
A set of new MSRs are provided as an abstract interface to save and restore the wrapping key, and to check the key status.
The wrapping key is saved in a platform-scoped state of non-volatile media. The backup itself and its path from the CPU are encrypted and integrity protected.
But this storage's non-volatility is not architecturally guaranteed across off states, such as S5 and G3.
The MSRs will be used to back up the key for S3/4 sleep states. Then the kernel code writes one of them to restore the key in each CPU state.
---
Changes from RFC v2:
* Update the changelog. (Dan Williams)
* Rename the MSRs. (Dan Williams)
--- arch/x86/include/asm/msr-index.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
@@ -941,4 +941,10 @@#define MSR_VM_IGNNE 0xc0010115#define MSR_VM_HSAVE_PA 0xc0010117 +/* MSRs for managing an internal wrapping key for Key Locker. */ +#define MSR_IA32_IWKEY_COPY_STATUS 0x00000990 +#define MSR_IA32_IWKEY_BACKUP_STATUS 0x00000991 +#define MSR_IA32_BACKUP_IWKEY_TO_PLATFORM 0x00000d91 +#define MSR_IA32_COPY_IWKEY_TO_LOCAL 0x00000d92 +#endif /* _ASM_X86_MSR_INDEX_H */
Signature · 1 line
2.17.1
PATCH v5 6/12 x86/keylocker: Define Key Locker CPUID leaf
In reply to Chang S. BaeDefine the feature-specific CPUID leaf and bits. Both Key Locker enabling code in the x86 core and AES Key Locker code in the crypto library will reference them.
---
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/include/asm/keylocker.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/keylocker.h +7 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <linux/bits.h> #include <asm/fpu/types.h> /**
@@ -21,5 +22,11 @@ struct iwkey {struct reg_128_bit encryption_key[2 ]; }; +#define KEYLOCKER_CPUID 0x019 +#define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0) +#define KEYLOCKER_CPUID_EBX_AESKLE BIT(0) +#define KEYLOCKER_CPUID_EBX_WIDE BIT(2) +#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
Signature · 1 line
2.17.1
PATCH v5 7/12 x86/cpu/keylocker: Load an internal wrapping key at boot-time
In reply to Chang S. BaeThe Internal Wrapping Key (IWKey) is an entity of Key Locker to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
IWKey needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
The kernel generates random bytes and load them at boot time. These bytes are flushed out immediately.
Setting the CR4.KL bit does not always enable the feature so ensure the dynamic CPU bit (CPUID.AESKLE) is set before loading the key.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt consumption, and that switching IWKey per VM is untenable, explicitly skip Key Locker setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.
Note, Dan wonders that given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but as Dave notes there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
--- arch/x86/include/asm/keylocker.h | 9 ++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/keylocker.c | 79 ++++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 2 + 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/include/asm/keylocker.h +9 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h>
@@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/Makefile +1 -0
@@ -144,6 +144,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/cpu/common.c +4 -1
@@ -57,6 +57,8 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> +#include <asm/uv/uv.h> #include <asm/sigframe.h>
@@ -1595,10 +1597,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker (c);/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c new file +79 -0
79 lines not on this page · raw message
arch/x86/kernel/smpboot.c +2 -0
@@ -82,6 +82,7 @@#include <asm/spec-ctrl.h> #include <asm/hw_irq.h> #include <asm/stackprotector.h> +#include <asm/keylocker.h> #ifdef CONFIG_ACPI_CPPC_LIB #include <acpi/cppc_acpi.h>
@@ -1489,6 +1490,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends ();mtrr_aps_init (); +destroy_keylocker_data (); }static int __initdata setup_possible_cpus =-1 ;
Signature · 1 line
2.17.1
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 140 lines · Chang S. Bae
The Internal Wrapping Key (IWKey) is an entity of Key Locker to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
IWKey needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
The kernel generates random bytes and load them at boot time. These bytes are flushed out immediately.
Setting the CR4.KL bit does not always enable the feature so ensure the dynamic CPU bit (CPUID.AESKLE) is set before loading the key.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt consumption, and that switching IWKey per VM is untenable, explicitly skip Key Locker setup in the
X86_FEATURE_HYPERVISORcase.Signed-off-by Chang S. Bae <[email protected]>Reviewed-by Dan Williams <[email protected]>---
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.Note, Dan wonders that given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but as Dave notes there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
--- arch/x86/include/asm/keylocker.h | 9 ++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/keylocker.c | 79 ++++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 2 + 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/keylocker.carch/x86/include/asm/keylocker.h +9
-0@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h> @@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */ arch/x86/kernel/Makefile +1
-0@@ -144,6 +144,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.oarch/x86/kernel/cpu/common.c +4
-1@@ -57,6 +57,8 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> +#include <asm/uv/uv.h> #include <asm/sigframe.h> @@ -1595,10 +1597,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker (c);/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {arch/x86/kernel/keylocker.c new file +20
-0@@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Setup Key Locker feature and support internal wrapping key +* management. +*/ + +#include <linux/random.h> +#include <linux/poison.h> + +#include <asm/fpu/api.h> +#include <asm/keylocker.h> +#include <asm/tlbflush.h> + +static __initdata struct keylocker_setup_data { +struct iwkey key; +} kl_setup; + +static void __init generate_keylocker_data (void ) +{
arch/x86/kernel/keylocker.c line 30
Quoted 10 lines
arch/x86/kernel/keylocker.c new file +10
-0+get_random_bytes (&kl_setup.key.integrity_key,sizeof (kl_setup.key.integrity_key)); +get_random_bytes (&kl_setup.key.encryption_key,sizeof (kl_setup.key.encryption_key)); +} + +void __init destroy_keylocker_data (void ) +{ +memset (&kl_setup.key,KEY_DESTROY ,sizeof (kl_setup.key)); +} + +static void __init load_keylocker (void )
I am late to this party by 6 months, but:load_keylocker() cannot be __init, as it gets called during SMP core onlining.
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 2 lines · Evan Green
Quoted 1 line · Chang S. Bae
<snip>
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static void __init load_keylocker(void)I am late to this party by 6 months, but:
load_keylocker()cannot be__init, as it gets called during SMP core onlining.
Yeah, it looks like the case with this patch only.
But the next patch [1] limits the call during boot time only:
if (c == &boot_cpu_data) {
...
load_keylocker();
...
} else {
...
if (!kl_setup.initialized) {
load_keylocker();
} else if (valid_kl) {
rc = copy_keylocker();
...
}
}kl_setup.initialized is set by native_smp_cpus_done() -> destroy_keylocker_data() when CPUs are booted. Then load_keylocker() is not called because the root key (aka IWKey) is no longer available in memory.
Now this 'valid_kl' flag should be always on unless the root key backup is corrupted. Then copy_keylocker() loads the root key from the backup in the platform state.
So I think the onlining CPU won't call it.
Maybe this bit can be much clarified in a separate (new) patch, instead of being part of another like [1].
Thanks,
Chang
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 34 lines · Chang S. Bae
Quoted 2 lines · Evan Green
Quoted 1 line · Chang S. Bae
<snip>
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static void __init load_keylocker(void)I am late to this party by 6 months, but:
load_keylocker()cannot be__init, as it gets called during SMP core onlining.Yeah, it looks like the case with this patch only.
But the next patch [1] limits the call during boot time only:
if (c == &boot_cpu_data) { ... load_keylocker(); ... } else { ... if (!kl_setup.initialized) { load_keylocker(); } else if (valid_kl) { rc = copy_keylocker(); ... } }kl_setup.initialized is set by
native_smp_cpus_done()->destroy_keylocker_data()when CPUs are booted. Thenload_keylocker()is not called because the root key (aka IWKey) is no longer available in memory.
Quoted 8 lines
Now this '
valid_kl' flag should be always on unless the root key backup is corrupted. Thencopy_keylocker()loads the root key from the backup in the platform state.So I think the onlining CPU won't call it.
Maybe this bit can be much clarified in a separate (new) patch, instead of being part of another like [1].
Whatever we ended up landing in the ChromeOS tree (which I think was v4 of this series) actively hit this bug in hibernation, which is how I found it. I couldn't get a full backtrace because the backtracing code tripped over itself as well for some reason. If the next patch in this series is different from what we landed in ChromeOS, then maybe your description is correct, but I haven't dug in to understand the delta.
-Evan
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 8 lines · Evan Green
Whatever we ended up landing in the ChromeOS tree (which I think was v4 of this series) actively hit this bug in hibernation, which is how I found it. I couldn't get a full backtrace because the backtracing code tripped over itself as well for some reason. If the next patch in this series is different from what we landed in ChromeOS, then maybe your description is correct, but I haven't dug in to understand the delta.
So the change from v4 is simply dropping CBC mode. Marvin who reported another issue told me that he pushed the fix to some Chrome repository. But I don't know that's the same repo that you mentioned. Are you able to locate that tree if possible?
Also, it would be nice to have more detail about that hibernation bug.
Thanks,
Chang
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 11 lines · Chang S. Bae
Quoted 8 lines · Evan Green
Whatever we ended up landing in the ChromeOS tree (which I think was v4 of this series) actively hit this bug in hibernation, which is how I found it. I couldn't get a full backtrace because the backtracing code tripped over itself as well for some reason. If the next patch in this series is different from what we landed in ChromeOS, then maybe your description is correct, but I haven't dug in to understand the delta.
Quoted 4 lines
So the change from v4 is simply dropping CBC mode. Marvin who reported another issue told me that he pushed the fix to some Chrome repository. But I don't know that's the same repo that you mentioned. Are you able to locate that tree if possible?
I see. The only ChromeOS tree I'm aware of where keylocker has landed is our 5.10 tree. This is the change where it landed:
https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/3373776/12
Quoted 2 lines
Also, it would be nice to have more detail about that hibernation bug.
Here's the log I've got that pointed me down this path:
https://pastebin.com/VvR1EHvE
Relevant bit pasted below:
<6>[43486.263035] Enabling non-boot CPUs ... <6>[43486.263081] x86: Booting SMP configuration: <6>[43486.263082] smpboot: Booting Node 0 Processor 1 APIC 0x1 <2>[43486.264010] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) <1>[43486.264019] BUG: unable to handle page fault for address: ffffffff94b483a6 <1>[43486.264021] #PF: supervisor instruction fetch in kernel mode <1>[43486.264023] #PF: error_code(0x0011) - permissions violation <6>[43486.264025] PGD 391c0e067 P4D 391c0e067 PUD 391c0f063 PMD 10006c063 PTE 8000000392148163 <4>[43486.264031] Oops: 0011 [#1] PREEMPT SMP NOPTI <4>[43486.264035] CPU: 1 PID: 0 Comm: swapper/1 Tainted: G U 5.10.136-19391-gadfe4d4b8c04 #1 b640352a7a0e5f1522aed724296ad63f90c007df <4>[43486.264036] Hardware name: Google Primus/Primus, BIOS Google_Primus.14505.145.0 06/23/2022 <4>[43486.264042] RIP: 0010:load_keylocker+0x0/0x7f <4>[43486.264044] Code: 02 46 0a 0c 07 08 44 0b 24 00 00 00 10 26 00 00 44 d5 e9 ff dd 00 00 00 00 41 0e 10 86 02 43 0d 06 42 8d 03 49 8c 04 02 61 0a <0c> 07 08 48 0b 00 24 00 00 00 38 26 00 00 fc d5 e9 ff ba 00 00 00 <4>[43486.264046] RSP: 0000:ffffb1c7000afe50 EFLAGS: 00010046 <4>[43486.264048] RAX: ffffffff9483a898 RBX: ffff8d64ef855440 RCX: 0000000000310800 <4>[43486.264049] RDX: 0000000000310800 RSI: 0000000000000000 RDI: 00000000003f0ea0 <4>[43486.264051] RBP: ffffb1c7000afe88 R08: 0000000000000000 R09: 0000000000003000 <4>[43486.264052] R10: 0000000000000500 R11: ffffffff92c6c775 R12: ffff8d64ef8554c0 <4>[43486.264053] R13: 0000000000000000 R14: 0000000000000082 R15: ffff8d64ef855460 <4>[43486.264055] FS: 0000000000000000(0000) GS:ffff8d64ef840000(0000) knlGS:0000000000000000 <4>[43486.264057] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 <4>[43486.264058] CR2: ffffffff94b483a6 CR3: 0000000391c0c001 CR4: 00000000003f0ea0 <4>[43486.264063] invalid opcode: 0000 [#2] PREEMPT SMP NOPTI <4>[43486.264065] CPU: 1 PID: 0 Comm: swapper/1 Tainted: G U 5.10.136-19391-gadfe4d4b8c04 #1 b640352a7a0e5f1522aed724296ad63f90c007df <4>[43486.264066] Hardware name: Google Primus/Primus, BIOS Google_Primus.14505.145.0 06/23/2022 <4>[43486.264069] RIP: 0010:__show_regs+0x2ed/0x338 <4>[43486.264071] Code: 81 fc 00 04 00 00 75 44 48 f7 05 ca 83 90 01 10 00 00 00 0f 84 fa fd ff ff 31 d2 48 f7 05 b7 83 90 01 10 00 00 00 74 07 31 c9 <0f> 01 ee 89 c2 48 c7 c7 90 38 29 94 4c 89 f6 48 83 c4 28 5b 41 5c <4>[43486.264072] RSP: 0000:ffffb1c7000afc90 EFLAGS: 00010046 <4>[43486.264074] RAX: 00000000ffff0ff0 RBX: 0000000000000000 RCX: 0000000000000000 <4>[43486.264075] RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffffffff94cf27f4 <4>[43486.264076] RBP: ffffb1c7000afce0 R08: 0000000000000000 R09: 00000000ffffdfff <4>[43486.264078] R10: ffffffff94658600 R11: 3fffffffffffffff R12: 0000000000000400 <4>[43486.264079] R13: ffff8d64ef840000 R14: ffffffff9435d0a9 R15: 00000000ffff0ff0 <4>[43486.264080] FS: 0000000000000000(0000) GS:ffff8d64ef840000(0000) knlGS:0000000000000000 <4>[43486.264082] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 <4>[43486.264083] CR2: ffffffff94b483a6 CR3: 0000000391c0c001 CR4: 00000000003f0ea0 <4>[43486.264085] invalid opcode: 0000 [#3] PREEMPT SMP NOPTI <4>[43486.264086] CPU: 1 PID: 0 Comm: swapper/1 Tainted: G U 5.10.136-19391-gadfe4d4b8c04 #1 b640352a7a0e5f1522aed724296ad63f90c007df <4>[43486.264088] Hardware name: Google Primus/Primus, BIOS Google_Primus.14505.145.0 06/23/2022 <4>[43486.264089] RIP: 0010:__show_regs+0x2ed/0x338
I landed this change, though I'm still working on verifying the issue goes away with this fix:
https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/3851401
I don't have direct access to this machine, but I wonder if a simple cpu hotplug might also exercise this path.
-Evan
Quoted 4 lines
Thanks,
Chang
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 3 lines · Evan Green
Here's the log I've got that pointed me down this path:
https://pastebin.com/VvR1EHvE
<3>[43486.261583] x86/keylocker: The key backup access failed with
read error.
<3>[43486.261584] x86/keylocker: Failed to restore internal wrapping key.
Looks like the IWKey backup was corrupted on that system.
Quoted 11 lines
Relevant bit pasted below:
<6>[43486.263035] Enabling non-boot CPUs ... <6>[43486.263081] x86: Booting SMP configuration: <6>[43486.263082] smpboot: Booting Node 0 Processor 1 APIC 0x1 <2>[43486.264010] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) <1>[43486.264019] BUG: unable to handle page fault for address: ffffffff94b483a6 <1>[43486.264021] #PF: supervisor instruction fetch in kernel mode <1>[43486.264023] #PF: error_code(0x0011) - permissions violation <6>[43486.264025] PGD 391c0e067 P4D 391c0e067 PUD 391c0f063 PMD
Quoted 8 lines
10006c063PTE 8000000392148163<4>[43486.264031] Oops: 0011 [#1] PREEMPT SMP NOPTI <4>[43486.264035] CPU: 1 PID: 0 Comm: swapper/1 Tainted: G U 5.10.136-19391-gadfe4d4b8c04 #1 b640352a7a0e5f1522aed724296ad63f90c007df <4>[43486.264036] Hardware name: Google Primus/Primus, BIOS Google_Primus.14505.145.0 06/23/2022 <4>[43486.264042] RIP: 0010:load_keylocker+0x0/0x7f
But, I don't get the reason why it hit this. On the wake-up path, copy_keylocker() is supposed to be called.
I added some printout in there, and it looks to be fine with me:
[ 218.488711] Enabling non-boot CPUs ...
[ 218.488794] x86: Booting SMP configuration:
[ 218.488795] smpboot: Booting Node 0 Processor 1 APIC 0x1
[ 218.490634] x86/keylocker: restore processor (id=1)
[ 218.491186] CPU1 is up
...Thanks,
Chang
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 33 lines · Chang S. Bae
Quoted 3 lines · Evan Green
Here's the log I've got that pointed me down this path:
https://pastebin.com/VvR1EHvE<3>[43486.261583] x86/keylocker: The key backup access failed with read error. <3>[43486.261584] x86/keylocker: Failed to restore internalwrapping key.
Looks like the IWKey backup was corrupted on that system.
Quoted 11 lines
Relevant bit pasted below:
<6>[43486.263035] Enabling non-boot CPUs ... <6>[43486.263081] x86: Booting SMP configuration: <6>[43486.263082] smpboot: Booting Node 0 Processor 1 APIC 0x1 <2>[43486.264010] kernel tried to execute NX-protected page - exploit attempt? (uid: 0) <1>[43486.264019] BUG: unable to handle page fault for address: ffffffff94b483a6 <1>[43486.264021] #PF: supervisor instruction fetch in kernel mode <1>[43486.264023] #PF: error_code(0x0011) - permissions violation <6>[43486.264025] PGD 391c0e067 P4D 391c0e067 PUD 391c0f063 PMDQuoted 8 lines
10006c063PTE 8000000392148163<4>[43486.264031] Oops: 0011 [#1] PREEMPT SMP NOPTI <4>[43486.264035] CPU: 1 PID: 0 Comm: swapper/1 Tainted: G U 5.10.136-19391-gadfe4d4b8c04 #1 b640352a7a0e5f1522aed724296ad63f90c007df <4>[43486.264036] Hardware name: Google Primus/Primus, BIOS Google_Primus.14505.145.0 06/23/2022 <4>[43486.264042] RIP: 0010:load_keylocker+0x0/0x7f
Quoted 2 lines
But, I don't get the reason why it hit this. On the wake-up path,
copy_keylocker()is supposed to be called.
Interesting, that's helpful. I thought I had a lead based on this, which was that in this case we were doing a hibernate to shutdown, rather than hibernate to S4. The IWKey backup is only valid down to S4, so a read error on resume from this type of hibernate might make sense. I know keylocker won't successfully maintain handles across a hibernate to shutdown and subsequent resume, but it shouldn't crash either.
But this still doesn't explain this crash, since in this case we're still on our way down and haven't even done the shutdown yet. We can see the "PM: hibernation: Image created (1536412 pages copied)" log line just before the keylocker read failure. So then it seems something's not working with the pre-hibernate CPU hotplug path?
Quoted 9 lines
I added some printout in there, and it looks to be fine with me:
[ 218.488711] Enabling non-boot CPUs ... [ 218.488794] x86: Booting SMP configuration: [ 218.488795] smpboot: Booting Node 0 Processor 1 APIC 0x1 [ 218.490634] x86/keylocker: restore processor (id=1) [ 218.491186] CPU1 is up ...
How were you exercising the CPU onlining in this case? Boot, cpu hotplug, or hibernate?
-Evan
Re: [PATCH v5 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 7 lines · Evan Green
On Wed, Aug 31, 2022 at 4:16 PM Chang S. Bae <[email protected]> wrote:
But this still doesn't explain this crash, since in this case we're still on our way down and haven't even done the shutdown yet. We can see the "PM: hibernation: Image created (1536412 pages copied)" log line just before the keylocker read failure. So then it seems something's not working with the pre-hibernate CPU hotplug path?
No, that backup crash message came from the boot-CPU's wakeup path:
__restore_processor_state()->restore_keylocker().
If the issue is repeatable, then I suspect that's something to do with the specific platform implementation. I don't have any detail yet on the customized systems. Let me check with some Chrome HW folks.
Quoted 2 lines
How were you exercising the CPU onlining in this case? Boot, cpu hotplug, or hibernate?
Hibernate.
Thanks,
Chang
PATCH v5 8/12 x86/PM/keylocker: Restore internal wrapping key on resume from ACPI S3/4
In reply to Chang S. BaeWhen the system enters the ACPI S3 or S4 sleep states, the internal wrapping key is discarded.
The primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even the prompt it does perform for unlocking the volume where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded. So it is motivated to meet dm-crypt's expectation that the key handles in the suspend-image remain valid after resume from an S-state.
Key Locker provides a mechanism to back up the internal wrapping key in non-volatile storage. The kernel requests a backup right after the key is loaded at boot time. It is copied back to each CPU upon wakeup.
While the backup may be maintained in NVM across S5 and G3 "off" states it is not architecturally guaranteed, nor is it expected by dm-crypt which expects to prompt for the key each time the volume is started.
The entirety of Key Locker needs to be disabled if the backup mechanism is not available unless CONFIG_SUSPEND=n, otherwise dm-crypt requires the backup to be available.
In the event of a key restore failure the kernel proceeds with an initialized IWKey state. This has the effect of invalidating any key handles that might be present in a suspend-image. When this happens dm-crypt will see I/O errors resulting from error returns from crypto_skcipher_{en,de}crypt(). While this will disrupt operations in the current boot, data is not at risk and access is restored at the next reboot to create new handles relative to the current IWKey.
Manage a feature-specific flag to communicate with the crypto implementation. This ensures to stop using the AES instructions upon the key restore failure while not turning off the feature.
---
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebased on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 4 + arch/x86/kernel/keylocker.c | 124 ++++++++++++++++++++++++++++++- arch/x86/power/cpu.c | 2 + 3 files changed, 128 insertions(+), 2 deletions(-)
arch/x86/include/asm/keylocker.h +4 -0
@@ -32,9 +32,13 @@ struct iwkey {#ifdef CONFIG_X86_KEYLOCKER void setup_keylocker (struct cpuinfo_x86 *c);void destroy_keylocker_data (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void );#else #define setup_keylocker (c) do { } while (0)#define destroy_keylocker_data () do { } while (0) +#define restore_keylocker () do { } while (0) +static inline bool valid_keylocker (void ) {return false; }#endif #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +122 -2
164 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -25,6 +25,7 @@#include <asm/cpu.h> #include <asm/mmu_context.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -262,6 +263,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)mtrr_bp_restore ();perf_restore_debug_store ();msr_restore_context (ctxt); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 1 line
2.17.1
PATCH v5 8/12 x86/PM/keylocker: Restore internal wrapping key on resume from ACPI S3/4
When the system enters the ACPI S3 or S4 sleep states, the internal wrapping key is discarded.
The primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even the prompt it does perform for unlocking the volume where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded. So it is motivated to meet dm-crypt's expectation that the key handles in the suspend-image remain valid after resume from an S-state.
Key Locker provides a mechanism to back up the internal wrapping key in non-volatile storage. The kernel requests a backup right after the key is loaded at boot time. It is copied back to each CPU upon wakeup.
While the backup may be maintained in NVM across S5 and G3 "off" states it is not architecturally guaranteed, nor is it expected by dm-crypt which expects to prompt for the key each time the volume is started.
The entirety of Key Locker needs to be disabled if the backup mechanism is not available unless CONFIG_SUSPEND=n, otherwise dm-crypt requires the backup to be available.
In the event of a key restore failure the kernel proceeds with an initialized IWKey state. This has the effect of invalidating any key handles that might be present in a suspend-image. When this happens dm-crypt will see I/O errors resulting from error returns from crypto_skcipher_{en,de}crypt(). While this will disrupt operations in the current boot, data is not at risk and access is restored at the next reboot to create new handles relative to the current IWKey.
Manage a feature-specific flag to communicate with the crypto implementation. This ensures to stop using the AES instructions upon the key restore failure while not turning off the feature.
---
Changes from v5:
* Fix the valid_kl flag setting when the feature is disabled. (Reported by Marvin Hsu <[email protected]>)
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebased on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 4 + arch/x86/kernel/keylocker.c | 128 ++++++++++++++++++++++++++++++- arch/x86/power/cpu.c | 2 + 3 files changed, 132 insertions(+), 2 deletions(-)
arch/x86/include/asm/keylocker.h +4 -0
@@ -32,9 +32,13 @@ struct iwkey {#ifdef CONFIG_X86_KEYLOCKER void setup_keylocker (struct cpuinfo_x86 *c);void destroy_keylocker_data (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void );#else #define setup_keylocker (c) do { } while (0)#define destroy_keylocker_data () do { } while (0) +#define restore_keylocker () do { } while (0) +static inline bool valid_keylocker (void ) {return false; }#endif #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +126 -2
169 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -25,6 +25,7 @@#include <asm/cpu.h> #include <asm/mmu_context.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -262,6 +263,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)mtrr_bp_restore ();perf_restore_debug_store ();msr_restore_context (ctxt); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 1 line
2.17.1
PATCH v5 9/12 x86/cpu: Add a configuration and command line option for Key Locker
In reply to Chang S. BaeAdd CONFIG_X86_KEYLOCKER to gate whether Key Locker is initialized at boot. The option is selected by the Key Locker cipher module CRYPTO_AES_KL (to be added in a later patch).
Add a new command line option "nokeylocker" to optionally override the default CONFIG_X86_KEYLOCKER=y behavior.
---
Changes from RFC v2:
* Make the option selected by CRYPTO_AES_KL. (Dan Williams)
* Massage the changelog and the config option description.
--- Documentation/admin-guide/kernel-parameters.txt | 2 ++ arch/x86/Kconfig | 3 +++ arch/x86/kernel/cpu/common.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -3405,6 +3405,8 @@ nohugevmalloc [PPC] Disable kernel huge vmalloc mappings. + nokeylocker [X86] Disable Key Locker hardware feature. + nosmt [KNL,S390] Disable symmetric multithreading (SMT). Equivalent to smt=1.
arch/x86/Kconfig +3 -0
@@ -1864,6 +1864,9 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_KEYLOCKER + bool + choice prompt "TSX enable mode" depends on CPU_SUP_INTEL
arch/x86/kernel/cpu/common.c +16 -0
@@ -364,6 +364,22 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)/* These bits should not change their value after CPU init is finished. */ static const unsigned long cr4_pinned_mask =X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_UMIP |X86_CR4_FSGSBASE ; + +static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg)) +return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup); +static DEFINE_STATIC_KEY_FALSE_RO (cr_pinning);static unsigned long cr4_pinned_bits__ro_after_init ;
Signature · 1 line
2.17.1
PATCH v5 10/12 crypto: x86/aes - Prepare for a new AES implementation
In reply to Chang S. BaeKey Locker's AES instruction set ('AES-KL') has a similar programming interface to AES-NI. The internal ABI in the assembly code will have the same prototype as AES-NI. Then, the glue code will be the same as AES-NI's.
Refactor the common C code to avoid code duplication. The AES-NI code uses it with a function pointer argument to call back the AES-NI assembly code. So will the AES-KL code.
Introduce wrappers for data transformation functions to return an error code. AES-KL may populate an error during data transformation.
Export those refactored functions and new wrappers that the AES-KL code will reference.
Also move some constant values to a common assembly code.
No functional change intended.
---
Changes from v4:
* Drop CBC mode changes. (Eric Biggers)
Changes from v3:
* Drop ECB and CTR mode changes. (Eric Biggers)
* Export symbols. (Eric Biggers)
Changes from RFC v2:
* Massage the changelog. (Dan Williams)
Changes from RFC v1:
* Added as a new patch. (Ard Biesheuvel)
--- arch/x86/crypto/Makefile | 2 +- arch/x86/crypto/aes-intel_asm.S | 26 ++++ arch/x86/crypto/aes-intel_glue.c | 125 +++++++++++++++ arch/x86/crypto/aes-intel_glue.h | 48 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +++---- arch/x86/crypto/aesni-intel_glue.c | 239 +++++++++-------------------- arch/x86/crypto/aesni-intel_glue.h | 17 ++ 7 files changed, 309 insertions(+), 206 deletions(-) create mode 100644 arch/x86/crypto/aes-intel_asm.S create mode 100644 arch/x86/crypto/aes-intel_glue.c create mode 100644 arch/x86/crypto/aes-intel_glue.h create mode 100644 arch/x86/crypto/aesni-intel_glue.h
arch/x86/crypto/Makefile +1 -1
@@ -47,7 +47,7 @@ chacha-x86_64-y := chacha-avx2-x86_64.o chacha-ssse3-x86_64.o chacha_glue.o chacha-x86_64-$(CONFIG_AS_AVX512) += chacha-avx512vl-x86_64.o obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o -aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o +aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aes-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o
arch/x86/crypto/aes-intel_asm.S new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Constant values shared between AES implementations: + */ + +.pushsection .rodata +.align 16 +.Lcts_permute_table: + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 + .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 +#ifdef __x86_64__ +.Lbswap_mask: + .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#endif +.popsection + +.section .rodata.cst16.gf128mul_x_ble_mask, "aM", @progbits, 16 +.align 16 +.Lgf128mul_x_ble_mask: + .octa 0x00000000000000010000000000000087 +.previous
arch/x86/crypto/aes-intel_glue.c new file +125 -0
125 lines not on this page · raw message
arch/x86/crypto/aes-intel_glue.h new file +48 -0
48 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +19 -39
130 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.c +73 -166
377 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/ + +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len); + +int aesni_enc (const void *ctx,u8 *out,const u8 *in); +int aesni_dec (const void *ctx,u8 *out,const u8 *in); + +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +
Signature · 1 line
2.17.1
PATCH v5 11/12 crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
In reply to Chang S. BaeKey Locker is a CPU feature to reduce key exfiltration opportunities while maintaining a programming interface similar to AES-NI. It converts the AES key into an encoded form, called the 'key handle'.
The key handle is a wrapped version of the clear-text key where the wrapping key has limited exposure. Once converted via setkey(), all subsequent data encryption using new AES instructions ('AES-KL') uses this key handle, reducing the exposure of private key material in memory.
AES-KL is analogous to that of AES-NI. Most assembly code is translated from the AES-NI code. They are operational in both 32-bit and 64-bit modes like AES-NI. However, users need to be aware of the following differences:
== Key Handle ==
AES-KL may fail with an invalid key handle. It could be corrupted or fail with handle restriction. A key handle may be encoded with some restrictions. The implementation restricts every handle only available in kernel mode via setkey().
=== AES Compliance ===
Key Locker is not AES compliant in that it lacks support for 192-bit keys. However, per the expectations of Linux crypto-cipher implementations the software cipher implementation must support all the AES compliant key-sizes. The AES-KL cipher implementation achieves this constraint by logging a warning and falling back to AES-NI. In other words the 192-bit key-size limitation for what can be converted into a Key Locker key-handle is only documented, not enforced. This along with the below performance and failure mode implications is an end-user consideration for selecting AES-KL vs AES-NI.
== API Limitation ==
The setkey() function transforms an AES key to a handle. An extended key is a usual outcome of setkey() in other AES cipher implementations. For this reason, a setkey() failure does not fall back to the other. So, AES-KL will be exposed via synchronous interfaces only.
== Wrapping-key Restore Failure ==
The setkey() failure is also possible with the wrapping-key restore failure. In an event of hardware failure, the wrapping key is lost from deep sleep states. Then, setkey() will fail with the ENODEV error. And the suspended data transforming task may resume but will fail due to the wrapping key change.
== Performance ==
This feature comes with some performance penalties vs AES-NI. The cryptsetup benchmark indicates Key Locker raw throughput can be ~5x slower than AES-NI. For disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth, but the potential performance difference is why AES-KL is advertised as a distinct cipher in /proc/crypto rather than the kernel transparently replacing AES-NI usage with AES-KL.
---
Changes from v3:
* Exclude non-AES-KL objects. (Eric Biggers)
* Simplify the assembler dependency check. (Peter Zijlstra)
* Trim the Kconfig help text. (Dan Williams)
* Fix a defined-but-not-used warning.
Changes from RFC v2:
* Move out each mode support in new patches.
* Update the changelog to describe the limitation and the tradeoff clearly. (Andy Lutomirski)
Changes from RFC v1:
* Rebased on the refactored code. (Ard Biesheuvel)
* Dropped exporting the single block interface. (Ard Biesheuvel)
* Fixed the fallback and error handling paths. (Ard Biesheuvel)
* Revised the module description. (Dave Hansen and Peter Zijlsta)
* Made the build depend on the binutils version to support new instructions. (Borislav Petkov and Peter Zijlstra)
* Updated the changelog accordingly.
--- arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aeskl-intel_asm.S | 184 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 115 ++++++++++++++++++ crypto/Kconfig | 36 ++++++ 4 files changed, 338 insertions(+) create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c
arch/x86/crypto/Makefile +3 -0
@@ -50,6 +50,9 @@ obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aes-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o +obj-$(CONFIG_CRYPTO_AES_KL) += aeskl-intel.o +aeskl-intel-y := aeskl-intel_asm.o aeskl-intel_glue.o + obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o sha1-ssse3-$(CONFIG_AS_SHA1_NI) += sha1_ni_asm.o
arch/x86/crypto/aeskl-intel_asm.S new file +184 -0
184 lines not on this page · raw message
arch/x86/crypto/aeskl-intel_glue.c new file +115 -0
115 lines not on this page · raw message
crypto/Kconfig +36 -0
@@ -1113,6 +1113,42 @@ config CRYPTO_AES_NI_INTEL ECB, CBC, LRW, XTS. The 64 bit version has additional acceleration for CTR. +config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12 + +config CRYPTO_AES_KL + tristate "AES cipher algorithms (AES-KL)" + depends on AS_HAS_KEYLOCKER + depends on CRYPTO_AES_NI_INTEL + select X86_KEYLOCKER + + help + Key Locker provides AES SIMD instructions (AES-KL) for secure + data encryption and decryption. While this new instruction + set is analogous to AES-NI, AES-KL supports to encode an AES + key to an encoded form ('key handle') and uses it to transform + data instead of accessing the AES key. + + The setkey() transforms an AES key to a key handle, then the AES + key is no longer needed for data transformation. A user may + displace their keys from possible exposition. + + This key encryption is done by the CPU-internal wrapping key. + This wrapping key support is provided with X86_KEYLOCKER. + + AES-KL supports 128-/256-bit keys only. While giving a 192-bit + key does not return an error, as the AES-NI driver is chosen to + process it, the claimed security property is not available with + that. + + Bare metal disk encryption is the preferred use case. Key Locker + usage requires explicit opt-in at setup time. So select it if + unsure. + + See Documentation/x86/keylocker.rst for more details. + config CRYPTO_AES_SPARC64 tristate "AES cipher algorithms (SPARC64)" depends on SPARC64
Signature · 1 line
2.17.1
PATCH v5 12/12 crypto: x86/aes-kl - Support XTS mode
In reply to Chang S. BaeImplement XTS mode using AES-KL. Export the methods with a lower priority than AES-NI to avoid from selected by default.
The assembly code clobbers more than eight 128-bit registers to make use of performant wide instructions that process eight 128-bit blocks at once. But that many 128-bit registers are not available in 32-bit mode, so support 64-bit mode only.
---
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/crypto/aeskl-intel_asm.S | 449 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 107 ++++++- 2 files changed, 553 insertions(+), 3 deletions(-)
arch/x86/crypto/aeskl-intel_asm.S +449 -0
@@ -182,3 +182,452 @@ SYM_FUNC_START(_aeskl_dec) ret SYM_FUNC_END(_aeskl_dec) +#ifdef __x86_64__ + +/* + * XTS implementation + */ + +/* + * _aeskl_gf128mul_x_ble: internal ABI + * Multiply in GF(2^128) for XTS IVs + * input: + * IV: current IV + * GF128MUL_MASK == mask with 0x87 and 0x01 + * output: + * IV: next IV + * changed: + * CTR: == temporary value + */ +#define _aeskl_gf128mul_x_ble() \ + pshufd $0x13, IV, KEY; \ + paddq IV, IV; \ + psrad $31, KEY; \ + pand GF128MUL_MASK, KEY; \ + pxor KEY, IV; + +/* + * int _aeskl_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, + * const u8 *src, unsigned int len, le128 *iv) + */ +SYM_FUNC_START(_aeskl_xts_encrypt) + FRAME_BEGIN + movdqa .Lgf128mul_x_ble_mask(%rip), GF128MUL_MASK + movups (IVP), IV + + mov 480(HANDLEP), KLEN + +.Lxts_enc8: + sub $128, LEN + jl .Lxts_enc1_pre + + movdqa IV, STATE1 + movdqu (INP), INC + pxor INC, STATE1 + movdqu IV, (OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE2 + movdqu 0x10(INP), INC + pxor INC, STATE2 + movdqu IV, 0x10(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE3 + movdqu 0x20(INP), INC + pxor INC, STATE3 + movdqu IV, 0x20(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE4 + movdqu 0x30(INP), INC + pxor INC, STATE4 + movdqu IV, 0x30(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE5 + movdqu 0x40(INP), INC + pxor INC, STATE5 + movdqu IV, 0x40(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE6 + movdqu 0x50(INP), INC + pxor INC, STATE6 + movdqu IV, 0x50(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE7 + movdqu 0x60(INP), INC + pxor INC, STATE7 + movdqu IV, 0x60(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE8 + movdqu 0x70(INP), INC + pxor INC, STATE8 + movdqu IV, 0x70(OUTP) + + cmp $16, KLEN + je .Lxts_enc8_128 + aesencwide256kl (%rdi) + jz .Lxts_enc_ret_err + jmp .Lxts_enc8_end +.Lxts_enc8_128: + aesencwide128kl (%rdi) + jz .Lxts_enc_ret_err + +.Lxts_enc8_end: + movdqu 0x00(OUTP), INC + pxor INC, STATE1 + movdqu STATE1, 0x00(OUTP) + + movdqu 0x10(OUTP), INC + pxor INC, STATE2 + movdqu STATE2, 0x10(OUTP) + + movdqu 0x20(OUTP), INC + pxor INC, STATE3 + movdqu STATE3, 0x20(OUTP) + + movdqu 0x30(OUTP), INC + pxor INC, STATE4 + movdqu STATE4, 0x30(OUTP) + + movdqu 0x40(OUTP), INC + pxor INC, STATE5 + movdqu STATE5, 0x40(OUTP) + + movdqu 0x50(OUTP), INC + pxor INC, STATE6 + movdqu STATE6, 0x50(OUTP) + + movdqu 0x60(OUTP), INC + pxor INC, STATE7 + movdqu STATE7, 0x60(OUTP) + + movdqu 0x70(OUTP), INC + pxor INC, STATE8 + movdqu STATE8, 0x70(OUTP) + + _aeskl_gf128mul_x_ble() + + add $128, INP + add $128, OUTP + test LEN, LEN + jnz .Lxts_enc8 + +.Lxts_enc_ret_iv: + movups IV, (IVP) +.Lxts_enc_ret_noerr: + xor AREG, AREG + jmp .Lxts_enc_ret +.Lxts_enc_ret_err: + mov $1, AREG +.Lxts_enc_ret: + FRAME_END + ret + +.Lxts_enc1_pre: + add $128, LEN + jz .Lxts_enc_ret_iv + sub $16, LEN + jl .Lxts_enc_cts4 + +.Lxts_enc1: + movdqu (INP), STATE1 + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_enc1_128 + aesenc256kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + jmp .Lxts_enc1_end +.Lxts_enc1_128: + aesenc128kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + +.Lxts_enc1_end: + pxor IV, STATE1 + _aeskl_gf128mul_x_ble() + + test LEN, LEN + jz .Lxts_enc1_out + + add $16, INP + sub $16, LEN + jl .Lxts_enc_cts1 + + movdqu STATE1, (OUTP) + add $16, OUTP + jmp .Lxts_enc1 + +.Lxts_enc1_out: + movdqu STATE1, (OUTP) + jmp .Lxts_enc_ret_iv + +.Lxts_enc_cts4: + movdqu STATE8, STATE1 + sub $16, OUTP + +.Lxts_enc_cts1: + lea .Lcts_permute_table(%rip), T1 + add LEN, INP /* rewind input pointer */ + add $16, LEN /* # bytes in final block */ + movups (INP), IN1 + + mov T1, IVP + add $32, IVP + add LEN, T1 + sub LEN, IVP + add OUTP, LEN + + movups (T1), STATE2 + movaps STATE1, STATE3 + pshufb STATE2, STATE1 + movups STATE1, (LEN) + + movups (IVP), STATE1 + pshufb STATE1, IN1 + pblendvb STATE3, IN1 + movaps IN1, STATE1 + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_enc1_cts_128 + aesenc256kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + jmp .Lxts_enc1_cts_end +.Lxts_enc1_cts_128: + aesenc128kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + +.Lxts_enc1_cts_end: + pxor IV, STATE1 + movups STATE1, (OUTP) + jmp .Lxts_enc_ret_noerr +SYM_FUNC_END(_aeskl_xts_encrypt) + +/* + * int _aeskl_xts_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, + * const u8 *src, unsigned int len, le128 *iv) + */ +SYM_FUNC_START(_aeskl_xts_decrypt) + FRAME_BEGIN + movdqa .Lgf128mul_x_ble_mask(%rip), GF128MUL_MASK + movups (IVP), IV + + mov 480(HANDLEP), KLEN + + test $15, LEN + jz .Lxts_dec8 + sub $16, LEN + +.Lxts_dec8: + sub $128, LEN + jl .Lxts_dec1_pre + + movdqa IV, STATE1 + movdqu (INP), INC + pxor INC, STATE1 + movdqu IV, (OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE2 + movdqu 0x10(INP), INC + pxor INC, STATE2 + movdqu IV, 0x10(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE3 + movdqu 0x20(INP), INC + pxor INC, STATE3 + movdqu IV, 0x20(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE4 + movdqu 0x30(INP), INC + pxor INC, STATE4 + movdqu IV, 0x30(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE5 + movdqu 0x40(INP), INC + pxor INC, STATE5 + movdqu IV, 0x40(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE6 + movdqu 0x50(INP), INC + pxor INC, STATE6 + movdqu IV, 0x50(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE7 + movdqu 0x60(INP), INC + pxor INC, STATE7 + movdqu IV, 0x60(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE8 + movdqu 0x70(INP), INC + pxor INC, STATE8 + movdqu IV, 0x70(OUTP) + + cmp $16, KLEN + je .Lxts_dec8_128 + aesdecwide256kl (%rdi) + jz .Lxts_dec_ret_err + jmp .Lxts_dec8_end +.Lxts_dec8_128: + aesdecwide128kl (%rdi) + jz .Lxts_dec_ret_err + +.Lxts_dec8_end: + movdqu 0x00(OUTP), INC + pxor INC, STATE1 + movdqu STATE1, 0x00(OUTP) + + movdqu 0x10(OUTP), INC + pxor INC, STATE2 + movdqu STATE2, 0x10(OUTP) + + movdqu 0x20(OUTP), INC + pxor INC, STATE3 + movdqu STATE3, 0x20(OUTP) + + movdqu 0x30(OUTP), INC + pxor INC, STATE4 + movdqu STATE4, 0x30(OUTP) + + movdqu 0x40(OUTP), INC + pxor INC, STATE5 + movdqu STATE5, 0x40(OUTP) + + movdqu 0x50(OUTP), INC + pxor INC, STATE6 + movdqu STATE6, 0x50(OUTP) + + movdqu 0x60(OUTP), INC + pxor INC, STATE7 + movdqu STATE7, 0x60(OUTP) + + movdqu 0x70(OUTP), INC + pxor INC, STATE8 + movdqu STATE8, 0x70(OUTP) + + _aeskl_gf128mul_x_ble() + + add $128, INP + add $128, OUTP + test LEN, LEN + jnz .Lxts_dec8 + +.Lxts_dec_ret_iv: + movups IV, (IVP) +.Lxts_dec_ret_noerr: + xor AREG, AREG + jmp .Lxts_dec_ret +.Lxts_dec_ret_err: + mov $1, AREG +.Lxts_dec_ret: + FRAME_END + ret + +.Lxts_dec1_pre: + add $128, LEN + jz .Lxts_dec_ret_iv + +.Lxts_dec1: + movdqu (INP), STATE1 + + add $16, INP + sub $16, LEN + jl .Lxts_dec_cts1 + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_end +.Lxts_dec1_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_end: + pxor IV, STATE1 + _aeskl_gf128mul_x_ble() + + test LEN, LEN + jz .Lxts_dec1_out + + movdqu STATE1, (OUTP) + add $16, OUTP + jmp .Lxts_dec1 + +.Lxts_dec1_out: + movdqu STATE1, (OUTP) + jmp .Lxts_dec_ret_iv + +.Lxts_dec_cts1: + movdqa IV, STATE5 + _aeskl_gf128mul_x_ble() + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_cts_pre_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_cts_pre_end +.Lxts_dec1_cts_pre_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_cts_pre_end: + pxor IV, STATE1 + + lea .Lcts_permute_table(%rip), T1 + add LEN, INP /* rewind input pointer */ + add $16, LEN /* # bytes in final block */ + movups (INP), IN1 + + mov T1, IVP + add $32, IVP + add LEN, T1 + sub LEN, IVP + add OUTP, LEN + + movups (T1), STATE2 + movaps STATE1, STATE3 + pshufb STATE2, STATE1 + movups STATE1, (LEN) + + movups (IVP), STATE1 + pshufb STATE1, IN1 + pblendvb STATE3, IN1 + movaps IN1, STATE1 + + pxor STATE5, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_cts_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_cts_end +.Lxts_dec1_cts_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_cts_end: + pxor STATE5, STATE1 + + movups STATE1, (OUTP) + jmp .Lxts_dec_ret_noerr + +SYM_FUNC_END(_aeskl_xts_decrypt) + +#endif
arch/x86/crypto/aeskl-intel_glue.c +104 -3
135 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 5 lines · Chang S. Bae
A couple of other things outside of these patches are still in progress:
* Support DM-crypt/cryptsetup for Key Locker usage (Andy Lutomirski) [2].
* Understand decryption under-performance (Eric Biggers and Milan Broz) [3][4].
I really like when contributors are clear about why they are posting their series and what their expectations are. This posting leaves me a bit confused as to what you expect the maintainers to do.
Should the maintainers ignore this series until those in-progress things are done? Or, do you expect that this could be merged as-is before those are resolved?
Quoted 7 lines · Dave
Quoted 5 lines · Chang S. Bae
A couple of other things outside of these patches are still in progress:
* Support DM-crypt/cryptsetup for Key Locker usage (Andy Lutomirski) [2].
* Understand decryption under-performance (Eric Biggers and Milan Broz) [3][4].
Quoted 7 lines
I really like when contributors are clear about why they are posting their series and what their expectations are. This posting leaves me a bit confused as to what you expect the maintainers to do.
Should the maintainers ignore this series until those in-progress things are done? Or, do you expect that this could be merged as-is before those are resolved?
Ah, right. Yeah, this is not super clear about that.
I think it makes sense to clarify those two points -- performance implication and user interaction in the usage case, before considering this feature support in the mainline.
But I wanted to address feedback on the patches with this posting.
Hopefully, this clarifies the status of this series.
Thanks,
Chang
v6 cover x86: Support Key Locker
In reply to Chang S. BaeHi all,
This feature enabling has been stalled due to the decryption performance issue which took a while to result in a solution. There was also a question on the userspace utility -- cryptsetup [6].
As posting this version, I wanted to make sure the resolutions are acknowledgeable with these updates, at first:
* People identified a gap between encryption and decryption speeds [1, 2]. Intel has identified the root cause and will make a fix available. The fix will ensure that the encryption and decryption speeds are comparable with each other.
This fix requires a microcode update and will go through a formal process of release toward the end of the year. As of now, listed below are the expected numbers [10] when the issue is fixed. Intel will provide the latest information on the request.
+---------------------------+---------------+-----------------+ | Cipher | Encryption | Decryption | | (AES-XTS) | (MiB/s) | (MiB/s) | +===========================+===============+=================+ | Microcode version (0xa4) | 1726.5 | 776.3 | +---------------------------+---------------+-----------------+ | With the potential fix | 2308.7 | 2305.9 | +---------------------------+---------------+-----------------+
The benchmark results came from:
$ cryptsetup benchmark -c aes-xts -s 256 which are approximate using memory only (no storage IO).
The fix is specific to the CPU model [8] which was measured when posting v3 [4]. Here is another results from the latest CPU [9]:
+--------------+---------------+-----------------+ | Cipher | Encryption | Decryption | | (AES-XTS) | (MiB/s) | (MiB/s) | +==============+===============+=================+ | AES-NI | 6738.7 | 6851.7 | +--------------+---------------+-----------------+ | AES-KL | 3425.4 | 3431.7 | +--------------+---------------+-----------------+
* Andy questioned the cryptsetup readiness to edit the cipher mode, e.g. AES-KL to AES-NI, for decrypting a keylocker-encrypted volume on non-keylocker systems [3].
The cryptsetup changes were prototyped as a proof-of-concept. The code and test details can be found in this repository:
git://github.com/intel-staging/keylocker.git cryptsetup
Once the enabling code is accepted in the mainline, I will follow-up with the DM-crypt [7] folks to add this ability.
The feature is already available on recent Intel client systems. Its usage along with the threat model and other details can be found in the v3 cover letter [4].
The code has some updates from the last posting [5]:
- Fix the 'valid_kl' flag not to be set when the feature is disabled. This was reported by Marvin Hsu <[email protected]>. Add the function comment about this. (Patch8)
- Improve the error handling in setup_keylocker(). All the error cases fall through the end that disables the feature. Otherwise, all the successful cases return immediately. (Patch8)
- Call out when disabling the feature on virtual machines. (Patch7)
- Ensure kernel_fpu_end() for the possible error (Patch10)
- Rebased on the upstream -- use the RET macro (Patch11/12)
- Clean up dead code -- cbc_crypt_common() (Patch10)
- Fix a typo (Patch1)
This series is based on the tip tree. The code is also available in this repository:
git://github.com/intel-staging/keylocker.git kl
Thanks to Charishma Gairuboyina and Lalithambika Krishnakumar for the help to update the performance results.
Thanks,
Chang
[1] https://lore.kernel.org/lkml/[email protected]/ [2] https://lore.kernel.org/lkml/[email protected]/ [3] https://lore.kernel.org/lkml/[email protected]/ [4] v3: https://lore.kernel.org/lkml/[email protected]/ [5] v5: https://lore.kernel.org/lkml/[email protected]/ [6] cryptsetup: https://gitlab.com/cryptsetup/cryptsetup [7] DM-crypt: https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/dm-crypt.html [8] Tiger Lake:
https://www.intel.com/content/www/us/en/products/docs/processors/embedded/11th-gen-product-brief.html
[9] Raptor Lake:
https://www.intel.com/content/www/us/en/newsroom/resources/13th-gen-core.html
[10] Intel publishes information about product performance at
https://www.Intel.com/PerformanceIndex
Chang S. Bae (12):
Documentation/x86: Document Key Locker
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/asm: Add a wrapper function for the LOADIWKEY instruction x86/msr-index: Add MSRs for Key Locker internal wrapping key x86/keylocker: Define Key Locker CPUID leaf x86/cpu/keylocker: Load an internal wrapping key at boot-time x86/PM/keylocker: Restore internal wrapping key on resume from ACPI
S3/4
x86/cpu: Add a configuration and command line option for Key Locker
crypto: x86/aes - Prepare for a new AES implementation
crypto: x86/aes-kl - Support AES algorithm using Key Locker
instructions
crypto: x86/aes-kl - Support XTS mode
.../admin-guide/kernel-parameters.txt | 2 + Documentation/x86/index.rst | 1 + Documentation/x86/keylocker.rst | 98 +++ arch/x86/Kconfig | 3 + arch/x86/crypto/Makefile | 5 +- arch/x86/crypto/aes-intel_asm.S | 26 + arch/x86/crypto/aes-intel_glue.c | 127 ++++ arch/x86/crypto/aes-intel_glue.h | 44 ++ arch/x86/crypto/aeskl-intel_asm.S | 633 ++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 216 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +- arch/x86/crypto/aesni-intel_glue.c | 235 ++----- arch/x86/crypto/aesni-intel_glue.h | 17 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/keylocker.h | 45 ++ arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/special_insns.h | 32 + arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 21 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/keylocker.c | 212 ++++++ arch/x86/kernel/smpboot.c | 2 + arch/x86/lib/x86-opcode-map.txt | 11 +- arch/x86/power/cpu.c | 2 + crypto/Kconfig | 36 + tools/arch/x86/lib/x86-opcode-map.txt | 11 +- 28 files changed, 1642 insertions(+), 214 deletions(-) create mode 100644 Documentation/x86/keylocker.rst create mode 100644 arch/x86/crypto/aes-intel_asm.S create mode 100644 arch/x86/crypto/aes-intel_glue.c create mode 100644 arch/x86/crypto/aes-intel_glue.h create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h create mode 100644 arch/x86/include/asm/keylocker.h create mode 100644 arch/x86/kernel/keylocker.c
base-commit: b2e7ae549d84c654744bf70d30a881bfa140a6e3
Signature · 1 line
2.17.1
PATCH v6 1/12 Documentation/x86: Document Key Locker
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
Changes from v5:
* Fix a typo: 'feature feature' -> 'feature'
Changes from RFC v2:
* Add as a new patch.
The preview is available here:
https://htmlpreview.github.io/?https://github.com/intel-staging/keylocker/kdoc/x86/keylocker.html
--- Documentation/x86/index.rst | 1 + Documentation/x86/keylocker.rst | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Documentation/x86/keylocker.rst
Documentation/x86/index.rst +1 -0
@@ -43,3 +43,4 @@ x86-specific Documentation features elf_auxvec xstate + keylocker
Documentation/x86/keylocker.rst new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v6 2/12 x86/cpufeature: Enumerate Key Locker feature
In reply to Chang S. BaeKey Locker is a CPU feature to minimize exposure of clear-text key material. An encoded form, called 'key handle', is referenced for data encryption or decryption instead of accessing the clear text key.
A wrapping key loaded in the CPU's software-inaccessible state is used to transform a user key into a key handle.
It supports Advanced Encryption Standard (AES) cipher algorithm with new SIMD instruction set like its predecessor (AES-NI). So a new AES implementation will follow in the kernel's crypto library.
Here add it to enumerate the hardware capability, but it will not be shown in /proc/cpuinfo as userspace usage is not supported.
Make the feature depend on XMM2 as it comes with AES SIMD instructions.
Add X86_FEATURE_KEYLOCKER to the disabled features mask. It will be enabled under a new config option.
---
Changes from RFC v2:
* Do not publish the feature flag to userspace.
* Update the changelog.
Changes from RFC v1:
* Updated the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -390,6 +390,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_KEYLOCKER (16*32+23)/* "" Key Locker */ #define X86_FEATURE_BUS_LOCK_DETECT (16*32+24)/* Bus Lock detect */ #define X86_FEATURE_CLDEMOTE (16*32+25)/* CLDEMOTE instruction */ #define X86_FEATURE_MOVDIRI (16*32+27)/* MOVDIRI instruction */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -38,6 +38,12 @@# define DISABLE_OSPKE (1<<(X86_FEATURE_OSPKE & 31))#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */ +#ifdef CONFIG_X86_KEYLOCKER +# define DISABLE_KEYLOCKER 0 +#else +# define DISABLE_KEYLOCKER (1<<(X86_FEATURE_KEYLOCKER & 31)) +#endif /* CONFIG_X86_KEYLOCKER */ +#ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0#else
@@ -138,7 +144,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_KEYLOCKER)#define DISABLED_MASK17 0#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK19 0
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -128,6 +128,8 @@#define X86_CR4_PCIDE _BITUL(X86_CR4_PCIDE_BIT)#define X86_CR4_OSXSAVE_BIT 18/* enable xsave and xrestore */ #define X86_CR4_OSXSAVE _BITUL(X86_CR4_OSXSAVE_BIT) +#define X86_CR4_KEYLOCKER_BIT 19/* enable Key Locker */ +#define X86_CR4_KEYLOCKER _BITUL(X86_CR4_KEYLOCKER_BIT)#define X86_CR4_SMEP_BIT 20/* enable SMEP support */ #define X86_CR4_SMEP _BITUL(X86_CR4_SMEP_BIT)#define X86_CR4_SMAP_BIT 21/* enable SMAP support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -82,6 +82,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, + { X86_FEATURE_KEYLOCKER, X86_FEATURE_XMM2 }, {} };
Signature · 1 line
2.17.1
PATCH v6 3/12 x86/insn: Add Key Locker instructions to the opcode map
In reply to Chang S. BaeAdd the following Key Locker instructions to the opcode map:
LOADIWKEY:
Load an CPU-internal wrapping key.
ENCODEKEY128:
Wrap a 128-bit AES key to a key handle.
ENCODEKEY256:
Wrap a 256-bit AES key to a key handle.
AESENC128KL:
Encrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESENC256KL:
Encrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESDEC128KL:
Decrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESDEC256KL:
Decrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESENCWIDE128KL:
Encrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESENCWIDE256KL:
Encrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
AESDECWIDE128KL:
Decrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESDECWIDE256KL:
Decrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
Details of these instructions can be found in Intel Software Developer's Manual.
---
Changes from RFC v1:
* Separated out the LOADIWKEY addition in a new patch.
* Included AES instructions to avoid warning messages when the AES Key Locker module is built.
--- arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- tools/arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
tools/arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
Signature · 1 line
2.17.1
PATCH v6 4/12 x86/asm: Add a wrapper function for the LOADIWKEY instruction
In reply to Chang S. BaeKey Locker introduces a CPU-internal wrapping key to encode a user key to a key handle. Then a key handle is referenced instead of the plain text key.
The new instruction loads an internal wrapping key in the software-inaccessible CPU state. It operates only in kernel mode.
Define struct iwkey to pass the key value.
The kernel will use this function to load a new key at boot time when the feature is enabled.
---
Changes from RFC v2:
* Separate out the code as a new patch.
* Improve the usability with the new struct as an argument. (Dan Williams)
Note, Dan wondered if:
WARN_ON(!irq_fpu_usable());
would be appropriate in the load_xmm_iwkey() function.
--- arch/x86/include/asm/keylocker.h | 25 ++++++++++++++++++++++ arch/x86/include/asm/special_insns.h | 32 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/keylocker.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_KEYLOCKER_H +#define _ASM_KEYLOCKER_H + +#ifndef __ASSEMBLY__ + +#include <asm/fpu/types.h> + +/** +* struct iwkey - A temporary internal wrapping key storage. +* @integrity_key: A 128-bit key to check that key handles have not +* been tampered with. +* @encryption_key: A 256-bit encryption key used in +* wrapping/unwrapping a clear text key. +* +* This storage should be flushed immediately after loaded. +*/ +struct iwkey { +struct reg_128_bit integrity_key; +struct reg_128_bit encryption_key[2 ]; +}; + +#endif /*__ASSEMBLY__ */ +#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +32 -0
@@ -9,6 +9,7 @@#include <asm/processor-flags.h> #include <linux/irqflags.h> #include <linux/jump_label.h> +#include <asm/keylocker.h> /* * The compiler should not reorder volatile asm statements with respect to each
@@ -296,6 +297,37 @@ static __always_inline void tile_release(void) asmvolatile (".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ); } +/** +* load_xmm_iwkey - Load a CPU-internal wrapping key +* @key: A struct iwkey pointer. +* +* Load @key to XMMs then do LOADIWKEY. After this, flush XMM +* registers. Caller is responsible for kernel_cpu_begin(). +*/ +static inline void load_xmm_iwkey (struct iwkey *key) +{ +struct reg_128_bit zeros = {0 }; + + asmvolatile ("movdqu %0, %%xmm0; movdqu %1, %%xmm1; movdqu %2, %%xmm2;" + ::"m" (key->integrity_key),"m" (key->encryption_key[0 ]), +"m" (key->encryption_key[1 ])); + +/* +* LOADIWKEY %xmm1,%xmm2 +* +* EAX and XMM0 are implicit operands. Load a key value +* from XMM0-2 to a software-invisible CPU state. With zero +* in EAX, CPU does not do hardware randomization and the key +* backup is allowed. +* +* This instruction is supported by binutils >= 2.36. +*/ + asmvolatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1" ::"a" (0 )); + + asmvolatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;" + ::"m" (zeros)); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
Signature · 1 line
2.17.1
PATCH v6 5/12 x86/msr-index: Add MSRs for Key Locker internal wrapping key
In reply to Chang S. BaeThe CPU state that contains the internal wrapping key is in the same power domain as the cache. So any sleep state that would invalidate the cache (like S3) also invalidates the state of the wrapping key.
But, since the state is inaccessible to software, it needs a special mechanism to save and restore the key during deep sleep.
A set of new MSRs are provided as an abstract interface to save and restore the wrapping key, and to check the key status.
The wrapping key is saved in a platform-scoped state of non-volatile media. The backup itself and its path from the CPU are encrypted and integrity protected.
But this storage's non-volatility is not architecturally guaranteed across off states, such as S5 and G3.
The MSRs will be used to back up the key for S3/4 sleep states. Then the kernel code writes one of them to restore the key in each CPU state.
---
Changes from RFC v2:
* Update the changelog. (Dan Williams)
* Rename the MSRs. (Dan Williams)
--- arch/x86/include/asm/msr-index.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
@@ -1115,4 +1115,10 @@* a #GP */ +/* MSRs for managing an internal wrapping key for Key Locker. */ +#define MSR_IA32_IWKEY_COPY_STATUS 0x00000990 +#define MSR_IA32_IWKEY_BACKUP_STATUS 0x00000991 +#define MSR_IA32_BACKUP_IWKEY_TO_PLATFORM 0x00000d91 +#define MSR_IA32_COPY_IWKEY_TO_LOCAL 0x00000d92 +#endif /* _ASM_X86_MSR_INDEX_H */
Signature · 1 line
2.17.1
PATCH v6 6/12 x86/keylocker: Define Key Locker CPUID leaf
In reply to Chang S. BaeDefine the feature-specific CPUID leaf and bits. Both Key Locker enabling code in the x86 core and AES Key Locker code in the crypto library will reference them.
---
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/include/asm/keylocker.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/keylocker.h +7 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <linux/bits.h> #include <asm/fpu/types.h> /**
@@ -21,5 +22,11 @@ struct iwkey {struct reg_128_bit encryption_key[2 ]; }; +#define KEYLOCKER_CPUID 0x019 +#define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0) +#define KEYLOCKER_CPUID_EBX_AESKLE BIT(0) +#define KEYLOCKER_CPUID_EBX_WIDE BIT(2) +#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
Signature · 1 line
2.17.1
PATCH v6 7/12 x86/cpu/keylocker: Load an internal wrapping key at boot-time
In reply to Chang S. BaeThe Internal Wrapping Key (IWKey) is an entity of Key Locker to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
IWKey needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
The kernel generates random bytes and load them at boot time. These bytes are flushed out immediately.
Setting the CR4.KL bit does not always enable the feature so ensure the dynamic CPU bit (CPUID.AESKLE) is set before loading the key.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt consumption, and that switching IWKey per VM is untenable, explicitly skip Key Locker setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from v5:
* Call out the disabling when the feature is available on a virtual machine. Then, it will turn off the feature flag
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.
Note, Dan wonders that given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but as Dave notes there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
--- arch/x86/include/asm/keylocker.h | 9 ++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/keylocker.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 2 + 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/include/asm/keylocker.h +9 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h>
@@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/Makefile +1 -0
@@ -133,6 +133,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/cpu/common.c +4 -1
@@ -58,6 +58,8 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> +#include <asm/uv/uv.h> #include <asm/sigframe.h> #include <asm/traps.h>
@@ -1874,10 +1876,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker (c);/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c new file +82 -0
82 lines not on this page · raw message
arch/x86/kernel/smpboot.c +2 -0
@@ -84,6 +84,7 @@#include <asm/hw_irq.h> #include <asm/stackprotector.h> #include <asm/sev.h> +#include <asm/keylocker.h> /* representing HT siblings of each logical CPU */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_sibling_map);
@@ -1500,6 +1501,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends ();cache_aps_init (); +destroy_keylocker_data (); }static int __initdata setup_possible_cpus =-1 ;
Signature · 1 line
2.17.1
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 13 lines · Chang S. Bae
arch/x86/include/asm/keylocker.h +1
-0@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h>
arch/x86/include/asm/keylocker.h struct iwkey { · line 38
Quoted 10 lines
arch/x86/include/asm/keylocker.h +7
-0@@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif
Shouldn't the !CONFIG_X86_KEYLOCKER stubs be static inline functions instead of macros, so that type checking works?
- Eric
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 12 lines · Eric Biggers
arch/x86/include/asm/keylocker.h struct iwkey { · line 38
Quoted 8 lines · Chang S. Bae
arch/x86/include/asm/keylocker.h +7
-0+#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif Shouldn't the !CONFIG_X86_KEYLOCKER stubs be static inline functions instead of macros, so that type checking works?
I think either way works here. This macro is just for nothing.
Thanks,
Chang
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 14 lines · Chang S. Bae
Quoted 12 lines · Eric Biggers
arch/x86/include/asm/keylocker.h struct iwkey { · line 38
Quoted 7 lines · Chang S. Bae
+#ifdef
CONFIG_X86_KEYLOCKERarch/x86/include/asm/keylocker.h +6
-0+void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif Shouldn't the !CONFIG_X86_KEYLOCKER stubs be static inline functions instead of
macros, so that type checking works?
Quoted 1 line
I think either way works here. This macro is just for nothing.
Chang, I do prefer the 'static inline' as a general rule. Think of this:
static inline void setup_keylocker(struct cpuinfo_x86 *c) {}
versus:
#define setup_keylocker(c) do { } while (0)
Imagine some dope does:
char c; ... setup_keylocker(c);
With the macro, they'll get no type warning. The inline actually makes it easier to find bugs because folks will get _some_ type checking no matter how they compile the code.
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 27 lines · Dave Hansen
Quoted 14 lines · Chang S. Bae
Quoted 12 lines · Eric Biggers
arch/x86/include/asm/keylocker.h struct iwkey { · line 38
Quoted 7 lines · Chang S. Bae
+#ifdef
CONFIG_X86_KEYLOCKERarch/x86/include/asm/keylocker.h +6
-0+void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +#define setup_keylocker (c) do { } while (0) +#define destroy_keylocker_data () do { } while (0) +#endif Shouldn't the !CONFIG_X86_KEYLOCKER stubs be static inline functions instead of
macros, so that type checking works?Quoted 1 line
I think either way works here. This macro is just for nothing.
Chang, I do prefer the 'static inline' as a general rule. Think of this:
static inline void
setup_keylocker(struct cpuinfo_x86 *c){}versus:
#define
setup_keylocker(c)do { } while (0)Imagine some dope does:
Quoted 7 lines
char c; ... setup_keylocker(c);With the macro, they'll get no type warning. The inline actually makes it easier to find bugs because folks will get _some_ type checking no matter how they compile the code.
Ah, when the prototype with one or more arguments, 'static inline' allows the check. Then it is not an 'either-way' thing.
Looking at the x86 code, there are some seemingly related:
$ git grep "do { } while (0)" arch/x86 | grep -v "()"
arch/x86/include/asm/kprobes.h:#define flush_insn_slot(p) do { } while (0)
arch/x86/include/asm/mc146818rtc.h:#define lock_cmos(reg) do { } while (0)
arch/x86/include/asm/pgtable.h:#define flush_tlb_fix_spurious_fault(vma, address, ptep) do { } while (0)
arch/x86/include/asm/preempt.h:#define init_task_preempt_count(p) do { } while (0)
arch/x86/kvm/ioapic.h:#define ASSERT(x) do { } while (0)
arch/x86/kvm/mmu/mmu_internal.h:#define pgprintk(x...) do { } while (0)
arch/x86/kvm/mmu/mmu_internal.h:#define rmap_printk(x...) do { } while (0)
arch/x86/kvm/mmu/mmu_internal.h:#define MMU_WARN_ON(x) do { } while (0)Now I feel owed for some potential cleanup work.
Thanks,
Chang
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 10 lines · Chang S. Bae
Quoted 3 lines
With the macro, they'll get no type warning. The inline actually makes it easier to find bugs because folks will get _some_ type checking no matter how they compile the code.
Ah, when the prototype with one or more arguments, 'static inline' allows the check. Then it is not an 'either-way' thing.
Looking at the x86 code, there are some seemingly related:
$ git grep "do { } while (0)" arch/x86 | grep -v "()"
...
Right. It's not a hard and fast rule. We certainly take code either way and there can be real reasons to do it one way versus the other.
RE: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
In reply to Chang S. BaeQuoted 2 lines
arch/x86/kernel/keylocker.c +0
-0
...
arch/x86/kernel/keylocker.c line 28
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+void __init destroy_keylocker_data (void ) +{ +memset (&kl_setup.key,KEY_DESTROY ,sizeof (kl_setup.key)); +}
That's a special value for garbage collected keyring keys assigned a keytype of ".dead". memzero() or memzero_explicit() might be better for this use case.
Re: [PATCH v6 07/12] x86/cpu/keylocker: Load an internal wrapping key at boot-time
Quoted 11 lines · Robert (Servers)
Quoted 2 lines
arch/x86/kernel/keylocker.c +0
-0...
arch/x86/kernel/keylocker.c line 28
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+void __init destroy_keylocker_data (void ) +{ +memset (&kl_setup.key,KEY_DESTROY ,sizeof (kl_setup.key)); +}That's a special value for garbage collected keyring keys assigned a keytype of ".dead".
memzero()ormemzero_explicit()might be better for this use case.
memzero() looks to be the same as memset() in x86:
$ git grep memzero arch/x86/ | grep define arch/x86/boot/compressed/misc.c:#define memzero(s, n) memset((s), 0, (n))
Instead, memzero_explicit() looks to be about the right call here:
/**
* memzero_explicit - Fill a region of memory (e.g. sensitive
* keying data) with 0s.
...
* Note: usually using memset() is just fine (!), but in cases
* where clearing out _local_ data at the end of a scope is
* necessary, memzero_explicit() should be used instead in
* order to prevent the compiler from optimising away zeroing.
...
Then,
void __init destroy_keylocker_data(void)
{
memzero_explicit(&kl_setup.key, sizeof(kl_setup.key));
}Thanks,
Chang
PATCH v6 8/12 x86/PM/keylocker: Restore internal wrapping key on resume from ACPI S3/4
In reply to Chang S. BaeWhen the system enters the ACPI S3 or S4 sleep states, the internal wrapping key is discarded.
The primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even the prompt it does perform for unlocking the volume where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded. So it is motivated to meet dm-crypt's expectation that the key handles in the suspend-image remain valid after resume from an S-state.
Key Locker provides a mechanism to back up the internal wrapping key in non-volatile storage. The kernel requests a backup right after the key is loaded at boot time. It is copied back to each CPU upon wakeup.
While the backup may be maintained in NVM across S5 and G3 "off" states it is not architecturally guaranteed, nor is it expected by dm-crypt which expects to prompt for the key each time the volume is started.
The entirety of Key Locker needs to be disabled if the backup mechanism is not available unless CONFIG_SUSPEND=n, otherwise dm-crypt requires the backup to be available.
In the event of a key restore failure the kernel proceeds with an initialized IWKey state. This has the effect of invalidating any key handles that might be present in a suspend-image. When this happens dm-crypt will see I/O errors resulting from error returns from crypto_skcipher_{en,de}crypt(). While this will disrupt operations in the current boot, data is not at risk and access is restored at the next reboot to create new handles relative to the current IWKey.
Manage a feature-specific flag to communicate with the crypto implementation. This ensures to stop using the AES instructions upon the key restore failure while not turning off the feature.
---
Changes from v5:
* Fix the 'valid_kl' flag not to be set when the feature is disabled. (Reported by Marvin Hsu [email protected]) Add the function comment about this.
* Improve the error handling in setup_keylocker(). All the error cases fall through the end that disables the feature. Otherwise, all the successful cases return immediately.
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebased on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 4 + arch/x86/kernel/keylocker.c | 138 ++++++++++++++++++++++++++++++- arch/x86/power/cpu.c | 2 + 3 files changed, 140 insertions(+), 4 deletions(-)
arch/x86/include/asm/keylocker.h +4 -0
@@ -32,9 +32,13 @@ struct iwkey {#ifdef CONFIG_X86_KEYLOCKER void setup_keylocker (struct cpuinfo_x86 *c);void destroy_keylocker_data (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void );#else #define setup_keylocker (c) do { } while (0)#define destroy_keylocker_data () do { } while (0) +#define restore_keylocker () do { } while (0) +static inline bool valid_keylocker (void ) {return false; }#endif #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +134 -4
182 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -27,6 +27,7 @@#include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -264,6 +265,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) x86_platform.restore_sched_clock_state ();cache_bp_restore ();perf_restore_debug_store (); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 1 line
2.17.1
Re: [PATCH v6 08/12] x86/PM/keylocker: Restore internal wrapping key on resume from ACPI S3/4
arch/x86/kernel/keylocker.c line 32
Quoted 12 lines · Chang S. Bae
arch/x86/kernel/keylocker.c +12
-0+/* +* This flag is set with IWKey load. When the key restore fails, it is +* reset. This restore state is exported to the crypto library, then AES-KL +* will not be used there. So, the feature is soft-disabled with this flag. +*/ +static bool valid_kl; + +bool valid_keylocker (void ) +{ +return valid_kl; +} +EXPORT_SYMBOL_GPL (valid_keylocker);
It would be simpler to export this bool directly.
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+if (status &BIT (0 )) +return 0 ; +else +return -EBUSY ;
[...]
arch/x86/kernel/keylocker.c void __ref setup_keylocker(struct cpuinfo_x86 *c) · line 136
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+pr_info ("x86/keylocker: Enabled.\n" ); +return ; + }else { +int rc;
The kernel coding style usually doesn't use 'else' after a return.
- Eric
Re: [PATCH v6 08/12] x86/PM/keylocker: Restore internal wrapping key on resume from ACPI S3/4
Quoted 15 lines · Eric Biggers
arch/x86/kernel/keylocker.c line 32
Quoted 12 lines · Chang S. Bae
arch/x86/kernel/keylocker.c +12
-0+/* +* This flag is set with IWKey load. When the key restore fails, it is +* reset. This restore state is exported to the crypto library, then AES-KL +* will not be used there. So, the feature is soft-disabled with this flag. +*/ +static bool valid_kl; + +bool valid_keylocker (void ) +{ +return valid_kl; +} +EXPORT_SYMBOL_GPL (valid_keylocker);It would be simpler to export this bool directly.
Yeah, but this wrapper is for code encapsulation. The code outside of the core code is not allowed to overwrite the value.
Perhaps, it is better to export it only with the AES-KL module:
#if IS_MODULE(CONFIG_CRYPTO_AES_KL)EXPORT_SYMBOL_GPL(valid_keylocker);
#endif
Quoted 11 lines
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+if (status &BIT (0 )) +return 0 ; +else +return -EBUSY ;[...]
arch/x86/kernel/keylocker.c void __ref setup_keylocker(struct cpuinfo_x86 *c) · line 136
Quoted 4 lines
arch/x86/kernel/keylocker.c +4
-0+pr_info ("x86/keylocker: Enabled.\n" ); +return ; + }else { +int rc;The kernel coding style usually doesn't use 'else' after a return.
Yeah, right. Will fix up.
Thanks,
Chang
PATCH v6 9/12 x86/cpu: Add a configuration and command line option for Key Locker
In reply to Chang S. BaeAdd CONFIG_X86_KEYLOCKER to gate whether Key Locker is initialized at boot. The option is selected by the Key Locker cipher module CRYPTO_AES_KL (to be added in a later patch).
Add a new command line option "nokeylocker" to optionally override the default CONFIG_X86_KEYLOCKER=y behavior.
---
Changes from RFC v2:
* Make the option selected by CRYPTO_AES_KL. (Dan Williams)
* Massage the changelog and the config option description.
--- Documentation/admin-guide/kernel-parameters.txt | 2 ++ arch/x86/Kconfig | 3 +++ arch/x86/kernel/cpu/common.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -3655,6 +3655,8 @@ nohugevmalloc [KNL,X86,PPC,ARM64] Disable kernel huge vmalloc mappings. + nokeylocker [X86] Disable Key Locker hardware feature. + nosmt [KNL,S390] Disable symmetric multithreading (SMT). Equivalent to smt=1.
arch/x86/Kconfig +3 -0
@@ -1894,6 +1894,9 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_KEYLOCKER + bool + choice prompt "TSX enable mode" depends on CPU_SUP_INTEL
arch/x86/kernel/cpu/common.c +16 -0
@@ -419,6 +419,22 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)static const unsigned long cr4_pinned_mask =X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_UMIP |X86_CR4_FSGSBASE |X86_CR4_CET ; + +static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg)) +return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup); +static DEFINE_STATIC_KEY_FALSE_RO (cr_pinning);static unsigned long cr4_pinned_bits__ro_after_init ;
Signature · 1 line
2.17.1
PATCH v6 10/12 crypto: x86/aes - Prepare for a new AES implementation
In reply to Chang S. BaeKey Locker's AES instruction set ('AES-KL') has a similar programming interface to AES-NI. The internal ABI in the assembly code will have the same prototype as AES-NI. Then, the glue code will be the same as AES-NI's.
Refactor the common C code to avoid code duplication. The AES-NI code uses it with a function pointer argument to call back the AES-NI assembly code. So will the AES-KL code.
Introduce wrappers for data transformation functions to return an error code. AES-KL may populate an error during data transformation.
Export those refactored functions and new wrappers that the AES-KL code will reference.
Also move some constant values to a common assembly code.
No functional change intended.
---
Changes from v5:
* Clean up the staled function definition -- cbc_crypt_common().
* Ensure kernel_fpu_end() for the possible error return from xts_crypt_common()->crypt1_fn().
Changes from v4:
* Drop CBC mode changes. (Eric Biggers)
Changes from v3:
* Drop ECB and CTR mode changes. (Eric Biggers)
* Export symbols. (Eric Biggers)
Changes from RFC v2:
* Massage the changelog. (Dan Williams)
Changes from RFC v1:
* Added as a new patch. (Ard Biesheuvel)
Link:
https://lore.kernel.org/lkml/CAMj1kXGa4f21eH0mdxd1pQsZMUjUr1Btq+Dgw-gC=O-yYft7xw@mail.gmail.com/
--- arch/x86/crypto/Makefile | 2 +- arch/x86/crypto/aes-intel_asm.S | 26 ++++ arch/x86/crypto/aes-intel_glue.c | 127 ++++++++++++++++ arch/x86/crypto/aes-intel_glue.h | 44 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +++---- arch/x86/crypto/aesni-intel_glue.c | 235 +++++++++-------------------- arch/x86/crypto/aesni-intel_glue.h | 17 +++ 7 files changed, 305 insertions(+), 204 deletions(-) create mode 100644 arch/x86/crypto/aes-intel_asm.S create mode 100644 arch/x86/crypto/aes-intel_glue.c create mode 100644 arch/x86/crypto/aes-intel_glue.h create mode 100644 arch/x86/crypto/aesni-intel_glue.h
arch/x86/crypto/Makefile +1 -1
@@ -47,7 +47,7 @@ chacha-x86_64-y := chacha-avx2-x86_64.o chacha-ssse3-x86_64.o chacha_glue.o chacha-x86_64-$(CONFIG_AS_AVX512) += chacha-avx512vl-x86_64.o obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o -aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o +aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aes-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o
arch/x86/crypto/aes-intel_asm.S new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Constant values shared between AES implementations: + */ + +.pushsection .rodata +.align 16 +.Lcts_permute_table: + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 + .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 +#ifdef __x86_64__ +.Lbswap_mask: + .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#endif +.popsection + +.section .rodata.cst16.gf128mul_x_ble_mask, "aM", @progbits, 16 +.align 16 +.Lgf128mul_x_ble_mask: + .octa 0x00000000000000010000000000000087 +.previous
arch/x86/crypto/aes-intel_glue.c new file +127 -0
127 lines not on this page · raw message
arch/x86/crypto/aes-intel_glue.h new file +44 -0
44 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +19 -39
130 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.c +71 -164
361 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/ + +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len); + +int aesni_enc (const void *ctx,u8 *out,const u8 *in); +int aesni_dec (const void *ctx,u8 *out,const u8 *in); + +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +
Signature · 1 line
2.17.1
Re: [PATCH v6 10/12] crypto: x86/aes - Prepare for a new AES implementation
Quoted 3 lines · Chang S. Bae
Refactor the common C code to avoid code duplication. The AES-NI code uses it with a function pointer argument to call back the AES-NI assembly code. So will the AES-KL code.
Actually, the AES-NI XTS glue code currently makes direct calls to the assembly code. This patch changes it to make indirect calls. Indirect calls are very expensive these days, partly due to all the speculative execution mitigations. So this patch likely causes a performance regression. How about making xts_crypt_common() and xts_setkey_common() be inline functions?
Another issue with having the above be exported symbols is that their names are too generic, so they could easily collide with another symbols in the kernel. To be exported symbols, they would need something x86-specific in their names.
Quoted 7 lines
arch/x86/crypto/Makefile | 2 +- arch/x86/crypto/aes-intel_asm.S | 26 ++++ arch/x86/crypto/aes-intel_glue.c | 127 ++++++++++++++++ arch/x86/crypto/aes-intel_glue.h | 44 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +++---- arch/x86/crypto/aesni-intel_glue.c | 235 +++++++++-------------------- arch/x86/crypto/aesni-intel_glue.h | 17 +++
It's confusing having aes-intel, aesni-intel, *and* aeskl-intel. Maybe call the first one "aes-helpers" or "aes-common" instead?
arch/x86/crypto/aes-intel_glue.h line 22
Quoted 4 lines
arch/x86/crypto/aes-intel_glue.h new file +4
-0+struct aes_xts_ctx { + u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AES_ALIGN_ATTR; + u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AES_ALIGN_ATTR; +};
This struct does not make sense. It should look like:
struct aes_xts_ctx {
struct crypto_aes_ctx tweak_ctx AES_ALIGN_ATTR;
struct crypto_aes_ctx crypt_ctx AES_ALIGN_ATTR;
};The runtime alignment to a 16-byte boundary should happen when translating the raw crypto_skcipher_ctx() into the pointer to the aes_xts_ctx. It should not happen when accessing each individual field in the aes_xts_ctx.
Quoted 6 lines
/*
- * int aesni_set_key(structcrypto_aes_ctx*ctx, const u8*in_key,
- * unsigned intkey_len)
+ * int _aesni_set_key(structcrypto_aes_ctx*ctx, const u8*in_key,
+ * unsigned intkey_len)
*/
It's conventional to use two leading underscores, not one.
- Eric
Re: [PATCH v6 10/12] crypto: x86/aes - Prepare for a new AES implementation
Quoted 10 lines · Eric Biggers
Quoted 3 lines · Chang S. Bae
Refactor the common C code to avoid code duplication. The AES-NI code uses it with a function pointer argument to call back the AES-NI assembly code. So will the AES-KL code.
Actually, the AES-NI XTS glue code currently makes direct calls to the assembly code. This patch changes it to make indirect calls. Indirect calls are very expensive these days, partly due to all the speculative execution mitigations. So this patch likely causes a performance regression. How about making
xts_crypt_common()andxts_setkey_common()be inline functions?
I guess this series is relevant:
https://lore.kernel.org/lkml/[email protected]/#t
Yeah, inlining those looks to be just a cut-and-paste work. Then I was curious about the performance impact.
So I picked one of my old machines. Then, I was able to quickly run through with these cases:
$ cryptsetup benchmark -c aes-xts -s 256
# Tests are approximate using memory only (no storage IO).
# Algorithm | Key | Encryption | Decryption
Upstream (6.4-rc1)
aes-xts 256b 3949.3 MiB/s 4014.2 MiB/s
aes-xts 256b 4016.1 MiB/s 4011.6 MiB/s
aes-xts 256b 4026.2 MiB/s 4018.4 MiB/s
aes-xts 256b 4009.2 MiB/s 4006.9 MiB/s
aes-xts 256b 4025.0 MiB/s 4016.4 MiB/sUpstream + V6
aes-xts 256b 3876.1 MiB/s 3963.6 MiB/s
aes-xts 256b 3926.3 MiB/s 3984.2 MiB/s
aes-xts 256b 3940.8 MiB/s 3961.2 MiB/s
aes-xts 256b 3929.7 MiB/s 3984.7 MiB/s
aes-xts 256b 3892.5 MiB/s 3942.5 MiB/sUpstream + V6 + {inlined helpers}
aes-xts 256b 3996.9 MiB/s 4085.4 MiB/s
aes-xts 256b 4087.6 MiB/s 4104.9 MiB/s
aes-xts 256b 4117.9 MiB/s 4130.2 MiB/s
aes-xts 256b 4098.4 MiB/s 4120.6 MiB/s
aes-xts 256b 4095.5 MiB/s 4111.5 MiB/sOkay, I'm a bit more convinced with this inlining. Will try to comment this along with the change.
Quoted 3 lines
Another issue with having the above be exported symbols is that their names are too generic, so they could easily collide with another symbols in the kernel. To be exported symbols, they would need something x86-specific in their names.
I think that's another good point though, they don't need to be exported once moved into the header so that inlined.
Quoted 10 lines
Quoted 7 lines
arch/x86/crypto/Makefile | 2 +- arch/x86/crypto/aes-intel_asm.S | 26 ++++ arch/x86/crypto/aes-intel_glue.c | 127 ++++++++++++++++ arch/x86/crypto/aes-intel_glue.h | 44 ++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +++---- arch/x86/crypto/aesni-intel_glue.c | 235 +++++++++-------------------- arch/x86/crypto/aesni-intel_glue.h | 17 +++It's confusing having aes-intel, aesni-intel, *and* aeskl-intel. Maybe call the first one "aes-helpers" or "aes-common" instead?
Yeah, I can see a few files named with helper. So, maybe s/aes-intel/aes-helpers/.
Quoted 6 lines
arch/x86/crypto/aes-intel_glue.h
Quoted 4 lines
arch/x86/crypto/aes-intel_glue.h new file +4
-0+struct aes_xts_ctx { + u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AES_ALIGN_ATTR; + u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AES_ALIGN_ATTR; +}; >This struct does not make sense. It should look like:
Quoted 8 lines
struct aes_xts_ctx { struct crypto_aes_ctx tweak_ctx AES_ALIGN_ATTR; struct crypto_aes_ctx crypt_ctx AES_ALIGN_ATTR; };The runtime alignment to a 16-byte boundary should happen when translating the raw
crypto_skcipher_ctx()into the pointer to theaes_xts_ctx. It should not happen when accessing each individual field in theaes_xts_ctx.
Oh, ugly. This came from mindless copy & paste here. I guess the fix could be a standalone patch. Or, it can be fixed along with this mess.
Quoted 8 lines
Quoted 6 lines
/*
- * int aesni_set_key(structcrypto_aes_ctx*ctx, const u8*in_key,
- * unsigned intkey_len)
+ * int _aesni_set_key(structcrypto_aes_ctx*ctx, const u8*in_key,
+ * unsigned intkey_len)
*/It's conventional to use two leading underscores, not one.
Yes, will fix.
Thanks,
Chang
PATCH v6 10/12 crypto: x86/aes - Prepare for a new AES implementation
Note, this only incorporates the fix for the performance issue. It is intended for those who want to run the series quickly. Otherwise, please ignore this. The next posting will address all the feedback.
---
Key Locker's AES instruction set ('AES-KL') has a similar programming interface to AES-NI. The internal ABI in the assembly code will have the same prototype as AES-NI. Then, the glue code will be the same as AES-NI's.
Refactor the common C code to avoid code duplication. The AES-NI code uses it with a function pointer argument to call back the AES-NI assembly code. So will the AES-KL code.
Introduce wrappers for data transformation functions to return an error code. AES-KL may populate an error during data transformation.
Export those refactored functions and new wrappers that the AES-KL code will reference.
Also move some constant values to a common assembly code.
No functional change intended.
---
Changes from v6:
* Inline the XTS helpers. Otherwise, measurable overhead is induced by indirect calls with the retpoline instrumentation. (Eric Biggers) Then, remove the glue helper .c file.
Changes from v5:
* Clean up the staled function definition -- cbc_crypt_common().
* Ensure kernel_fpu_end() for the possible error return from xts_crypt_common()->crypt1_fn().
Changes from v4:
* Drop CBC mode changes. (Eric Biggers)
Changes from v3:
* Drop ECB and CTR mode changes. (Eric Biggers)
* Export symbols. (Eric Biggers)
Changes from RFC v2:
* Massage the changelog. (Dan Williams)
Changes from RFC v1:
* Added as a new patch. (Ard Biesheuvel)
--- arch/x86/crypto/aes-intel_asm.S | 26 ++++ arch/x86/crypto/aes-intel_glue.h | 162 ++++++++++++++++++++ arch/x86/crypto/aesni-intel_asm.S | 58 +++---- arch/x86/crypto/aesni-intel_glue.c | 235 +++++++++-------------------- arch/x86/crypto/aesni-intel_glue.h | 17 +++ 5 files changed, 295 insertions(+), 203 deletions(-) create mode 100644 arch/x86/crypto/aes-intel_asm.S create mode 100644 arch/x86/crypto/aes-intel_glue.h create mode 100644 arch/x86/crypto/aesni-intel_glue.h
arch/x86/crypto/aes-intel_asm.S new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Constant values shared between AES implementations: + */ + +.pushsection .rodata +.align 16 +.Lcts_permute_table: + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 + .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 +#ifdef __x86_64__ +.Lbswap_mask: + .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#endif +.popsection + +.section .rodata.cst16.gf128mul_x_ble_mask, "aM", @progbits, 16 +.align 16 +.Lgf128mul_x_ble_mask: + .octa 0x00000000000000010000000000000087 +.previous
arch/x86/crypto/aes-intel_glue.h new file +162 -0
162 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +19 -39
130 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.c +71 -164
361 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/ + +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len); + +int aesni_enc (const void *ctx,u8 *out,const u8 *in); +int aesni_dec (const void *ctx,u8 *out,const u8 *in); + +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +
Signature · 1 line
2.17.1
Re: [PATCH v6 10/12] crypto: x86/aes - Prepare for a new AES implementation
Quoted 6 lines · Chang S. Bae
arch/x86/crypto/aes-intel_glue.h new file +6
-0+ +struct aes_xts_ctx { + struct crypto_aes_ctx tweak_ctx AES_ALIGN_ATTR; + struct crypto_aes_ctx crypt_ctx AES_ALIGN_ATTR; +}; +
arch/x86/crypto/aes-intel_glue.h line 43
Quoted 10 lines
arch/x86/crypto/aes-intel_glue.h new file +10
-0+static inline struct crypto_aes_ctx *aes_ctx (void *raw_ctx) +{ +unsigned long addr = (unsigned long )raw_ctx; +unsigned long align =AES_ALIGN ; + +if (align <=crypto_tfm_ctx_alignment ()) + align =1 ; + +return (struct crypto_aes_ctx *)ALIGN (addr, align); +}
It seems you took my suggestion to fix the definition of struct aes_xts_ctx, but you didn't make the corresponding change to the runtime alignment code. There should be a helper function aes_xts_ctx() that is used like:
struct aes_xts_ctx *ctx = aes_xts_ctx(tfm);
It would do the runtime alignment. Then, aes_ctx() should be removed.
- Eric
Re: [PATCH v6 10/12] crypto: x86/aes - Prepare for a new AES implementation
Quoted 12 lines · Eric Biggers
Quoted 11 lines · Chang S. Bae
(quoted diff) +11
-0+ +struct aes_xts_ctx { + struct crypto_aes_ctx tweak_ctx AES_ALIGN_ATTR; + struct crypto_aes_ctx crypt_ctx AES_ALIGN_ATTR; +}; + +static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx) +{ + unsigned long addr = (unsigned long)raw_ctx; + unsigned long align = AES_ALIGN; +
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if (align <= crypto_tfm_ctx_alignment()) + align = 1; + + return (struct crypto_aes_ctx *)ALIGN(addr, align); +}It seems you took my suggestion to fix the definition of struct
aes_xts_ctx, but you didn't make the corresponding change to the runtime alignment code.
Sigh. This particular change was unintentionally leaked here from my WIP code. Yes, I'm aware of your comment:
Quoted 9 lines
The runtime alignment to a 16-byte boundary should happen when translating the raw
crypto_skcipher_ctx()into the pointer to theaes_xts_ctx. It should not happen when accessing each individual field in theaes_xts_ctx.There should be a helper function
aes_xts_ctx()that is used like:struct aes_xts_ctx *ctx = aes_xts_ctx(tfm);It would do the runtime alignment. Then,
aes_ctx()should be removed.
Yes, I could think of some changes like the one below. I guess the aeskl code can live with it. The aesni glue code still wants aes_cts() as it deals with other modes. Then, that can be left there as it is.
arch/x86/crypto/aes-intel_glue.h +3 -3
@@ -31,15 +31,15 @@ struct aes_xts_ctx { struct crypto_aes_ctx crypt_ctx AES_ALIGN_ATTR; }; -static inline struct crypto_aes_ctx *aes_ctx (void *raw_ctx) +static inline struct aes_xts_ctx *aes_xts_ctx (struct crypto_skcipher *tfm) { -unsigned long addr = (unsigned long )raw_ctx; +unsigned long addr = (unsigned long )crypto_skcipher_ctx (tfm);unsigned long align =AES_ALIGN ;if (align <=crypto_tfm_ctx_alignment ()) align =1 ; -return (struct crypto_aes_ctx *)ALIGN (addr, align); +return (struct aes_xts_ctx *)ALIGN (addr, align); } static inline int
@@ -47,7 +47,7 @@ xts_setkey_common(struct crypto_skcipher *tfm, const u8 *key, unsigned int keyle int (*fn)(struct crypto_tfm *tfm, void *ctx, const u8
*in_key,
unsigned int key_len))
{
- struct aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct aes_xts_ctx *ctx = aes_xts_ctx(tfm);
int err;
err = xts_verify_key(tfm, key, keylen);
@@ -72,7 +72,7 @@ xts_crypt_common(struct skcipher_request *req,
int (*crypt1_fn)(const void *ctx, u8 *out, const u8 *in))
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
- struct aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct aes_xts_ctx *ctx = aes_xts_ctx(tfm);
int tail = req->cryptlen % AES_BLOCK_SIZE;
struct skcipher_request subreq;
struct skcipher_walk walk;
@@ -108,7 +108,7 @@ xts_crypt_common(struct skcipher_request *req,
kernel_fpu_begin();
/* calculate first value of T */
- err = crypt1_fn(aes_ctx(&ctx->tweak_ctx), walk.iv, walk.iv);
+ err = crypt1_fn(&ctx->tweak_ctx, walk.iv, walk.iv);
if (err) {
kernel_fpu_end();
return err;@@ -120,7 +120,7 @@ xts_crypt_common(struct skcipher_request *req,
if (nbytes < walk.total)
nbytes &= ~(AES_BLOCK_SIZE - 1);
- err = crypt_fn(aes_ctx(&ctx->crypt_ctx), walk.dst.virt.addr, walk.src.virt.addr,
+ err = crypt_fn(&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr,
nbytes, walk.iv);
kernel_fpu_end();
if (err)
@@ -148,7 +148,7 @@ xts_crypt_common(struct skcipher_request *req,
return err;
kernel_fpu_begin();
- err = crypt_fn(aes_ctx(&ctx->crypt_ctx), walk.dst.virt.addr, walk.src.virt.addr,
+ err = crypt_fn(&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr,
walk.nbytes, walk.iv);
kernel_fpu_end();
if (err)Thanks,
Chang
PATCH v6 11/12 crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
In reply to Chang S. BaeKey Locker is a CPU feature to reduce key exfiltration opportunities while maintaining a programming interface similar to AES-NI. It converts the AES key into an encoded form, called the 'key handle'.
The key handle is a wrapped version of the clear-text key where the wrapping key has limited exposure. Once converted via setkey(), all subsequent data encryption using new AES instructions ('AES-KL') uses this key handle, reducing the exposure of private key material in memory.
AES-KL is analogous to that of AES-NI. Most assembly code is translated from the AES-NI code. They are operational in both 32-bit and 64-bit modes like AES-NI. However, users need to be aware of the following differences:
== Key Handle ==
AES-KL may fail with an invalid key handle. It could be corrupted or fail with handle restriction. A key handle may be encoded with some restrictions. The implementation restricts every handle only available in kernel mode via setkey().
=== AES Compliance ===
Key Locker is not AES compliant in that it lacks support for 192-bit keys. However, per the expectations of Linux crypto-cipher implementations the software cipher implementation must support all the AES compliant key-sizes. The AES-KL cipher implementation achieves this constraint by logging a warning and falling back to AES-NI. In other words the 192-bit key-size limitation for what can be converted into a Key Locker key-handle is only documented, not enforced. This along with the below performance and failure mode implications is an end-user consideration for selecting AES-KL vs AES-NI.
== API Limitation ==
The setkey() function transforms an AES key to a handle. An extended key is a usual outcome of setkey() in other AES cipher implementations. For this reason, a setkey() failure does not fall back to the other. So, AES-KL will be exposed via synchronous interfaces only.
== Wrapping-key Restore Failure ==
The setkey() failure is also possible with the wrapping-key restore failure. In an event of hardware failure, the wrapping key is lost from deep sleep states. Then, setkey() will fail with the ENODEV error. And the suspended data transforming task may resume but will fail due to the wrapping key change.
== Performance ==
This feature comes with some performance penalties vs AES-NI. The cryptsetup benchmark indicates Key Locker raw throughput can be ~5x slower than AES-NI. For disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth, but the potential performance difference is why AES-KL is advertised as a distinct cipher in /proc/crypto rather than the kernel transparently replacing AES-NI usage with AES-KL.
---
Changes from v5:
* Replace the ret instruction with RET as rebased on the upstream -- commit f94909ceb1ed ("x86: Prepare asm files for straight-line-speculation").
Changes from v3:
* Exclude non-AES-KL objects. (Eric Biggers)
* Simplify the assembler dependency check. (Peter Zijlstra)
* Trim the Kconfig help text. (Dan Williams)
* Fix a defined-but-not-used warning.
Changes from RFC v2:
* Move out each mode support in new patches.
* Update the changelog to describe the limitation and the tradeoff clearly. (Andy Lutomirski)
Changes from RFC v1:
* Rebased on the refactored code. (Ard Biesheuvel)
* Dropped exporting the single block interface. (Ard Biesheuvel)
* Fixed the fallback and error handling paths. (Ard Biesheuvel)
* Revised the module description. (Dave Hansen and Peter Zijlsta)
* Made the build depend on the binutils version to support new instructions. (Borislav Petkov and Peter Zijlstra)
* Updated the changelog accordingly.
--- arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aeskl-intel_asm.S | 184 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 115 ++++++++++++++++++ crypto/Kconfig | 36 ++++++ 4 files changed, 338 insertions(+) create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c
arch/x86/crypto/Makefile +3 -0
@@ -50,6 +50,9 @@ obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aes-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o +obj-$(CONFIG_CRYPTO_AES_KL) += aeskl-intel.o +aeskl-intel-y := aeskl-intel_asm.o aeskl-intel_glue.o + obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o sha1-ssse3-$(CONFIG_AS_SHA1_NI) += sha1_ni_asm.o
arch/x86/crypto/aeskl-intel_asm.S new file +184 -0
184 lines not on this page · raw message
arch/x86/crypto/aeskl-intel_glue.c new file +115 -0
115 lines not on this page · raw message
crypto/Kconfig +36 -0
@@ -573,6 +573,42 @@ config CRYPTO_TWOFISH See https://www.schneier.com/twofish.html for further information. +config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12 + +config CRYPTO_AES_KL + tristate "AES cipher algorithms (AES-KL)" + depends on AS_HAS_KEYLOCKER + depends on CRYPTO_AES_NI_INTEL + select X86_KEYLOCKER + + help + Key Locker provides AES SIMD instructions (AES-KL) for secure + data encryption and decryption. While this new instruction + set is analogous to AES-NI, AES-KL supports to encode an AES + key to an encoded form ('key handle') and uses it to transform + data instead of accessing the AES key. + + The setkey() transforms an AES key to a key handle, then the AES + key is no longer needed for data transformation. A user may + displace their keys from possible exposition. + + This key encryption is done by the CPU-internal wrapping key. + This wrapping key support is provided with X86_KEYLOCKER. + + AES-KL supports 128-/256-bit keys only. While giving a 192-bit + key does not return an error, as the AES-NI driver is chosen to + process it, the claimed security property is not available with + that. + + Bare metal disk encryption is the preferred use case. Key Locker + usage requires explicit opt-in at setup time. So select it if + unsure. + + See Documentation/x86/keylocker.rst for more details. + config CRYPTO_TWOFISH_COMMON tristate help
Signature · 1 line
2.17.1
Re: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Quoted 1 line · Chang S. Bae
[PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Thanks for dropping the unnecessary modes of operation (CBC, ECB, CTR). It simplified the patchset quite a bit!
Now that only AES-XTS is included, can you please also merge this patch with the following patch? As-is, this patch is misleading since it doesn't actually add "support" for anything at all. It actually just adds an unfinished AES-XTS implementation, which patch 12 then finishes. I assume that the current patchset organization is left over from when you were trying to support multiple modes of operation. IMO, it would be much easier to review if patches 11-12 were merged into one patch that adds the new AES-XTS implementation.
Quoted 4 lines
For disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth, but the potential performance difference is why AES-KL is advertised as a distinct cipher in
/proc/cryptorather than the kernel transparently replacing AES-NI usage with AES-KL.
This does not correctly describe what is going on. Actually, this patchset registers the AES-KL XTS algorithm with the usual name "xts(aes)". So, it can potentially be used by any AES-XTS user. It seems that you're actually relying on the algorithm priorities to prioritize AES-NI, as you've assigned priority 200 to AES-KL, whereas AES-NI has priority 401. Is that what you intend, and if so can you please update your explanation to properly explain this?
The alternative would be to use a unique algorithm name, such as "keylocker-xts(aes)". I'm not sure that would be better, given that the algorithms are compatible. However, that actually would seem to match the explanation you gave more closely, so perhaps that's what you actually intended?
Quoted 1 line
arch/x86/crypto/aeskl-intel_asm.S +0
-0
[...]
Quoted 9 lines
arch/x86/crypto/aeskl-intel_asm.S +9
-0+#ifdef __x86_64__ +#define AREG %rax +#define HANDLEP %rdi +#define OUTP %rsi +#define KLEN %r9d +#define INP %rdx +#define T1 %r10 +#define LEN %rcx +#define IVP %r8
arch/x86/crypto/aeskl-intel_asm.S line 65
Quoted 10 lines
arch/x86/crypto/aeskl-intel_asm.S +10
-0+#else +#define AREG %eax +#define HANDLEP %edi +#define OUTP AREG +#define KLEN %ebx +#define INP %edx +#define T1 %ecx +#define LEN %esi +#define IVP %ebp +#endif
I strongly recommend skipping the 32-bit support, as it's unlikely to be worth the effort.
And actually, aeskl-intel_glue.c only registers the algorithm for 64-bit anyway... So the 32-bit code paths are untested dead code.
Quoted 13 lines
arch/x86/crypto/aeskl-intel_asm.S +13
-0+static inline int aeskl_enc(const void *ctx, u8 *out, const u8 *in) +{ + if (unlikely(keylength(ctx) == AES_KEYSIZE_192)) + return -EINVAL; + else if (!valid_keylocker()) + return -ENODEV; + else if (_aeskl_enc(ctx, out, in)) + return -EINVAL; + else + return 0; +} + +static inline int aeskl_dec(const void *ctx, u8 *out, const u8 *in)
arch/x86/crypto/aeskl-intel_asm.S
Quoted 10 lines
arch/x86/crypto/aeskl-intel_asm.S +10
-0+{ + if (unlikely(keylength(ctx) == AES_KEYSIZE_192)) + return -EINVAL; + else if (!valid_keylocker()) + return -ENODEV; + else if (_aeskl_dec(ctx, out, in)) + return -EINVAL; + else + return 0; +}
I don't think the above two functions should exist. keylength() and valid_keylocker() should be checked before calling xts_crypt_common(), and the assembly functions should just return the correct error code (-EINVAL, apparently) instead of an unspecified nonzero value. That would leave nothing for a wrapper function to do.
Note: if you take this suggestion, the assembly functions would need to use SYM_TYPED_FUNC_START instead of SYM_FUNC_START.
- Eric
Re: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Quoted 5 lines · Eric Biggers
Quoted 1 line · Chang S. Bae
[PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Thanks for dropping the unnecessary modes of operation (CBC, ECB, CTR). It simplified the patchset quite a bit!
Yeah. But, there are more things to go away here as you pointed out here.
I thought some generic establishment (patch10) then introduce some mode-specific code (patch11). Considerably, this incremental change was expected to help reviewers.
Now I realize this introduces dead code on its hindsight. And this approach seems not helping that much.
Quoted 7 lines
Now that only AES-XTS is included, can you please also merge this patch with the following patch? As-is, this patch is misleading since it doesn't actually add "support" for anything at all. It actually just adds an unfinished AES-XTS implementation, which patch 12 then finishes. I assume that the current patchset organization is left over from when you were trying to support multiple modes of operation. IMO, it would be much easier to review if patches 11-12 were merged into one patch that adds the new AES-XTS implementation.
Yes, I agree to merge them.
Quoted 11 lines
Quoted 4 lines
For disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth, but the potential performance difference is why AES-KL is advertised as a distinct cipher in
/proc/cryptorather than the kernel transparently replacing AES-NI usage with AES-KL.This does not correctly describe what is going on. Actually, this patchset registers the AES-KL XTS algorithm with the usual name "xts(aes)". So, it can potentially be used by any AES-XTS user. It seems that you're actually relying on the algorithm priorities to prioritize AES-NI, as you've assigned priority 200 to AES-KL, whereas AES-NI has priority 401. Is that what you intend, and if so can you please update your explanation to properly explain this?
I think AES-KL could be a drop-in replacement for AES-NI IF it performs well -- something on par with AES-NI or better, AND it also supports all the key sizes. But, it can't be the default because that's not the case (at least for now).
Quoted 4 lines
The alternative would be to use a unique algorithm name, such as "keylocker-xts(aes)". I'm not sure that would be better, given that the algorithms are compatible. However, that actually would seem to match the explanation you gave more closely, so perhaps that's what you actually intended?
I think those AES implementations are functionally the same to end users. The key envelopment is not something user-visible to my understanding. So, I thought that same name makes sense.
Now looking at the changelog, this text in the 'performance' section appears to be relevant:
Quoted 3 lines
the potential performance difference is why AES-KL is advertised as a distinct cipher in
/proc/cryptorather than the kernel transparently replacing AES-NI usage with AES-KL.
But, this does not seem to be clear enough. Maybe, this exposition story can go under a new section. The changelog is already tl;dr...
Quoted 5 lines
I strongly recommend skipping the 32-bit support, as it's unlikely to be worth the effort.
And actually,
aeskl-intel_glue.conly registers the algorithm for 64-bit anyway... So the 32-bit code paths are untested dead code.
Yeah, will do. Also, I'd make the module available only with X86-64. Then, a bit of comments for the reason should come along.
Quoted 30 lines
Quoted 13 lines
arch/x86/crypto/aeskl-intel_glue.c new file +13
-0+static inline int aeskl_enc (const void *ctx,u8 *out,const u8 *in) +{ +if (unlikely (keylength (ctx) ==AES_KEYSIZE_192 )) +return -EINVAL ; +else if (!valid_keylocker ()) +return -ENODEV ; +else if (_aeskl_enc (ctx, out, in)) +return -EINVAL ; +else +return 0 ; +} + +static inline int aeskl_dec (const void *ctx,u8 *out,const u8 *in)arch/x86/crypto/aeskl-intel_glue.c line 87
Quoted 10 lines
arch/x86/crypto/aeskl-intel_glue.c new file +10
-0+{ +if (unlikely (keylength (ctx) ==AES_KEYSIZE_192 )) +return -EINVAL ; +else if (!valid_keylocker ()) +return -ENODEV ; +else if (_aeskl_dec (ctx, out, in)) +return -EINVAL ; +else +return 0 ; +}I don't think the above two functions should exist.
keylength()andvalid_keylocker()should be checked before callingxts_crypt_common(), and the assembly functions should just return the correct error code (-EINVAL, apparently) instead of an unspecified nonzero value. That would leave nothing for a wrapper function to do.
Quoted 2 lines
Note: if you take this suggestion, the assembly functions would need to use
SYM_TYPED_FUNC_STARTinstead ofSYM_FUNC_START.
I thought this is something benign to stay here. But, yes, I agree that it is better to simplify the code.
Thanks,
Chang
Re: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Quoted 2 lines · Chang S. Bae
I thought this is something benign to stay here. But, yes, I agree that it is better to simplify the code.
After staring at this a bit, I realized that at least the feature flag check needs to stay there. This can populate a proper error code when the feature abruptly gets disabled (most likely due to the backup failure).
Thanks,
Chang
Re: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
In reply to Eric BiggersOn 5/6/23 02:01, Eric Biggers wrote:
...
Quoted 11 lines
This does not correctly describe what is going on. Actually, this patchset registers the AES-KL XTS algorithm with the usual name "xts(aes)". So, it can potentially be used by any AES-XTS user. It seems that you're actually relying on the algorithm priorities to prioritize AES-NI, as you've assigned priority 200 to AES-KL, whereas AES-NI has priority 401. Is that what you intend, and if so can you please update your explanation to properly explain this?
The alternative would be to use a unique algorithm name, such as "keylocker-xts(aes)". I'm not sure that would be better, given that the algorithms are compatible. However, that actually would seem to match the explanation you gave more closely, so perhaps that's what you actually intended?
Sorry to be late in-game, but as this is intended for LUKS/dm-crypt use, I have a comment here:
LUKS2 will no longer support algorithms with the dash in the name for dm-crypt (iow "aes-generic" or something like that will no longer work, and I am afraid you will need aes-kl/keylocker-xts here to force to use AES-KL for dm-crypt).
One reason is described in https://gitlab.com/cryptsetup/cryptsetup/-/issues/809,
but the major problem is that cryptsetup used CIPHER-MODE-IV syntax (that mixes badly with the dash in algorithm names). And we still rely on internal conversions of common modes to that syntax (currently it worked only by a luck).
When I added the "capi" format for dm-crypt for algorithms specification, I made a mistake in that it allows everything, including crypto driver platform-specific names.
The intention was to keep the kernel to decide which crypto driver will be used. So, this is perhaps fine for dm-crypt now but LUKS is a portable format, and a generic algorithm (like AES) should not depend on a specific driver or CPU feature.
IOW, implement xts(aes) and let the user prioritize the driver (no changes needed for LUKS header then, AES-KL is loaded automatically) or/and create a wrapper (similar to paes, that we already support) that will force to use AES-KL (...but without the dash in the name, please :)
If there is a problem with it, please create an issue for cryptsetup upstream to discuss it there (before the kernel part is merged!), so we can find some solution - I would like to avoid incompatibilities later.
Thanks,
Milan
RE: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
In reply to Chang S. BaeQuoted 2 lines
crypto/Kconfig +0
-0
...
crypto/Kconfig config CRYPTO_TWOFISH · line 585
Quoted 10 lines
crypto/Kconfig +10
-0+config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12 + +config CRYPTO_AES_KL + tristate "AES cipher algorithms (AES-KL)" + depends on AS_HAS_KEYLOCKER + depends on CRYPTO_AES_NI_INTEL + select X86_KEYLOCKER
This material belongs in arch/x86/Kconfig now (which didn't exist when this patch series began).
RE: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Quoted 4 lines
-----Original Message-----
From: Elliott, Robert (Servers) <[email protected]>
...
Quoted 2 lines
This material belongs in
arch/x86/Kconfignow (which didn't exist when this patch series began).
Sorry, omitted one level:
arch/x86/crypto/Kconfig
Re: [PATCH v6 11/12] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions
Quoted 7 lines · Robert (Servers)
...
Quoted 2 lines
This material belongs in
arch/x86/Kconfignow (which didn't exist when this patch series began).Sorry, omitted one level:
arch/x86/crypto/Kconfig
Thanks for pointing this out! Otherwise, it might happen to be based on the old mess before 28a936ef44e1 ("crypto: Kconfig - move x86 entries to a submenu")
Thanks,
Chang
PATCH v6 12/12 crypto: x86/aes-kl - Support XTS mode
In reply to Chang S. BaeImplement XTS mode using AES-KL. Export the methods with a lower priority than AES-NI to avoid from selected by default.
The assembly code clobbers more than eight 128-bit registers to make use of performant wide instructions that process eight 128-bit blocks at once. But that many 128-bit registers are not available in 32-bit mode, so support 64-bit mode only.
---
Changes from v5:
* Replace the ret instruction with RET as rebased on the upstream -- commit f94909ceb1ed ("x86: Prepare asm files for straight-line-speculation").
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/crypto/aeskl-intel_asm.S | 449 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 107 ++++++- 2 files changed, 553 insertions(+), 3 deletions(-)
arch/x86/crypto/aeskl-intel_asm.S +449 -0
@@ -182,3 +182,452 @@ SYM_FUNC_START(_aeskl_dec) RET SYM_FUNC_END(_aeskl_dec) +#ifdef __x86_64__ + +/* + * XTS implementation + */ + +/* + * _aeskl_gf128mul_x_ble: internal ABI + * Multiply in GF(2^128) for XTS IVs + * input: + * IV: current IV + * GF128MUL_MASK == mask with 0x87 and 0x01 + * output: + * IV: next IV + * changed: + * CTR: == temporary value + */ +#define _aeskl_gf128mul_x_ble() \ + pshufd $0x13, IV, KEY; \ + paddq IV, IV; \ + psrad $31, KEY; \ + pand GF128MUL_MASK, KEY; \ + pxor KEY, IV; + +/* + * int _aeskl_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, + * const u8 *src, unsigned int len, le128 *iv) + */ +SYM_FUNC_START(_aeskl_xts_encrypt) + FRAME_BEGIN + movdqa .Lgf128mul_x_ble_mask(%rip), GF128MUL_MASK + movups (IVP), IV + + mov 480(HANDLEP), KLEN + +.Lxts_enc8: + sub $128, LEN + jl .Lxts_enc1_pre + + movdqa IV, STATE1 + movdqu (INP), INC + pxor INC, STATE1 + movdqu IV, (OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE2 + movdqu 0x10(INP), INC + pxor INC, STATE2 + movdqu IV, 0x10(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE3 + movdqu 0x20(INP), INC + pxor INC, STATE3 + movdqu IV, 0x20(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE4 + movdqu 0x30(INP), INC + pxor INC, STATE4 + movdqu IV, 0x30(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE5 + movdqu 0x40(INP), INC + pxor INC, STATE5 + movdqu IV, 0x40(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE6 + movdqu 0x50(INP), INC + pxor INC, STATE6 + movdqu IV, 0x50(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE7 + movdqu 0x60(INP), INC + pxor INC, STATE7 + movdqu IV, 0x60(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE8 + movdqu 0x70(INP), INC + pxor INC, STATE8 + movdqu IV, 0x70(OUTP) + + cmp $16, KLEN + je .Lxts_enc8_128 + aesencwide256kl (%rdi) + jz .Lxts_enc_ret_err + jmp .Lxts_enc8_end +.Lxts_enc8_128: + aesencwide128kl (%rdi) + jz .Lxts_enc_ret_err + +.Lxts_enc8_end: + movdqu 0x00(OUTP), INC + pxor INC, STATE1 + movdqu STATE1, 0x00(OUTP) + + movdqu 0x10(OUTP), INC + pxor INC, STATE2 + movdqu STATE2, 0x10(OUTP) + + movdqu 0x20(OUTP), INC + pxor INC, STATE3 + movdqu STATE3, 0x20(OUTP) + + movdqu 0x30(OUTP), INC + pxor INC, STATE4 + movdqu STATE4, 0x30(OUTP) + + movdqu 0x40(OUTP), INC + pxor INC, STATE5 + movdqu STATE5, 0x40(OUTP) + + movdqu 0x50(OUTP), INC + pxor INC, STATE6 + movdqu STATE6, 0x50(OUTP) + + movdqu 0x60(OUTP), INC + pxor INC, STATE7 + movdqu STATE7, 0x60(OUTP) + + movdqu 0x70(OUTP), INC + pxor INC, STATE8 + movdqu STATE8, 0x70(OUTP) + + _aeskl_gf128mul_x_ble() + + add $128, INP + add $128, OUTP + test LEN, LEN + jnz .Lxts_enc8 + +.Lxts_enc_ret_iv: + movups IV, (IVP) +.Lxts_enc_ret_noerr: + xor AREG, AREG + jmp .Lxts_enc_ret +.Lxts_enc_ret_err: + mov $1, AREG +.Lxts_enc_ret: + FRAME_END + RET + +.Lxts_enc1_pre: + add $128, LEN + jz .Lxts_enc_ret_iv + sub $16, LEN + jl .Lxts_enc_cts4 + +.Lxts_enc1: + movdqu (INP), STATE1 + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_enc1_128 + aesenc256kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + jmp .Lxts_enc1_end +.Lxts_enc1_128: + aesenc128kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + +.Lxts_enc1_end: + pxor IV, STATE1 + _aeskl_gf128mul_x_ble() + + test LEN, LEN + jz .Lxts_enc1_out + + add $16, INP + sub $16, LEN + jl .Lxts_enc_cts1 + + movdqu STATE1, (OUTP) + add $16, OUTP + jmp .Lxts_enc1 + +.Lxts_enc1_out: + movdqu STATE1, (OUTP) + jmp .Lxts_enc_ret_iv + +.Lxts_enc_cts4: + movdqu STATE8, STATE1 + sub $16, OUTP + +.Lxts_enc_cts1: + lea .Lcts_permute_table(%rip), T1 + add LEN, INP /* rewind input pointer */ + add $16, LEN /* # bytes in final block */ + movups (INP), IN1 + + mov T1, IVP + add $32, IVP + add LEN, T1 + sub LEN, IVP + add OUTP, LEN + + movups (T1), STATE2 + movaps STATE1, STATE3 + pshufb STATE2, STATE1 + movups STATE1, (LEN) + + movups (IVP), STATE1 + pshufb STATE1, IN1 + pblendvb STATE3, IN1 + movaps IN1, STATE1 + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_enc1_cts_128 + aesenc256kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + jmp .Lxts_enc1_cts_end +.Lxts_enc1_cts_128: + aesenc128kl (HANDLEP), STATE1 + jz .Lxts_enc_ret_err + +.Lxts_enc1_cts_end: + pxor IV, STATE1 + movups STATE1, (OUTP) + jmp .Lxts_enc_ret_noerr +SYM_FUNC_END(_aeskl_xts_encrypt) + +/* + * int _aeskl_xts_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, + * const u8 *src, unsigned int len, le128 *iv) + */ +SYM_FUNC_START(_aeskl_xts_decrypt) + FRAME_BEGIN + movdqa .Lgf128mul_x_ble_mask(%rip), GF128MUL_MASK + movups (IVP), IV + + mov 480(HANDLEP), KLEN + + test $15, LEN + jz .Lxts_dec8 + sub $16, LEN + +.Lxts_dec8: + sub $128, LEN + jl .Lxts_dec1_pre + + movdqa IV, STATE1 + movdqu (INP), INC + pxor INC, STATE1 + movdqu IV, (OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE2 + movdqu 0x10(INP), INC + pxor INC, STATE2 + movdqu IV, 0x10(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE3 + movdqu 0x20(INP), INC + pxor INC, STATE3 + movdqu IV, 0x20(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE4 + movdqu 0x30(INP), INC + pxor INC, STATE4 + movdqu IV, 0x30(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE5 + movdqu 0x40(INP), INC + pxor INC, STATE5 + movdqu IV, 0x40(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE6 + movdqu 0x50(INP), INC + pxor INC, STATE6 + movdqu IV, 0x50(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE7 + movdqu 0x60(INP), INC + pxor INC, STATE7 + movdqu IV, 0x60(OUTP) + + _aeskl_gf128mul_x_ble() + movdqa IV, STATE8 + movdqu 0x70(INP), INC + pxor INC, STATE8 + movdqu IV, 0x70(OUTP) + + cmp $16, KLEN + je .Lxts_dec8_128 + aesdecwide256kl (%rdi) + jz .Lxts_dec_ret_err + jmp .Lxts_dec8_end +.Lxts_dec8_128: + aesdecwide128kl (%rdi) + jz .Lxts_dec_ret_err + +.Lxts_dec8_end: + movdqu 0x00(OUTP), INC + pxor INC, STATE1 + movdqu STATE1, 0x00(OUTP) + + movdqu 0x10(OUTP), INC + pxor INC, STATE2 + movdqu STATE2, 0x10(OUTP) + + movdqu 0x20(OUTP), INC + pxor INC, STATE3 + movdqu STATE3, 0x20(OUTP) + + movdqu 0x30(OUTP), INC + pxor INC, STATE4 + movdqu STATE4, 0x30(OUTP) + + movdqu 0x40(OUTP), INC + pxor INC, STATE5 + movdqu STATE5, 0x40(OUTP) + + movdqu 0x50(OUTP), INC + pxor INC, STATE6 + movdqu STATE6, 0x50(OUTP) + + movdqu 0x60(OUTP), INC + pxor INC, STATE7 + movdqu STATE7, 0x60(OUTP) + + movdqu 0x70(OUTP), INC + pxor INC, STATE8 + movdqu STATE8, 0x70(OUTP) + + _aeskl_gf128mul_x_ble() + + add $128, INP + add $128, OUTP + test LEN, LEN + jnz .Lxts_dec8 + +.Lxts_dec_ret_iv: + movups IV, (IVP) +.Lxts_dec_ret_noerr: + xor AREG, AREG + jmp .Lxts_dec_ret +.Lxts_dec_ret_err: + mov $1, AREG +.Lxts_dec_ret: + FRAME_END + RET + +.Lxts_dec1_pre: + add $128, LEN + jz .Lxts_dec_ret_iv + +.Lxts_dec1: + movdqu (INP), STATE1 + + add $16, INP + sub $16, LEN + jl .Lxts_dec_cts1 + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_end +.Lxts_dec1_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_end: + pxor IV, STATE1 + _aeskl_gf128mul_x_ble() + + test LEN, LEN + jz .Lxts_dec1_out + + movdqu STATE1, (OUTP) + add $16, OUTP + jmp .Lxts_dec1 + +.Lxts_dec1_out: + movdqu STATE1, (OUTP) + jmp .Lxts_dec_ret_iv + +.Lxts_dec_cts1: + movdqa IV, STATE5 + _aeskl_gf128mul_x_ble() + + pxor IV, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_cts_pre_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_cts_pre_end +.Lxts_dec1_cts_pre_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_cts_pre_end: + pxor IV, STATE1 + + lea .Lcts_permute_table(%rip), T1 + add LEN, INP /* rewind input pointer */ + add $16, LEN /* # bytes in final block */ + movups (INP), IN1 + + mov T1, IVP + add $32, IVP + add LEN, T1 + sub LEN, IVP + add OUTP, LEN + + movups (T1), STATE2 + movaps STATE1, STATE3 + pshufb STATE2, STATE1 + movups STATE1, (LEN) + + movups (IVP), STATE1 + pshufb STATE1, IN1 + pblendvb STATE3, IN1 + movaps IN1, STATE1 + + pxor STATE5, STATE1 + + cmp $16, KLEN + je .Lxts_dec1_cts_128 + aesdec256kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + jmp .Lxts_dec1_cts_end +.Lxts_dec1_cts_128: + aesdec128kl (HANDLEP), STATE1 + jz .Lxts_dec_ret_err + +.Lxts_dec1_cts_end: + pxor STATE5, STATE1 + + movups STATE1, (OUTP) + jmp .Lxts_dec_ret_noerr + +SYM_FUNC_END(_aeskl_xts_decrypt) + +#endif
arch/x86/crypto/aeskl-intel_glue.c +104 -3
135 lines not on this page · raw message
Signature · 1 line
2.17.1
v7 cover x86: Support Key Locker
In reply to Chang S. BaeHi all,
The previous posting [v6] was intended to deliver the hardware performance update primarily by revamping the old series. Then, multiple feedbacks were received. This revision thus incorporates the changes to address them. So, the primary goal here is to meet those reviewers' expectations at first.
The series has two parts of changes -- the x86 core and its crypto library. In this revision, much more changes were made on the latter. Also, overall changelogs were revisited to be more reviewable.
Here is the overview of those feedbacks/changes (skip some trivial):
Part 1. Crypto Library (PATCH10-12):
PATCH12: AES-KL driver
- Merge all the AES-KL patches. (Eric Biggers)
- Make the driver for the 64-bit mode only. (Eric Biggers)
- Rework the key-size check code (Eric Biggers)
- Adjust the Kconfig change (Robert Elliott)
- Update the changelog. (Eric Biggers)
- Adjust the ASM code to return a proper error code. (Eric Biggers)
PATCH11: AES-NI rework
- Inline the helper code to avoid the indirect call. (Eric Biggers)
- Rename the filename: aes-intel* -> aes-helper*. (Eric Biggers)
- Don't export symbols yet here. Instead, do it when needed later.
- Improve the coding style (Eric Biggers)
PATCH10: the AESNI-XTS field type fix
- Add as a new patch. (Eric Biggers)
Part 2. The X86 Core (PATCH1-9):
PATCH09: a chicken bit and a Kconfig option
- Rebase on the upstream: commit a894a8a56b57 ("Documentation:
kernel-parameters: sort all "no..." parameters")
PATCH08: the wrapping key recovery
- Limit the symbol export only when needed.
PATCH07: the wrapping key load at boot-time
- Use memzero_explicit() instead of memset(). (Robert Elliott)
- Improve the function prototype. (Eric Biggers and Dave Hansen)
PATCH01: documentation
- Rebase on the upstream -- commit ff61f0791ce9 ("docs: move x86 documentation into Documentation/arch/"). (Nathan Huckleberry)
This version can be also found here:
git://github.com/intel-staging/keylocker.git kl-v7
[v6] -- the last posting:
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Chang
Chang S. Bae (12):
Documentation/x86: Document Key Locker
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/asm: Add a wrapper function for the LOADIWKEY instruction x86/msr-index: Add MSRs for Key Locker wrapping key x86/keylocker: Define Key Locker CPUID leaf x86/cpu/keylocker: Load a wrapping key at boot-time x86/PM/keylocker: Restore the wrapping key on the resume from ACPI
S3/4
x86/cpu: Add a configuration and command line option for Key Locker
crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
crypto: x86/aes - Prepare for a new AES implementation
crypto: x86/aes-kl - Implement the AES-XTS algorithm
.../admin-guide/kernel-parameters.txt | 2 + Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 97 +++ arch/x86/Kconfig | 3 + arch/x86/crypto/Kconfig | 22 + arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aes-helper_asm.S | 22 + arch/x86/crypto/aes-helper_glue.h | 161 +++++ arch/x86/crypto/aeskl-intel_asm.S | 580 ++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 216 +++++++ arch/x86/crypto/aesni-intel_asm.S | 55 +- arch/x86/crypto/aesni-intel_glue.c | 242 +++----- arch/x86/crypto/aesni-intel_glue.h | 17 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/keylocker.h | 45 ++ arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/special_insns.h | 32 + arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 21 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/keylocker.c | 212 +++++++ arch/x86/kernel/smpboot.c | 2 + arch/x86/lib/x86-opcode-map.txt | 11 +- arch/x86/power/cpu.c | 2 + tools/arch/x86/lib/x86-opcode-map.txt | 11 +- 27 files changed, 1573 insertions(+), 203 deletions(-) create mode 100644 Documentation/arch/x86/keylocker.rst create mode 100644 arch/x86/crypto/aes-helper_asm.S create mode 100644 arch/x86/crypto/aes-helper_glue.h create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h create mode 100644 arch/x86/include/asm/keylocker.h create mode 100644 arch/x86/kernel/keylocker.c
base-commit: 3a128547bd4425cdef27c606efc88e1eb03a2dba
Signature · 1 line
2.17.1
PATCH v7 1/12 Documentation/x86: Document Key Locker
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
Changes from v6:
* Rebase on the upstream -- commit ff61f0791ce9 ("docs: move x86 documentation into Documentation/arch/"). (Nathan Huckleberry)
* Remove a duplicated sentence -- 'But there is no AES-KL instruction to process a 192-bit key.'
* Update the text for clarity and readability:
- Clarify the error code and exemplify the backup failure
- Use 'wrapping key' instead of less readable 'IWKey'
Changes from v5:
* Fix a typo: 'feature feature' -> 'feature'
Changes from RFC v2:
* Add as a new patch.
The preview is available here:
https://htmlpreview.github.io/?https://github.com/intel-staging/keylocker/kdoc/arch/x86/keylocker.html
--- Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 97 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Documentation/arch/x86/keylocker.rst
Documentation/arch/x86/index.rst +1 -0
@@ -42,3 +42,4 @@ x86-specific Documentation features elf_auxvec xstate + keylocker
Documentation/arch/x86/keylocker.rst new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 2/12 x86/cpufeature: Enumerate Key Locker feature
In reply to Chang S. BaeKey Locker is a CPU feature to minimize exposure of clear-text key material. An encoded form, called 'key handle', is referenced for data encryption or decryption instead of accessing the clear text key.
A wrapping key loaded in the CPU's software-inaccessible state is used to transform a user key into a key handle. On rarely unexpected hardware failure, the key could be lost.
Here enumerate this hardware capability. It will not be shown up in /proc/cpuinfo as userspace usage is not supported. This is because there is no ABI to coordinate the wrapping-key failure.
The feature supports Advanced Encryption Standard (AES) cipher algorithm with new SIMD instruction set like its predecessor (AES-NI). Mark the feature depending on XMM2. The new AES implementation will be in the crypto library.
Add X86_FEATURE_KEYLOCKER to the disabled features mask at the moment. It will be enabled under a new config option.
---
Changes from v6:
* Massage the changelog -- re-organize the change descriptions
Changes from RFC v2:
* Do not publish the feature flag to userspace.
* Update the changelog.
Changes from RFC v1:
* Updated the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -389,6 +389,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_KEYLOCKER (16*32+23)/* "" Key Locker */ #define X86_FEATURE_BUS_LOCK_DETECT (16*32+24)/* Bus Lock detect */ #define X86_FEATURE_CLDEMOTE (16*32+25)/* CLDEMOTE instruction */ #define X86_FEATURE_MOVDIRI (16*32+27)/* MOVDIRI instruction */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -38,6 +38,12 @@# define DISABLE_OSPKE (1<<(X86_FEATURE_OSPKE & 31))#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */ +#ifdef CONFIG_X86_KEYLOCKER +# define DISABLE_KEYLOCKER 0 +#else +# define DISABLE_KEYLOCKER (1<<(X86_FEATURE_KEYLOCKER & 31)) +#endif /* CONFIG_X86_KEYLOCKER */ +#ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0#else
@@ -126,7 +132,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_KEYLOCKER)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK19 0
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -128,6 +128,8 @@#define X86_CR4_PCIDE _BITUL(X86_CR4_PCIDE_BIT)#define X86_CR4_OSXSAVE_BIT 18/* enable xsave and xrestore */ #define X86_CR4_OSXSAVE _BITUL(X86_CR4_OSXSAVE_BIT) +#define X86_CR4_KEYLOCKER_BIT 19/* enable Key Locker */ +#define X86_CR4_KEYLOCKER _BITUL(X86_CR4_KEYLOCKER_BIT)#define X86_CR4_SMEP_BIT 20/* enable SMEP support */ #define X86_CR4_SMEP _BITUL(X86_CR4_SMEP_BIT)#define X86_CR4_SMAP_BIT 21/* enable SMAP support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -81,6 +81,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, + { X86_FEATURE_KEYLOCKER, X86_FEATURE_XMM2 }, {} };
Signature · 1 line
2.17.1
PATCH v7 3/12 x86/insn: Add Key Locker instructions to the opcode map
In reply to Chang S. BaeThe x86 instruction decoder needs to know these new instructions that are going to be used in the crypto library as well as the x86 core code. Add the following:
LOADIWKEY:
Load a CPU-internal wrapping key.
ENCODEKEY128:
Wrap a 128-bit AES key to a key handle.
ENCODEKEY256:
Wrap a 256-bit AES key to a key handle.
AESENC128KL:
Encrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESENC256KL:
Encrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESDEC128KL:
Decrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESDEC256KL:
Decrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESENCWIDE128KL:
Encrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESENCWIDE256KL:
Encrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
AESDECWIDE128KL:
Decrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESDECWIDE256KL:
Decrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
The detail can be found in Intel Software Developer Manual.
---
Changes from v6:
* Massage the changelog -- add the reason a bit.
Changes from RFC v1:
* Separated out the LOADIWKEY addition in a new patch.
* Included AES instructions to avoid warning messages when the AES Key Locker module is built.
--- arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- tools/arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
tools/arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 4/12 x86/asm: Add a wrapper function for the LOADIWKEY instruction
In reply to Chang S. BaeKey Locker introduces a CPU-internal wrapping key to encode a user key to a key handle. Then a key handle is referenced instead of the plain text key.
LOADIWKEY loads a wrapping key in the software-inaccessible CPU state. It operates only in kernel mode.
The kernel will use this to load a new key at boot time. Establish a wrapper to prepare for this use. Also, define struct iwkey to pass the key value to it.
---
Changes from v6:
* Massage the changelog -- clarify the reason and the changes a bit.
Changes from v5:
* Fix a typo: kernel_cpu_begin() -> kernel_fpu_begin()
Changes from RFC v2:
* Separate out the code as a new patch.
* Improve the usability with the new struct as an argument. (Dan Williams)
Note, Dan wondered if:
WARN_ON(!irq_fpu_usable());
would be appropriate in the load_xmm_iwkey() function.
--- arch/x86/include/asm/keylocker.h | 25 ++++++++++++++++++++++ arch/x86/include/asm/special_insns.h | 32 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/keylocker.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_KEYLOCKER_H +#define _ASM_KEYLOCKER_H + +#ifndef __ASSEMBLY__ + +#include <asm/fpu/types.h> + +/** +* struct iwkey - A temporary wrapping key storage. +* @integrity_key: A 128-bit key to check that key handles have not +* been tampered with. +* @encryption_key: A 256-bit encryption key used in +* wrapping/unwrapping a clear text key. +* +* This storage should be flushed immediately after loaded. +*/ +struct iwkey { +struct reg_128_bit integrity_key; +struct reg_128_bit encryption_key[2 ]; +}; + +#endif /*__ASSEMBLY__ */ +#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +32 -0
@@ -9,6 +9,7 @@#include <asm/processor-flags.h> #include <linux/irqflags.h> #include <linux/jump_label.h> +#include <asm/keylocker.h> /* * The compiler should not reorder volatile asm statements with respect to each
@@ -283,6 +284,37 @@ static __always_inline void tile_release(void) asmvolatile (".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ); } +/** +* load_xmm_iwkey - Load a CPU-internal wrapping key +* @key: A struct iwkey pointer. +* +* Load @key to XMMs then do LOADIWKEY. After this, flush XMM +* registers. Caller is responsible for kernel_fpu_begin(). +*/ +static inline void load_xmm_iwkey (struct iwkey *key) +{ +struct reg_128_bit zeros = {0 }; + + asmvolatile ("movdqu %0, %%xmm0; movdqu %1, %%xmm1; movdqu %2, %%xmm2;" + ::"m" (key->integrity_key),"m" (key->encryption_key[0 ]), +"m" (key->encryption_key[1 ])); + +/* +* LOADIWKEY %xmm1,%xmm2 +* +* EAX and XMM0 are implicit operands. Load a key value +* from XMM0-2 to a software-invisible CPU state. With zero +* in EAX, CPU does not do hardware randomization and the key +* backup is allowed. +* +* This instruction is supported by binutils >= 2.36. +*/ + asmvolatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1" ::"a" (0 )); + + asmvolatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;" + ::"m" (zeros)); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 5/12 x86/msr-index: Add MSRs for Key Locker wrapping key
In reply to Chang S. BaeThe CPU state that contains the wrapping key is in the same power domain as the cache. So any sleep state that would invalidate the cache (like S3) also invalidates the state of the wrapping key.
But, since the state is inaccessible to software, it needs a special mechanism to save and restore the key during deep sleep.
A set of new MSRs are provided as an abstract interface to save and restore the wrapping key, and to check the key status. The wrapping key is saved in a platform-scoped state of non-volatile media. The backup itself and its path from the CPU are encrypted and integrity protected.
Define those MSRs to be used to save and restore the key for S3/4 sleep states.
But the backup storage's non-volatility is not architecturally guaranteed across off-states, such as S5 and G3. Then, the kernel may generate a new key on the next boot.
---
Changes from v6:
* Tweak the changelog -- put the last for those about other sleep states
Changes from RFC v2:
* Update the changelog. (Dan Williams)
* Rename the MSRs. (Dan Williams)
--- arch/x86/include/asm/msr-index.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
@@ -1117,4 +1117,10 @@* a #GP */ +/* MSRs for managing a CPU-internal wrapping key for Key Locker. */ +#define MSR_IA32_IWKEY_COPY_STATUS 0x00000990 +#define MSR_IA32_IWKEY_BACKUP_STATUS 0x00000991 +#define MSR_IA32_BACKUP_IWKEY_TO_PLATFORM 0x00000d91 +#define MSR_IA32_COPY_IWKEY_TO_LOCAL 0x00000d92 +#endif /* _ASM_X86_MSR_INDEX_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 6/12 x86/keylocker: Define Key Locker CPUID leaf
In reply to Chang S. BaeBoth Key Locker enabling code in the x86 core and AES Key Locker code in the crypto library will need to reference some CPUID bits. Define these feature-specific CPUID leaf and bits.
---
Changes from v6:
* Tweak the changelog -- comment the reason first and then brief the change.
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/include/asm/keylocker.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/keylocker.h +7 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <linux/bits.h> #include <asm/fpu/types.h> /**
@@ -21,5 +22,11 @@ struct iwkey {struct reg_128_bit encryption_key[2 ]; }; +#define KEYLOCKER_CPUID 0x019 +#define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0) +#define KEYLOCKER_CPUID_EBX_AESKLE BIT(0) +#define KEYLOCKER_CPUID_EBX_WIDE BIT(2) +#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 7/12 x86/cpu/keylocker: Load a wrapping key at boot-time
In reply to Chang S. BaeThe wrapping key is an entity to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
The wrapping key needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives.
But there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
Therefore, generate random bytes and load them at boot time. Ensure the dynamic CPU bit (CPUID.AESKLE) is set before the load and flush out these bytes after that.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt consumption, and that switching wrapping key per VM is untenable, explicitly skip this setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from v6:
* Switch to use 'static inline' for the empty functions, instead of macro that disallows type checks. (Eric Biggers and Dave Hansen)
* Use memzero_explicit() to wipe out the key data instead of writing the poison value over there. (Robert Elliott)
* Massage the changelog for the better readability.
Changes from v5:
* Call out the disabling when the feature is available on a virtual machine. Then, it will turn off the feature flag
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.
Note, Dan wonders that given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but as Dave notes there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
--- arch/x86/include/asm/keylocker.h | 9 ++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/keylocker.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 2 + 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/include/asm/keylocker.h +9 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h>
@@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +static inline void setup_keylocker (struct cpuinfo_x86 *c) { } +static inline void destroy_keylocker_data (void ) { } +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/Makefile +1 -0
@@ -134,6 +134,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/cpu/common.c +4 -1
@@ -58,6 +58,8 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> +#include <asm/uv/uv.h> #include <asm/sigframe.h> #include <asm/traps.h>
@@ -1834,10 +1836,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker (c);/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c new file +82 -0
82 lines not on this page · raw message
arch/x86/kernel/smpboot.c +2 -0
@@ -86,6 +86,7 @@#include <asm/hw_irq.h> #include <asm/stackprotector.h> #include <asm/sev.h> +#include <asm/keylocker.h> /* representing HT siblings of each logical CPU */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_sibling_map);
@@ -1386,6 +1387,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends ();cache_aps_init (); +destroy_keylocker_data (); }static int __initdata setup_possible_cpus =-1 ;
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 8/12 x86/PM/keylocker: Restore the wrapping key on the resume from ACPI S3/4
In reply to Chang S. BaeThe primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even if the prompt performs for unlocking the volume, where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded.
== Wrapping-key Restore ==
So it is motivated to meet dm-crypt's expectation that the key handles in the suspend-image remain valid after resume from an S-state. But, when the system enters the ACPI S3 or S4 sleep states, the wrapping key is discarded.
Key Locker provides a mechanism to back up the wrapping key in non-volatile storage. So, request a backup right after the key is loaded at boot time, and copy it back to each CPU upon wakeup. Then, the entirety of Key Locker has to be disabled if the backup mechanism is not available unless CONFIG_SUSPEND=n.
== Restore Failure ==
In the event of a key restore failure, the kernel proceeds with an initialized wrapping key state. This has the effect of invalidating any key handles that might be present in a suspend-image. When this happens, dm-crypt will see I/O errors resulting from error returns from crypto_skcipher_encrypt()/decrypt().
While this will disrupt operations in the current boot, data is not at risk and access is restored with new handles created by the new wrapping key at the next boot. Also, manage a feature-specific flag to communicate with the crypto implementation. This ensures to stop using the AES instructions upon the key restore failure while not turning off the feature.
== Off-states ==
While the backup may be maintained in non-volatile media across S5 and G3 "off" states, it is neither architecturally guaranteed nor it is expected by dm-crypt as prompting for the key whenever the volume is started. Then, a reboot can cover this case with a new wrapping key.
---
Changes from v6:
* Limit the symbol export only when needed.
* Improve the coding style -- reduce an indent after 'if() { ... return; }'. (Eric Biggers)
* Fix the coding style -- reduce an indent after if() {...return;}. (Eric Biggers) Tweak the comment along with that.
* Improve the function prototype, instead of using a macro. (Eric Biggers and Dave Hansen)
* Update the documentation:
- Massage the changelog to clarify the problem-and-solution by sections
- Clarify the comment about the key restore failure.
Changes from v5:
* Fix the 'valid_kl' flag not to be set when the feature is disabled. (Reported by Marvin Hsu [email protected]) Add the function comment about this.
* Improve the error handling in setup_keylocker(). All the error cases fall through the end that disables the feature. Otherwise, all the successful cases return immediately.
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebased on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 4 + arch/x86/kernel/keylocker.c | 136 ++++++++++++++++++++++++++++++- arch/x86/power/cpu.c | 2 + 3 files changed, 139 insertions(+), 3 deletions(-)
arch/x86/include/asm/keylocker.h +4 -0
@@ -32,9 +32,13 @@ struct iwkey {#ifdef CONFIG_X86_KEYLOCKER void setup_keylocker (struct cpuinfo_x86 *c);void destroy_keylocker_data (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void );#else static inline void setup_keylocker (struct cpuinfo_x86 *c) { }static inline void destroy_keylocker_data (void ) { } +static inline void restore_keylocker (void ) { } +static inline bool valid_keylocker (void ) {return false; }#endif #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +133 -3
181 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -27,6 +27,7 @@#include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -264,6 +265,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) x86_platform.restore_sched_clock_state ();cache_bp_restore ();perf_restore_debug_store (); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 9/12 x86/cpu: Add a configuration and command line option for Key Locker
In reply to Chang S. BaeAdd CONFIG_X86_KEYLOCKER to gate whether Key Locker is initialized at boot. The option is selected by the Key Locker cipher module CRYPTO_AES_KL (to be added in a later patch).
Add a new command line option "nokeylocker" to optionally override the default CONFIG_X86_KEYLOCKER=y behavior.
---
Changes from v6:
* Rebase on the upstream: commit a894a8a56b57 ("Documentation:
kernel-parameters: sort all "no..." parameters")
Changes from RFC v2:
* Make the option selected by CRYPTO_AES_KL. (Dan Williams)
* Massage the changelog and the config option description.
--- Documentation/admin-guide/kernel-parameters.txt | 2 ++ arch/x86/Kconfig | 3 +++ arch/x86/kernel/cpu/common.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -3749,6 +3749,8 @@ kernel and module base offset ASLR (Address Space Layout Randomization). + nokeylocker [X86] Disable Key Locker hardware feature. + no-kvmapf [X86,KVM] Disable paravirtualized asynchronous page fault handling.
arch/x86/Kconfig +3 -0
@@ -1879,6 +1879,9 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_KEYLOCKER + bool + choice prompt "TSX enable mode" depends on CPU_SUP_INTEL
arch/x86/kernel/cpu/common.c +16 -0
@@ -402,6 +402,22 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)static const unsigned long cr4_pinned_mask =X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_UMIP |X86_CR4_FSGSBASE |X86_CR4_CET ; + +static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg)) +return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup); +static DEFINE_STATIC_KEY_FALSE_RO (cr_pinning);static unsigned long cr4_pinned_bits__ro_after_init ;
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 10/12 crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
In reply to Chang S. BaeEvery field in struct aesni_xts_ctx is a pointer to a byte array. Each array has a size of struct crypto_aes_ctx. Then, the field can be redefined as that struct type instead of the obscure pointer.
Subsequently, the address to struct aesni_xts_ctx should be aligned right away. This can also simplify the runtime alignment code.
Redefine struct aesni_xts_ctx, and align its address on the front. This draws a rework by refactoring the common alignment code. Then, clean up the alignment for the old pointers.
---
Changes from v6:
* Add as a new patch. (Eric Biggers)
This fix was considered to be better addressed before the preparatory AES-NI code rework.
--- arch/x86/crypto/aesni-intel_glue.c | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +22 -16
@@ -61,8 +61,8 @@ struct generic_gcmaes_ctx { };struct aesni_xts_ctx { - u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR; - u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR; + struct crypto_aes_ctx tweak_ctx AESNI_ALIGN_ATTR; + struct crypto_aes_ctx crypt_ctx AESNI_ALIGN_ATTR; };#define GCM_BLOCK_LEN 16
@@ -219,14 +219,20 @@ generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm) }#endif +static inline unsigned long aes_align_addr (unsigned long addr) +{ +return (crypto_tfm_ctx_alignment () >=AESNI_ALIGN ) ? +ALIGN (addr,1 ) :ALIGN (addr,AESNI_ALIGN ); +} +static inline struct crypto_aes_ctx *aes_ctx (void *raw_ctx) { -unsigned long addr = (unsigned long )raw_ctx; -unsigned long align =AESNI_ALIGN ; +return (struct crypto_aes_ctx *)aes_align_addr ((unsigned long )raw_ctx); +} -if (align <=crypto_tfm_ctx_alignment ()) - align =1 ; -return (struct crypto_aes_ctx *)ALIGN (addr, align); +static inline struct aesni_xts_ctx *aes_xts_ctx (struct crypto_skcipher *tfm) +{ +return (struct aesni_xts_ctx *)aes_align_addr ((unsigned long )crypto_skcipher_ctx (tfm)); } static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
@@ -883,7 +889,7 @@ static int helper_rfc4106_decrypt(struct aead_request *req)static int xts_aesni_setkey (struct crypto_skcipher *tfm,const u8 *key,unsigned int keylen) { -struct aesni_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct aesni_xts_ctx *ctx =aes_xts_ctx (tfm);int err; err =xts_verify_key (tfm, key, keylen);
@@ -893,20 +899,20 @@ static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, keylen /=2 ;/* first half of xts-key is for crypt */ - err =aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_crypt_ctx, + err =aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->crypt_ctx, key, keylen);if (err)return err;/* second half of xts-key is for tweak */ -return aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_tweak_ctx, +return aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->tweak_ctx, key + keylen, keylen); }static int xts_crypt (struct skcipher_request *req,bool encrypt) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); -struct aesni_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct aesni_xts_ctx *ctx =aes_xts_ctx (tfm);int tail = req->cryptlen %AES_BLOCK_SIZE ;struct skcipher_request subreq;struct skcipher_walk walk;
@@ -942,7 +948,7 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)kernel_fpu_begin ();/* calculate first value of T */ -aesni_enc (aes_ctx (ctx->raw_tweak_ctx), walk.iv, walk.iv); +aesni_enc (&ctx->tweak_ctx, walk.iv, walk.iv);while (walk.nbytes >0 ) {int nbytes = walk.nbytes;
@@ -951,11 +957,11 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt) nbytes &= ~(AES_BLOCK_SIZE -1 );if (encrypt) -aesni_xts_encrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_encrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, nbytes, walk.iv);else -aesni_xts_decrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_decrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, nbytes, walk.iv);kernel_fpu_end ();
@@ -983,11 +989,11 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)kernel_fpu_begin ();if (encrypt) -aesni_xts_encrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_encrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, walk.nbytes, walk.iv);else -aesni_xts_decrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_decrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, walk.nbytes, walk.iv);kernel_fpu_end ();
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [PATCH v7 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
arch/x86/crypto/aesni-intel_glue.c generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm) · line 226
Quoted 5 lines · Chang S. Bae
arch/x86/crypto/aesni-intel_glue.c +5
-0+static inline unsigned long aes_align_addr (unsigned long addr) +{ +return (crypto_tfm_ctx_alignment () >=AESNI_ALIGN ) ? +ALIGN (addr,1 ) :ALIGN (addr,AESNI_ALIGN ); +}
Wouldn't it be simpler to make this take and return 'void *'? Then the callers wouldn't need to cast to/from 'unsigned long'.
Also, aligning to a 1-byte boundary is a no-op.
So, please consider the following:
static inline void *aes_align_addr(void *addr)
{
if (crypto_tfm_ctx_alignment() >= AES_ALIGN)
return addr;
return PTR_ALIGN(addr, AES_ALIGN);
}Also, aesni_rfc4106_gcm_ctx_get() and generic_gcmaes_ctx_get() should call this too, rather than duplicating similar code.
- Eric
Re: [PATCH v7 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
Quoted 15 lines · Eric Biggers
arch/x86/crypto/aesni-intel_glue.c generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm) · line 226
Quoted 5 lines · Chang S. Bae
arch/x86/crypto/aesni-intel_glue.c +5
-0+static inline unsigned long aes_align_addr (unsigned long addr) +{ +return (crypto_tfm_ctx_alignment () >=AESNI_ALIGN ) ? +ALIGN (addr,1 ) :ALIGN (addr,AESNI_ALIGN ); +}Wouldn't it be simpler to make this take and return 'void *'? Then the callers wouldn't need to cast to/from 'unsigned long'.
Also, aligning to a 1-byte boundary is a no-op.
So, please consider the following:
static inline void *aes_align_addr(void *addr)
Quoted 8 lines
{ if (crypto_tfm_ctx_alignment() >= AES_ALIGN) return addr; return PTR_ALIGN(addr, AES_ALIGN); }Also,
aesni_rfc4106_gcm_ctx_get()andgeneric_gcmaes_ctx_get()should call this too, rather than duplicating similar code.
Indeed, your suggestion can improve the overall code there.
Thanks!
Chang
PATCH v7 11/12 crypto: x86/aes - Prepare for a new AES implementation
In reply to Chang S. BaeKey Locker's AES instruction set ('AES-KL') has a similar programming interface to AES-NI. The internal ABI in the assembly code will have the same prototype as AES-NI. Then, the glue code will be the same as AES-NI's.
The new AES code will support the XTS mode alone as disk encryption is the only intended use case.
Refactor the XTS-related code to avoid code duplication, and also move some constant values to be shareable. Introduce wrappers for data transformation functions to return an error code as AES-KL may populate it.
The refactored code needs to invoke the implementation-specific functions. Then, while reusable, the code has to be inlined to the caller to avoid the indirect call for its possible overhead.
Neither functional change nor performance regression is intended.
---
Changes from v6:
* Inline the helper code to avoid the indirect call. (Eric Biggers)
* Rename the filename: aes-intel* -> aes-helper*. (Eric Biggers)
* Don't export symbols yet here. Instead, do it when needed later.
* Improve the coding style:
- Follow the symbol convention: '_' -> '__' (Eric Biggers)
- Fix a style issue -- 'dst = src = ...' catched by checkpatch.pl:
"CHECK: multiple assignments should be avoided"
* Cleanup: move some define back to AES-NI code as not used by AES-KL
Changes from v5:
* Clean up the staled function definition -- cbc_crypt_common().
* Ensure kernel_fpu_end() for the possible error return from xts_crypt_common()->crypt1_fn().
Changes from v4:
* Drop CBC mode changes. (Eric Biggers)
Changes from v3:
* Drop ECB and CTR mode changes. (Eric Biggers)
* Export symbols. (Eric Biggers)
Changes from RFC v2:
* Massage the changelog. (Dan Williams)
Changes from RFC v1:
* Added as a new patch. (Ard Biesheuvel)
Link:
https://lore.kernel.org/lkml/CAMj1kXGa4f21eH0mdxd1pQsZMUjUr1Btq+Dgw-gC=O-yYft7xw@mail.gmail.com/
--- arch/x86/crypto/aes-helper_asm.S | 22 +++ arch/x86/crypto/aes-helper_glue.h | 161 +++++++++++++++++++++ arch/x86/crypto/aesni-intel_asm.S | 47 +++---- arch/x86/crypto/aesni-intel_glue.c | 218 ++++++++--------------------- 4 files changed, 257 insertions(+), 191 deletions(-) create mode 100644 arch/x86/crypto/aes-helper_asm.S create mode 100644 arch/x86/crypto/aes-helper_glue.h
arch/x86/crypto/aes-helper_asm.S new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Constant values shared between AES implementations: + */ + +.pushsection .rodata +.align 16 +.Lcts_permute_table: + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 + .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 +.popsection + +.section .rodata.cst16.gf128mul_x_ble_mask, "aM", @progbits, 16 +.align 16 +.Lgf128mul_x_ble_mask: + .octa 0x00000000000000010000000000000087 +.previous
arch/x86/crypto/aes-helper_glue.h new file +161 -0
161 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +17 -30
116 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.c +57 -161
337 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v7 12/12 crypto: x86/aes-kl - Implement the AES-XTS algorithm
In reply to Chang S. BaeKey Locker is a CPU feature to reduce key exfiltration opportunities. It converts the AES key into an encoded form, called 'key handle', to reduce the exposure of private key material in memory.
This key conversion as well as all subsequent data transformation are provided by new AES instructions ('AES-KL'). AES-KL is analogous to that of AES-NI as maintains a similar programming interface.
Support the XTS mode as the primary use case is dm-crypt. The implementation has some details worth mentioning, which differentiate itself from others, that users may need to be aware of:
== Key Handle Restriction ==
A key handle may be encoded with some restrictions. Restrict every handle only available in kernel mode via setkey().
Subsequently the key handle could be corrupted or fail with handle restrictions. Then, encrypt()/decrypt() returns -EINVAL.
== API Limitation ==
The setkey() function transforms an AES key into a handle. But, an extended key is a usual outcome of setkey() in other AES cipher implementations. For this reason, a setkey() failure does not fall back to the other. So, expose AES-KL methods via synchronous interfaces only.
=== AES Compliance ===
Key Locker is not AES compliant as it lacks 192-bit key support. However, per the expectations of Linux crypto-cipher implementations the software cipher implementation must support all the AES-compliant key sizes.
The AES-KL cipher implementation achieves this constraint by logging a warning and falling back to AES-NI. In other words, the 192-bit key-size limitation for what can be converted into a key handle is only documented, not enforced.
== Wrapping Key Restore Failure ==
The failure of setkey() as well as encode()/decode() is also possible with the wrapping key failure. In the event of hardware failure, the wrapping key is lost from deep sleep states. Then, those functions return -ENODEV as the feature is disabled.
== Userspace Exposition ==
Some hardware implementations may have some performance penalties. E.g., the cryptsetup benchmark indicates the raw throughput is measurably slower than AES-NI. But, for disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth.
This, along with the above points, is an end-user consideration for selecting AES-KL over AES-NI. Thus, advertise it with a unique name 'xts-aes-aeskl' in /proc/crypto while not replacing AES-NI under the generic name 'xts(aes)' with a lower priority.
== 64-bit Only ==
AES-KL provides wide instructions that process eight blocks at once which can boost the AES performance. Leveraging those, the code needs to clobber more than eight 128-bit registers.
But, the 32-bit does not have enough wide registers. Then, the performance is unlikely better than 64-bit which has already a gap vs. AES-NI. So, simply make it for the 64-bit mode only at the moment.
---
Changes from v6:
* Merge all the AES-KL patches. (Eric Biggers)
* Make the driver for the 64-bit mode only. (Eric Biggers)
* Rework the key-size check code:
- Trim unnecessary checks. (Eric Biggers)
- Document the reason
- Make sure both XTS keys with the same size
* Adjust the Kconfig change:
- Move the location. (Robert Elliott)
- Trim the description to follow others such as AES-NI.
* Update the changelog:
- Explain the priority value for the common name under 'User Exposition' (renamed from 'Performance'). (Eric Biggers)
- Trim the introduction
- Switch to more imperative mood for those explaining the code change
- Add a new section '64-bit Only'
* Adjust the ASM code to return a proper error code. (Eric Biggers)
* Update assembly code macros:
- Remove unused one.
- Document the reason for the duplicated ones.
Changes from v5:
* Replace the ret instruction with RET as rebased on the upstream -- commit f94909ceb1ed ("x86: Prepare asm files for straight-line-speculation").
Changes from v3:
* Exclude non-AES-KL objects. (Eric Biggers)
* Simplify the assembler dependency check. (Peter Zijlstra)
* Trim the Kconfig help text. (Dan Williams)
* Fix a defined-but-not-used warning.
Changes from RFC v2:
* Move out each mode support in new patches.
* Update the changelog to describe the limitation and the tradeoff clearly. (Andy Lutomirski)
Changes from RFC v1:
* Rebased on the refactored code. (Ard Biesheuvel)
* Dropped exporting the single block interface. (Ard Biesheuvel)
* Fixed the fallback and error handling paths. (Ard Biesheuvel)
* Revised the module description. (Dave Hansen and Peter Zijlsta)
* Made the build depend on the binutils version to support new instructions. (Borislav Petkov and Peter Zijlstra)
* Updated the changelog accordingly.
--- arch/x86/crypto/Kconfig | 22 ++ arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aeskl-intel_asm.S | 580 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 216 +++++++++++ arch/x86/crypto/aesni-intel_asm.S | 8 +- arch/x86/crypto/aesni-intel_glue.c | 40 +- arch/x86/crypto/aesni-intel_glue.h | 17 + 7 files changed, 873 insertions(+), 13 deletions(-) create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h
arch/x86/crypto/Kconfig +22 -0
@@ -2,6 +2,11 @@ menu "Accelerated Cryptographic Algorithms for CPU (x86)" +config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12 + config CRYPTO_CURVE25519_X86 tristate "Public key crypto: Curve25519 (ADX)" depends on X86 && 64BIT
@@ -29,6 +34,23 @@ config CRYPTO_AES_NI_INTEL Architecture: x86 (32-bit and 64-bit) using: - AES-NI (AES new instructions) +config CRYPTO_AES_KL + tristate "Ciphers: AES, modes: XTS (AES-KL)" + depends on X86 && 64BIT + depends on AS_HAS_KEYLOCKER + depends on CRYPTO_AES_NI_INTEL + select X86_KEYLOCKER + + help + Block cipher: AES cipher algorithms + Length-preserving ciphers: AES with XTS + + Architecture: x86 (64-bit) using: + - AES-KL (AES Key Locker) + - AES-NI for a 192-bit key + + See Documentation/arch/x86/keylocker.rst for more details. + config CRYPTO_BLOWFISH_X86_64 tristate "Ciphers: Blowfish, modes: ECB, CBC" depends on X86 && 64BIT
arch/x86/crypto/Makefile +3 -0
@@ -50,6 +50,9 @@ obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o +obj-$(CONFIG_CRYPTO_AES_KL) += aeskl-intel.o +aeskl-intel-y := aeskl-intel_asm.o aeskl-intel_glue.o + obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o sha1-ssse3-$(CONFIG_AS_SHA1_NI) += sha1_ni_asm.o
arch/x86/crypto/aeskl-intel_asm.S new file +580 -0
580 lines not on this page · raw message
arch/x86/crypto/aeskl-intel_glue.c new file +216 -0
216 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +4 -4
@@ -1821,10 +1821,10 @@ SYM_FUNC_START_LOCAL(_key_expansion_256b) SYM_FUNC_END(_key_expansion_256b) /* - * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, - * unsigned int key_len) + * int __aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, + * unsigned int key_len) */ -SYM_FUNC_START(aesni_set_key) +SYM_FUNC_START(__aesni_set_key) FRAME_BEGIN #ifndef __x86_64__ pushl KEYP
@@ -1933,7 +1933,7 @@ SYM_FUNC_START(aesni_set_key) #endif FRAME_END RET -SYM_FUNC_END(aesni_set_key) +SYM_FUNC_END(__aesni_set_key) /* * void __aesni_enc(const void *ctx, u8 *dst, const u8 *src)
arch/x86/crypto/aesni-intel_glue.c +31 -9
@@ -37,6 +37,7 @@#include <linux/static_call.h> #include "aes-helper_glue.h" +#include "aesni-intel_glue.h" #define RFC4106_HASH_SUBKEY_SIZE 16#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
@@ -72,8 +73,8 @@ struct gcm_context_data {u8 hash_keys[GCM_BLOCK_LEN *16 ]; }; -asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, -unsigned int key_len); +asmlinkage int __aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, +unsigned int key_len); asmlinkage void __aesni_enc(const void *ctx, u8 *out, const u8 *in); asmlinkage void __aesni_dec(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
@@ -89,17 +90,32 @@ asmlinkage void aesni_cts_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out, asmlinkage void aesni_cts_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,const u8 *in,unsigned int len,u8 *iv); -static int aesni_enc (const void *ctx,u8 *out,const u8 *in) +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key, +unsigned int key_len) +{ +return __aesni_set_key (ctx, in_key, key_len); +} +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_set_key); +#endif + +int aesni_enc (const void *ctx,u8 *out,const u8 *in) {__aesni_enc (ctx, out, in);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_enc); +#endif -static int aesni_dec (const void *ctx,u8 *out,const u8 *in) +int aesni_dec (const void *ctx,u8 *out,const u8 *in) {__aesni_dec (ctx, out, in);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_dec); +#endif #define AVX_GEN2_OPTSIZE 640#define AVX_GEN4_OPTSIZE 4096
@@ -110,19 +126,25 @@ asmlinkage void __aesni_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, asmlinkage void __aesni_xts_decrypt(const struct crypto_aes_ctx *ctx, u8 *out,const u8 *in,unsigned int len,u8 *iv); -static int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, -unsigned int len,u8 *iv) +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv) {__aesni_xts_encrypt (ctx, out, in, len, iv);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_xts_encrypt); +#endif -static int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, -unsigned int len,u8 *iv) +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv) {__aesni_xts_decrypt (ctx, out, in, len, iv);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_xts_decrypt); +#endif #ifdef CONFIG_X86_64
@@ -256,7 +278,7 @@ static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); + err =__aesni_set_key (ctx, in_key, key_len);kernel_fpu_end (); }
arch/x86/crypto/aesni-intel_glue.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/ + +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len); + +int aesni_enc (const void *ctx,u8 *out,const u8 *in); +int aesni_dec (const void *ctx,u8 *out,const u8 *in); + +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [PATCH v7 12/12] crypto: x86/aes-kl - Implement the AES-XTS algorithm
Quoted 7 lines · Chang S. Bae
== API Limitation ==
The
setkey()function transforms an AES key into a handle. But, an extended key is a usual outcome ofsetkey()in other AES cipher implementations. For this reason, asetkey()failure does not fall back to the other. So, expose AES-KL methods via synchronous interfaces only.
I don't understand what this paragraph is trying to say.
Quoted 6 lines
arch/x86/crypto/aeskl-intel_glue.c new file +6
-0+/* +* The below wrappers for the encryption/decryption functions +* incorporate the feature availability check: +* +* In the rare event of hardware failure, the wrapping key can be lost +* after wake-up from a deep sleep state. Then, this check helps to
arch/x86/crypto/aeskl-intel_glue.c line 79
Quoted 10 lines
arch/x86/crypto/aeskl-intel_glue.c new file +10
-0+* avoid any subsequent misuse with populating a proper error code. +*/ + +static inline int aeskl_enc (const void *ctx,u8 *out,const u8 *in) +{ +if (!valid_keylocker ()) +return -ENODEV ; + +return __aeskl_enc (ctx, out, in); +}
Is it not sufficient for the valid_keylocker() check to occur at the top level (in xts_encrypt() and xts_decrypt()), which would seem to be a better place to do it? Is this because valid_keylocker() needs to be checked in every kernel_fpu_begin() section separately, to avoid a race condition? If that's indeed the reason, can you explain that in the comment?
arch/x86/crypto/aeskl-intel_glue.c line 122
Quoted 10 lines
arch/x86/crypto/aeskl-intel_glue.c new file +10
-0+static inline int xts_keylen (struct skcipher_request *req,u32 *keylen) +{ +struct aes_xts_ctx *ctx =aes_xts_ctx (crypto_skcipher_reqtfm (req)); + +if (ctx->crypt_ctx.key_length != ctx->tweak_ctx.key_length) +return -EINVAL ; + + *keylen = ctx->crypt_ctx.key_length; +return 0 ; +}
This is odd. Why would the key lengths be different here?
arch/x86/crypto/aeskl-intel_glue.c line 202
Quoted 6 lines
arch/x86/crypto/aeskl-intel_glue.c new file +6
-0+ err =simd_register_skciphers_compat (aeskl_skciphers,ARRAY_SIZE (aeskl_skciphers), + aeskl_simd_skciphers); +if (err) +return err; + +return 0 ;
This can be simplified to:
return simd_register_skciphers_compat(aeskl_skciphers, ARRAY_SIZE(aeskl_skciphers), aeskl_simd_skciphers);
- Eric
Re: [PATCH v7 12/12] crypto: x86/aes-kl - Implement the AES-XTS algorithm
Quoted 10 lines · Eric Biggers
Quoted 7 lines · Chang S. Bae
== API Limitation ==
The
setkey()function transforms an AES key into a handle. But, an extended key is a usual outcome ofsetkey()in other AES cipher implementations. For this reason, asetkey()failure does not fall back to the other. So, expose AES-KL methods via synchronous interfaces only.I don't understand what this paragraph is trying to say.
This text comes with this particular comment as I look back:
Quoted 4 lines
This basically implies that we cannot expose the cipher interface at all, and so AES-KL can only be used by callers that use the asynchronous interface, which rules out 802.11, s/w kTLS, macsec and kerberos.
https://lore.kernel.org/lkml/CAMj1kXGa4f21eH0mdxd1pQsZMUjUr1Btq+Dgw-gC=O-yYft7xw@mail.gmail.com/
Then, I realize that at that moment the dm-crypt use model was not clearly out yet.
This seems to be carried over the versions. But, now, it has XTS only. Then, this becomes less relevant which makes confusion I guess.
I think this can go away as claiming the usage clearly now.
Quoted 15 lines
Quoted 15 lines
arch/x86/crypto/aeskl-intel_glue.c new file +14
-0+/* +* The below wrappers for the encryption/decryption functions +* incorporate the feature availability check: +* +* In the rare event of hardware failure, the wrapping key can be lost +* after wake-up from a deep sleep state. Then, this check helps to +* avoid any subsequent misuse with populating a proper error code. +*/ + +static inline int aeskl_enc (const void *ctx,u8 *out,const u8 *in) +{ +if (!valid_keylocker ()) +return -ENODEV ; +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return __aeskl_enc(ctx, out, in); +}Is it not sufficient for the
valid_keylocker()check to occur at the top level (inxts_encrypt()andxts_decrypt()), which would seem to be a better place to do it? Is this becausevalid_keylocker()needs to be checked in everykernel_fpu_begin()section separately, to avoid a race condition? If that's indeed the reason, can you explain that in the comment?
Maybe something like this:
/*
* In the event of hardware failure, the wrapping key can be lost
* from a sleep state. Then, the feature is not usable anymore. This
* feature state can be found via valid_keylocker().
*
* Such disabling could be anywhere preemptible, outside
* kernel_fpu_begin()/end(). So, to avoid the race condition, check
* the feature availability on every use in the below wrappers. */
Quoted 13 lines
arch/x86/crypto/aeskl-intel_glue.c line 122
Quoted 11 lines
arch/x86/crypto/aeskl-intel_glue.c new file +10
-0+static inline int xts_keylen (struct skcipher_request *req,u32 *keylen) +{ +struct aes_xts_ctx *ctx =aes_xts_ctx (crypto_skcipher_reqtfm (req)); + +if (ctx->crypt_ctx.key_length != ctx->tweak_ctx.key_length) +return -EINVAL ; + + *keylen = ctx->crypt_ctx.key_length; +return 0 ; +}This is odd. Why would the key lengths be different here?
I thought it was logical to do such sanity check. But, in practice, they are the same.
Looks like this entire crypto code is treated as performance-critical or so.
Quoted 10 lines
arch/x86/crypto/aeskl-intel_glue.c line 202
Quoted 7 lines
arch/x86/crypto/aeskl-intel_glue.c new file +6
-0+ err =simd_register_skciphers_compat (aeskl_skciphers,ARRAY_SIZE (aeskl_skciphers), + aeskl_simd_skciphers); +if (err) +return err; + +return 0 ;This can be simplified to:
Quoted 3 lines
return simd_register_skciphers_compat(aeskl_skciphers, ARRAY_SIZE(aeskl_skciphers), aeskl_simd_skciphers);
Oh, obviously!
Thanks,
Chang
v8 cover x86: Support Key Locker
In reply to Chang S. BaeHi all,
Posting V8 here, a brief status of this enabling:
The last two revisions trend to update the crypto code mainly. The existing AEX-XTS code was improved further before being shared with the new code. Then, the new implementation was tuned for the AES-XTS mode which aligns with the claimed use case -- dm-crypt.
But, I'd say some additional change might be still needed.
The overall changes in charge of the last review:
* PATCH12:
- Clarify some documentation (Eric Biggers)
- Simplify (Eric Biggers) and cleanup code
* PATCH11:
- Remove dead code.
* PATCH10:
- Deduplicate the alignment code (Eric Biggers)
The series can be found in this repo:
git://github.com/intel-staging/keylocker.git kl-v8
The overall diff was populated in:
https://raw.githubusercontent.com/intel-staging/keylocker/diff/kl-v8-vs-v7.diff
The feature is already available on recent Intel client systems. The V3 cover letter covered the usage, the threat model and other details:
https://lore.kernel.org/lkml/[email protected]/
And the V6 cover followed up with updating the performance data:
https://lore.kernel.org/lkml/[email protected]/
V7 posting:
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Chang
Chang S. Bae (12):
Documentation/x86: Document Key Locker
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/asm: Add a wrapper function for the LOADIWKEY instruction x86/msr-index: Add MSRs for Key Locker wrapping key x86/keylocker: Define Key Locker CPUID leaf x86/cpu/keylocker: Load a wrapping key at boot-time x86/PM/keylocker: Restore the wrapping key on the resume from ACPI
S3/4
x86/cpu: Add a configuration and command line option for Key Locker
crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
crypto: x86/aes - Prepare for a new AES-XTS implementation
crypto: x86/aes-kl - Implement the AES-XTS algorithm
.../admin-guide/kernel-parameters.txt | 2 + Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 97 +++ arch/x86/Kconfig | 3 + arch/x86/crypto/Kconfig | 22 + arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aes-helper_asm.S | 22 + arch/x86/crypto/aes-helper_glue.h | 161 +++++ arch/x86/crypto/aeskl-intel_asm.S | 552 ++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 188 ++++++ arch/x86/crypto/aesni-intel_asm.S | 55 +- arch/x86/crypto/aesni-intel_glue.c | 241 +++----- arch/x86/crypto/aesni-intel_glue.h | 16 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/keylocker.h | 45 ++ arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/special_insns.h | 32 + arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 21 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/keylocker.c | 212 +++++++ arch/x86/kernel/smpboot.c | 2 + arch/x86/lib/x86-opcode-map.txt | 11 +- arch/x86/power/cpu.c | 2 + tools/arch/x86/lib/x86-opcode-map.txt | 11 +- 27 files changed, 1507 insertions(+), 211 deletions(-) create mode 100644 Documentation/arch/x86/keylocker.rst create mode 100644 arch/x86/crypto/aes-helper_asm.S create mode 100644 arch/x86/crypto/aes-helper_glue.h create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h create mode 100644 arch/x86/include/asm/keylocker.h create mode 100644 arch/x86/kernel/keylocker.c
base-commit: 054377e4774eee812b7930933d7a354ed5a7ddd6
Signature · 1 line
2.17.1
PATCH v8 1/12 Documentation/x86: Document Key Locker
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
Changes from v6:
* Rebase on the upstream -- commit ff61f0791ce9 ("docs: move x86 documentation into Documentation/arch/"). (Nathan Huckleberry)
* Remove a duplicated sentence -- 'But there is no AES-KL instruction to process a 192-bit key.'
* Update the text for clarity and readability:
- Clarify the error code and exemplify the backup failure
- Use 'wrapping key' instead of less readable 'IWKey'
Changes from v5:
* Fix a typo: 'feature feature' -> 'feature'
Changes from RFC v2:
* Add as a new patch.
The preview is available here:
https://htmlpreview.github.io/?https://github.com/intel-staging/keylocker/kdoc/arch/x86/keylocker.html
--- Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 97 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Documentation/arch/x86/keylocker.rst
Documentation/arch/x86/index.rst +1 -0
@@ -42,3 +42,4 @@ x86-specific Documentation features elf_auxvec xstate + keylocker
Documentation/arch/x86/keylocker.rst new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v8 01/12] Documentation/x86: Document Key Locker
Quoted 27 lines · Chang S. Bae
+==============
Documentation/arch/x86/keylocker.rst new file +26
-0+x86 Key Locker +============== + +Introduction +============ + +Key Locker is a CPU feature to reduce key exfiltration opportunities +while maintaining a programming interface similar to AES-NI. It +converts the AES key into an encoded form, called the 'key handle'. +The key handle is a wrapped version of the clear-text key where the +wrapping key has limited exposure. Once converted, all subsequent data +encryption using new AES instructions (AES-KL) uses this key handle, +reducing the exposure of private key material in memory. + +CPU-internal Wrapping Key +========================= + +The CPU-internal wrapping key is an entity in a software-invisible CPU +state. On every system boot, a new key is loaded. So the key handle that +was encoded by the old wrapping key is no longer usable on system shutdown +or reboot. + +And the key may be lost on the following exceptional situation upon wakeup: + +Wrapping Key Restore Failure +----------------------------
Documentation/arch/x86/keylocker.rst line 39
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+ +The CPU state is volatile with the ACPI S3/4 sleep states. When the system +supports those states, the key has to be backed up so that it is restored +on wake up. The kernel saves the key in non-volatile media. + +The event of a wrapping key restore failure upon resume from suspend, all +established key handles become invalid. In flight dm-crypt operations +receive error results from pending operations. In the likely scenario that +dm-crypt is hosting the root filesystem the recovery is identical to if a +storage controller failed to resume from suspend, reboot. If the volume
"... resume from suspend or reboot."
Quoted 48 lines
Documentation/arch/x86/keylocker.rst new file +48
-0+impacted by a wrapping key restore failure is a data-volume then it is +possible that I/O errors on that volume do not bring down the rest of the +system. However, a reboot is still required because the kernel will have +soft-disabled Key Locker. Upon the failure, the crypto library code will +return -ENODEV on every AES-KL function call. The Key Locker implementation +only loads a new wrapping key at initial boot, not any time after like +resume from suspend. + +Use Case and Non-use Cases +========================== + +Bare metal disk encryption is the only intended use case. + +Userspace usage is not supported because there is no ABI provided to +communicate and coordinate wrapping-key restore failure to userspace. For +now, key restore failures are only coordinated with kernel users. But the +kernel can not prevent userspace from using the feature's AES instructions +('AES-KL') when the feature has been enabled. So, the lack of userspace +support is only documented, not actively enforced. + +Key Locker is not expected to be advertised to guest VMs and the kernel +implementation ignores it even if the VMM enumerates the capability. The +expectation is that a guest VM wants private wrapping key state, but the +architecture does not provide that. An emulation of that capability, by +caching per-VM wrapping keys in memory, defeats the purpose of Key Locker. +The backup / restore facility is also not performant enough to be suitable +for guest VM context switches. + +AES Instruction Set +=================== + +The feature accompanies a new AES instruction set. This instruction set is +analogous to AES-NI. A set of AES-NI instructions can be mapped to an +AES-KL instruction. For example, AESENC128KL is responsible for ten rounds +of transformation, which is equivalent to nine times AESENC and one +AESENCLAST in AES-NI. + +But they have some notable differences: + +* AES-KL provides a secure data transformation using an encrypted key. + +* If an invalid key handle is provided, e.g. a corrupted one or a handle + restriction failure, the instruction fails with setting RFLAGS.ZF. The + crypto library implementation includes the flag check to return -EINVAL. + Note that this flag is also set if the wrapping key is changed, e.g., + because of the backup error. + +* AES-KL implements support for 128-bit and 256-bit keys, but there is no
Documentation/arch/x86/keylocker.rst line 97
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+ AES-KL instruction to process an 192-bit key. The AES-KL cipher + implementation logs a warning message with a 192-bit key and then falls + back to AES-NI. So, this 192-bit key-size limitation is only documented, + not enforced. It means the key will remain in clear-text in memory. This + is to meet Linux crypto-cipher expectation that each implementation must + support all the AES-compliant key sizes. + +* Some AES-KL hardware implementation may have noticeable performance + overhead when compared with AES-NI instructions. +
The rest is LGTM, thanks!
Anyway,
Signature · 1 line
An old man doll... just what I always wanted! - Clara
Randy Dunlap
reviewerRe: [PATCH v8 01/12] Documentation/x86: Document Key Locker
In reply to Chang S. BaeQuoted 42 lines · Chang S. Bae
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
--- Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 97 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Documentation/arch/x86/keylocker.rstDocumentation/arch/x86/keylocker.rst new file +25
-0@@ -0,0 +1,97 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============== +x86 Key Locker +============== + +Introduction +============ + +Key Locker is a CPU feature to reduce key exfiltration opportunities +while maintaining a programming interface similar to AES-NI. It +converts the AES key into an encoded form, called the 'key handle'. +The key handle is a wrapped version of the clear-text key where the +wrapping key has limited exposure. Once converted, all subsequent data +encryption using new AES instructions (AES-KL) uses this key handle, +reducing the exposure of private key material in memory. + +CPU-internal Wrapping Key +========================= + +The CPU-internal wrapping key is an entity in a software-invisible CPU +state. On every system boot, a new key is loaded. So the key handle that +was encoded by the old wrapping key is no longer usable on system shutdown +or reboot. +
Documentation/arch/x86/keylocker.rst line 35
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+And the key may be lost on the following exceptional situation upon wakeup: + +Wrapping Key Restore Failure +---------------------------- + +The CPU state is volatile with the ACPI S3/4 sleep states. When the system +supports those states, the key has to be backed up so that it is restored +on wake up. The kernel saves the key in non-volatile media. + +The event of a wrapping key restore failure upon resume from suspend, all
Upon the event of a ...
Documentation/arch/x86/keylocker.rst line 40
Quoted 5 lines
Documentation/arch/x86/keylocker.rst new file +5
-0+established key handles become invalid. In flight dm-crypt operations +receive error results from pending operations. In the likely scenario that +dm-crypt is hosting the root filesystem the recovery is identical to if a +storage controller failed to resume from suspend, reboot. If the volume +impacted by a wrapping key restore failure is a data-volume then it is
data volume
Quoted 40 lines
Documentation/arch/x86/keylocker.rst new file +40
-0+possible that I/O errors on that volume do not bring down the rest of the +system. However, a reboot is still required because the kernel will have +soft-disabled Key Locker. Upon the failure, the crypto library code will +return -ENODEV on every AES-KL function call. The Key Locker implementation +only loads a new wrapping key at initial boot, not any time after like +resume from suspend. + +Use Case and Non-use Cases +========================== + +Bare metal disk encryption is the only intended use case. + +Userspace usage is not supported because there is no ABI provided to +communicate and coordinate wrapping-key restore failure to userspace. For +now, key restore failures are only coordinated with kernel users. But the +kernel can not prevent userspace from using the feature's AES instructions +('AES-KL') when the feature has been enabled. So, the lack of userspace +support is only documented, not actively enforced. + +Key Locker is not expected to be advertised to guest VMs and the kernel +implementation ignores it even if the VMM enumerates the capability. The +expectation is that a guest VM wants private wrapping key state, but the +architecture does not provide that. An emulation of that capability, by +caching per-VM wrapping keys in memory, defeats the purpose of Key Locker. +The backup / restore facility is also not performant enough to be suitable +for guest VM context switches. + +AES Instruction Set +=================== + +The feature accompanies a new AES instruction set. This instruction set is +analogous to AES-NI. A set of AES-NI instructions can be mapped to an +AES-KL instruction. For example, AESENC128KL is responsible for ten rounds +of transformation, which is equivalent to nine times AESENC and one +AESENCLAST in AES-NI. + +But they have some notable differences: + +* AES-KL provides a secure data transformation using an encrypted key. +
Documentation/arch/x86/keylocker.rst line 90
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+* If an invalid key handle is provided, e.g. a corrupted one or a handle + restriction failure, the instruction fails with setting RFLAGS.ZF. The + crypto library implementation includes the flag check to return -EINVAL. + Note that this flag is also set if the wrapping key is changed, e.g., + because of the backup error. + +* AES-KL implements support for 128-bit and 256-bit keys, but there is no + AES-KL instruction to process an 192-bit key. The AES-KL cipher + implementation logs a warning message with a 192-bit key and then falls + back to AES-NI. So, this 192-bit key-size limitation is only documented,
Is it logged anywhere? i.e., a kernel log message?
Quoted 7 lines
Documentation/arch/x86/keylocker.rst new file +7
-0+ not enforced. It means the key will remain in clear-text in memory. This + is to meet Linux crypto-cipher expectation that each implementation must + support all the AES-compliant key sizes. + +* Some AES-KL hardware implementation may have noticeable performance + overhead when compared with AES-NI instructions. +
Signature · 1 line
~Randy
Re: [PATCH v8 01/12] Documentation/x86: Document Key Locker
Quoted 8 lines · Randy Dunlap
Documentation/arch/x86/keylocker.rst line 90
Quoted 5 lines · Chang S. Bae
Documentation/arch/x86/keylocker.rst new file +5
-0+ +* AES-KL implements support for 128-bit and 256-bit keys, but there is no + AES-KL instruction to process an 192-bit key. The AES-KL cipher + implementation logs a warning message with a 192-bit key and then falls + back to AES-NI. So, this 192-bit key-size limitation is only documented,Is it logged anywhere? i.e., a kernel log message?
Yes, this is the relevant change in the last patch:
Quoted 3 lines
(quoted diff) +3
-0+static int aeskl_setkey(struct crypto_tfm *tfm, void *raw_ctx, const u8 *in_key, + unsigned int keylen) +{
...
Quoted 2 lines
(quoted diff) +2
-0+ if (unlikely(keylen == AES_KEYSIZE_192)) { + pr_warn_once("AES-KL does not support 192-bit key. Use AES-NI.\n");
...
Quoted 1 line
+}
Thanks,
Chang
PATCH v8 2/12 x86/cpufeature: Enumerate Key Locker feature
In reply to Chang S. BaeKey Locker is a CPU feature to minimize exposure of clear-text key material. An encoded form, called 'key handle', is referenced for data encryption or decryption instead of accessing the clear text key.
A wrapping key loaded in the CPU's software-inaccessible state is used to transform a user key into a key handle. On rarely unexpected hardware failure, the key could be lost.
Here enumerate this hardware capability. It will not be shown up in /proc/cpuinfo as userspace usage is not supported. This is because there is no ABI to coordinate the wrapping-key failure.
The feature supports Advanced Encryption Standard (AES) cipher algorithm with new SIMD instruction set like its predecessor (AES-NI). Mark the feature depending on XMM2. The new AES implementation will be in the crypto library.
Add X86_FEATURE_KEYLOCKER to the disabled features mask at the moment. It will be enabled under a new config option.
---
Changes from v6:
* Massage the changelog -- re-organize the change descriptions
Changes from RFC v2:
* Do not publish the feature flag to userspace.
* Update the changelog.
Changes from RFC v1:
* Updated the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -389,6 +389,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_KEYLOCKER (16*32+23)/* "" Key Locker */ #define X86_FEATURE_BUS_LOCK_DETECT (16*32+24)/* Bus Lock detect */ #define X86_FEATURE_CLDEMOTE (16*32+25)/* CLDEMOTE instruction */ #define X86_FEATURE_MOVDIRI (16*32+27)/* MOVDIRI instruction */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -38,6 +38,12 @@# define DISABLE_OSPKE (1<<(X86_FEATURE_OSPKE & 31))#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */ +#ifdef CONFIG_X86_KEYLOCKER +# define DISABLE_KEYLOCKER 0 +#else +# define DISABLE_KEYLOCKER (1<<(X86_FEATURE_KEYLOCKER & 31)) +#endif /* CONFIG_X86_KEYLOCKER */ +#ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0#else
@@ -126,7 +132,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_KEYLOCKER)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK19 0
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -128,6 +128,8 @@#define X86_CR4_PCIDE _BITUL(X86_CR4_PCIDE_BIT)#define X86_CR4_OSXSAVE_BIT 18/* enable xsave and xrestore */ #define X86_CR4_OSXSAVE _BITUL(X86_CR4_OSXSAVE_BIT) +#define X86_CR4_KEYLOCKER_BIT 19/* enable Key Locker */ +#define X86_CR4_KEYLOCKER _BITUL(X86_CR4_KEYLOCKER_BIT)#define X86_CR4_SMEP_BIT 20/* enable SMEP support */ #define X86_CR4_SMEP _BITUL(X86_CR4_SMEP_BIT)#define X86_CR4_SMAP_BIT 21/* enable SMAP support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -81,6 +81,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, + { X86_FEATURE_KEYLOCKER, X86_FEATURE_XMM2 }, {} };
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 3/12 x86/insn: Add Key Locker instructions to the opcode map
In reply to Chang S. BaeThe x86 instruction decoder needs to know these new instructions that are going to be used in the crypto library as well as the x86 core code. Add the following:
LOADIWKEY:
Load a CPU-internal wrapping key.
ENCODEKEY128:
Wrap a 128-bit AES key to a key handle.
ENCODEKEY256:
Wrap a 256-bit AES key to a key handle.
AESENC128KL:
Encrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESENC256KL:
Encrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESDEC128KL:
Decrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESDEC256KL:
Decrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESENCWIDE128KL:
Encrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESENCWIDE256KL:
Encrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
AESDECWIDE128KL:
Decrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESDECWIDE256KL:
Decrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
The detail can be found in Intel Software Developer Manual.
---
Changes from v6:
* Massage the changelog -- add the reason a bit.
Changes from RFC v1:
* Separated out the LOADIWKEY addition in a new patch.
* Included AES instructions to avoid warning messages when the AES Key Locker module is built.
--- arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- tools/arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
tools/arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 4/12 x86/asm: Add a wrapper function for the LOADIWKEY instruction
In reply to Chang S. BaeKey Locker introduces a CPU-internal wrapping key to encode a user key to a key handle. Then a key handle is referenced instead of the plain text key.
LOADIWKEY loads a wrapping key in the software-inaccessible CPU state. It operates only in kernel mode.
The kernel will use this to load a new key at boot time. Establish a wrapper to prepare for this use. Also, define struct iwkey to pass the key value to it.
---
Changes from v6:
* Massage the changelog -- clarify the reason and the changes a bit.
Changes from v5:
* Fix a typo: kernel_cpu_begin() -> kernel_fpu_begin()
Changes from RFC v2:
* Separate out the code as a new patch.
* Improve the usability with the new struct as an argument. (Dan Williams)
Note, Dan wondered if:
WARN_ON(!irq_fpu_usable());
would be appropriate in the load_xmm_iwkey() function.
--- arch/x86/include/asm/keylocker.h | 25 ++++++++++++++++++++++ arch/x86/include/asm/special_insns.h | 32 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/keylocker.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_KEYLOCKER_H +#define _ASM_KEYLOCKER_H + +#ifndef __ASSEMBLY__ + +#include <asm/fpu/types.h> + +/** +* struct iwkey - A temporary wrapping key storage. +* @integrity_key: A 128-bit key to check that key handles have not +* been tampered with. +* @encryption_key: A 256-bit encryption key used in +* wrapping/unwrapping a clear text key. +* +* This storage should be flushed immediately after loaded. +*/ +struct iwkey { +struct reg_128_bit integrity_key; +struct reg_128_bit encryption_key[2 ]; +}; + +#endif /*__ASSEMBLY__ */ +#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +32 -0
@@ -9,6 +9,7 @@#include <asm/processor-flags.h> #include <linux/irqflags.h> #include <linux/jump_label.h> +#include <asm/keylocker.h> /* * The compiler should not reorder volatile asm statements with respect to each
@@ -283,6 +284,37 @@ static __always_inline void tile_release(void) asmvolatile (".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ); } +/** +* load_xmm_iwkey - Load a CPU-internal wrapping key +* @key: A struct iwkey pointer. +* +* Load @key to XMMs then do LOADIWKEY. After this, flush XMM +* registers. Caller is responsible for kernel_fpu_begin(). +*/ +static inline void load_xmm_iwkey (struct iwkey *key) +{ +struct reg_128_bit zeros = {0 }; + + asmvolatile ("movdqu %0, %%xmm0; movdqu %1, %%xmm1; movdqu %2, %%xmm2;" + ::"m" (key->integrity_key),"m" (key->encryption_key[0 ]), +"m" (key->encryption_key[1 ])); + +/* +* LOADIWKEY %xmm1,%xmm2 +* +* EAX and XMM0 are implicit operands. Load a key value +* from XMM0-2 to a software-invisible CPU state. With zero +* in EAX, CPU does not do hardware randomization and the key +* backup is allowed. +* +* This instruction is supported by binutils >= 2.36. +*/ + asmvolatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1" ::"a" (0 )); + + asmvolatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;" + ::"m" (zeros)); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 5/12 x86/msr-index: Add MSRs for Key Locker wrapping key
In reply to Chang S. BaeThe CPU state that contains the wrapping key is in the same power domain as the cache. So any sleep state that would invalidate the cache (like S3) also invalidates the state of the wrapping key.
But, since the state is inaccessible to software, it needs a special mechanism to save and restore the key during deep sleep.
A set of new MSRs are provided as an abstract interface to save and restore the wrapping key, and to check the key status. The wrapping key is saved in a platform-scoped state of non-volatile media. The backup itself and its path from the CPU are encrypted and integrity protected.
Define those MSRs to be used to save and restore the key for S3/4 sleep states.
But the backup storage's non-volatility is not architecturally guaranteed across off-states, such as S5 and G3. Then, the kernel may generate a new key on the next boot.
---
Changes from v6:
* Tweak the changelog -- put the last for those about other sleep states
Changes from RFC v2:
* Update the changelog. (Dan Williams)
* Rename the MSRs. (Dan Williams)
--- arch/x86/include/asm/msr-index.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
@@ -1117,4 +1117,10 @@* a #GP */ +/* MSRs for managing a CPU-internal wrapping key for Key Locker. */ +#define MSR_IA32_IWKEY_COPY_STATUS 0x00000990 +#define MSR_IA32_IWKEY_BACKUP_STATUS 0x00000991 +#define MSR_IA32_BACKUP_IWKEY_TO_PLATFORM 0x00000d91 +#define MSR_IA32_COPY_IWKEY_TO_LOCAL 0x00000d92 +#endif /* _ASM_X86_MSR_INDEX_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 6/12 x86/keylocker: Define Key Locker CPUID leaf
In reply to Chang S. BaeBoth Key Locker enabling code in the x86 core and AES Key Locker code in the crypto library will need to reference some CPUID bits. Define these feature-specific CPUID leaf and bits.
---
Changes from v6:
* Tweak the changelog -- comment the reason first and then brief the change.
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/include/asm/keylocker.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/keylocker.h +7 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <linux/bits.h> #include <asm/fpu/types.h> /**
@@ -21,5 +22,11 @@ struct iwkey {struct reg_128_bit encryption_key[2 ]; }; +#define KEYLOCKER_CPUID 0x019 +#define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0) +#define KEYLOCKER_CPUID_EBX_AESKLE BIT(0) +#define KEYLOCKER_CPUID_EBX_WIDE BIT(2) +#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 7/12 x86/cpu/keylocker: Load a wrapping key at boot-time
In reply to Chang S. BaeThe wrapping key is an entity to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
The wrapping key needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives.
But there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
Therefore, generate random bytes and load them at boot time. Ensure the dynamic CPU bit (CPUID.AESKLE) is set before the load and flush out these bytes after that.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt consumption, and that switching wrapping key per VM is untenable, explicitly skip this setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from v6:
* Switch to use 'static inline' for the empty functions, instead of macro that disallows type checks. (Eric Biggers and Dave Hansen)
* Use memzero_explicit() to wipe out the key data instead of writing the poison value over there. (Robert Elliott)
* Massage the changelog for the better readability.
Changes from v5:
* Call out the disabling when the feature is available on a virtual machine. Then, it will turn off the feature flag
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.
Note, Dan wonders that given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled when the first dm-crypt user arrives, but as Dave notes there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
--- arch/x86/include/asm/keylocker.h | 9 ++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/keylocker.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 2 + 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/include/asm/keylocker.h +9 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <asm/processor.h> #include <linux/bits.h> #include <asm/fpu/types.h>
@@ -28,5 +29,13 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (struct cpuinfo_x86 *c); +void destroy_keylocker_data (void ); +#else +static inline void setup_keylocker (struct cpuinfo_x86 *c) { } +static inline void destroy_keylocker_data (void ) { } +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/Makefile +1 -0
@@ -134,6 +134,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/cpu/common.c +4 -1
@@ -58,6 +58,8 @@#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> +#include <asm/keylocker.h> +#include <asm/uv/uv.h> #include <asm/sigframe.h> #include <asm/traps.h>
@@ -1834,10 +1836,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker (c);/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c new file +82 -0
82 lines not on this page · raw message
arch/x86/kernel/smpboot.c +2 -0
@@ -86,6 +86,7 @@#include <asm/hw_irq.h> #include <asm/stackprotector.h> #include <asm/sev.h> +#include <asm/keylocker.h> /* representing HT siblings of each logical CPU */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_sibling_map);
@@ -1371,6 +1372,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)nmi_selftest ();impress_friends ();cache_aps_init (); +destroy_keylocker_data (); }static int __initdata setup_possible_cpus =-1 ;
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 8/12 x86/PM/keylocker: Restore the wrapping key on the resume from ACPI S3/4
In reply to Chang S. BaeThe primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even if the prompt performs for unlocking the volume, where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded.
== Wrapping-key Restore ==
So it is motivated to meet dm-crypt's expectation that the key handles in the suspend-image remain valid after resume from an S-state. But, when the system enters the ACPI S3 or S4 sleep states, the wrapping key is discarded.
Key Locker provides a mechanism to back up the wrapping key in non-volatile storage. So, request a backup right after the key is loaded at boot time, and copy it back to each CPU upon wakeup. Then, the entirety of Key Locker has to be disabled if the backup mechanism is not available unless CONFIG_SUSPEND=n.
== Restore Failure ==
In the event of a key restore failure, the kernel proceeds with an initialized wrapping key state. This has the effect of invalidating any key handles that might be present in a suspend-image. When this happens, dm-crypt will see I/O errors resulting from error returns from crypto_skcipher_encrypt()/decrypt().
While this will disrupt operations in the current boot, data is not at risk and access is restored with new handles created by the new wrapping key at the next boot. Also, manage a feature-specific flag to communicate with the crypto implementation. This ensures to stop using the AES instructions upon the key restore failure while not turning off the feature.
== Off-states ==
While the backup may be maintained in non-volatile media across S5 and G3 "off" states, it is neither architecturally guaranteed nor it is expected by dm-crypt as prompting for the key whenever the volume is started. Then, a reboot can cover this case with a new wrapping key.
---
Changes from v6:
* Limit the symbol export only when needed.
* Improve the coding style -- reduce an indent after 'if() { ... return; }'. (Eric Biggers)
* Fix the coding style -- reduce an indent after if() {...return;}. (Eric Biggers) Tweak the comment along with that.
* Improve the function prototype, instead of using a macro. (Eric Biggers and Dave Hansen)
* Update the documentation:
- Massage the changelog to clarify the problem-and-solution by sections
- Clarify the comment about the key restore failure.
Changes from v5:
* Fix the 'valid_kl' flag not to be set when the feature is disabled. (Reported by Marvin Hsu [email protected]) Add the function comment about this.
* Improve the error handling in setup_keylocker(). All the error cases fall through the end that disables the feature. Otherwise, all the successful cases return immediately.
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebased on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 4 + arch/x86/kernel/keylocker.c | 136 ++++++++++++++++++++++++++++++- arch/x86/power/cpu.c | 2 + 3 files changed, 139 insertions(+), 3 deletions(-)
arch/x86/include/asm/keylocker.h +4 -0
@@ -32,9 +32,13 @@ struct iwkey {#ifdef CONFIG_X86_KEYLOCKER void setup_keylocker (struct cpuinfo_x86 *c);void destroy_keylocker_data (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void );#else static inline void setup_keylocker (struct cpuinfo_x86 *c) { }static inline void destroy_keylocker_data (void ) { } +static inline void restore_keylocker (void ) { } +static inline bool valid_keylocker (void ) {return false; }#endif #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +133 -3
181 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -27,6 +27,7 @@#include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -264,6 +265,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) x86_platform.restore_sched_clock_state ();cache_bp_restore ();perf_restore_debug_store (); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 9/12 x86/cpu: Add a configuration and command line option for Key Locker
In reply to Chang S. BaeAdd CONFIG_X86_KEYLOCKER to gate whether Key Locker is initialized at boot. The option is selected by the Key Locker cipher module CRYPTO_AES_KL (to be added in a later patch).
Add a new command line option "nokeylocker" to optionally override the default CONFIG_X86_KEYLOCKER=y behavior.
---
Changes from v6:
* Rebase on the upstream: commit a894a8a56b57 ("Documentation:
kernel-parameters: sort all "no..." parameters")
Changes from RFC v2:
* Make the option selected by CRYPTO_AES_KL. (Dan Williams)
* Massage the changelog and the config option description.
--- Documentation/admin-guide/kernel-parameters.txt | 2 ++ arch/x86/Kconfig | 3 +++ arch/x86/kernel/cpu/common.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +2 -0
@@ -3749,6 +3749,8 @@ kernel and module base offset ASLR (Address Space Layout Randomization). + nokeylocker [X86] Disable Key Locker hardware feature. + no-kvmapf [X86,KVM] Disable paravirtualized asynchronous page fault handling.
arch/x86/Kconfig +3 -0
@@ -1879,6 +1879,9 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_KEYLOCKER + bool + choice prompt "TSX enable mode" depends on CPU_SUP_INTEL
arch/x86/kernel/cpu/common.c +16 -0
@@ -402,6 +402,22 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)static const unsigned long cr4_pinned_mask =X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_UMIP |X86_CR4_FSGSBASE |X86_CR4_CET ; + +static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg)) +return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup); +static DEFINE_STATIC_KEY_FALSE_RO (cr_pinning);static unsigned long cr4_pinned_bits__ro_after_init ;
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Borislav Petkov
maintainerRe: [PATCH v8 09/12] x86/cpu: Add a configuration and command line option for Key Locker
Quoted 4 lines · Chang S. Bae
arch/x86/kernel/cpu/common.c +4
-0+static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg))
arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 419
Quoted 10 lines
arch/x86/kernel/cpu/common.c +10
-0+return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup);
Can we stop adding those just to remove them at some point later but simply do:
clearcpuid=keylocker
?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 09/12] x86/cpu: Add a configuration and command line option for Key Locker
Quoted 19 lines · Borislav Petkov
Quoted 4 lines · Chang S. Bae
arch/x86/kernel/cpu/common.c +4
-0+static __init int x86_nokeylocker_setup (char *arg) +{ +/* Expect an exact match without trailing characters. */ +if (strlen (arg))arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 419
Quoted 10 lines
arch/x86/kernel/cpu/common.c +10
-0+return 0 ; + +if (!cpu_feature_enabled (X86_FEATURE_KEYLOCKER )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_KEYLOCKER ); +pr_info ("x86/keylocker: Disabled by kernel command line.\n" ); +return 1 ; +} +__setup ("nokeylocker" , x86_nokeylocker_setup);Can we stop adding those just to remove them at some point later but simply do:
Quoted 3 lines
clearcpuid=keylocker
?
Oh, I was not sure about this policy. Thanks, now I'm glad that I have confidence in removing this.
Chang
PATCH v8 10/12 crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
In reply to Chang S. BaeEvery field in struct aesni_xts_ctx is a pointer to a byte array. Each array has a size of struct crypto_aes_ctx. Then, the field can be redefined as that struct type instead of the obscure pointer.
Subsequently, the address to struct aesni_xts_ctx should be aligned right away rather than on every access to the field.
Thus, redefine struct aesni_xts_ctx, and align its address on the front. This draws a rework by refactoring the common alignment code.
Then, the refactored code itself appears to be useful to simplify the overall runtime alignment. So, use it for other modes.
---
Changes from v7:
* Massage the helper function to be usable for other alignment code such as aesni_rfc4106_gcm_ctx_get() and generic_gcmaes_ctx_get(). (Eric Biggers)
Changes from v6:
* Add as a new patch. (Eric Biggers)
This fix was considered to be better addressed before the preparatory AES-NI code rework.
--- arch/x86/crypto/aesni-intel_glue.c | 51 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 26 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +25 -26
@@ -61,8 +61,8 @@ struct generic_gcmaes_ctx { };struct aesni_xts_ctx { - u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR; - u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR; + struct crypto_aes_ctx tweak_ctx AESNI_ALIGN_ATTR; + struct crypto_aes_ctx crypt_ctx AESNI_ALIGN_ATTR; };#define GCM_BLOCK_LEN 16
@@ -80,6 +80,13 @@ struct gcm_context_data {u8 hash_keys[GCM_BLOCK_LEN *16 ]; }; +static inline void *aes_align_addr (void *addr) +{ +if (crypto_tfm_ctx_alignment () >=AESNI_ALIGN ) +return addr; +return PTR_ALIGN (addr,AESNI_ALIGN ); +} + asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,unsigned int key_len); asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in);
@@ -201,32 +208,24 @@ static __ro_after_init DEFINE_STATIC_KEY_FALSE(gcm_use_avx2);static inline struct aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get (struct crypto_aead *tfm) { -unsigned long align =AESNI_ALIGN ; - -if (align <=crypto_tfm_ctx_alignment ()) - align =1 ; -return PTR_ALIGN (crypto_aead_ctx (tfm), align); +return (struct aesni_rfc4106_gcm_ctx *)aes_align_addr (crypto_aead_ctx (tfm)); }static inline struct generic_gcmaes_ctx *generic_gcmaes_ctx_get (struct crypto_aead *tfm) { -unsigned long align =AESNI_ALIGN ; - -if (align <=crypto_tfm_ctx_alignment ()) - align =1 ; -return PTR_ALIGN (crypto_aead_ctx (tfm), align); +return (struct generic_gcmaes_ctx *)aes_align_addr (crypto_aead_ctx (tfm)); }#endif static inline struct crypto_aes_ctx *aes_ctx (void *raw_ctx) { -unsigned long addr = (unsigned long )raw_ctx; -unsigned long align =AESNI_ALIGN ; +return (struct crypto_aes_ctx *)aes_align_addr (raw_ctx); +} -if (align <=crypto_tfm_ctx_alignment ()) - align =1 ; -return (struct crypto_aes_ctx *)ALIGN (addr, align); +static inline struct aesni_xts_ctx *aes_xts_ctx (struct crypto_skcipher *tfm) +{ +return (struct aesni_xts_ctx *)aes_align_addr (crypto_skcipher_ctx (tfm)); } static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
@@ -883,7 +882,7 @@ static int helper_rfc4106_decrypt(struct aead_request *req)static int xts_aesni_setkey (struct crypto_skcipher *tfm,const u8 *key,unsigned int keylen) { -struct aesni_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct aesni_xts_ctx *ctx =aes_xts_ctx (tfm);int err; err =xts_verify_key (tfm, key, keylen);
@@ -893,20 +892,20 @@ static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, keylen /=2 ;/* first half of xts-key is for crypt */ - err =aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_crypt_ctx, + err =aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->crypt_ctx, key, keylen);if (err)return err;/* second half of xts-key is for tweak */ -return aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_tweak_ctx, +return aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->tweak_ctx, key + keylen, keylen); }static int xts_crypt (struct skcipher_request *req,bool encrypt) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); -struct aesni_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct aesni_xts_ctx *ctx =aes_xts_ctx (tfm);int tail = req->cryptlen %AES_BLOCK_SIZE ;struct skcipher_request subreq;struct skcipher_walk walk;
@@ -942,7 +941,7 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)kernel_fpu_begin ();/* calculate first value of T */ -aesni_enc (aes_ctx (ctx->raw_tweak_ctx), walk.iv, walk.iv); +aesni_enc (&ctx->tweak_ctx, walk.iv, walk.iv);while (walk.nbytes >0 ) {int nbytes = walk.nbytes;
@@ -951,11 +950,11 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt) nbytes &= ~(AES_BLOCK_SIZE -1 );if (encrypt) -aesni_xts_encrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_encrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, nbytes, walk.iv);else -aesni_xts_decrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_decrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, nbytes, walk.iv);kernel_fpu_end ();
@@ -983,11 +982,11 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)kernel_fpu_begin ();if (encrypt) -aesni_xts_encrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_encrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, walk.nbytes, walk.iv);else -aesni_xts_decrypt (aes_ctx (ctx->raw_crypt_ctx), +aesni_xts_decrypt (&ctx->crypt_ctx, walk.dst.virt.addr, walk.src.virt.addr, walk.nbytes, walk.iv);kernel_fpu_end ();
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [PATCH v8 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
Quoted 1 line · Chang S. Bae
Every field in struct
aesni_xts_ctxis a pointer to a byte array.
A byte array. Not a pointer to a byte array.
Quoted 2 lines
Then, the field can be redefined as that struct type instead of the obscure pointer.
There's no pointer.
arch/x86/crypto/aesni-intel_glue.c static __ro_after_init DEFINE_STATIC_KEY_FALSE(gcm_use_avx2); · line 212
Quoted 10 lines
static inline struct
aesni_rfc4106_gcm_ctx*aesni_rfc4106_gcm_ctx_get(structcrypto_aead*tfm) {arch/x86/crypto/aesni-intel_glue.c +1
-5-unsigned long align =AESNI_ALIGN ; - -if (align <=crypto_tfm_ctx_alignment ()) - align =1 ; -return PTR_ALIGN (crypto_aead_ctx (tfm), align); +return (struct aesni_rfc4106_gcm_ctx *)aes_align_addr (crypto_aead_ctx (tfm)); }
Explicit casts from 'void *' are unnecessary.
Quoted 13 lines
static int xts_aesni_setkey(struct
crypto_skcipher*tfm, const u8 *key,arch/x86/crypto/aesni-intel_glue.c +1
-2unsigned int keylen) { -struct aesni_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct aesni_xts_ctx *ctx =aes_xts_ctx (tfm);int err; err =xts_verify_key (tfm, key, keylen);@@ -893,20 +892,20 @@ static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, keylen /=2 ;/* first half of xts-key is for crypt */ - err = aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_crypt_ctx,
arch/x86/crypto/aesni-intel_glue.c static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, · line 903
Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +2
-1+ err =aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->crypt_ctx, key, keylen);if (err)return err;/* second half of xts-key is for tweak */ -return aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_tweak_ctx, +return aes_set_key_common (crypto_skcipher_tfm (tfm), &ctx->tweak_ctx, key + keylen, keylen); }
To re-iterate what I said on v6, the runtime alignment to a 16-byte boundary should happen when translating the raw crypto_skcipher_ctx() into the pointer to the aes_xts_ctx. It should not happen when accessing each individual field in the aes_xts_ctx.
Yet, this code is still doing runtime alignment when accessing each individual field, as the second argument to aes_set_key_common() is 'void *raw_ctx' which aes_set_key_common() runtime-aligns to crypto_aes_ctx.
We should keep everything consistent, which means making aes_set_key_common() take a pointer to crypto_aes_ctx and not do the runtime alignment.
- Eric
Re: [PATCH v8 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
Quoted 6 lines · Eric Biggers
To re-iterate what I said on v6, the runtime alignment to a 16-byte boundary should happen when translating the raw
crypto_skcipher_ctx()into the pointer to theaes_xts_ctx. It should not happen when accessing each individual field in theaes_xts_ctx.
Quoted 6 lines
Yet, this code is still doing runtime alignment when accessing each individual field, as the second argument to
aes_set_key_common()is 'void*raw_ctx' whichaes_set_key_common()runtime-aligns tocrypto_aes_ctx.We should keep everything consistent, which means making
aes_set_key_common()take a pointer tocrypto_aes_ctxand not do the runtime alignment.
Let me clarify what is the problem this patch tried to solve here. The current struct aesni_xts_ctx is ugly. So, the main story is let's fix it before using the code for AES-KL.
Then, the rework part may be applicable for code re-usability. That seems to be okay to do here.
Fixing the runtime alignment entirely seems to be touching other code than AES-XTS. Yes, that's ideal cleanup for consistency. But, it seems to be less relevant in this series. I'd be happy to follow up on that improvement though.
Thanks,
Chang
Re: [PATCH v8 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
Quoted 18 lines · Chang S. Bae
Quoted 6 lines · Eric Biggers
To re-iterate what I said on v6, the runtime alignment to a 16-byte boundary should happen when translating the raw
crypto_skcipher_ctx()into the pointer to theaes_xts_ctx. It should not happen when accessing each individual field in theaes_xts_ctx.Quoted 6 lines
Yet, this code is still doing runtime alignment when accessing each individual field, as the second argument to
aes_set_key_common()is 'void*raw_ctx' whichaes_set_key_common()runtime-aligns tocrypto_aes_ctx.We should keep everything consistent, which means making
aes_set_key_common()take a pointer tocrypto_aes_ctxand not do the runtime alignment.Let me clarify what is the problem this patch tried to solve here. The current struct
aesni_xts_ctxis ugly. So, the main story is let's fix it before using the code for AES-KL.
Quoted 7 lines
Then, the rework part may be applicable for code re-usability. That seems to be okay to do here.
Fixing the runtime alignment entirely seems to be touching other code than AES-XTS. Yes, that's ideal cleanup for consistency. But, it seems to be less relevant in this series. I'd be happy to follow up on that improvement though.
IMO the issue is that your patch makes the code (including the XTS code) inconsistent because it makes it use a mix of both approaches: it aligns each field individually, *and* it aligns the ctx up-front. I was hoping to switch fully from the former approach to the latter approach, instead of switching from the former approach to a mix of the two approaches as you are proposing.
The following on top of this patch is what I am asking for. I think it would be appropriate to fold into this patch.
arch/x86/crypto/aesni-intel_glue.c +5 -4
@@ -228,10 +228,10 @@ static inline struct aesni_xts_ctx *aes_xts_ctx(struct crypto_skcipher *tfm)return (struct aesni_xts_ctx *)aes_align_addr (crypto_skcipher_ctx (tfm)); } -static int aes_set_key_common (struct crypto_tfm *tfm,void *raw_ctx, +static int aes_set_key_common (struct crypto_tfm *tfm, +struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len) { -struct crypto_aes_ctx *ctx =aes_ctx (raw_ctx);int err; if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
@@ -252,7 +252,8 @@ static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,static int aes_set_key (struct crypto_tfm *tfm,const u8 *in_key,unsigned int key_len) { -return aes_set_key_common (tfm,crypto_tfm_ctx (tfm), in_key, key_len); +return aes_set_key_common (tfm,aes_ctx (crypto_tfm_ctx (tfm)), + in_key, key_len); }static void aesni_encrypt (struct crypto_tfm *tfm,u8 *dst,const u8 *src)
@@ -285,7 +286,7 @@ static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int len) {return aes_set_key_common (crypto_skcipher_tfm (tfm), -crypto_skcipher_ctx (tfm), key, len); +aes_ctx (crypto_skcipher_ctx (tfm)), key, len); }static int ecb_encrypt (struct skcipher_request *req)
Re: [PATCH v8 10/12] crypto: x86/aesni - Use the proper data type in struct aesni_xts_ctx
Quoted 4 lines · Eric Biggers
IMO the issue is that your patch makes the code (including the XTS code) inconsistent because it makes it use a mix of both approaches: it aligns each field individually, *and* it aligns the ctx up-front.
But, the code did this already:
common_rfc4106_set_key()
-> aesni_rfc4106_gcm_ctx_get(aead) // align ->ctx
-> aes_set_key_common()
-> aes_ctx() // here, this aligns again
And generic_gcmaes_set_key() as well.
Hmm, maybe a separate patch can spell out this issue more clearly for the record, no?
Thanks,
Chang
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH 1/1 crypto: x86/aesni: Align the address before aes_set_key_common()
aes_set_key_common() performs runtime alignment to the void *raw_ctx pointer. This facilitates consistent access to the 16byte-aligned address during key extension.
However, the alignment is already handlded in the GCM-related setkey functions before invoking the common function. Consequently, the alignment in the common function is unnecessary for those functions.
To establish a consistent approach throughout the glue code, remove the aes_ctx() call from its current location. Instead, place it at each call site where the runtime alignment is currently absent.
---
The need for this fix was discovered during Eric's review of the Key Locker series [1]. Considering the upstream code also requires this improvement, this is applicable regardless of the Key Locker enabling [2].
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
--- arch/x86/crypto/aesni-intel_glue.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +6 -6
@@ -229,10 +229,10 @@ static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)return (struct crypto_aes_ctx *)ALIGN (addr, align); } -static int aes_set_key_common (struct crypto_tfm *tfm,void *raw_ctx, +static int aes_set_key_common (struct crypto_tfm *tfm, +struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len) { -struct crypto_aes_ctx *ctx =aes_ctx (raw_ctx);int err; if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
@@ -253,7 +253,7 @@ static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,static int aes_set_key (struct crypto_tfm *tfm,const u8 *in_key,unsigned int key_len) { -return aes_set_key_common (tfm,crypto_tfm_ctx (tfm), in_key, key_len); +return aes_set_key_common (tfm,aes_ctx (crypto_tfm_ctx (tfm)), in_key, key_len); }static void aesni_encrypt (struct crypto_tfm *tfm,u8 *dst,const u8 *src)
@@ -286,7 +286,7 @@ static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int len) {return aes_set_key_common (crypto_skcipher_tfm (tfm), -crypto_skcipher_ctx (tfm), key, len); +aes_ctx (crypto_skcipher_ctx (tfm)), key, len); }static int ecb_encrypt (struct skcipher_request *req)
@@ -893,13 +893,13 @@ static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, keylen /=2 ;/* first half of xts-key is for crypt */ - err =aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_crypt_ctx, + err =aes_set_key_common (crypto_skcipher_tfm (tfm),aes_ctx (ctx->raw_crypt_ctx), key, keylen);if (err)return err;/* second half of xts-key is for tweak */ -return aes_set_key_common (crypto_skcipher_tfm (tfm), ctx->raw_tweak_ctx, +return aes_set_key_common (crypto_skcipher_tfm (tfm),aes_ctx (ctx->raw_tweak_ctx), key + keylen, keylen); }
Signature · 1 line
2.34.1
Herbert Xu
maintainerRe: [PATCH] crypto: x86/aesni: Align the address before aes_set_key_common()
Quoted 24 lines · Chang S. Bae
aes_set_key_common()performs runtime alignment to the void*raw_ctxpointer. This facilitates consistent access to the 16byte-aligned address during key extension.However, the alignment is already handlded in the GCM-related setkey functions before invoking the common function. Consequently, the alignment in the common function is unnecessary for those functions.
To establish a consistent approach throughout the glue code, remove the
aes_ctx()call from its current location. Instead, place it at each call site where the runtime alignment is currently absent.Suggested-by Eric Biggers <[email protected]>Signed-off-by Chang S. Bae <[email protected]>---
The need for this fix was discovered during Eric's review of the Key Locker series [1]. Considering the upstream code also requires this improvement, this is applicable regardless of the Key Locker enabling [2].
Quoted 5 lines
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/--- arch/x86/crypto/aesni-intel_glue.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Patch applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH v8 11/12 crypto: x86/aes - Prepare for a new AES-XTS implementation
In reply to Chang S. BaeKey Locker's AES instruction set ('AES-KL') has a similar programming interface to AES-NI. The internal ABI in the assembly code will have the same prototype as AES-NI. Then, the glue code will be the same as AES-NI's.
The new AES code will support the XTS mode alone as disk encryption is the only intended use case.
Refactor the XTS-related code to avoid code duplication, and also move some constant values to be shareable. Introduce wrappers for data transformation functions to return an error code as AES-KL may populate it.
The refactored code needs to invoke the implementation-specific functions. Then, while reusable, the code has to be inlined to the caller to avoid the indirect call for its possible overhead.
Neither functional change nor performance regression is intended.
---
Changes from v7:
* Remove aesni_dec() as not referenced by the refactored helpers. But, keep the ASM symbol '__aesni_dec' to make it appears consistent with its counterpart '__aesni_enc'.
* Call out 'AES-XTS' in the subject.
Changes from v6:
* Inline the helper code to avoid the indirect call. (Eric Biggers)
* Rename the filename: aes-intel* -> aes-helper*. (Eric Biggers)
* Don't export symbols yet here. Instead, do it when needed later.
* Improve the coding style:
- Follow the symbol convention: '_' -> '__' (Eric Biggers)
- Fix a style issue -- 'dst = src = ...' catched by checkpatch.pl:
"CHECK: multiple assignments should be avoided"
* Cleanup: move some define back to AES-NI code as not used by AES-KL
Changes from v5:
* Clean up the staled function definition -- cbc_crypt_common().
* Ensure kernel_fpu_end() for the possible error return from xts_crypt_common()->crypt1_fn().
Changes from v4:
* Drop CBC mode changes. (Eric Biggers)
Changes from v3:
* Drop ECB and CTR mode changes. (Eric Biggers)
* Export symbols. (Eric Biggers)
Changes from RFC v2:
* Massage the changelog. (Dan Williams)
Changes from RFC v1:
* Added as a new patch. (Ard Biesheuvel)
Link:
https://lore.kernel.org/lkml/CAMj1kXGa4f21eH0mdxd1pQsZMUjUr1Btq+Dgw-gC=O-yYft7xw@mail.gmail.com/
--- arch/x86/crypto/aes-helper_asm.S | 22 +++ arch/x86/crypto/aes-helper_glue.h | 161 ++++++++++++++++++++++ arch/x86/crypto/aesni-intel_asm.S | 47 +++---- arch/x86/crypto/aesni-intel_glue.c | 209 +++++++---------------------- 4 files changed, 249 insertions(+), 190 deletions(-) create mode 100644 arch/x86/crypto/aes-helper_asm.S create mode 100644 arch/x86/crypto/aes-helper_glue.h
arch/x86/crypto/aes-helper_asm.S new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Constant values shared between AES implementations: + */ + +.pushsection .rodata +.align 16 +.Lcts_permute_table: + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 + .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + .byte 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 +.popsection + +.section .rodata.cst16.gf128mul_x_ble_mask, "aM", @progbits, 16 +.align 16 +.Lgf128mul_x_ble_mask: + .octa 0x00000000000000010000000000000087 +.previous
arch/x86/crypto/aes-helper_glue.h new file +161 -0
161 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +17 -30
116 lines not on this page · raw message
arch/x86/crypto/aesni-intel_glue.c +49 -160
313 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v8 12/12 crypto: x86/aes-kl - Implement the AES-XTS algorithm
In reply to Chang S. BaeKey Locker is a CPU feature to reduce key exfiltration opportunities. It converts the AES key into an encoded form, called 'key handle', to reduce the exposure of private key material in memory.
This key conversion as well as all subsequent data transformation are provided by new AES instructions ('AES-KL'). AES-KL is analogous to that of AES-NI as maintains a similar programming interface.
Support the XTS mode as the primary use case is dm-crypt. The implementation has some details worth mentioning, which differentiate itself from others, that users may need to be aware of:
== Key Handle Restriction ==
A key handle may be encoded with some restrictions. Restrict every handle only available in kernel mode via setkey().
Subsequently the key handle could be corrupted or fail with handle restrictions. Then, encrypt()/decrypt() returns -EINVAL.
=== AES Compliance ===
Key Locker is not AES compliant as it lacks 192-bit key support. However, per the expectations of Linux crypto-cipher implementations the software cipher implementation must support all the AES-compliant key sizes.
The AES-KL cipher implementation achieves this constraint by logging a warning and falling back to AES-NI. In other words, the 192-bit key-size limitation for what can be converted into a key handle is only documented, not enforced.
== Wrapping Key Restore Failure ==
The failure of setkey() as well as encode()/decode() is also possible with the wrapping key failure. In the event of hardware failure, the wrapping key is lost from deep sleep states. Then, those functions return -ENODEV as the feature is disabled.
== Userspace Exposition ==
Some hardware implementations may have some performance penalties. E.g., the cryptsetup benchmark indicates the raw throughput is measurably slower than AES-NI. But, for disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth.
This, along with the above points, is an end-user consideration for selecting AES-KL over AES-NI. Thus, advertise it with a unique name 'xts-aes-aeskl' in /proc/crypto while not replacing AES-NI under the generic name 'xts(aes)' with a lower priority.
== 64-bit Only ==
AES-KL provides wide instructions that process eight blocks at once which can boost the AES performance. Leveraging those, the code needs to clobber more than eight 128-bit registers.
But, the 32-bit does not have enough wide registers. Then, the performance is unlikely better than 64-bit which has already a gap vs. AES-NI. So, simply make it for the 64-bit mode only at the moment.
---
Changes from v7:
* Update the changelog -- remove 'API Limitation'. (Eric Biggers)
* Update the comment for valid_keylocker(). (Eric Biggers)
* Improve the code:
- Remove the key-length check and simplify the code. (Eric Biggers)
- Remove aeskl_dec() and __aeskl_dec() as not needed.
- Simplify the register-function return handling. (Eric)
- Rename setkey functions for coherent naming:
aeskl_setkey() -> __aeskl_setkey(),
aeskl_setkey_common() -> aeskl_setkey(),
aeskl_xts_setkey() -> xts_setkey()- Revert an unnecessary comment.
Changes from v6:
* Merge all the AES-KL patches. (Eric Biggers)
* Make the driver for the 64-bit mode only. (Eric Biggers)
* Rework the key-size check code:
- Trim unnecessary checks. (Eric Biggers)
- Document the reason
- Make sure both XTS keys with the same size
* Adjust the Kconfig change:
- Move the location. (Robert Elliott)
- Trim the description to follow others such as AES-NI.
* Update the changelog:
- Explain the priority value for the common name under 'User Exposition' (renamed from 'Performance'). (Eric Biggers)
- Trim the introduction
- Switch to more imperative mood for those explaining the code change
- Add a new section '64-bit Only'
* Adjust the ASM code to return a proper error code. (Eric Biggers)
* Update assembly code macros:
- Remove unused one.
- Document the reason for the duplicated ones.
Changes from v5:
* Replace the ret instruction with RET as rebased on the upstream -- commit f94909ceb1ed ("x86: Prepare asm files for straight-line-speculation").
Changes from v3:
* Exclude non-AES-KL objects. (Eric Biggers)
* Simplify the assembler dependency check. (Peter Zijlstra)
* Trim the Kconfig help text. (Dan Williams)
* Fix a defined-but-not-used warning.
Changes from RFC v2:
* Move out each mode support in new patches.
* Update the changelog to describe the limitation and the tradeoff clearly. (Andy Lutomirski)
Changes from RFC v1:
* Rebased on the refactored code. (Ard Biesheuvel)
* Dropped exporting the single block interface. (Ard Biesheuvel)
* Fixed the fallback and error handling paths. (Ard Biesheuvel)
* Revised the module description. (Dave Hansen and Peter Zijlsta)
* Made the build depend on the binutils version to support new instructions. (Borislav Petkov and Peter Zijlstra)
* Updated the changelog accordingly.
--- arch/x86/crypto/Kconfig | 22 ++ arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aeskl-intel_asm.S | 552 +++++++++++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 188 ++++++++++ arch/x86/crypto/aesni-intel_asm.S | 8 +- arch/x86/crypto/aesni-intel_glue.c | 35 +- arch/x86/crypto/aesni-intel_glue.h | 16 + 7 files changed, 812 insertions(+), 12 deletions(-) create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aesni-intel_glue.h
arch/x86/crypto/Kconfig +22 -0
@@ -2,6 +2,11 @@ menu "Accelerated Cryptographic Algorithms for CPU (x86)" +config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12 + config CRYPTO_CURVE25519_X86 tristate "Public key crypto: Curve25519 (ADX)" depends on X86 && 64BIT
@@ -29,6 +34,23 @@ config CRYPTO_AES_NI_INTEL Architecture: x86 (32-bit and 64-bit) using: - AES-NI (AES new instructions) +config CRYPTO_AES_KL + tristate "Ciphers: AES, modes: XTS (AES-KL)" + depends on X86 && 64BIT + depends on AS_HAS_KEYLOCKER + depends on CRYPTO_AES_NI_INTEL + select X86_KEYLOCKER + + help + Block cipher: AES cipher algorithms + Length-preserving ciphers: AES with XTS + + Architecture: x86 (64-bit) using: + - AES-KL (AES Key Locker) + - AES-NI for a 192-bit key + + See Documentation/arch/x86/keylocker.rst for more details. + config CRYPTO_BLOWFISH_X86_64 tristate "Ciphers: Blowfish, modes: ECB, CBC" depends on X86 && 64BIT
arch/x86/crypto/Makefile +3 -0
@@ -50,6 +50,9 @@ obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o +obj-$(CONFIG_CRYPTO_AES_KL) += aeskl-intel.o +aeskl-intel-y := aeskl-intel_asm.o aeskl-intel_glue.o + obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o sha1-ssse3-$(CONFIG_AS_SHA1_NI) += sha1_ni_asm.o
arch/x86/crypto/aeskl-intel_asm.S new file +552 -0
552 lines not on this page · raw message
arch/x86/crypto/aeskl-intel_glue.c new file +188 -0
188 lines not on this page · raw message
arch/x86/crypto/aesni-intel_asm.S +4 -4
@@ -1821,10 +1821,10 @@ SYM_FUNC_START_LOCAL(_key_expansion_256b) SYM_FUNC_END(_key_expansion_256b) /* - * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, - * unsigned int key_len) + * int __aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, + * unsigned int key_len) */ -SYM_FUNC_START(aesni_set_key) +SYM_FUNC_START(__aesni_set_key) FRAME_BEGIN #ifndef __x86_64__ pushl KEYP
@@ -1933,7 +1933,7 @@ SYM_FUNC_START(aesni_set_key) #endif FRAME_END RET -SYM_FUNC_END(aesni_set_key) +SYM_FUNC_END(__aesni_set_key) /* * void __aesni_enc(const void *ctx, u8 *dst, const u8 *src)
arch/x86/crypto/aesni-intel_glue.c +27 -8
@@ -37,6 +37,7 @@#include <linux/static_call.h> #include "aes-helper_glue.h" +#include "aesni-intel_glue.h" #define RFC4106_HASH_SUBKEY_SIZE 16#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
@@ -72,8 +73,8 @@ struct gcm_context_data {u8 hash_keys[GCM_BLOCK_LEN *16 ]; }; -asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, -unsigned int key_len); +asmlinkage int __aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, +unsigned int key_len); asmlinkage void __aesni_enc(const void *ctx, u8 *out, const u8 *in); asmlinkage void __aesni_dec(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
@@ -89,11 +90,23 @@ asmlinkage void aesni_cts_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out, asmlinkage void aesni_cts_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,const u8 *in,unsigned int len,u8 *iv); -static int aesni_enc (const void *ctx,u8 *out,const u8 *in) +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key, +unsigned int key_len) +{ +return __aesni_set_key (ctx, in_key, key_len); +} +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_set_key); +#endif + +int aesni_enc (const void *ctx,u8 *out,const u8 *in) {__aesni_enc (ctx, out, in);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_enc); +#endif #define AVX_GEN2_OPTSIZE 640#define AVX_GEN4_OPTSIZE 4096
@@ -104,19 +117,25 @@ asmlinkage void __aesni_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, asmlinkage void __aesni_xts_decrypt(const struct crypto_aes_ctx *ctx, u8 *out,const u8 *in,unsigned int len,u8 *iv); -static int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, -unsigned int len,u8 *iv) +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv) {__aesni_xts_encrypt (ctx, out, in, len, iv);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_xts_encrypt); +#endif -static int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, -unsigned int len,u8 *iv) +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv) {__aesni_xts_decrypt (ctx, out, in, len, iv);return 0 ; } +#if IS_MODULE (CONFIG_CRYPTO_AES_KL ) +EXPORT_SYMBOL_GPL (aesni_xts_decrypt); +#endif #ifdef CONFIG_X86_64
@@ -242,7 +261,7 @@ static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); + err =__aesni_set_key (ctx, in_key, key_len);kernel_fpu_end (); }
arch/x86/crypto/aesni-intel_glue.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/ + +int aesni_set_key (struct crypto_aes_ctx *ctx,const u8 *in_key,unsigned int key_len); + +int aesni_enc (const void *ctx,u8 *out,const u8 *in); + +int aesni_xts_encrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +int aesni_xts_decrypt (const struct crypto_aes_ctx *ctx,u8 *out,const u8 *in, +unsigned int len,u8 *iv); +
Signature · 6 lines
2.17.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Re: [PATCH v8 12/12] crypto: x86/aes-kl - Implement the AES-XTS algorithm
Quoted 3 lines · Chang S. Bae
== Key Handle Restriction ==
A key handle may be encoded with some restrictions.
It's unclear what this means. Please avoid passive tense and the word "may" like this. I think you mean something like "The AES-KL instruction set supports selecting key usage restrictions at key handle creation time."
Quoted 1 line
Restrict every handle only available in kernel mode via
setkey().
I think you mean something like "Restrict all key handles created by the kernel to kernel mode use only."
Can you also mention why you are doing this? I suppose it might as well be done, but I'm not seeing how it would actually matter.
What other sorts of key usage restrictions does AES-KL support? Are any other ones useful here?
Quoted 2 lines
Subsequently the key handle could be corrupted or fail with handle restrictions. Then, encrypt()/decrypt() returns
-EINVAL.
Aren't these scenarios actually impossible? At least without memory corruption.
Quoted 3 lines
== Userspace Exposition ==
Some hardware implementations may have some performance penalties.
Likewise, please avoid vague statements like this. This makes it unclear whether this is something that happens in the real world or whether it's just theoretical. You indeed have actual benchmark results that show that AES-KL is much slower than AES-NI on current CPUs, right?
Quoted 2 lines
But, for disk encryption, storage bandwidth may be the bottleneck before encryption bandwidth.
Again, please try to be less vague. E.g. "With a slow storage device, storage bandwidth is the bottleneck, even if disk encryption is enabled..."
Quoted 2 lines
Thus, advertise it with a unique name 'xts-aes-aeskl' in
/proc/cryptowhile not replacing AES-NI under the generic name 'xts(aes)' with a lower priority.
The above sentence seems to say that xts-aes-aeskl does *not* have a lower priority than xts-aes-aesni. But actually it does.
Quoted 2 lines
Then, the performance is unlikely better than 64-bit which has already a gap vs. AES-NI.
I don't understand what this sentence is trying to say.
arch/x86/crypto/Kconfig line 8
Quoted 12 lines
arch/x86/crypto/Kconfig +4
-0@@ -2,6 +2,11 @@ menu "Accelerated Cryptographic Algorithms for CPU (x86)" +config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12
It looks like arch/x86/Kconfig.assembler would be a better place for this.
Quoted 29 lines
arch/x86/crypto/aeskl-intel_asm.S new file +23
-0@@ -0,0 +1,552 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Implement AES algorithm using AES Key Locker instructions. + * + * Most code is based from the AES-NI implementation, aesni-intel_asm.S + * + */ + +#include <linux/linkage.h> +#include <linux/cfi_types.h> +#include <asm/errno.h> +#include <asm/inst.h> +#include <asm/frame.h> +#include "aes-helper_asm.S" + +.text + +#define STATE1 %xmm0 +#define STATE2 %xmm1 +#define STATE3 %xmm2 +#define STATE4 %xmm3 +#define STATE5 %xmm4 +#define STATE6 %xmm5
arch/x86/crypto/aeskl-intel_asm.S line 33
Quoted 10 lines
arch/x86/crypto/aeskl-intel_asm.S new file +10
-0+#define STATE7 %xmm6 +#define STATE8 %xmm7 +#define STATE STATE1 + +#define IV %xmm9 +#define KEY %xmm10 +#define INC %xmm13 + +#define IN1 %xmm8 +#define IN IN1
Why do both IN1 and IN exist? Shouldn't there just be IN?
arch/x86/crypto/aeskl-intel_asm.S line 35
Quoted 2 lines
arch/x86/crypto/aeskl-intel_asm.S new file +2
-0+ +#define AREG %rax
Shouldn't %rax just be hardcoded?
arch/x86/crypto/aeskl-intel_asm.S line 36
Quoted 1 line
arch/x86/crypto/aeskl-intel_asm.S new file +1
-0+#define HANDLEP %rdi
This should be called CTX, to match the function prototypes.
arch/x86/crypto/aeskl-intel_asm.S line 44
Quoted 1 line
arch/x86/crypto/aeskl-intel_asm.S new file +1
-0+#define UKEYP OUTP
This should be called IN_KEY, to match the function prototypes.
Quoted 14 lines
arch/x86/crypto/aeskl-intel_asm.S new file +14
-0+#define GF128MUL_MASK %xmm11 + +/* + * int __aeskl_setkey(struct crypto_aes_ctx *ctx, const u8 *in_key, unsigned int key_len) + */ +SYM_FUNC_START(__aeskl_setkey) + FRAME_BEGIN + movl %edx, 480(HANDLEP) + movdqu (UKEYP), STATE1 + mov $1, %eax + cmp $16, %dl + je .Lsetkey_128 + + movdqu 0x10(UKEYP), STATE2
arch/x86/crypto/aeskl-intel_asm.S line 68
Quoted 10 lines
arch/x86/crypto/aeskl-intel_asm.S new file +10
-0+ encodekey256 %eax, %eax + movdqu STATE4, 0x30(HANDLEP) + jmp .Lsetkey_end +.Lsetkey_128: + encodekey128 %eax, %eax + +.Lsetkey_end: + movdqu STATE1, (HANDLEP) + movdqu STATE2, 0x10(HANDLEP) + movdqu STATE3, 0x20(HANDLEP)
The moves to the ctx should use movdqa, since it is aligned.
arch/x86/crypto/aeskl-intel_asm.S line 73
Quoted 5 lines
arch/x86/crypto/aeskl-intel_asm.S new file +5
-0+ + xor AREG, AREG + FRAME_END + RET +SYM_FUNC_END(__aeskl_setkey)
This function always returns 0, so it really should return void.
Quoted 17 lines
arch/x86/crypto/aeskl-intel_asm.S new file +17
-0+/* + * int __aeskl_enc(const void *ctx, u8 *dst, const u8 *src) + */ +SYM_FUNC_START(__aeskl_enc) + FRAME_BEGIN + movdqu (INP), STATE + movl 480(HANDLEP), KLEN + + cmp $16, KLEN + je .Lenc_128 + aesenc256kl (HANDLEP), STATE + jz .Lenc_err + jmp .Lenc_noerr +.Lenc_128: + aesenc128kl (HANDLEP), STATE + jz .Lenc_err +
arch/x86/crypto/aeskl-intel_asm.S line 101
Quoted 10 lines
arch/x86/crypto/aeskl-intel_asm.S new file +10
-0+.Lenc_noerr: + xor AREG, AREG + jmp .Lenc_end +.Lenc_err: + mov $(-EINVAL), AREG +.Lenc_end: + movdqu STATE, (OUTP) + FRAME_END + RET +SYM_FUNC_END(__aeskl_enc)
In the common case (successful AES-256 encryption) this is executing 'jmp' twice. I think the code should be rearranged to eliminate these jmps.
arch/x86/crypto/aeskl-intel_asm.S line 133
Quoted 5 lines
arch/x86/crypto/aeskl-intel_asm.S new file +5
-0+/* + * int __aeskl_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, + * const u8 *src, unsigned int len, le128 *iv) + */ +SYM_FUNC_START(__aeskl_xts_encrypt)
__aeskl_xts_encrypt() and __aeskl_xts_decrypt() are very similar. To reduce code duplication, can you consider generating them from a macro that takes an argument that indicates whether it is encrypt or decrypt?
Quoted 27 lines
arch/x86/crypto/aeskl-intel_asm.S new file +27
-0+static int aeskl_setkey(struct crypto_tfm *tfm, void *raw_ctx, const u8 *in_key, + unsigned int keylen) +{ + struct crypto_aes_ctx *ctx = (struct crypto_aes_ctx *)raw_ctx; + int err; + + if (!crypto_simd_usable()) + return -EBUSY; + + if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && + keylen != AES_KEYSIZE_256) + return -EINVAL; + + kernel_fpu_begin(); + if (unlikely(keylen == AES_KEYSIZE_192)) { + pr_warn_once("AES-KL does not support 192-bit key. Use AES-NI.\n"); + err = aesni_set_key(ctx, in_key, keylen); + } else { + if (!valid_keylocker()) + err = -ENODEV; + else + err = __aeskl_setkey(ctx, in_key, keylen); + } + kernel_fpu_end(); + + return err; +}
[...]
arch/x86/crypto/aeskl-intel_asm.S
Quoted 1 line
arch/x86/crypto/aeskl-intel_asm.S new file +1
-0+ .cra_ctxsize = XTS_AES_CTX_SIZE,
[...]
Something that your AES-KL code does that's a bit ugly is that it abuses 'struct crypto_aes_ctx' to store a Keylocker key handle instead of the actual AES key schedule which the struct is supposed to be for.
The proper way to represent that would be to make the tfm context for xts-aes-aeskl be a union of crypto_aes_ctx and a Keylocker specific context.
If you don't do that and instead keep the proposed workaround, then please add a comment somewhere that very clearly explains how the struct is being used. Above aeskl_setkey() or above .cra_ctxsize might be a good place.
arch/x86/crypto/aesni-intel_glue.h line 6
Quoted 12 lines
arch/x86/crypto/aesni-intel_glue.h new file +6
-0@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Support for Intel AES-NI instructions. This file contains function +* prototypes to be referenced for other AES implementations +*/
It would be helpful if this comment was more concrete, like "These are AES-NI functions that are used by the AES-KL code as a fallback when it is given a 192-bit key. Key Locker does not support 192-bit keys."
- Eric
Re: [PATCH v8 12/12] crypto: x86/aes-kl - Implement the AES-XTS algorithm
Quoted 4 lines · Eric Biggers
On Sat, Jun 03, 2023 at 08:22:27AM -0700, Chang S. Bae wrote:
Can you also mention why you are doing this? I suppose it might as well be done, but I'm not seeing how it would actually matter.
While this crypto implementation is in the kernel mode, userspace can call it:
https://docs.kernel.org/crypto/userspace-if.html
And those AES instructions are executable in userspace.
Say someone takes a key handle out of the kernel code and then decrypts some disk image from userspace. At least, this is enforced not to do.
Quoted 2 lines
What other sorts of key usage restrictions does AES-KL support? Are any other ones useful here?
Besides this, there are additional bits to restrict using encryption and decryption respectively.
This can be found in Section 1.1.1.1 'Handle Restrictions' in its whitepaper:
https://www.intel.com/content/www/us/en/develop/download/intel-key-locker-specification.html
Quoted 4 lines
Quoted 2 lines
Subsequently the key handle could be corrupted or fail with handle restrictions. Then, encrypt()/decrypt() returns
-EINVAL.Aren't these scenarios actually impossible? At least without memory corruption.
Yes, in the dm-crypt path, I think. But, the key handle can be tainted in the userspace -> API path.
I think this may help users as this feature can do some integrity checks at first and then populate an error right away if it goes wrong.
Quoted 5 lines
Quoted 2 lines
Thus, advertise it with a unique name 'xts-aes-aeskl' in
/proc/cryptowhile not replacing AES-NI under the generic name 'xts(aes)' with a lower priority.The above sentence seems to say that xts-aes-aeskl does *not* have a lower priority than xts-aes-aesni. But actually it does.
No, it does not say that. This needs to call out the latter part more clearly.
Quoted 4 lines
Quoted 2 lines
Then, the performance is unlikely better than 64-bit which has already a gap vs. AES-NI.
I don't understand what this sentence is trying to say.
This is in another section for explaining why 64-bitness only. I kinda added another point to avoid 32-bit code. But, anyways it is known that 32-bit kernel mode is being deprecated. Then, the 128-bit register story seems to be enough there.
Quoted 6 lines
arch/x86/crypto/Kconfig line 8
Quoted 4 lines
arch/x86/crypto/Kconfig +4
-0+config AS_HAS_KEYLOCKER + def_bool $(as-instr,encodekey256 %eax$(comma)%eax) + help + Supported by binutils >= 2.36 and LLVM integrated assembler >= V12It looks like
arch/x86/Kconfig.assemblerwould be a better place for this.
Yeah, the commit 5e8ebd841a44 ("x86: probe assembler capabilities via kconfig instead of makefile") moved those over there.
Quoted 5 lines
arch/x86/crypto/aeskl-intel_asm.S line 33
Quoted 3 lines
arch/x86/crypto/aeskl-intel_asm.S new file +3
-0+ +#define IN1 %xmm8 +#define IN IN1Why do both IN1 and IN exist? Shouldn't there just be IN?
Oh, this is a silly leftover from the CBC code as it has multiple inputs.
#define IN %xmm8 then, s/IN1/IN/g
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +#define AREG %raxShouldn't %rax just be hardcoded?
I thought this (or any other) renaming helps to read. Maybe I'm missing something. Can I get to know your thought on this?
Quoted 7 lines
Quoted 1 line
+#define HANDLEP %rdi
This should be called CTX, to match the function prototypes.
Quoted 1 line
+#define UKEYP OUTP
This should be called
IN_KEY, to match the function prototypes.
Okay. But, OTOH, the prototype itself is somewhat generic. Then its argument naming does not always match with what is supposed to be meant in the code. Thus, AES-NI renamed those like
ctx -> KEYP
in_key -> UKEY
...So, another option can be leaving some comments there, e.g. '# ctx is renamed to KEYP'.
Quoted 7 lines
arch/x86/crypto/aeskl-intel_asm.S line 68
Quoted 5 lines
arch/x86/crypto/aeskl-intel_asm.S new file +5
-0+ +.Lsetkey_end: + movdqu STATE1, (HANDLEP) + movdqu STATE2, 0x10(HANDLEP) + movdqu STATE3, 0x20(HANDLEP)The moves to the ctx should use movdqa, since it is aligned.
Reading the manual, the difference is whether generating #GP or not when any misaligned memory operand comes. Then, MOVDQA all here seems to be saying please check the alignment every time.
But, HANDLEP is known to have an aligned address. Then, the plain move seems to be enough and coherent with the glue code -- avoid unnecessary sanity checks.
Quoted 7 lines
arch/x86/crypto/aeskl-intel_asm.S line 73
Quoted 5 lines
arch/x86/crypto/aeskl-intel_asm.S new file +5
-0+ + xor AREG, AREG + FRAME_END + RET +SYM_FUNC_END(__aeskl_setkey)This function always returns 0, so it really should return void.
Yeah, fair enough.
Quoted 2 lines
In the common case (successful AES-256 encryption) this is executing 'jmp' twice. I think the code should be rearranged to eliminate these jmps.
Ah, right. I think a good point! Let me tweak this for those most likely cases.
Quoted 3 lines
__aeskl_xts_encrypt()and__aeskl_xts_decrypt()are very similar. To reduce code duplication, can you consider generating them from a macro that takes an argument that indicates whether it is encrypt or decrypt?
Yeah, I can see the code that prepares operands is common between them. But, I'm not sure folding them together can make it more readable.
Quoted 6 lines
Something that your AES-KL code does that's a bit ugly is that it abuses 'struct
crypto_aes_ctx' to store a Keylocker key handle instead of the actual AES key schedule which the struct is supposed to be for.The proper way to represent that would be to make the tfm context for xts-aes-aeskl be a union of
crypto_aes_ctxand a Keylocker specific context.
Agreed. I think this is likely the fallout of that struct aesni_xts_ctx fix. Previously, the field was a byte array which itself is not necessarily representing the extended-key format. Now the fix changed it to be more specific. Accordingly, Key Locker has to specify it.
Thanks,
Chang
PATCH 1/1 crypto: x86/aesni - Update aesni_set_key() to return void
In reply to Eric BiggersThe aesni_set_key() implementation has no error case, yet its prototype specifies to return an error code.
Modify the function prototype to return void and adjust the related code.
---
Previously, Eric identified a similar case in my AES-KL code [1]. Then, this parallel issue was realized.
[1]: https://lore.kernel.org/lkml/[email protected]/
--- arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/crypto/aesni-intel_asm.S +2 -3
@@ -1820,8 +1820,8 @@ SYM_FUNC_START_LOCAL(_key_expansion_256b) SYM_FUNC_END(_key_expansion_256b) /* - * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, - * unsigned int key_len) + * void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, + * unsigned int key_len) */ SYM_FUNC_START(aesni_set_key) FRAME_BEGIN
@@ -1926,7 +1926,6 @@ SYM_FUNC_START(aesni_set_key) sub $0x10, UKEYP cmp TKEYP, KEYP jb .Ldec_key_loop - xor AREG, AREG #ifndef __x86_64__ popl KEYP #endif
arch/x86/crypto/aesni-intel_glue.c +3 -3
@@ -87,8 +87,8 @@ static inline void *aes_align_addr(void *addr)return PTR_ALIGN (addr,AESNI_ALIGN ); } -asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, -unsigned int key_len); +asmlinkage void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, +unsigned int key_len); asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_dec(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
@@ -241,7 +241,7 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); +aesni_set_key (ctx, in_key, key_len);kernel_fpu_end (); }
Signature · 1 line
2.34.1
Re: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
Quoted 14 lines · Chang S. Bae
The
aesni_set_key()implementation has no error case, yet its prototype specifies to return an error code.Modify the function prototype to return void and adjust the related code.
Signed-off-by Chang S. Bae <[email protected]>Cc Eric Biggers <[email protected]>---
Previously, Eric identified a similar case in my AES-KL code [1]. Then, this parallel issue was realized.
Quoted 5 lines
[1]: https://lore.kernel.org/lkml/[email protected]/
--- arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-)
- Eric
Re: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
In reply to Chang S. BaeQuoted 60 lines · Chang S. Bae
The
aesni_set_key()implementation has no error case, yet its prototype specifies to return an error code.Modify the function prototype to return void and adjust the related code.
Signed-off-by Chang S. Bae <[email protected]>Cc Eric Biggers <[email protected]>---
Previously, Eric identified a similar case in my AES-KL code [1]. Then, this parallel issue was realized.[1]: https://lore.kernel.org/lkml/[email protected]/
--- arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-)arch/x86/crypto/aesni-intel_asm.S +2
-3@@ -1820,8 +1820,8 @@ SYM_FUNC_START_LOCAL(_key_expansion_256b) SYM_FUNC_END(_key_expansion_256b) /* - * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, - * unsigned int key_len) + * void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, + * unsigned int key_len) */ SYM_FUNC_START(aesni_set_key) FRAME_BEGIN@@ -1926,7 +1926,6 @@ SYM_FUNC_START(aesni_set_key) sub $0x10, UKEYP cmp TKEYP, KEYP jb .Ldec_key_loop - xor AREG, AREG #ifndef __x86_64__ popl KEYP #endifarch/x86/crypto/aesni-intel_glue.c +2
-2@@ -87,8 +87,8 @@ static inline void *aes_align_addr(void *addr)return PTR_ALIGN (addr,AESNI_ALIGN ); } -asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, -unsigned int key_len); +asmlinkage void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, +unsigned int key_len); asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_dec(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
arch/x86/crypto/aesni-intel_glue.c static int aes_set_key_common(struct crypto_aes_ctx *ctx, · line 244
Quoted 5 lines
arch/x86/crypto/aesni-intel_glue.c +1
-1@@ -241,7 +241,7 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); +aesni_set_key (ctx, in_key, key_len);
This will leave 'err' uninitialized.
Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +0
-1kernel_fpu_end (); } --2.34.1
Re: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
Quoted 10 lines · Ard Biesheuvel
arch/x86/crypto/aesni-intel_glue.c static int aes_set_key_common(struct crypto_aes_ctx *ctx, · line 244
Quoted 7 lines · Chang S. Bae
arch/x86/crypto/aesni-intel_glue.c +1
-1@@ -241,7 +241,7 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); +aesni_set_key (ctx, in_key, key_len);This will leave 'err' uninitialized.
Ah, right. Thanks for catching it.
Also, upon reviewing aes_expandkey(), I noticed there's no error case, except for the key length sanity check.
While addressing this, perhaps additional cleanup is considerable like:
@@ -233,19 +233,20 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx,
{
int err;- if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
- key_len != AES_KEYSIZE_256)
- return -EINVAL;
+ err = aes_check_keylen(key_len);
+ if (err)
+ return err;
if (!crypto_simd_usable())
- err = aes_expandkey(ctx, in_key, key_len);
+ /* no error with a valid key length */
+ aes_expandkey(ctx, in_key, key_len);
else {
kernel_fpu_begin();
- err = aesni_set_key(ctx, in_key, key_len);
+ aesni_set_key(ctx, in_key, key_len);
kernel_fpu_end();
}- return err;
+ return 0;
}
Thanks,
Chang
Re: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
Quoted 37 lines · Chang S. Bae
Quoted 10 lines · Ard Biesheuvel
arch/x86/crypto/aesni-intel_glue.c static int aes_set_key_common(struct crypto_aes_ctx *ctx, · line 244
Quoted 7 lines · Chang S. Bae
arch/x86/crypto/aesni-intel_glue.c +1
-1@@ -241,7 +241,7 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, err =aes_expandkey (ctx, in_key, key_len); else {kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); +aesni_set_key (ctx, in_key, key_len);This will leave 'err' uninitialized.
Ah, right. Thanks for catching it.
Also, upon reviewing
aes_expandkey(), I noticed there's no error case, except for the key length sanity check.While addressing this, perhaps additional cleanup is considerable like:
(quoted diff) +5
-4@@ -233,19 +233,20 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, { int err; - if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 && - key_len != AES_KEYSIZE_256) - return -EINVAL; + err = aes_check_keylen(key_len); + if (err) + return err; if (!crypto_simd_usable()) - err = aes_expandkey(ctx, in_key, key_len); + /* no error with a valid key length */ + aes_expandkey(ctx, in_key, key_len); else {
Review static int aes_set_key_common(struct · line 252
Quoted 10 lines
(quoted diff) +2
-2kernel_fpu_begin(); - err = aesni_set_key(ctx, in_key, key_len); + aesni_set_key(ctx, in_key, key_len); kernel_fpu_end(); } - return err; + return 0; }
I wonder whether we need aesni_set_key() at all.
Re: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
Quoted 2 lines · Ard Biesheuvel
I wonder whether we need
aesni_set_key()at all.
The following looks to be relevant from the AES-NI whitepaper [1]:
The Relative Cost of the Key Expansion
...
Some less frequent applications require frequent key scheduling. For example, some random number generators may rekey frequently to achieve forward secrecy. One extreme example is a Davies-Meyer hashing construction, which uses a block cipher primitive as a compression function, and the cipher is re-keyed for each processed data block.
Although these are not the mainstream usage models of the AES instructions, we point out that the AESKEYGENASSIST and AESIMC instructions facilitate Key Expansion procedure which is lookup tables free, and faster than software only key expansion. In addition, we point out that unrolling of the key expansion code, which is provided in the previous sections, improves the key expansion performance. The AES256 case can also utilize the instruction AESENCLAST, for the sbox transformation, that is faster than using AESKEYGENASSIST.
Thanks,
Chang
v2 cover crypto: x86/aesni - Simplify AES key expansion code
In reply to Chang S. BaePreviously, V1 [1] was posted to update the aesni_set_key() prototype to remove an unnecessary error code return type.
During the review process, it was discovered that both aes_expandkey() and the AES-NI glue code redundantly check the key size. V2 includes a cleanup to invoke aes_expandkey() immediately if AES-NI is unusable.
Thanks,
Chang
[1]: https://lore.kernel.org/lkml/[email protected]/
Chang S. Bae (2):
crypto: x86/aesni - Rearrange AES key size check
crypto: x86/aesni - Update aesni_set_key() to return void
arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-)
base-commit: e8f897f4afef0031fe618a8e94127a0934896aba
Signature · 1 line
2.34.1
PATCH v2 1/2 crypto: x86/aesni - Rearrange AES key size check
aes_expandkey() already includes an AES key size check. If AES-NI is unusable, invoke the function without the size check.
Also, use aes_check_keylen() instead of open code.
---
V2 <- V1:
* Add as a new patch.
--- arch/x86/crypto/aesni-intel_glue.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +8 -10
@@ -233,18 +233,16 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx, {int err; -if (key_len !=AES_KEYSIZE_128 && key_len !=AES_KEYSIZE_192 && - key_len !=AES_KEYSIZE_256 ) -return -EINVAL ; -if (!crypto_simd_usable ()) - err =aes_expandkey (ctx, in_key, key_len); -else { -kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); -kernel_fpu_end (); - } +return aes_expandkey (ctx, in_key, key_len); + err =aes_check_keylen (key_len); +if (err) +return err; + +kernel_fpu_begin (); + err =aesni_set_key (ctx, in_key, key_len); +kernel_fpu_end ();return err; }
Signature · 1 line
2.34.1
PATCH v2 2/2 crypto: x86/aesni - Update aesni_set_key() to return void
In reply to Chang S. BaeThe aesni_set_key() implementation has no error case, yet its prototype specifies to return an error code.
Modify the function prototype to return void and adjust the related code.
---
V2 <- V1:
* Ensure not to return an uninitialized value. (Ard Biesheuvel)
Previously, Eric identified a similar case in my AES-KL code [1]. Then, this parallel issue was realized.
[1]: https://lore.kernel.org/lkml/[email protected]/
--- arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/crypto/aesni-intel_asm.S +2 -3
@@ -1820,8 +1820,8 @@ SYM_FUNC_START_LOCAL(_key_expansion_256b) SYM_FUNC_END(_key_expansion_256b) /* - * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, - * unsigned int key_len) + * void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, + * unsigned int key_len) */ SYM_FUNC_START(aesni_set_key) FRAME_BEGIN
@@ -1926,7 +1926,6 @@ SYM_FUNC_START(aesni_set_key) sub $0x10, UKEYP cmp TKEYP, KEYP jb .Ldec_key_loop - xor AREG, AREG #ifndef __x86_64__ popl KEYP #endif
arch/x86/crypto/aesni-intel_glue.c +4 -4
@@ -87,8 +87,8 @@ static inline void *aes_align_addr(void *addr)return PTR_ALIGN (addr,AESNI_ALIGN ); } -asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, -unsigned int key_len); +asmlinkage void aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key, +unsigned int key_len); asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_dec(const void *ctx, u8 *out, const u8 *in); asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
@@ -241,9 +241,9 @@ static int aes_set_key_common(struct crypto_aes_ctx *ctx,return err;kernel_fpu_begin (); - err =aesni_set_key (ctx, in_key, key_len); +aesni_set_key (ctx, in_key, key_len);kernel_fpu_end (); -return err; +return 0 ; } static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Signature · 1 line
2.34.1
Herbert Xu
maintainerRe: [PATCH v2 0/2] crypto: x86/aesni - Simplify AES key expansion code
In reply to Chang S. BaeQuoted 16 lines · Chang S. Bae
Previously, V1 [1] was posted to update the
aesni_set_key()prototype to remove an unnecessary error code return type.During the review process, it was discovered that both
aes_expandkey()and the AES-NI glue code redundantly check the key size. V2 includes a cleanup to invokeaes_expandkey()immediately if AES-NI is unusable.Thanks,
Chang[1]: https://lore.kernel.org/lkml/[email protected]/
Chang S. Bae (2):
crypto: x86/aesni - Rearrange AES key size check
crypto: x86/aesni - Updateaesni_set_key()to return void
Quoted 8 lines
arch/x86/crypto/aesni-intel_asm.S | 5 ++--- arch/x86/crypto/aesni-intel_glue.c | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-)base-commit:
e8f897f4afef0031fe618a8e94127a0934896abaSignature · 1 line
2.34.1
All applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
kernel test robot
build botRe: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void
In reply to Chang S. Baekernel test robot noticed the following build warnings:
Hi Chang,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.9]
[cannot apply to herbert-cryptodev-2.6/master herbert-crypto-2.6/master linus/master next-20240523] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Chang-S-Bae/crypto-x86-aesni-Update-aesni_set_key-to-return-void/20240523-071024 base: v6.9 patch link: https://lore.kernel.org/r/20240311213232.128240-1-chang.seok.bae%40intel.com patch subject: [PATCH] crypto: x86/aesni - Update aesni_set_key() to return void config: i386-buildonly-randconfig-003-20240523 (https://download.01.org/0day-ci/archive/20240523/[email protected]/config) compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240523/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> arch/x86/crypto/aesni-intel_glue.c:240:6: warning: variable 'err' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
240 | if (!crypto_simd_usable())
| ^~~~~~~~~~~~~~~~~~~~~
arch/x86/crypto/aesni-intel_glue.c:248:9: note: uninitialized use occurs here
248 | return err;
| ^~~
arch/x86/crypto/aesni-intel_glue.c:240:2: note: remove the 'if' if its condition is always true
240 | if (!crypto_simd_usable())
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
241 | err = aes_expandkey(ctx, in_key, key_len);
| ~
242 | else {
| ~~~~~~
243 | kernel_fpu_begin();
| ~~~~~~~~~~~~~~~~~~~
244 | aesni_set_key(ctx, in_key, key_len);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245 | kernel_fpu_end();
| ~~~~~~~~~~~~~~~~~
246 | }
| ~
arch/x86/crypto/aesni-intel_glue.c:234:9: note: initialize the variable 'err' to silence this warning
234 | int err;
| ^
| = 01 warning generated.
vim +240 arch/x86/crypto/aesni-intel_glue.c
e12a68b3c6ac2c Chang S. Bae 2023-09-28 230
28b776098379fb Eric Biggers 2023-07-14 231 static int aes_set_key_common(struct crypto_aes_ctx *ctx,
54b6a1bd5364ac Huang Ying 2009-01-18 232 const u8 *in_key, unsigned int key_len)
54b6a1bd5364ac Huang Ying 2009-01-18 233 {
54b6a1bd5364ac Huang Ying 2009-01-18 234 int err;
54b6a1bd5364ac Huang Ying 2009-01-18 235
54b6a1bd5364ac Huang Ying 2009-01-18 236 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
674f368a952c48 Eric Biggers 2019-12-30 237 key_len != AES_KEYSIZE_256)
54b6a1bd5364ac Huang Ying 2009-01-18 238 return -EINVAL;
54b6a1bd5364ac Huang Ying 2009-01-18 239
f2abe0d72b2167 Eric Biggers 2019-03-12 @240 if (!crypto_simd_usable())
2c53fd11f76246 Ard Biesheuvel 2019-07-02 241 err = aes_expandkey(ctx, in_key, key_len);
54b6a1bd5364ac Huang Ying 2009-01-18 242 else {
54b6a1bd5364ac Huang Ying 2009-01-18 243 kernel_fpu_begin();
36ac212f978e58 Chang S. Bae 2024-03-11 244 aesni_set_key(ctx, in_key, key_len);
54b6a1bd5364ac Huang Ying 2009-01-18 245 kernel_fpu_end();
54b6a1bd5364ac Huang Ying 2009-01-18 246 }
54b6a1bd5364ac Huang Ying 2009-01-18 247
54b6a1bd5364ac Huang Ying 2009-01-18 248 return err;
54b6a1bd5364ac Huang Ying 2009-01-18 249 }
54b6a1bd5364ac Huang Ying 2009-01-18 250 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
v9 cover x86: Support Key Locker
In reply to Chang S. BaeHi all,
As posting this version, I wanted to make sure these code changes were acknowledgeable at first:
The previous enabling process has been paused to address vulnerabilities [1][2] that could compromise Key Locker's ability to protect AES keys. Now, with the mainlining of mitigations [3][4], patches (Patch 10-11) were added to ensure the application of these mitigations.
During this period, there was a significant change in the mainline commit b81fac906a8f ("x86/fpu: Move FPU initialization into arch_cpu_finalize_init()"). This affected Key Locker's initialization code, which clobbers XMM registers for loading a wrapping key, as it depends on FPU initialization.
In this revision, the setup code was adjusted to separate the initialization part to be invoked during arch_initcall(). The remaining code for copying the wrapping key from the backup resides in the identify_cpu() -> setup_keylocker() path. This separation simplifies the code and resolves an issue with hotplug.
The remaining changes mainly focus on the AES crypto driver, addressing feedback from Eric. Notably, while doing so, it was realized better to disallow a module build. Key Locker's AES instructions do not support 192-bit keys. Supporting a module build would require exporting some AES-NI functions, leading to performance-impacting indirect calls. I think we can revisit module support later if necessary.
Then, the following is a summary of changes per patch since v8 [6]:
PATCH7-8:
* Invoke the setup code via arch_initcall() due to upstream changes delaying the FPU setup.
PATCH9-11:
* Add new patches for security and hotplug support clarification
PATCH12:
* Drop the "nokeylocker" option. (Borislav Petkov)
PATCH13:
* Introduce 'union x86_aes_ctx'. (Eric Biggers)
* Ensure 'inline' for wrapper functions.
PATCH14:
* Combine the XTS enc/dec assembly code in a macro. (Eric Biggers)
* Define setkey() as void instead of returning 'int'. (Eric Biggers)
* Rearrange the assembly code to reduce jumps, especially for success cases. (Eric Biggers)
* Update the changelog for clarification. (Eric Biggers)
* Exclude module build.
This series is based on my AES-NI setkey() cleanup [7], which has been recently merged into the crypto repository [8], and I thought it was better to go first. You can also find this series here:
git://github.com/intel-staging/keylocker.git kl-v9
Thanks,
Chang
[1] Gather Data Sampling (GDS)
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/gather-data-sampling.html
[2] Register File Data Sampling (RFDS)
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/advisory-guidance/register-file-data-sampling.html
[3] Mainlining of GDS mitigation
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=64094e7e3118aff4b0be8ff713c242303e139834 [4] Mainlining of RFDS Mitigation https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0e33cf955f07e3991e45109cb3e29fbc9ca51d06 [5] Initialize FPU late https://lore.kernel.org/lkml/[email protected]/
[6] V8: https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/
[8] git://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
Chang S. Bae (14):
Documentation/x86: Document Key Locker
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/asm: Add a wrapper function for the LOADIWKEY instruction x86/msr-index: Add MSRs for Key Locker wrapping key x86/keylocker: Define Key Locker CPUID leaf x86/cpu/keylocker: Load a wrapping key at boot time x86/PM/keylocker: Restore the wrapping key on the resume from ACPI
S3/4
x86/hotplug/keylocker: Ensure wrapping key backup capability x86/cpu/keylocker: Check Gather Data Sampling mitigation x86/cpu/keylocker: Check Register File Data Sampling mitigation x86/Kconfig: Add a configuration for Key Locker
crypto: x86/aes - Prepare for new AES-XTS implementation
crypto: x86/aes-kl - Implement the AES-XTS algorithm
Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 96 +++++ arch/x86/Kconfig | 3 + arch/x86/Kconfig.assembler | 5 + arch/x86/crypto/Kconfig | 17 + arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aes-helper_asm.S | 22 ++ arch/x86/crypto/aes-helper_glue.h | 168 ++++++++ arch/x86/crypto/aeskl-intel_asm.S | 412 ++++++++++++++++++++ arch/x86/crypto/aeskl-intel_glue.c | 187 +++++++++ arch/x86/crypto/aeskl-intel_glue.h | 35 ++ arch/x86/crypto/aesni-intel_asm.S | 47 +-- arch/x86/crypto/aesni-intel_glue.c | 193 ++------- arch/x86/crypto/aesni-intel_glue.h | 40 ++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/keylocker.h | 42 ++ arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/special_insns.h | 28 ++ arch/x86/include/uapi/asm/processor-flags.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/common.c | 4 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/keylocker.c | 219 +++++++++++ arch/x86/lib/x86-opcode-map.txt | 11 +- arch/x86/power/cpu.c | 2 + tools/arch/x86/lib/x86-opcode-map.txt | 11 +- 27 files changed, 1363 insertions(+), 202 deletions(-) create mode 100644 Documentation/arch/x86/keylocker.rst create mode 100644 arch/x86/crypto/aes-helper_asm.S create mode 100644 arch/x86/crypto/aes-helper_glue.h create mode 100644 arch/x86/crypto/aeskl-intel_asm.S create mode 100644 arch/x86/crypto/aeskl-intel_glue.c create mode 100644 arch/x86/crypto/aeskl-intel_glue.h create mode 100644 arch/x86/crypto/aesni-intel_glue.h create mode 100644 arch/x86/include/asm/keylocker.h create mode 100644 arch/x86/kernel/keylocker.c
base-commit: 3a447c31d337bdec7fbc605a7a1e00aff4c492d0
Signature · 1 line
2.34.1
PATCH v9 1/14 Documentation/x86: Document Key Locker
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
---
Changes from v8:
* Change wording of documentation slightly. (Randy Dunlap and Bagas Sanjaya)
Changes from v6:
* Rebase on the upstream -- commit ff61f0791ce9 ("docs: move x86 documentation into Documentation/arch/"). (Nathan Huckleberry)
* Remove a duplicated sentence -- 'But there is no AES-KL instruction to process a 192-bit key.'
* Update the text for clarity and readability:
- Clarify the error code and exemplify the backup failure
- Use 'wrapping key' instead of less readable 'IWKey'
Changes from v5:
* Fix a typo: 'feature feature' -> 'feature'
Changes from RFC v2:
* Add as a new patch.
The preview is available here:
https://htmlpreview.github.io/?https://github.com/intel-staging/keylocker/kdoc/arch/x86/keylocker.html
--- Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 96 ++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Documentation/arch/x86/keylocker.rst
Documentation/arch/x86/index.rst +1 -0
@@ -43,3 +43,4 @@ x86-specific Documentation features elf_auxvec xstate + keylocker
Documentation/arch/x86/keylocker.rst new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v9 01/14] Documentation/x86: Document Key Locker
Hi,
Quoted 76 lines · Chang S. Bae
Document the overview of the feature along with relevant consideration when provisioning dm-crypt volumes with AES-KL instead of AES-NI.
Signed-off-by Chang S. Bae <[email protected]>Reviewed-by Dan Williams <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Cc Randy Dunlap <[email protected]>---
Changes from v8:
* Change wording of documentation slightly. (Randy Dunlap and Bagas Sanjaya)Changes from v6:
* Rebase on the upstream -- commitff61f0791ce9("docs: move x86 documentation intoDocumentation/arch/"). (Nathan Huckleberry)
* Remove a duplicated sentence -- 'But there is no AES-KL instruction to process a 192-bit key.'
* Update the text for clarity and readability:
- Clarify the error code and exemplify the backup failure
- Use 'wrapping key' instead of less readable 'IWKey'Changes from v5:
* Fix a typo: 'feature feature' -> 'feature'Changes from RFC v2:
* Add as a new patch.The preview is available here:
https://htmlpreview.github.io/?https://github.com/intel-staging/keylocker/kdoc/arch/x86/keylocker.html--- Documentation/arch/x86/index.rst | 1 + Documentation/arch/x86/keylocker.rst | 96 ++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Documentation/arch/x86/keylocker.rstDocumentation/arch/x86/index.rst +1
-0@@ -43,3 +43,4 @@ x86-specific Documentation features elf_auxvec xstate + keylockerDocumentation/arch/x86/keylocker.rst new file +26
-0@@ -0,0 +1,96 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============== +x86 Key Locker +============== + +Introduction +============ + +Key Locker is a CPU feature to reduce key exfiltration opportunities +while maintaining a programming interface similar to AES-NI. It +converts the AES key into an encoded form, called the 'key handle'. +The key handle is a wrapped version of the clear-text key where the +wrapping key has limited exposure. Once converted, all subsequent data +encryption using new AES instructions (AES-KL) uses this key handle, +reducing the exposure of private key material in memory. + +CPU-internal Wrapping Key +========================= + +The CPU-internal wrapping key is an entity in a software-invisible CPU +state. On every system boot, a new key is loaded. So the key handle that +was encoded by the old wrapping key is no longer usable on system shutdown +or reboot. + +And the key may be lost on the following exceptional situation upon wakeup:
Documentation/arch/x86/keylocker.rst line 36
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+ +Wrapping Key Restore Failure +---------------------------- + +The CPU state is volatile with the ACPI S3/4 sleep states. When the system +supports those states, the key has to be backed up so that it is restored +on wake up. The kernel saves the key in non-volatile media. + +Upon the event of a wrapping key restore failure upon resume from suspend, +all established key handles become invalid. In flight dm-crypt operations
In-flight
Quoted 50 lines
Documentation/arch/x86/keylocker.rst new file +50
-0+receive error results from pending operations. In the likely scenario that +dm-crypt is hosting the root filesystem the recovery is identical to if a +storage controller failed to resume from suspend or reboot. If the volume +impacted by a wrapping key restore failure is a data volume then it is +possible that I/O errors on that volume do not bring down the rest of the +system. However, a reboot is still required because the kernel will have +soft-disabled Key Locker. Upon the failure, the crypto library code will +return -ENODEV on every AES-KL function call. The Key Locker implementation +only loads a new wrapping key at initial boot, not any time after like +resume from suspend. + +Use Case and Non-use Cases +========================== + +Bare metal disk encryption is the only intended use case. + +Userspace usage is not supported because there is no ABI provided to +communicate and coordinate wrapping-key restore failure to userspace. For +now, key restore failures are only coordinated with kernel users. But the +kernel can not prevent userspace from using the feature's AES instructions +('AES-KL') when the feature has been enabled. So, the lack of userspace +support is only documented, not actively enforced. + +Key Locker is not expected to be advertised to guest VMs and the kernel +implementation ignores it even if the VMM enumerates the capability. The +expectation is that a guest VM wants private wrapping key state, but the +architecture does not provide that. An emulation of that capability, by +caching per-VM wrapping keys in memory, defeats the purpose of Key Locker. +The backup / restore facility is also not performant enough to be suitable +for guest VM context switches. + +AES Instruction Set +=================== + +The feature accompanies a new AES instruction set. This instruction set is +analogous to AES-NI. A set of AES-NI instructions can be mapped to an +AES-KL instruction. For example, AESENC128KL is responsible for ten rounds +of transformation, which is equivalent to nine times AESENC and one +AESENCLAST in AES-NI. + +But they have some notable differences: + +* AES-KL provides a secure data transformation using an encrypted key. + +* If an invalid key handle is provided, e.g. a corrupted one or a handle + restriction failure, the instruction fails with setting RFLAGS.ZF. The + crypto library implementation includes the flag check to return -EINVAL. + Note that this flag is also set if the wrapping key is changed, e.g., + because of the backup error. +
Documentation/arch/x86/keylocker.rst line 96
Quoted 10 lines
Documentation/arch/x86/keylocker.rst new file +10
-0+* AES-KL implements support for 128-bit and 256-bit keys, but there is no + AES-KL instruction to process an 192-bit key. The AES-KL cipher + implementation logs a warning message with a 192-bit key and then falls + back to AES-NI. So, this 192-bit key-size limitation is only documented, + not enforced. It means the key will remain in clear-text in memory. This + is to meet Linux crypto-cipher expectation that each implementation must + support all the AES-compliant key sizes. + +* Some AES-KL hardware implementation may have noticeable performance + overhead when compared with AES-NI instructions.
thanks.
Signature · 1 line
#Randy
PATCH v9 2/14 x86/cpufeature: Enumerate Key Locker feature
In reply to Chang S. BaeKey Locker is a CPU feature to minimize exposure of clear-text key material. An encoded form, called 'key handle', is referenced for data encryption or decryption instead of accessing the clear text key.
A wrapping key loaded in the CPU's software-inaccessible state is used to transform a user key into a key handle. On rarely unexpected hardware failure, the key could be lost.
Here enumerate this hardware capability. It will not be shown up in /proc/cpuinfo as userspace usage is not supported. This is because there is no ABI to coordinate the wrapping-key failure.
The feature supports Advanced Encryption Standard (AES) cipher algorithm with new SIMD instruction set like its predecessor (AES-NI). Mark the feature having dependency on XMM2 like AES-NI has. The new AES implementation will be in the crypto library.
Add X86_FEATURE_KEYLOCKER to the disabled feature list. It will be enabled by a new Kconfig option.
---
Changes from v6:
* Massage the changelog -- re-organize the change descriptions
Changes from RFC v2:
* Do not publish the feature flag to userspace.
* Update the changelog.
Changes from RFC v1:
* Updated the changelog.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/cpuid-deps.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -399,6 +399,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_KEYLOCKER (16*32+23)/* "" Key Locker */ #define X86_FEATURE_BUS_LOCK_DETECT (16*32+24)/* Bus Lock detect */ #define X86_FEATURE_CLDEMOTE (16*32+25)/* CLDEMOTE instruction */ #define X86_FEATURE_MOVDIRI (16*32+27)/* MOVDIRI instruction */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -38,6 +38,12 @@# define DISABLE_OSPKE (1<<(X86_FEATURE_OSPKE & 31))#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */ +#ifdef CONFIG_X86_KEYLOCKER +# define DISABLE_KEYLOCKER 0 +#else +# define DISABLE_KEYLOCKER (1<<(X86_FEATURE_KEYLOCKER & 31)) +#endif /* CONFIG_X86_KEYLOCKER */ +#ifdef CONFIG_X86_5LEVEL # define DISABLE_LA57 0#else
@@ -150,7 +156,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_KEYLOCKER)#define DISABLED_MASK17 0#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK19 (DISABLE_SEV_SNP)
arch/x86/include/uapi/asm/processor-flags.h +2 -0
@@ -128,6 +128,8 @@#define X86_CR4_PCIDE _BITUL(X86_CR4_PCIDE_BIT)#define X86_CR4_OSXSAVE_BIT 18/* enable xsave and xrestore */ #define X86_CR4_OSXSAVE _BITUL(X86_CR4_OSXSAVE_BIT) +#define X86_CR4_KEYLOCKER_BIT 19/* enable Key Locker */ +#define X86_CR4_KEYLOCKER _BITUL(X86_CR4_KEYLOCKER_BIT)#define X86_CR4_SMEP_BIT 20/* enable SMEP support */ #define X86_CR4_SMEP _BITUL(X86_CR4_SMEP_BIT)#define X86_CR4_SMAP_BIT 21/* enable SMAP support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, { X86_FEATURE_FRED, X86_FEATURE_LKGS }, { X86_FEATURE_FRED, X86_FEATURE_WRMSRNS }, + { X86_FEATURE_KEYLOCKER, X86_FEATURE_XMM2 }, {} };
Signature · 1 line
2.34.1
PATCH v9 3/14 x86/insn: Add Key Locker instructions to the opcode map
In reply to Chang S. BaeThe x86 instruction decoder needs to know these new instructions that are going to be used in the crypto library as well as the x86 core code. Add the following:
LOADIWKEY:
Load a CPU-internal wrapping key.
ENCODEKEY128:
Wrap a 128-bit AES key to a key handle.
ENCODEKEY256:
Wrap a 256-bit AES key to a key handle.
AESENC128KL:
Encrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESENC256KL:
Encrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESDEC128KL:
Decrypt a 128-bit block of data using a 128-bit AES key indicated by a key handle.
AESDEC256KL:
Decrypt a 128-bit block of data using a 256-bit AES key indicated by a key handle.
AESENCWIDE128KL:
Encrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESENCWIDE256KL:
Encrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
AESDECWIDE128KL:
Decrypt 8 128-bit blocks of data using a 128-bit AES key indicated by a key handle.
AESDECWIDE256KL:
Decrypt 8 128-bit blocks of data using a 256-bit AES key indicated by a key handle.
The detail can be found in Intel Software Developer Manual.
---
Changes from v6:
* Massage the changelog -- add the reason a bit.
Changes from RFC v1:
* Separated out the LOADIWKEY addition in a new patch.
* Included AES instructions to avoid warning messages when the AES Key Locker module is built.
--- arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- tools/arch/x86/lib/x86-opcode-map.txt | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
tools/arch/x86/lib/x86-opcode-map.txt +7 -4
@@ -800,11 +800,12 @@ cb: sha256rnds2 Vdq,Wdq | vrcp28ss/d Vx,Hx,Wx (66),(ev) cc: sha256msg1 Vdq,Wdq | vrsqrt28ps/d Vx,Wx (66),(ev) cd: sha256msg2 Vdq,Wdq | vrsqrt28ss/d Vx,Hx,Wx (66),(ev) cf: vgf2p8mulb Vx,Wx (66) +d8: AESENCWIDE128KL Qpi (F3),(000),(00B) | AESENCWIDE256KL Qpi (F3),(000),(10B) | AESDECWIDE128KL Qpi (F3),(000),(01B) | AESDECWIDE256KL Qpi (F3),(000),(11B) db: VAESIMC Vdq,Wdq (66),(v1) -dc: vaesenc Vx,Hx,Wx (66) -dd: vaesenclast Vx,Hx,Wx (66) -de: vaesdec Vx,Hx,Wx (66) -df: vaesdeclast Vx,Hx,Wx (66) +dc: vaesenc Vx,Hx,Wx (66) | LOADIWKEY Vx,Hx (F3) | AESENC128KL Vpd,Qpi (F3) +dd: vaesenclast Vx,Hx,Wx (66) | AESDEC128KL Vpd,Qpi (F3) +de: vaesdec Vx,Hx,Wx (66) | AESENC256KL Vpd,Qpi (F3) +df: vaesdeclast Vx,Hx,Wx (66) | AESDEC256KL Vpd,Qpi (F3) f0: MOVBE Gy,My | MOVBE Gw,Mw (66) | CRC32 Gd,Eb (F2) | CRC32 Gd,Eb (66&F2) f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v)
@@ -814,6 +815,8 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My, f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) f9: MOVDIRI My,Gy +fa: ENCODEKEY128 Ew,Ew (F3) +fb: ENCODEKEY256 Ew,Ew (F3) EndTable Table: 3-byte opcode 2 (0x0f 0x3a)
Signature · 1 line
2.34.1
PATCH v9 4/14 x86/asm: Add a wrapper function for the LOADIWKEY instruction
In reply to Chang S. BaeKey Locker introduces a CPU-internal wrapping key to encode a user key to a key handle. Then a key handle is referenced instead of the plain text key.
LOADIWKEY loads a wrapping key in the software-inaccessible CPU state. It operates only in kernel mode.
The kernel will use this to load a new key at boot time. Establish an accessor for the feature setup, and define struct iwkey to pass a key value.
---
Changes from v6:
* Massage the changelog -- clarify the reason and the changes a bit.
Changes from v5:
* Fix a typo: kernel_cpu_begin() -> kernel_fpu_begin()
Changes from RFC v2:
* Separate out the code as a new patch.
* Improve the usability with the new struct as an argument. (Dan Williams)
Previously, Dan questioned the necessity of 'WARN_ON(!irq_fpu_usable())' in the load_xmm_iwkey() function. However, it's worth noting that the function comment emphasizes the caller's responsibility for invoking kernel_fpu_begin(), which effectively performs the sanity check through kernel_fpu_begin_mask().
--- arch/x86/include/asm/keylocker.h | 25 +++++++++++++++++++++++++ arch/x86/include/asm/special_insns.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/keylocker.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_KEYLOCKER_H +#define _ASM_KEYLOCKER_H + +#ifndef __ASSEMBLY__ + +#include <asm/fpu/types.h> + +/** +* struct iwkey - A temporary wrapping key storage. +* @integrity_key: A 128-bit key used to verify the integrity of +* key handles +* @encryption_key: A 256-bit encryption key used for wrapping and +* unwrapping clear text keys. +* +* This storage should be flushed immediately after being loaded. +*/ +struct iwkey { +struct reg_128_bit integrity_key; +struct reg_128_bit encryption_key[2 ]; +}; + +#endif /*__ASSEMBLY__ */ +#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +28 -0
@@ -9,6 +9,7 @@#include <linux/errno.h> #include <linux/irqflags.h> #include <linux/jump_label.h> +#include <asm/keylocker.h> /* * The compiler should not reorder volatile asm statements with respect to each
@@ -301,6 +302,33 @@ static __always_inline void tile_release(void) asmvolatile (".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0" ); } +/** +* load_xmm_iwkey - Load a CPU-internal wrapping key into XMM registers. +* @key: A pointer to a struct iwkey containing the key data. +* +* The caller is responsible for invoking kernel_fpu_begin() before. +*/ +static inline void load_xmm_iwkey (struct iwkey *key) +{ +struct reg_128_bit zeros = {0 }; + + asmvolatile ("movdqu %0, %%xmm0; movdqu %1, %%xmm1; movdqu %2, %%xmm2;" + ::"m" (key->integrity_key),"m" (key->encryption_key[0 ]), +"m" (key->encryption_key[1 ])); + +/* +* 'LOADIWKEY %xmm1,%xmm2' loads a key from XMM0-2 into a +* software-invisible CPU state. With zero in EAX, CPU does not +* perform hardware randomization and allows key backup. +* +* This instruction is supported by binutils >= 2.36. +*/ + asmvolatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1" ::"a" (0 )); + + asmvolatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;" + ::"m" (zeros)); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
Signature · 1 line
2.34.1
PATCH v9 5/14 x86/msr-index: Add MSRs for Key Locker wrapping key
In reply to Chang S. BaeThe wrapping key resides in the same power domain as the CPU cache. Consequently, any sleep state that invalidates the cache, such as S3, also affects the wrapping key's state.
However, as the wrapping key's state is inaccessible to software, a specialized mechanism is necessary to save and restore the key during deep sleep.
A set of new MSRs is provided as an abstract interface for saving, restoring, and checking the wrapping key's status. The wrapping key is securely saved in a platform-scoped state using non-volatile media. Both the backup storage and its path from the CPU are encrypted and integrity-protected to ensure security.
Define those MSRs for saving and restoring the key during S3/4 sleep states.
Note that the non-volatility of the backup storage is not architecturally guaranteed across off-states such as S5 and G3. In such cases, the kernel may generate a new key during the next boot.
---
Changes from v8:
* Tweak the changelog.
Changes from v6:
* Tweak the changelog -- put the last for those about other sleep states
Changes from RFC v2:
* Update the changelog. (Dan Williams)
* Rename the MSRs. (Dan Williams)
--- arch/x86/include/asm/msr-index.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
@@ -1192,4 +1192,10 @@* a #GP */ +/* MSRs for managing a CPU-internal wrapping key for Key Locker. */ +#define MSR_IA32_IWKEY_COPY_STATUS 0x00000990 +#define MSR_IA32_IWKEY_BACKUP_STATUS 0x00000991 +#define MSR_IA32_BACKUP_IWKEY_TO_PLATFORM 0x00000d91 +#define MSR_IA32_COPY_IWKEY_TO_LOCAL 0x00000d92 +#endif /* _ASM_X86_MSR_INDEX_H */
Signature · 1 line
2.34.1
PATCH v9 6/14 x86/keylocker: Define Key Locker CPUID leaf
In reply to Chang S. BaeBoth Key Locker enabling code in the x86 core and AES Key Locker code in the crypto library will need to reference feature-specific CPUID bits. Define this CPUID leaf and bits.
---
Changes from v6:
* Tweak the changelog -- comment the reason first and then brief the change.
Changes from RFC v2:
* Separate out the code as a new patch.
--- arch/x86/include/asm/keylocker.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/keylocker.h +7 -0
@@ -5,6 +5,7 @@#ifndef __ASSEMBLY__ +#include <linux/bits.h> #include <asm/fpu/types.h> /**
@@ -21,5 +22,11 @@ struct iwkey {struct reg_128_bit encryption_key[2 ]; }; +#define KEYLOCKER_CPUID 0x019 +#define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0) +#define KEYLOCKER_CPUID_EBX_AESKLE BIT(0) +#define KEYLOCKER_CPUID_EBX_WIDE BIT(2) +#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
Signature · 1 line
2.34.1
PATCH v9 7/14 x86/cpu/keylocker: Load a wrapping key at boot time
In reply to Chang S. BaeThe wrapping key is an entity to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
The wrapping key needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled before the first dm-crypt user arrives.
But there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
Therefore, generate random bytes and load them at boot time, involving clobbering XMM registers. Perform this process under arch_initcall(), ensuring that it occurs after FPU initialization. Finally, flush out random bytes after loading.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt use, and that switching wrapping key per virtual machine is impractical, explicitly skip this setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from v8:
* Invoke the setup code via arch_initcall(). The move was due to the upstream changes. Commit b81fac906a8f ("x86/fpu: Move FPU initialization into arch_cpu_finalize_init()") delays the FPU setup.
* Tweak code comments and the changelog.
* Revoke the review tag as the code change is significant.
Changes from v6:
* Switch to use 'static inline' for the empty functions, instead of macro that disallows type checks. (Eric Biggers and Dave Hansen)
* Use memzero_explicit() to wipe out the key data instead of writing the poison value over there. (Robert Elliott)
* Massage the changelog for the better readability.
Changes from v5:
* Call out the disabling when the feature is available on a virtual machine. Then, it will turn off the feature flag
Changes from RFC v2:
* Make bare metal only.
* Clean up the code (e.g. dynamically allocate the key cache). (Dan Williams)
* Massage the changelog.
* Move out the LOADIWKEY wrapper and the Key Locker CPUID defines.
--- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/keylocker.c | 77 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/kernel/Makefile +1 -0
@@ -137,6 +137,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/keylocker.c new file +77 -0
77 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v9 7/14 x86/cpu/keylocker: Load a wrapping key at boot time
The wrapping key is an entity to encode a clear text key into a key handle. This key is a pivot in protecting user keys. So the value has to be randomized before being loaded in the software-invisible CPU state.
The wrapping key needs to be established before the first user. Given that the only proposed Linux use case for Key Locker is dm-crypt, the feature could be lazily enabled before the first dm-crypt user arrives.
But there is no precedent for late enabling of CPU features and it adds maintenance burden without demonstrative benefit outside of minimizing the visibility of Key Locker to userspace.
Therefore, generate random bytes and load them at boot time, involving clobbering XMM registers. Perform this process under arch_initcall(), ensuring that it occurs after FPU initialization. Finally, flush out random bytes after loading.
Given that the Linux Key Locker support is only intended for bare metal dm-crypt use, and that switching wrapping key per virtual machine is impractical, explicitly skip this setup in the X86_FEATURE_HYPERVISOR case.
---
Changes from v9:
* Include 'tlbflush.h' back, which was once removed by mistake.
--- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/keylocker.c | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/kernel/Makefile +1 -0
@@ -137,6 +137,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o obj-$(CONFIG_TRACING) += tracepoint.o obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o obj-$(CONFIG_X86_UMIP) += umip.o +obj-$(CONFIG_X86_KEYLOCKER) += keylocker.o obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
arch/x86/kernel/keylocker.c new file +78 -0
78 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH v9 8/14 x86/PM/keylocker: Restore the wrapping key on the resume from ACPI S3/4
In reply to Chang S. BaeThe primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even if the prompt performs for unlocking the volume, where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded.
== Wrapping-key Restore ==
To meet dm-crypt's expectations, the key handles in the suspend-image has to remain valid after resuming from an S-state. However, when the system enters ACPI S3 or S4 sleep states, the wrapping key is discarded.
Key Locker provides a mechanism to back up the wrapping key in non-volatile storage. Therefore, upon boot, request a backup of the wrapping key and copy it back to each CPU upon wakeup. If the backup mechanism is unavailable, disable the feature unless CONFIG_SUSPEND=n.
== Restore Failure ==
In the event of a key restore failure, the kernel proceeds with an initialized wrapping key state. This action invalidates any key handles present in the suspend-image, leading to I/O errors in dm-crypt operations.
However, data integrity remains intact, and access is restored with new handles created by the new wrapping key at the next boot. At least, manage a feature-specific flag to communicate with the crypto implementation, ensuring to stop using AES instructions upon the key restore failure, instead of abruptly disabling the feature.
== Off-states ==
While the backup may persist in non-volatile media across S5 and G3 "off" states, it is neither architecturally guaranteed nor expected by dm-crypt. Therefore, a reboot can address this scenario with a new wrapping key, as dm-crypt prompts for the key whenever the volume is started.
---
Changes from v8:
* Rebase on the previous patch (patch7) changes, separating the wrapping key restoration code from the initial load. Previously, the identify_cpu() -> setup_keylocker() sequence in the hotplug path could hit __init code, leading to an explosion. This change removes the initialization code from the hotplug path. (Sangwhan Moon)
* Turn copy_keylocker() to return bool for simplification.
* Rename the flag for clarity: 'valid_kl' -> 'valid_wrapping_key'.
* Don't export symbol for valid_keylocker(), as AES-KL will be built-in. (see patch14 for detail).
* Tweak code comments and the changelog.
* Revoke the review tag as the code change is significant.
Changes from v6:
* Limit the symbol export only when needed.
* Improve the coding style -- reduce an indent after 'if() { ... return; }'. (Eric Biggers)
* Fix the coding style -- reduce an indent after if() {...return;}. (Eric Biggers) Tweak the comment along with that.
* Improve the function prototype, instead of using a macro. (Eric Biggers and Dave Hansen)
* Update the documentation:
- Massage the changelog to clarify the problem-and-solution by sections
- Clarify the comment about the key restore failure.
Changes from v5:
* Fix the 'valid_kl' flag not to be set when the feature is disabled. (Reported by Marvin Hsu [email protected]) Add the function comment about this.
* Improve the error handling in setup_keylocker(). All the error cases fall through the end that disables the feature. Otherwise, all the successful cases return immediately.
Changes from v4:
* Update the changelog and title. (Rafael Wysocki)
Changes from v3:
* Fix the build issue with !X86_KEYLOCKER. (Eric Biggers)
Changes from RFC v2:
* Change the backup key failure handling. (Dan Williams)
Changes from RFC v1:
* Folded the warning message into the if condition check. (Rafael Wysocki)
* Rebase on the changes of the previous patches.
* Added error code for key restoration failures.
* Moved the restore helper.
* Added function descriptions.
--- arch/x86/include/asm/keylocker.h | 10 ++++ arch/x86/kernel/cpu/common.c | 4 +- arch/x86/kernel/keylocker.c | 88 ++++++++++++++++++++++++++++++++ arch/x86/power/cpu.c | 2 + 4 files changed, 103 insertions(+), 1 deletion(-)
arch/x86/include/asm/keylocker.h +10 -0
@@ -28,5 +28,15 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void ); +#else +static inline void setup_keylocker (void ) { } +static inline void restore_keylocker (void ) { } +static inline bool valid_keylocker (void ) {return false; } +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/cpu/common.c +3 -1
@@ -62,6 +62,7 @@#include <asm/intel-family.h> #include <asm/cpu_device_id.h> #include <asm/fred.h> +#include <asm/keylocker.h> #include <asm/uv/uv.h> #include <asm/ia32.h> #include <asm/set_memory.h>
@@ -1826,10 +1827,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker ();/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c +88 -0
116 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -27,6 +27,7 @@#include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -264,6 +265,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) x86_platform.restore_sched_clock_state ();cache_bp_restore ();perf_restore_debug_store (); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 1 line
2.34.1
PATCH v9 8/14 x86/PM/keylocker: Restore the wrapping key on the resume from ACPI S3/4
The primary use case for the feature is bare metal dm-crypt. The key needs to be restored properly on wakeup, as dm-crypt does not prompt for the key on resume from suspend. Even if the prompt performs for unlocking the volume, where the hibernation image is stored, it still expects to reuse the key handles within the hibernation image once it is loaded.
== Wrapping-key Restore ==
To meet dm-crypt's expectations, the key handles in the suspend-image has to remain valid after resuming from an S-state. However, when the system enters ACPI S3 or S4 sleep states, the wrapping key is discarded.
Key Locker provides a mechanism to back up the wrapping key in non-volatile storage. Therefore, upon boot, request a backup of the wrapping key and copy it back to each CPU upon wakeup. If the backup mechanism is unavailable, disable the feature unless CONFIG_SUSPEND=n.
== Restore Failure ==
In the event of a key restore failure, the kernel proceeds with an initialized wrapping key state. This action invalidates any key handles present in the suspend-image, leading to I/O errors in dm-crypt operations.
However, data integrity remains intact, and access is restored with new handles created by the new wrapping key at the next boot. At least, manage a feature-specific flag to communicate with the crypto implementation, ensuring to stop using AES instructions upon the key restore failure, instead of abruptly disabling the feature.
== Off-states ==
While the backup may persist in non-volatile media across S5 and G3 "off" states, it is neither architecturally guaranteed nor expected by dm-crypt. Therefore, a reboot can address this scenario with a new wrapping key, as dm-crypt prompts for the key whenever the volume is started.
---
Change from v9:
* Export valid_keylocker() again for the AES-KL module.
--- arch/x86/include/asm/keylocker.h | 10 ++++ arch/x86/kernel/cpu/common.c | 4 +- arch/x86/kernel/keylocker.c | 89 ++++++++++++++++++++++++++++++++ arch/x86/power/cpu.c | 2 + 4 files changed, 104 insertions(+), 1 deletion(-)
arch/x86/include/asm/keylocker.h +10 -0
@@ -28,5 +28,15 @@ struct iwkey {#define KEYLOCKER_CPUID_EBX_WIDE BIT(2)#define KEYLOCKER_CPUID_EBX_BACKUP BIT(4) +#ifdef CONFIG_X86_KEYLOCKER +void setup_keylocker (void ); +void restore_keylocker (void ); +extern bool valid_keylocker (void ); +#else +static inline void setup_keylocker (void ) { } +static inline void restore_keylocker (void ) { } +static inline bool valid_keylocker (void ) {return false; } +#endif +#endif /*__ASSEMBLY__ */ #endif /* _ASM_KEYLOCKER_H */
arch/x86/kernel/cpu/common.c +3 -1
@@ -62,6 +62,7 @@#include <asm/intel-family.h> #include <asm/cpu_device_id.h> #include <asm/fred.h> +#include <asm/keylocker.h> #include <asm/uv/uv.h> #include <asm/ia32.h> #include <asm/set_memory.h>
@@ -1834,10 +1835,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)/* Disable the PN if appropriate */ squash_the_stupid_serial_number (c); -/* Set up SMEP/SMAP/UMIP */ +/* Setup various Intel-specific CPU security features */ setup_smep (c);setup_smap (c);setup_umip (c); +setup_keylocker ();/* Enable FSGSBASE instructions if available. */ if (cpu_has (c,X86_FEATURE_FSGSBASE )) {
arch/x86/kernel/keylocker.c +89 -0
118 lines not on this page · raw message
arch/x86/power/cpu.c +2 -0
@@ -27,6 +27,7 @@#include <asm/mmu_context.h> #include <asm/cpu_device_id.h> #include <asm/microcode.h> +#include <asm/keylocker.h> #ifdef CONFIG_X86_32 __visible unsigned long saved_context_ebx;
@@ -264,6 +265,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) x86_platform.restore_sched_clock_state ();cache_bp_restore ();perf_restore_debug_store (); +restore_keylocker (); c = &cpu_data (smp_processor_id ());if (cpu_has (c,X86_FEATURE_MSR_IA32_FEAT_CTL ))
Signature · 1 line
2.34.1
PATCH v9 9/14 x86/hotplug/keylocker: Ensure wrapping key backup capability
In reply to Chang S. BaeTo facilitate CPU hotplug, the wrapping key needs to be loaded during CPU hotplug bringup. setup_keylocker() already establishes the routine for the wakeup path by copying the key from the backup state.
Disable the feature if it's missing with CONFIG_HOTPLUG_CPU=y. Also, update the code comment to indicate support for CPU hotplug.
---
Changes from v8:
* Add as a new patch.
--- arch/x86/kernel/keylocker.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kernel/keylocker.c +5 -3
@@ -69,6 +69,8 @@ static bool copy_keylocker(void)* On wakeup, APs copy a wrapping key after the boot CPU verifies a valid * backup status through restore_keylocker(). Subsequently, they adhere * to the error handling protocol by invalidating the flag. +* +* This setup routine is also invoked in the hotplug bringup path. */ void setup_keylocker (void ) {
@@ -135,11 +137,11 @@ static int __init init_keylocker(void)/* * The backup is critical for restoring the wrapping key upon -* wakeup. +* wakeup or during hotplug bringup. */ backup_available = !!(ebx &KEYLOCKER_CPUID_EBX_BACKUP ); -if (!backup_available &&IS_ENABLED (CONFIG_SUSPEND )) { -pr_debug ("x86/keylocker: No key backup with possible S3/4.\n" ); +if (!backup_available && (IS_ENABLED (CONFIG_SUSPEND ) ||IS_ENABLED (CONFIG_HOTPLUG_CPU ))) { +pr_debug ("x86/keylocker: No key backup with possible S3/4 or CPU hotplug.\n" ); goto clear_cap; }
Signature · 1 line
2.34.1
PATCH v9 10/14 x86/cpu/keylocker: Check Gather Data Sampling mitigation
In reply to Chang S. BaeGather Data Sampling is a transient execution side channel issue in some CPU models. The stale data in registers is not guaranteed as secure when this vulnerability is not addressed.
In the Key Locker usage during AES transformations, the temporary storage of the original key in registers poses a risk. The key material can be staled in some implementations, leading to susceptibility to leakage of the AES key.
To mitigate this vulnerability, a qualified microcode image must be applied. Software then verifies the mitigation state using MSRs. Add code to ensure that the mitigation is installed and securely locked. Disable the feature, otherwise.
---
Changes from v8:
* Add as a new patch.
Note that the code follows the guidance from [1]:
"Intel recommends that system software does not enable Key Locker (by
setting CR4.KL) unless the GDS mitigation is enabled (IA32_MCU_OPT_CTRL[GDS_MITG_DIS] (bit 4) is 0) and locked (IA32_MCU_OPT_CTRL [GDS_MITG_LOCK](bit 5) is 1)."
--- arch/x86/kernel/keylocker.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
arch/x86/kernel/keylocker.c +35 -0
@@ -7,6 +7,7 @@#include <linux/random.h> #include <linux/string.h> +#include <asm/cpu.h> #include <asm/fpu/api.h> #include <asm/keylocker.h> #include <asm/msr.h>
@@ -112,6 +113,37 @@ void restore_keylocker(void) valid_wrapping_key = false; } +/* +* The mitigation is implemented at a microcode level. Ensure that the +* microcode update is applied and the mitigation is locked. +*/ +static bool __init have_gds_mitigation (void ) +{ +u64 mcu_ctrl; + +/* GDS_CTRL is set if new microcode is loaded. */ +if (!(x86_read_arch_cap_msr () &ARCH_CAP_GDS_CTRL )) + goto vulnerable; + +/* If GDS_MITG_LOCKED is set, GDS_MITG_DIS is forced to 0. */ +rdmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl); +if (mcu_ctrl &GDS_MITG_LOCKED ) +return true; + +vulnerable: +pr_warn ("x86/keylocker: Susceptible to the GDS vulnerability.\n" ); +return false; +} + +/* Check if Key Locker is secure enough to be used. */ +static bool __init secure_keylocker (void ) +{ +if (boot_cpu_has_bug (X86_BUG_GDS ) && !have_gds_mitigation ()) +return false; + +return true; +} +static int __init init_keylocker (void ) {u32 eax, ebx, ecx, edx;
@@ -125,6 +157,9 @@ static int __init init_keylocker(void) goto clear_cap; } +if (!secure_keylocker ()) + goto clear_cap; +cr4_set_bits (X86_CR4_KEYLOCKER );/* AESKLE depends on CR4.KEYLOCKER */
Signature · 1 line
2.34.1
Pawan Gupta
reviewerRe: [PATCH v9 10/14] x86/cpu/keylocker: Check Gather Data Sampling mitigation
Quoted 6 lines · Chang S. Bae
arch/x86/kernel/keylocker.c +6
-0+/* +* The mitigation is implemented at a microcode level. Ensure that the +* microcode update is applied and the mitigation is locked. +*/ +static bool __init have_gds_mitigation (void ) +{
arch/x86/kernel/keylocker.c void restore_keylocker(void) · line 131
Quoted 10 lines
arch/x86/kernel/keylocker.c +10
-0+u64 mcu_ctrl; + +/* GDS_CTRL is set if new microcode is loaded. */ +if (!(x86_read_arch_cap_msr () &ARCH_CAP_GDS_CTRL )) + goto vulnerable; + +/* If GDS_MITG_LOCKED is set, GDS_MITG_DIS is forced to 0. */ +rdmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl); +if (mcu_ctrl &GDS_MITG_LOCKED ) +return true;
Similar to RFDS, above checks can be simplified to:
if (gds_mitigation == GDS_MITIGATION_FULL_LOCKED) return true;
Quoted 5 lines
arch/x86/kernel/keylocker.c +5
-0+ +vulnerable: +pr_warn ("x86/keylocker: Susceptible to the GDS vulnerability.\n" ); +return false; +}
PATCH v9 10/14 x86/cpu/keylocker: Check Gather Data Sampling mitigation
Gather Data Sampling is a transient execution side channel issue in some CPU models. The stale data in registers is not guaranteed as secure when this vulnerability is not addressed.
In the Key Locker usage during AES transformations, the temporary storage of the original key in registers poses a risk. The key material can be staled in some implementations, leading to susceptibility to leakage of the AES key.
To mitigate this vulnerability, a qualified microcode image must be applied. Add code to ensure that the mitigation is installed and securely locked. Disable the feature, otherwise.
Expand gds_ucode_mitigated() to examine the lock state.
---
Changes from v9:
* Removed MSR reads and utilized the helper function. (Pawan Gupta)
Alternatively, 'gds_mitigation' can be exported and referenced directly. Using 'gds_mitigation == GDS_MITIGATION_FULL_LOCKED' may also be readable. However, it was opted to expand gds_ucode_mitigated() for consistency, as it is already established.
Note that this approach aligns with Intel's guidance, as the bugs.c code checks the following MSR bits:
"Intel recommends that system software does not enable Key Locker (by
setting CR4.KL) unless the GDS mitigation is enabled (IA32_MCU_OPT_CTRL[GDS_MITG_DIS] (bit 4) is 0) and locked (IA32_MCU_OPT_CTRL [GDS_MITG_LOCK](bit 5) is 1)."
For more information, refer to Intel's technical documentation on Gather Data Sampling:
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/gather-data-sampling.html
--- arch/x86/include/asm/processor.h | 7 ++++++- arch/x86/kernel/cpu/bugs.c | 5 ++++- arch/x86/kernel/keylocker.c | 12 ++++++++++++ arch/x86/kvm/x86.c | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-)
arch/x86/include/asm/processor.h +6 -1
@@ -721,7 +721,12 @@ enum mds_mitigations { MDS_MITIGATION_VMWERV, }; -extern bool gds_ucode_mitigated (void ); +enum mitigation_info { +MITG_FULL , +MITG_LOCKED , +}; + +extern bool gds_ucode_mitigated (enum mitigation_info mitg);/* * Make previous memory operations globally visible before
arch/x86/kernel/cpu/bugs.c +4 -1
@@ -752,8 +752,11 @@ static const char * const gds_strings[] = { [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status", }; -bool gds_ucode_mitigated (void ) +bool gds_ucode_mitigated (enum mitigation_info mitg) { +if (mitg ==MITG_LOCKED ) +return gds_mitigation ==GDS_MITIGATION_FULL_LOCKED ; +return (gds_mitigation ==GDS_MITIGATION_FULL || gds_mitigation ==GDS_MITIGATION_FULL_LOCKED ); }
arch/x86/kernel/keylocker.c +12 -0
@@ -113,6 +113,15 @@ void restore_keylocker(void) valid_wrapping_key = false; } +/* Check if Key Locker is secure enough to be used. */ +static bool __init secure_keylocker (void ) +{ +if (boot_cpu_has_bug (X86_BUG_GDS ) && !gds_ucode_mitigated (MITG_LOCKED )) +return false; + +return true; +} +static int __init init_keylocker (void ) {u32 eax, ebx, ecx, edx;
@@ -126,6 +135,9 @@ static int __init init_keylocker(void) goto clear_cap; } +if (!secure_keylocker ()) + goto clear_cap; +cr4_set_bits (X86_CR4_KEYLOCKER );/* AESKLE depends on CR4.KEYLOCKER */
arch/x86/kvm/x86.c +1 -1
@@ -1675,7 +1675,7 @@ static u64 kvm_get_arch_capabilities(void)*/ } -if (!boot_cpu_has_bug (X86_BUG_GDS ) ||gds_ucode_mitigated ()) +if (!boot_cpu_has_bug (X86_BUG_GDS ) ||gds_ucode_mitigated (MITG_FULL )) data |=ARCH_CAP_GDS_NO ;return data;
Signature · 1 line
2.40.1
Pawan Gupta
reviewerRe: [PATCH v9a 10/14] x86/cpu/keylocker: Check Gather Data Sampling mitigation
Quoted 114 lines · Chang S. Bae
Gather Data Sampling is a transient execution side channel issue in some CPU models. The stale data in registers is not guaranteed as secure when this vulnerability is not addressed.
In the Key Locker usage during AES transformations, the temporary storage of the original key in registers poses a risk. The key material can be staled in some implementations, leading to susceptibility to leakage of the AES key.
To mitigate this vulnerability, a qualified microcode image must be applied. Add code to ensure that the mitigation is installed and securely locked. Disable the feature, otherwise.
Expand
gds_ucode_mitigated()to examine the lock state.Signed-off-by Chang S. Bae <[email protected]>Cc Dave Hansen <[email protected]>Cc Pawan Gupta <[email protected]>---
Changes from v9:
* Removed MSR reads and utilized the helper function. (Pawan Gupta)Alternatively, '
gds_mitigation' can be exported and referenced directly. Using 'gds_mitigation==GDS_MITIGATION_FULL_LOCKED' may also be readable. However, it was opted to expandgds_ucode_mitigated()for consistency, as it is already established.Note that this approach aligns with Intel's guidance, as the
bugs.ccode checks the following MSR bits:
"Intel recommends that system software does not enable Key Locker (by
setting CR4.KL) unless the GDS mitigation is enabled (IA32_MCU_OPT_CTRL[GDS_MITG_DIS] (bit 4) is 0) and locked (IA32_MCU_OPT_CTRL[GDS_MITG_LOCK](bit 5) is 1)."For more information, refer to Intel's technical documentation on Gather Data Sampling:
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/gather-data-sampling.html--- arch/x86/include/asm/processor.h | 7 ++++++- arch/x86/kernel/cpu/bugs.c | 5 ++++- arch/x86/kernel/keylocker.c | 12 ++++++++++++ arch/x86/kvm/x86.c | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-)arch/x86/include/asm/processor.h +6
-1@@ -721,7 +721,12 @@ enum mds_mitigations { MDS_MITIGATION_VMWERV, }; -extern bool gds_ucode_mitigated (void ); +enum mitigation_info { +MITG_FULL , +MITG_LOCKED , +}; + +extern bool gds_ucode_mitigated (enum mitigation_info mitg);/* * Make previous memory operations globally visible before arch/x86/kernel/cpu/bugs.c +4
-1@@ -752,8 +752,11 @@ static const char * const gds_strings[] = { [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status", }; -bool gds_ucode_mitigated (void ) +bool gds_ucode_mitigated (enum mitigation_info mitg) { +if (mitg ==MITG_LOCKED ) +return gds_mitigation ==GDS_MITIGATION_FULL_LOCKED ; +return (gds_mitigation ==GDS_MITIGATION_FULL || gds_mitigation ==GDS_MITIGATION_FULL_LOCKED ); }arch/x86/kernel/keylocker.c +12
-0@@ -113,6 +113,15 @@ void restore_keylocker(void) valid_wrapping_key = false; } +/* Check if Key Locker is secure enough to be used. */ +static bool __init secure_keylocker (void ) +{ +if (boot_cpu_has_bug (X86_BUG_GDS ) && !gds_ucode_mitigated (MITG_LOCKED )) +return false; + +return true; +} +static int __init init_keylocker (void ) {u32 eax, ebx, ecx, edx;@@ -126,6 +135,9 @@ static int __init init_keylocker(void) goto clear_cap; } +if (!secure_keylocker ()) + goto clear_cap; +cr4_set_bits (X86_CR4_KEYLOCKER );/* AESKLE depends on CR4.KEYLOCKER */ arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static u64 kvm_get_arch_capabilities(void) · line 1681
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -1675,7 +1675,7 @@ static u64 kvm_get_arch_capabilities(void)*/ } -if (!boot_cpu_has_bug (X86_BUG_GDS ) ||gds_ucode_mitigated ()) +if (!boot_cpu_has_bug (X86_BUG_GDS ) ||gds_ucode_mitigated (MITG_FULL )) data |=ARCH_CAP_GDS_NO ;return data;
Repurposing gds_ucode_mitigated() to check for the locked state is adding a bit of a churn. We can introduce gds_mitigation_locked() instead.
Is below looking okay:
arch/x86/include/asm/processor.h +1 -0
@@ -722,6 +722,7 @@ enum mds_mitigations { };extern bool gds_ucode_mitigated (void ); +extern bool gds_mitigation_locked (void );/* * Make previous memory operations globally visible before
arch/x86/kernel/cpu/bugs.c +5 -0
@@ -753,6 +753,11 @@ bool gds_ucode_mitigated(void) }EXPORT_SYMBOL_GPL (gds_ucode_mitigated); +bool gds_mitigation_locked (void ) +{ +return gds_mitigation ==GDS_MITIGATION_FULL_LOCKED ; +} +void update_gds_msr (void ) {u64 mcu_ctrl_after;
arch/x86/kernel/keylocker.c +12 -0
@@ -112,6 +112,15 @@ void restore_keylocker(void) valid_wrapping_key = false; } +/* Check if Key Locker is secure enough to be used. */ +static bool __init secure_keylocker (void ) +{ +if (boot_cpu_has_bug (X86_BUG_GDS ) && !gds_mitigation_locked ()) +return false; + +return true; +} +static int __init init_keylocker (void ) {u32 eax, ebx, ecx, edx;
@@ -125,6 +134,9 @@ static int __init init_keylocker(void) goto clear_cap; } +if (!secure_keylocker ()) + goto clear_cap; +cr4_set_bits (X86_CR4_KEYLOCKER );/* AESKLE depends on CR4.KEYLOCKER */
Re: [PATCH v9a 10/14] x86/cpu/keylocker: Check Gather Data Sampling mitigation
Quoted 4 lines · Pawan Gupta
Repurposing
gds_ucode_mitigated()to check for the locked state is adding a bit of a churn. We can introducegds_mitigation_locked()instead.
I thought this option but I was less convinced about adding a new function for every new but slightly different check.
Thanks,
Chang
Pawan Gupta
reviewer[PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
In reply to Chang S. BaeIn order to safely enable Intel Keylocker feature, Gather Data Sampling (GDS) mitigation should be enabled and locked. Hardware provides a way to lock the mitigation, such that the mitigation cannot be disabled until the CPU is reset. Currently, GDS mitigation is enabled without the lock.
Below is the recommendation from Intel:
"Intel recommends that system software does not enable Key Locker (by setting CR4.KL) unless the GDS mitigation is enabled (IA32_MCU_OPT_CTRL [GDS_MITG_DIS] (bit 4) is 0) and locked (IA32_MCU_OPT_CTRL [GDS_MITG_LOCK](bit 5) is 1). This will prevent an adversary that takes control of the system from turning off the mitigation in order to infer the keys behind Key Locker handles." [1]
When GDS mitigation is enabled, and Keylocker feature is present, also lock the mitigation.
[1] Gather Data Sampling (ID# 785676)
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/gather-data-sampling.html
---
This should ideally go before the patch that enables Keylocker. It is only compile tested.
arch/x86/kernel/cpu/bugs.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/bugs.c +10 -2
@@ -755,8 +755,8 @@ EXPORT_SYMBOL_GPL(gds_ucode_mitigated);void update_gds_msr (void ) { -u64 mcu_ctrl_after; -u64 mcu_ctrl; +u64 mcu_ctrl, mcu_ctrl_after; +u64 gds_lock =0 ;switch (gds_mitigation) {case GDS_MITIGATION_OFF :
@@ -769,6 +769,8 @@ void update_gds_msr(void)* the same state. Make sure the mitigation is enabled on all * CPUs. */ + gds_lock =GDS_MITG_LOCKED ; + fallthrough;case GDS_MITIGATION_FULL :rdmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl); mcu_ctrl &= ~GDS_MITG_DIS ;
@@ -779,6 +781,7 @@ void update_gds_msr(void)return ; } + mcu_ctrl |= gds_lock;wrmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl);/*
@@ -840,6 +843,11 @@ static void __init gds_select_mitigation(void) gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; } +/* Keylocker can only be enabled when GDS mitigation is locked */ +if (boot_cpu_has (X86_FEATURE_KEYLOCKER ) && + gds_mitigation ==GDS_MITIGATION_FULL ) + gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; +update_gds_msr (); out:pr_info ("%s\n" , gds_strings[gds_mitigation]);
---
base-commit: 0bbac3facb5d6cc0171c45c9873a2dc96bea9680
change-id: 20240418-gds-lock-26ecbce88470
Best regards,
Signature · 2 lines
Thanks, Pawan
Re: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
arch/x86/kernel/cpu/bugs.c void update_gds_msr(void) · line 772
Quoted 9 lines · Pawan Gupta
arch/x86/kernel/cpu/bugs.c +2
-0+u64 gds_lock =0 ;switch (gds_mitigation) {case GDS_MITIGATION_OFF :@@ -769,6 +769,8 @@ void update_gds_msr(void)* the same state. Make sure the mitigation is enabled on all * CPUs. */ + gds_lock =GDS_MITG_LOCKED ;
Can't we just drop the new gds_lock var and set mcu_ctrl |= GDS_MITG_LOCKED here?
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +2
-0+ fallthrough;case GDS_MITIGATION_FULL :rdmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl); mcu_ctrl &= ~GDS_MITG_DIS ;@@ -779,6 +781,7 @@ void update_gds_msr(void)return ; } + mcu_ctrl |= gds_lock;wrmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl);
Pawan Gupta
reviewerRe: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
Quoted 11 lines · Daniel Sneddon
arch/x86/kernel/cpu/bugs.c void update_gds_msr(void) · line 772
Quoted 9 lines · Pawan Gupta
arch/x86/kernel/cpu/bugs.c +2
-0+u64 gds_lock =0 ;switch (gds_mitigation) {case GDS_MITIGATION_OFF :@@ -769,6 +769,8 @@ void update_gds_msr(void)* the same state. Make sure the mitigation is enabled on all * CPUs. */ + gds_lock =GDS_MITG_LOCKED ;Can't we just drop the new
gds_lockvar and setmcu_ctrl|=GDS_MITG_LOCKEDhere?
Unfortunately no, because ...
Quoted 3 lines
arch/x86/kernel/cpu/bugs.c void update_gds_msr(void) · line 775
Quoted 3 lines
arch/x86/kernel/cpu/bugs.c +1
-0+ fallthrough;case GDS_MITIGATION_FULL :rdmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl);
... mcu_ctrl is read here, it will overwrite any previous value.
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +1
-0mcu_ctrl &= ~GDS_MITG_DIS ;@@ -779,6 +781,7 @@ void update_gds_msr(void)return ; } + mcu_ctrl |= gds_lock;wrmsrl (MSR_IA32_MCU_OPT_CTRL , mcu_ctrl);
Re: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
Quoted 1 line · Pawan Gupta
...
mcu_ctrlis read here, it will overwrite any previous value.Ah, yep. Bummer.
Re: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
In reply to Pawan Guptaarch/x86/kernel/cpu/bugs.c static void __init gds_select_mitigation(void) · line 850
Quoted 11 lines · Pawan Gupta
/*
arch/x86/kernel/cpu/bugs.c +5
-0@@ -840,6 +843,11 @@ static void __init gds_select_mitigation(void) gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; } +/* Keylocker can only be enabled when GDS mitigation is locked */ +if (boot_cpu_has (X86_FEATURE_KEYLOCKER ) && + gds_mitigation ==GDS_MITIGATION_FULL ) + gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; +
I'm having trouble understanding this change:
gds_select_mitigation()
{
...
if (gds_mitigation == GDS_MITIGATION_FORCE)
gds_mitigation = GDS_MITIGATION_FULL;
rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
if (mcu_ctrl & GDS_MITG_LOCKED) {
...
gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
}
if (boot_cpu_has(X86_FEATURE_KEYLOCKER) &&
gds_mitigation == GDS_MITIGATION_FULL)
gds_mitigation = GDS_MITIGATION_FULL_LOCKED;As I understand it, gds_mitigation is set to GDS_MITIGATION_FULL only if the gds force option is enabled but IA32_MCU_OPT_CTRL[GDS_MITG_LOCK] is not set.
Then, if the CPU has Key Locker, this code sets gds_mitigation to GDS_MITIGATION_FULL_LOCKED, which seems contradictory. I'm not sure why this change is necessary.
I'm also not convinced that the Key Locker series needs to modify this function. The Key Locker setup code should simply check the current mitigation status and enable the feature only if proper mitigation is in place. Am I missing something here?
Thanks,
Chang
Pawan Gupta
reviewerRe: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
Quoted 29 lines · Chang S. Bae
arch/x86/kernel/cpu/bugs.c static void __init gds_select_mitigation(void) · line 849
Quoted 9 lines · Pawan Gupta
/*
arch/x86/kernel/cpu/bugs.c +5
-0@@ -840,6 +843,11 @@ static void __init gds_select_mitigation(void) gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; } +/* Keylocker can only be enabled when GDS mitigation is locked */ +if (boot_cpu_has (X86_FEATURE_KEYLOCKER ) && + gds_mitigation ==GDS_MITIGATION_FULL ) + gds_mitigation =GDS_MITIGATION_FULL_LOCKED ; +I'm having trouble understanding this change:
gds_select_mitigation(){ ... if (gds_mitigation == GDS_MITIGATION_FORCE) gds_mitigation = GDS_MITIGATION_FULL; rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl); if (mcu_ctrl & GDS_MITG_LOCKED) { ... gds_mitigation = GDS_MITIGATION_FULL_LOCKED; } if (boot_cpu_has(X86_FEATURE_KEYLOCKER) && gds_mitigation == GDS_MITIGATION_FULL) gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
Quoted 2 lines
As I understand it,
gds_mitigationis set toGDS_MITIGATION_FULLonly if the gds force option is enabled but IA32_MCU_OPT_CTRL[GDS_MITG_LOCK] is not set.
Not true, GDS_MITIGATION_FULL is the default. Cmdline gather_data_sampling=force deploys a software fallback mitigation when the microcode mitigation is not present. But, when microcode mitigation is present, mitigation is set to GDS_MITIGATION_FULL.
Quoted 8 lines
Then, if the CPU has Key Locker, this code sets
gds_mitigationtoGDS_MITIGATION_FULL_LOCKED, which seems contradictory. I'm not sure why this change is necessary.I'm also not convinced that the Key Locker series needs to modify this function. The Key Locker setup code should simply check the current mitigation status and enable the feature only if proper mitigation is in place. Am I missing something here?
To enable Key Locker feature, "proper mitigation" is microcode mitigation enabled and the GDS_MITG_LOCK bit set in MSR_IA32_MCU_OPT_CTRL. Do you agree?
If not via this patch, how is GDS_MITG_LOCK going to be set?
Below is from Intel's documentation:
"Intel recommends that system software does not enable Key Locker (by setting CR4.KL) unless the GDS mitigation is enabled (IA32_MCU_OPT_CTRL [GDS_MITG_DIS] (bit 4) is 0) and locked (IA32_MCU_OPT_CTRL [GDS_MITG_LOCK](bit 5) is 1). This will prevent an adversary that takes control of the system from turning off the mitigation in order to infer the keys behind Key Locker handles.
To support GDS mitigation locking for Key Locker, microcode updates for Tiger Lake systems enable the following model-specific behavior for GDS_MITG_LOCK. On these systems, a write to IA32_MCU_OPT_CTRL MSR with GDS_MITG_DIS (bit 4) value 0 and GDS_MITG_LOCK (bit 5) value 1 will lock both bits at these values until reset."
Re: [PATCH 15/14] x86/gds: Lock GDS mitigation when keylocker feature is present
Quoted 5 lines · Pawan Gupta
To enable Key Locker feature, "proper mitigation" is microcode mitigation enabled and the
GDS_MITG_LOCKbit set inMSR_IA32_MCU_OPT_CTRL. Do you agree?Quoted 1 line
If not via this patch, how is
GDS_MITG_LOCKgoing to be set?
The lock bit seems to be set by microcode when SGX is available. However, if the lock bit is not set for Key Locker, it does seem odd. Introducing kernel code to override this situation might be seen as a workaround rather than a proper solution, potentially leading to more confusion.
I'd rather investigate the behavior of the microcode further, verify its consistency, and gain a clearer understanding of the requirement for this lock bit.
Thanks,
Chang
PATCH v9 11/14 x86/cpu/keylocker: Check Register File Data Sampling mitigation
In reply to Chang S. BaeThe Register File Data Sampling vulnerability may allow malicious userspace programs to infer stale kernel register data, potentially exposing sensitive key values, including AES keys.
To address this vulnerability, a microcode update needs to be applied to the CPU, which modifies the VERW instruction to flush the affected CPU buffers.
The kernel already has a facility to flush CPU buffers before returning to userspace, which is indicated by the X86_FEATURE_CLEAR_CPU_BUF flag.
Ensure the mitigation before enabling Key Locker. Do not enable the feature on CPUs affected by the vulnerability but lacks mitigation.
---
Change from v8:
* Add as a new patch.
Note that the code change follows the mitigation guidance [1]:
"Software loading Key Locker keys using LOADIWKEY should execute a VERW
to clear registers before transitioning to untrusted code to prevent later software from inferring the loaded key."
--- arch/x86/kernel/keylocker.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
arch/x86/kernel/keylocker.c +17 -0
@@ -135,12 +135,29 @@ static bool __init have_gds_mitigation(void)return false; } +/* +* IA32_ARCH_CAPABILITIES MSR is retrieved during the setting of +* X86_BUG_RFDS. Ensure that the mitigation is applied to flush CPU +* buffers by checking the flag. +*/ +static bool __init have_rfds_mitigation (void ) +{ +if (boot_cpu_has (X86_FEATURE_CLEAR_CPU_BUF )) +return true; + +pr_warn ("x86/keylocker: Susceptible to the RFDS vulnerability.\n" ); +return false; +} +/* Check if Key Locker is secure enough to be used. */ static bool __init secure_keylocker (void ) {if (boot_cpu_has_bug (X86_BUG_GDS ) && !have_gds_mitigation ())return false; +if (boot_cpu_has_bug (X86_BUG_RFDS ) && !have_rfds_mitigation ()) +return false; +return true; }
Signature · 1 line
2.34.1
Pawan Gupta
reviewerRe: [PATCH v9 11/14] x86/cpu/keylocker: Check Register File Data Sampling mitigation
Quoted 38 lines · Chang S. Bae
The Register File Data Sampling vulnerability may allow malicious userspace programs to infer stale kernel register data, potentially exposing sensitive key values, including AES keys.
To address this vulnerability, a microcode update needs to be applied to the CPU, which modifies the VERW instruction to flush the affected CPU buffers.
The kernel already has a facility to flush CPU buffers before returning to userspace, which is indicated by the
X86_FEATURE_CLEAR_CPU_BUFflag.Ensure the mitigation before enabling Key Locker. Do not enable the feature on CPUs affected by the vulnerability but lacks mitigation.
Signed-off-by Chang S. Bae <[email protected]>Cc Dave Hansen <[email protected]>Cc Pawan Gupta <[email protected]>---
Change from v8:
* Add as a new patch.Note that the code change follows the mitigation guidance [1]:
"Software loading Key Locker keys using LOADIWKEY should execute a VERW
to clear registers before transitioning to untrusted code to prevent later software from inferring the loaded key."--- arch/x86/kernel/keylocker.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)arch/x86/kernel/keylocker.c +0
-0@@ -135,12 +135,29 @@ static bool __init have_gds_mitigation(void)return false; }
arch/x86/kernel/keylocker.c static bool __init have_gds_mitigation(void) · line 146
Quoted 10 lines
arch/x86/kernel/keylocker.c +9
-0+/* +* IA32_ARCH_CAPABILITIES MSR is retrieved during the setting of +* X86_BUG_RFDS. Ensure that the mitigation is applied to flush CPU +* buffers by checking the flag. +*/ +static bool __init have_rfds_mitigation (void ) +{ +if (boot_cpu_has (X86_FEATURE_CLEAR_CPU_BUF )) +return true;
X86_FEATURE_CLEAR_CPU_BUF is also set by other VERW based mitigations like MDS. The feature flag does not guarantee that the microcode required to mitigate RFDS is loaded.
A more robust check would be:
if (rfds_mitigation == RFDS_MITIGATION_VERW) return true;
And it would be apt to move this function to arch/x86/kernel/cpu/bugs.c
Quoted 16 lines
arch/x86/kernel/keylocker.c +8
-0+ +pr_warn ("x86/keylocker: Susceptible to the RFDS vulnerability.\n" ); +return false; +} +/* Check if Key Locker is secure enough to be used. */ static bool __init secure_keylocker (void ) {if (boot_cpu_has_bug (X86_BUG_GDS ) && !have_gds_mitigation ())return false; +if (boot_cpu_has_bug (X86_BUG_RFDS ) && !have_rfds_mitigation ()) +return false; +return true; }
PATCH v9 11/14 x86/cpu/keylocker: Check Register File Data Sampling mitigation
The Register File Data Sampling vulnerability may allow malicious userspace programs to infer stale kernel register data, potentially exposing sensitive key values, including AES keys.
To address this vulnerability, a microcode update needs to be applied to the CPU, which modifies the VERW instruction to flush the affected CPU buffers.
Reference the 'rfds_mitigation' variable to check the mitigation status. Do not enable Key Locker on CPUs affected by the vulnerability but lacking mitigation.
---
Changes from v9:
* Remove the helper function and simplify the code by directly reading the status variable. (Pawan Gupta)
Note that this code change aligns with mitigation guidance, which recommends:
"Software loading Key Locker keys using LOADIWKEY should execute a VERW
to clear registers before transitioning to untrusted code to prevent later software from inferring the loaded key."
For more information, refer to Intel's guidance on Register File Data Sampling:
https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/advisory-guidance/register-file-data-sampling.html
--- arch/x86/include/asm/processor.h | 8 ++++++++ arch/x86/kernel/cpu/bugs.c | 8 +------- arch/x86/kernel/keylocker.c | 3 +++ 3 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/include/asm/processor.h +8 -0
@@ -728,6 +728,14 @@ enum mitigation_info {extern bool gds_ucode_mitigated (enum mitigation_info mitg); +enum rfds_mitigations { +RFDS_MITIGATION_OFF , +RFDS_MITIGATION_VERW , +RFDS_MITIGATION_UCODE_NEEDED , +}; + +extern enum rfds_mitigations rfds_mitigation; +/* * Make previous memory operations globally visible before * a WRMSR.
arch/x86/kernel/cpu/bugs.c +1 -7
@@ -483,14 +483,8 @@ early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);#undef pr_fmt#define pr_fmt (fmt) "Register File Data Sampling: " fmt -enum rfds_mitigations { -RFDS_MITIGATION_OFF , -RFDS_MITIGATION_VERW , -RFDS_MITIGATION_UCODE_NEEDED , -}; -/* Default mitigation for Register File Data Sampling */ -static enum rfds_mitigations rfds_mitigation__ro_after_init = +enum rfds_mitigations rfds_mitigation__ro_after_init =IS_ENABLED (CONFIG_MITIGATION_RFDS ) ?RFDS_MITIGATION_VERW :RFDS_MITIGATION_OFF ;static const char *const rfds_strings[] = {
arch/x86/kernel/keylocker.c +3 -0
@@ -119,6 +119,9 @@ static bool __init secure_keylocker(void)if (boot_cpu_has_bug (X86_BUG_GDS ) && !gds_ucode_mitigated (MITG_LOCKED ))return false; +if (boot_cpu_has_bug (X86_BUG_RFDS ) && rfds_mitigation !=RFDS_MITIGATION_VERW ) +return false; +return true; }
Signature · 1 line
2.40.1
PATCH v9 12/14 x86/Kconfig: Add a configuration for Key Locker
In reply to Chang S. BaeAdd CONFIG_X86_KEYLOCKER to gate whether Key Locker is initialized at boot. The option is selected by the Key Locker cipher module CRYPTO_AES_KL (to be added in a later patch).
---
Changes from v8:
* Drop the "nokeylocker" option. (Borislav Petkov)
Changes from v6:
* Rebase on the upstream: commit a894a8a56b57 ("Documentation:
kernel-parameters: sort all "no..." parameters")
Changes from RFC v2:
* Make the option selected by CRYPTO_AES_KL. (Dan Williams)
* Massage the changelog and the config option description.
--- arch/x86/Kconfig | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/Kconfig +3 -0
@@ -1878,6 +1878,9 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config X86_KEYLOCKER + bool + choice prompt "TSX enable mode" depends on CPU_SUP_INTEL
Signature · 1 line
2.34.1