Skip to contentSkip to conversation

x86: Support Intel Key Locker

RFC PATCH Superseded Chang S. Bae 6 participants 28 messages May 14, 2021 – Dec 6, 2021

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86: Support Intel Key Locker

Key Locker [1][2] is a new security feature available in new Intel CPUs to protect data encryption keys for the Advanced Encryption Standard algorithm. The protection limits the amount of time an AES key is exposed in memory by sealing a key and referencing it with new AES instructions.

The new AES instruction set is a successor of Intel's AES-NI (AES New Instruction). Users may switch to the Key Locker version from crypto libraries. This series includes a new AES implementation for the Crypto API, which was validated through the crypto unit tests. The performance in the test cases was measured and found comparable to the AES-NI version.

Key Locker introduces a (CPU-)internal key to encode AES keys. The kernel needs to load it and ensure it unchanged as long as CPUs are operational.

The series has three parts:
* PATCH1-7: Implement the internal key management
* PATCH8-10: Add a new AES implementation in the Crypto library
* PATCH11: Provide the hardware randomization option

Dan has asked for this to go out as another RFC to have more conversation before asking the maintainers to consider this implementation.

Changes from RFC v1 [3]:
* Refactored the AES-NI implementation and fix the AES-KL in the Crypto API. (Ard Biesheuvel)
* Revised the AES implementation description. (Dave Hansen and Peter Zijlsta).
* Noted the binutils version and made it prerequisite. (Peter Zijlstra and Borislav Petkov)
* Reorganized helper functions. With that, simplified the feature enablement check.
* Added to flush cache lines when removing key from memory.
* Separated the opcode map update into a new patch. Also, included AES instructions.
* Refactored the LOADIWKEY instruction in a new helper.
* Folded the backup error warning. (Rafael Wysocki)
* Massaged changelog accordingly.

[1] Intel Architecture Instruction Set Extensions Programming Reference:
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/architecture-instruction-set-extensions-programming-reference.pdf
[2] Intel Key Locker Specification:
https://software.intel.com/content/dam/develop/external/us/en/documents/343965-intel-key-locker-specification.pdf
[3] RFC v1: https://lore.kernel.org/lkml/[email protected]/

Chang S. Bae (11):
x86/cpufeature: Enumerate Key Locker feature
x86/insn: Add Key Locker instructions to the opcode map x86/cpu: Load Key Locker internal key at boot-time x86/msr-index: Add MSRs for Key Locker internal key x86/power: Restore Key Locker internal key from the ACPI S3/4 sleep
states
x86/cpu: Add a config option and a chicken bit for Key Locker selftests/x86: Test Key Locker internal key maintenance
crypto: x86/aes-ni - Improve error handling
crypto: x86/aes-ni - Refactor to prepare a new AES implementation
crypto: x86/aes-kl - Support AES algorithm using Key Locker
instructions
x86/cpu: Support the hardware randomization option for Key Locker
internal key

 .../admin-guide/kernel-parameters.txt         |    2 +
 arch/x86/Kconfig                              |   14 +
 arch/x86/crypto/Makefile                      |    5 +-
 arch/x86/crypto/aes-intel_asm.S               |   26 +
 arch/x86/crypto/aes-intel_glue.c              |  208 +++
 arch/x86/crypto/aes-intel_glue.h              |   61 +
 arch/x86/crypto/aeskl-intel_asm.S             | 1181 +++++++++++++++++
 arch/x86/crypto/aeskl-intel_glue.c            |  390 ++++++
 arch/x86/crypto/aesni-intel_asm.S             |   90 +-
 arch/x86/crypto/aesni-intel_glue.c            |  310 +----
 arch/x86/crypto/aesni-intel_glue.h            |   88 ++
 arch/x86/include/asm/cpufeatures.h            |    1 +
 arch/x86/include/asm/disabled-features.h      |    8 +-
 arch/x86/include/asm/keylocker.h              |   30 +
 arch/x86/include/asm/msr-index.h              |    6 +
 arch/x86/include/asm/special_insns.h          |   36 +
 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                   |  264 ++++
 arch/x86/kernel/smpboot.c                     |    2 +
 arch/x86/lib/x86-opcode-map.txt               |   11 +-
 arch/x86/power/cpu.c                          |    2 +
 crypto/Kconfig                                |   23 +
 drivers/char/random.c                         |    6 +
 include/linux/random.h                        |    2 +
 tools/arch/x86/lib/x86-opcode-map.txt         |   11 +-
 tools/testing/selftests/x86/Makefile          |    2 +-
 tools/testing/selftests/x86/keylocker.c       |  177 +++
 30 files changed, 2638 insertions(+), 343 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/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
 create mode 100644 tools/testing/selftests/x86/keylocker.c

base-commit: 6efb943b8616ec53a5e444193dccf1af9ad627b5 --
2.17.1

#1 lore raw

RFC PATCH v2 1/11 x86/cpufeature: Enumerate Key Locker feature

Intel's Key Locker is a new security feature providing a mechanism to protect data encryption keys when processing the Advanced Encryption Standard (AES) algorithm.

The feature accompanies new AES instructions as a successor to Intel's AES New Instructions (AES-NI). It also follows AES-NI's other feature dependency.

Here add it to enumerate the hardware capability, shown as 'keylocker' in /proc/cpuinfo.

Define the feature-specific CPUID leaf and bits for the feature enablement.

Add X86_FEATURE_KEYLOCKER to the disabled features mask for the use of compile-time configuration.

---
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/asm/keylocker.h            | 18 ++++++++++++++++++
 arch/x86/include/uapi/asm/processor-flags.h |  2 ++
 arch/x86/kernel/cpu/cpuid-deps.c            |  1 +
 5 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/include/asm/keylocker.h
arch/x86/include/asm/cpufeatures.h +1 -0
index ac37830ae941..578cf3fe7182 100644
@@ -359,6 +359,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
index b7dd944dc867..96acda147a50 100644
@@ -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
@@ -88,7 +94,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/asm/keylocker.h new file +18 -0
new file mode 100644
index 000000000000..0597d6b1cd05
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _ASM_KEYLOCKER_H
+#define _ASM_KEYLOCKER_H
+
+#ifndef __ASSEMBLY__
+
+#include <linux/bits.h>
+
+#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)
+#define KEYLOCKER_CPUID_ECX_RAND	BIT(1)
+
+#endif /*__ASSEMBLY__ */
+#endif /* _ASM_KEYLOCKER_H */
arch/x86/include/uapi/asm/processor-flags.h +2 -0
index bcba3c643e63..b958a95a0908 100644
@@ -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
index defda61f372d..ce2020b9ff4d 100644
@@ -75,6 +75,7 @@ static const struct cpuid_dep cpuid_deps[] = {
 	{ X86_FEATURE_SGX_LC,			X86_FEATURE_SGX	      },
 	{ X86_FEATURE_SGX1,			X86_FEATURE_SGX       },
 	{ X86_FEATURE_SGX2,			X86_FEATURE_SGX1      },
+	{ X86_FEATURE_KEYLOCKER,		X86_FEATURE_XMM2      },
 	{}
 };
 
Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH v2 2/11 x86/insn: Add Key Locker instructions to the opcode map

In reply to Chang S. Bae

Add 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
index ec31f5b60323..eb702fc6572e 100644
@@ -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
index ec31f5b60323..eb702fc6572e 100644
@@ -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
#3 lore raw

RFC PATCH v2 3/11 x86/cpu: Load Key Locker internal key at boot-time

In reply to Chang S. Bae

Internal (Wrapping) Key is a new entity of Intel Key Locker feature. This internal key is loaded in a software-inaccessible CPU state and used to encode a data encryption key.

The kernel makes random data and loads it as the internal key in each CPU. The data need to be flushed as soon as the load is done.

System firmware may disable the feature. Check the dynamic CPUID bit (KEYLOCKER_CPUID_EBX_AESKLE) at first.

---
Changes from RFC v1:
* Added to flush cache line when removing an internal key value. Also, renamed the helper.
* Reorganized the helper functions.
* Changed to reference boot CPU's feature flag consistently, and to update it if the feature is disabled.
* Refactored LOADIWKEY instruction in a new helper.
* Added function descriptions.

---
 arch/x86/include/asm/keylocker.h     |   9 ++
 arch/x86/include/asm/special_insns.h |  36 ++++++++
 arch/x86/kernel/Makefile             |   1 +
 arch/x86/kernel/cpu/common.c         |   5 +-
 arch/x86/kernel/keylocker.c          | 125 +++++++++++++++++++++++++++
 arch/x86/kernel/smpboot.c            |   2 +
 6 files changed, 177 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/kernel/keylocker.c
arch/x86/include/asm/keylocker.h +9 -0
index 0597d6b1cd05..870832f007ec 100644
@@ -5,6 +5,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <asm/processor.h>
 #include <linux/bits.h>
 
 #define KEYLOCKER_CPUID			0x019
@@ -14,5 +15,13 @@
 #define KEYLOCKER_CPUID_EBX_BACKUP	BIT(4)
 #define KEYLOCKER_CPUID_ECX_RAND	BIT(1)
 
+#ifdef CONFIG_X86_KEYLOCKER
+void setup_keylocker(struct cpuinfo_x86 *c);
+void flush_keylocker_data(void);
+#else
+#define setup_keylocker(c) do { } while (0)
+#define flush_keylocker_data() do { } while (0)
+#endif
+
 #endif /*__ASSEMBLY__ */
 #endif /* _ASM_KEYLOCKER_H */
arch/x86/include/asm/special_insns.h +36 -0
index 2acd6cb62328..0511b2d72ab8 100644
@@ -306,6 +306,42 @@ static inline int enqcmds(void __iomem *dst, const void *src)
 	return 0;
 }
 
+/**
+ * loadiwkey() - Load CPU-internal wrapping key
+ * @keyconf:	Indicate the key source and the key backup allowance.
+ *
+ * The key value is presumed in XMM0-2 registers. So make sure the states
+ * are available before calling this.
+ *
+ * If bit 0 of @keyconf is set, key backup is not permitted. If bit 1 is
+ * set, a hardware-randomized key is loaded.
+ *
+ * Returns:	-EAGAIN if hardware randomization fails; otherwise, 0.
+ *
+ */
+static inline int __must_check loadiwkey(u32 keyconf)
+{
+	int zf;
+
+	/*
+	 * LOADIWKEY %xmm1,%xmm2
+	 *
+	 * EAX and XMM0 are implicit operands. Load key value from XMM0-2
+	 * to software-invisible CPU state.
+	 *
+	 * This instruction is supported by binutils >= 2.36.
+	 */
+	asm volatile (".byte 0xf3,0x0f,0x38,0xdc,0xd1"
+		      CC_SET(z)
+		      : CC_OUT(z) (zf)
+		      : "a"(keyconf));
+
+	/* Hardware randomization failure is indicated via EFLAGS.ZF=1. */
+	if (zf)
+		return -EAGAIN;
+	return 0;
+}
+
 #endif /* __KERNEL__ */
 
 #endif /* _ASM_X86_SPECIAL_INSNS_H */
arch/x86/kernel/Makefile +1 -0
index 0704c2a94272..5662876e175b 100644
@@ -143,6 +143,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
index a1b756c49a93..2e64371acb81 100644
@@ -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 "cpu.h"
@@ -1555,10 +1557,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 +125 -0

125 lines not on this page · raw message

arch/x86/kernel/smpboot.c +2 -0
index 0ad5214f598a..0266d70a44be 100644
@@ -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>
@@ -1429,6 +1430,7 @@ void __init native_smp_cpus_done(unsigned int max_cpus)
 	nmi_selftest();
 	impress_friends();
 	mtrr_aps_init();
+	flush_keylocker_data();
 }
 
 static int __initdata setup_possible_cpus = -1;
Signature · 1 line
2.17.1
#4 lore raw

RFC PATCH v2 4/11 x86/msr-index: Add MSRs for Key Locker internal key

In reply to Chang S. Bae

Key Locker internal key in a CPU state can be backed up in a platform register. The backup can be also copied back to a CPU state. This mechanism is useful to restore the key (after system sleep).

Add MSRs for the internal key backup, copy, and status check.

---
 arch/x86/include/asm/msr-index.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/include/asm/msr-index.h +6 -0
index 742d89a00721..f8e78782c8f4 100644
@@ -935,4 +935,10 @@
 #define MSR_VM_IGNNE                    0xc0010115
 #define MSR_VM_HSAVE_PA                 0xc0010117
 
+/* MSRs for Key Locker Internal (Wrapping) Key management */
+#define MSR_IA32_COPY_LOCAL_TO_PLATFORM	0x00000d91
+#define MSR_IA32_COPY_PLATFORM_TO_LOCAL	0x00000d92
+#define MSR_IA32_COPY_STATUS		0x00000990
+#define MSR_IA32_IWKEYBACKUP_STATUS	0x00000991
+
 #endif /* _ASM_X86_MSR_INDEX_H */
Signature · 1 line
2.17.1
#5 lore raw

RFC PATCH v2 5/11 x86/power: Restore Key Locker internal key from the ACPI S3/4 sleep states

In reply to Chang S. Bae

When the system state switches to these sleep states, the internal key gets reset. Since this system transition is transparent to userspace, the internal key needs to be restored properly.

Key Locker provides a mechanism to back up the internal key in non-volatile memory. The kernel requests a backup right after the key loaded at boot-time and copies it later when the system wakes up.

The backup during the S5 sleep state is not trusted. It is overwritten by a new key at the next boot.

On a system with the S3/4 states, enable the feature only when the backup mechanism is supported.

Disable the feature when the copy fails (or the backup corrupts). The shutdown is considered too noisy. A new key is considerable only when threads can be synchronously suspended.

---
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 |   3 +
 arch/x86/kernel/keylocker.c      | 122 +++++++++++++++++++++++++++++--
 arch/x86/power/cpu.c             |   2 +
 3 files changed, 122 insertions(+), 5 deletions(-)
arch/x86/include/asm/keylocker.h +3 -0
index 870832f007ec..74b806346bee 100644
@@ -7,6 +7,7 @@
 
 #include <asm/processor.h>
 #include <linux/bits.h>
+#include <asm/msr.h>
 
 #define KEYLOCKER_CPUID			0x019
 #define KEYLOCKER_CPUID_EAX_SUPERVISOR	BIT(0)
@@ -18,9 +19,11 @@
 #ifdef CONFIG_X86_KEYLOCKER
 void setup_keylocker(struct cpuinfo_x86 *c);
 void flush_keylocker_data(void);
+void restore_keylocker(void);
 #else
 #define setup_keylocker(c) do { } while (0)
 #define flush_keylocker_data() do { } while (0)
+#define restore_keylocker() do { } while (0)
 #endif
 
 #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +117 -5

167 lines not on this page · raw message

arch/x86/power/cpu.c +2 -0
index 3a070e7cdb8b..ace94f07701a 100644
@@ -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;
@@ -261,6 +262,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
maintainer Acked-by on v2 5/11
#6 lore raw

Re: [RFC PATCH v2 05/11] x86/power: Restore Key Locker internal key from the ACPI S3/4 sleep states

Quoted 15 lines · Chang S. Bae

When the system state switches to these sleep states, the internal key gets reset. Since this system transition is transparent to userspace, the internal key needs to be restored properly.

Key Locker provides a mechanism to back up the internal key in non-volatile memory. The kernel requests a backup right after the key loaded at boot-time and copies it later when the system wakes up.

The backup during the S5 sleep state is not trusted. It is overwritten by a new key at the next boot.

On a system with the S3/4 states, enable the feature only when the backup mechanism is supported.

Quoted 8 lines

Disable the feature when the copy fails (or the backup corrupts). The shutdown is considered too noisy. A new key is considerable only when threads can be synchronously suspended.

Quoted 238 lines

---
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 |   3 +
 arch/x86/kernel/keylocker.c      | 122 +++++++++++++++++++++++++++++--
 arch/x86/power/cpu.c             |   2 +
 3 files changed, 122 insertions(+), 5 deletions(-)
arch/x86/include/asm/keylocker.h +3 -0
index 870832f007ec..74b806346bee 100644
@@ -7,6 +7,7 @@
 
 #include <asm/processor.h>
 #include <linux/bits.h>
+#include <asm/msr.h>
 
 #define KEYLOCKER_CPUID                        0x019
 #define KEYLOCKER_CPUID_EAX_SUPERVISOR BIT(0)
@@ -18,9 +19,11 @@
 #ifdef CONFIG_X86_KEYLOCKER
 void setup_keylocker(struct cpuinfo_x86 *c);
 void flush_keylocker_data(void);
+void restore_keylocker(void);
 #else
 #define setup_keylocker(c) do { } while (0)
 #define flush_keylocker_data() do { } while (0)
+#define restore_keylocker() do { } while (0)
 #endif
 
 #endif /*__ASSEMBLY__ */
arch/x86/kernel/keylocker.c +117 -5
index d590815de508..0f60350944fa 100644
@@ -5,6 +5,8 @@
  */
 
 #include <linux/random.h>
+#include <linux/acpi.h>
+#include <linux/delay.h>
 
 #include <asm/cacheflush.h>
 #include <asm/fpu/api.h>
@@ -12,10 +14,13 @@
 #include <asm/keylocker.h>
 #include <asm/tlbflush.h>
 
+static bool keybackup_available;
+
 /* Internal (Wrapping) Key size fits in three 128-bit registers. */
 #define KEYSIZE_128BIT 3
 
 static struct _keydata {
+       bool valid;
        struct reg_128_bit value[KEYSIZE_128BIT];
 } keydata;
 
@@ -30,6 +35,8 @@ static void make_keylocker_data(void)
 
        for (i = 0; i < KEYSIZE_128BIT; i++)
                get_random_bytes(&keydata.value[i], sizeof(struct reg_128_bit));
+
+       keydata.valid = true;
 }
 
 /**
@@ -47,6 +54,8 @@ void flush_keylocker_data(void)
 
        memset(keyaddr, 0, size);
        clflush_cache_range(keyaddr, size);
+
+       keydata.valid = false;
 }
 
 #define KEYSRC_SWRAND          0
@@ -79,6 +88,40 @@ static int load_keylocker(void)
        return err;
 }
 
+#define KEYRESTORE_RETRY       1
+
+/**
+ * copy_keylocker() - Copy the internal key from the backup.
+ *
+ * Request hardware to copy the key in non-volatile memory to the CPU state. Do this
+ * again if the copy fails. The key may not be ready when the precedent backup is
+ * still in progress.
+ *
+ * Returns:    -EBUSY if the copy fails, -ENODEV if no backup is available, or 0 if
+ *             successful.
+ */
+static int copy_keylocker(void)
+{
+       int i;
+
+       if (!keybackup_available)
+               return -ENODEV;
+
+       wrmsrl(MSR_IA32_COPY_PLATFORM_TO_LOCAL, 1);
+
+       for (i = 0; i <= KEYRESTORE_RETRY; i++) {
+               u64 status;
+
+               if (i)
+                       udelay(1);
+
+               rdmsrl(MSR_IA32_COPY_STATUS, status);
+               if (status & BIT(0))
+                       return 0;
+       }
+       return -EBUSY;
+}
+
 /**
  * setup_keylocker() - Enable the feature if supported.
  * @c:         A pointer to struct cpuinfo_x86
@@ -104,13 +147,43 @@ void setup_keylocker(struct cpuinfo_x86 *c)
                        goto disable;
                }
 
+               keybackup_available = (ebx & KEYLOCKER_CPUID_EBX_BACKUP);
+               /* Internal key backup is essential with S3/4 states. */
+               if (!keybackup_available &&
+                   (acpi_sleep_state_supported(ACPI_STATE_S3) ||
+                    acpi_sleep_state_supported(ACPI_STATE_S4))) {
+                       pr_debug("x86/keylocker: no key backup support with possible S3/4.\n");
+                       goto disable;
+               }
+
                make_keylocker_data();
-       }
 
-       err = load_keylocker();
-       if (err) {
-               pr_err_once("x86/keylocker: Failed to load internal key (rc: %d).\n", err);
-               goto disable;
+               err = load_keylocker();
+               if (err) {
+                       pr_err_once("x86/keylocker: Failed to load internal key (rc: %d).\n", err);
+                       goto disable;
+               }
+
+               /* Back up the internal key in non-volatile memory if supported. */
+               if (keybackup_available)
+                       wrmsrl(MSR_IA32_COPY_LOCAL_TO_PLATFORM, 1);
+       } else {
+
+               /*
+                * Load the internal key directly when available in memory, which is only
+                * possible at boot-time.
+                *
+                * NB: When system wakes up, this path also recovers the internal key.
+                */
+               if (keydata.valid)
+                       err = load_keylocker();
+               else
+                       err = copy_keylocker();
+               if (err) {
+                       pr_err_once("x86/keylocker: Fail to %s internal key (rc: %d).\n",
+                                   keydata.valid ? "load" : "copy", err);
+                       goto disable;
+               }
        }
 
        pr_info_once("x86/keylocker: Enabled.\n");
@@ -123,3 +196,42 @@ void setup_keylocker(struct cpuinfo_x86 *c)
        /* Make sure the feature disabled for kexec-reboot. */
        cr4_clear_bits(X86_CR4_KEYLOCKER);
 }
+
+/**
+ * restore_keylocker() - Restore the internal key.
+ *
+ * The boot CPU executes this while other CPUs restore it through the setup function.
+ *
+ * Returns:    Nothing
+ */
+void restore_keylocker(void)
+{
+       u64 backup_status;
+       int err;
+
+       if (!boot_cpu_has(X86_FEATURE_KEYLOCKER))
+               return;
+
+       /*
+        * IA32_IWKEYBACKUP_STATUS MSR contains a bitmap that indicates an invalid backup if bit 0
+        * is set and a read (or write) error if bit 2 is set.
+        */
+       rdmsrl(MSR_IA32_IWKEYBACKUP_STATUS, backup_status);
+       if (WARN(!(backup_status & BIT(0)),
+                "x86/keylocker: Internal key backup access failed with %s.\n",
+                (backup_status & BIT(2)) ? "read error" : "invalid status"))
+               goto disable_out;
+
+       err = copy_keylocker();
+       if (err) {
+               pr_err("x86/keylocker: Internal key restoration failed (rc: %d).\n", err);
+               goto disable_out;
+       }
+
+       return;
+
+disable_out:
+       pr_info("x86/keylocker: Disabled with internal key restoration failure.\n");
+       setup_clear_cpu_cap(X86_FEATURE_KEYLOCKER);
+       cr4_clear_bits(X86_CR4_KEYLOCKER);
+}
arch/x86/power/cpu.c +2 -1
index 3a070e7cdb8b..ace94f07701a 100644
@@ -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;
@@ -261,6 +262,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))
--

2.17.1

#7 lore raw

RFC PATCH v2 6/11 x86/cpu: Add a config option and a chicken bit for Key Locker

In reply to Chang S. Bae

Add a kernel config option to enable the feature (disabled by default) at compile-time.

Also, add a new command-line parameter -- 'nokeylocker' to disable the feature at boot-time.

---
 Documentation/admin-guide/kernel-parameters.txt |  2 ++
 arch/x86/Kconfig                                | 14 ++++++++++++++
 arch/x86/kernel/cpu/common.c                    | 16 ++++++++++++++++
 3 files changed, 32 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +2 -0
index cb89dbdedc46..d0afe36c1802 100644
@@ -3284,6 +3284,8 @@
 
 	nohugevmalloc	[PPC] Disable kernel huge vmalloc mappings.
 
+	nokeylocker	[X86] Disables Key Locker hardware feature.
+
 	nosmt		[KNL,S390] Disable symmetric multithreading (SMT).
 			Equivalent to smt=1.
 
arch/x86/Kconfig +14 -0
index 0045e1b44190..de8eeb705ed8 100644
@@ -1885,6 +1885,20 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS
 
 	  If unsure, say y.
 
+config X86_KEYLOCKER
+	prompt "Key Locker"
+	def_bool n
+	depends on CPU_SUP_INTEL
+	help
+	  Key Locker is a new security feature to protect data encryption
+	  keys for the Advanced Encryption Standard (AES) algorithm.
+
+	  When enabled, every CPU has a unique internal key to wrap AES
+	  keys in an encoded format.  The internal key is not accessible
+	  to software once loaded.
+
+	  If unsure, say y.
+
 choice
 	prompt "TSX enable mode"
 	depends on CPU_SUP_INTEL
arch/x86/kernel/cpu/common.c +16 -0
index 2e64371acb81..c655cce30c2b 100644
@@ -353,6 +353,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
#8 lore raw

RFC PATCH v2 7/11 selftests/x86: Test Key Locker internal key maintenance

In reply to Chang S. Bae

The test validates the internal key to be the same in all CPUs.

It performs the validation again with the Suspend-To-RAM (ACPI S3) state.

---
Changes from RFC v1:
* Commented the binutils version number for ENCODEKEY128 (Peter Zijlstra)

---
 tools/testing/selftests/x86/Makefile    |   2 +-
 tools/testing/selftests/x86/keylocker.c | 177 ++++++++++++++++++++++++
 2 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/keylocker.c
tools/testing/selftests/x86/Makefile +1 -1
index 333980375bc7..09237cc84108 100644
@@ -13,7 +13,7 @@ CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie)
 TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \
 			check_initial_reg_state sigreturn iopl ioperm \
 			test_vsyscall mov_ss_trap \
-			syscall_arg_fault fsgsbase_restore
+			syscall_arg_fault fsgsbase_restore keylocker
 TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			test_FCMOV test_FCOMI test_FISTTP \
 			vdso_restorer
tools/testing/selftests/x86/keylocker.c new file +177 -0

177 lines not on this page · raw message

Signature · 1 line
2.17.1
#9 lore raw

RFC PATCH v2 8/11 crypto: x86/aes-ni - Improve error handling

In reply to Chang S. Bae

Some error case in the glue code is possibly ignored and thus not handled correctly. Make sure each error code is not overwritten.

---
Changes from RFC v1:
* Added as a new patch. This change prepares to address Ard's feedback.

---
 arch/x86/crypto/aesni-intel_glue.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +9 -9
index 2144e54a6c89..685943f0e5a3 100644
@@ -291,7 +291,7 @@ static int ecb_encrypt(struct skcipher_request *req)
 			      nbytes & AES_BLOCK_MASK);
 		kernel_fpu_end();
 		nbytes &= AES_BLOCK_SIZE - 1;
-		err = skcipher_walk_done(&walk, nbytes);
+		err |= skcipher_walk_done(&walk, nbytes);
 	}
 
 	return err;
@@ -313,7 +313,7 @@ static int ecb_decrypt(struct skcipher_request *req)
 			      nbytes & AES_BLOCK_MASK);
 		kernel_fpu_end();
 		nbytes &= AES_BLOCK_SIZE - 1;
-		err = skcipher_walk_done(&walk, nbytes);
+		err |= skcipher_walk_done(&walk, nbytes);
 	}
 
 	return err;
@@ -335,7 +335,7 @@ static int cbc_encrypt(struct skcipher_request *req)
 			      nbytes & AES_BLOCK_MASK, walk.iv);
 		kernel_fpu_end();
 		nbytes &= AES_BLOCK_SIZE - 1;
-		err = skcipher_walk_done(&walk, nbytes);
+		err |= skcipher_walk_done(&walk, nbytes);
 	}
 
 	return err;
@@ -357,7 +357,7 @@ static int cbc_decrypt(struct skcipher_request *req)
 			      nbytes & AES_BLOCK_MASK, walk.iv);
 		kernel_fpu_end();
 		nbytes &= AES_BLOCK_SIZE - 1;
-		err = skcipher_walk_done(&walk, nbytes);
+		err |= skcipher_walk_done(&walk, nbytes);
 	}
 
 	return err;
@@ -522,7 +522,7 @@ static int ctr_crypt(struct skcipher_request *req)
 			nbytes = 0;
 		}
 		kernel_fpu_end();
-		err = skcipher_walk_done(&walk, nbytes);
+		err |= skcipher_walk_done(&walk, nbytes);
 	}
 	return err;
 }
@@ -691,7 +691,7 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
 		}
 		kernel_fpu_end();
 
-		err = skcipher_walk_done(&walk, 0);
+		err |= skcipher_walk_done(&walk, 0);
 	}
 
 	if (err)
@@ -862,7 +862,7 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)
 		skcipher_request_set_crypt(&subreq, req->src, req->dst,
 					   blocks * AES_BLOCK_SIZE, req->iv);
 		req = &subreq;
-		err = skcipher_walk_virt(&walk, req, false);
+		err |= skcipher_walk_virt(&walk, req, false);
 	} else {
 		tail = 0;
 	}
@@ -888,7 +888,7 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)
 					  nbytes, walk.iv);
 		kernel_fpu_end();
 
-		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
+		err |= skcipher_walk_done(&walk, walk.nbytes - nbytes);
 
 		if (walk.nbytes > 0)
 			kernel_fpu_begin();
@@ -905,7 +905,7 @@ static int xts_crypt(struct skcipher_request *req, bool encrypt)
 		skcipher_request_set_crypt(req, src, dst, AES_BLOCK_SIZE + tail,
 					   req->iv);
 
-		err = skcipher_walk_virt(&walk, &subreq, false);
+		err |= skcipher_walk_virt(&walk, &subreq, false);
 		if (err)
 			return err;
 
Signature · 1 line
2.17.1
#10 lore raw

RFC PATCH v2 9/11 crypto: x86/aes-ni - Refactor to prepare a new AES implementation

In reply to Chang S. Bae

Refactor some glue code and constant values of assembly code in preparation for a new AES implementation.

Also, introduce wrappers for data transformation functions to return an error value. New AES instructions may populate an error condition while AES-NI does not.

No functional change.

---
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   | 208 +++++++++++++++++++
 arch/x86/crypto/aes-intel_glue.h   |  61 ++++++
 arch/x86/crypto/aesni-intel_asm.S  |  90 ++++-----
 arch/x86/crypto/aesni-intel_glue.c | 308 ++++-------------------------
 arch/x86/crypto/aesni-intel_glue.h |  88 +++++++++
 7 files changed, 452 insertions(+), 331 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
index d0959e7b809f..30d558df67cd 100644
@@ -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
new file mode 100644
index 000000000000..98abf875af79
@@ -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 +208 -0

208 lines not on this page · raw message

arch/x86/crypto/aes-intel_glue.h new file +61 -0

61 lines not on this page · raw message

arch/x86/crypto/aesni-intel_asm.S +35 -55

203 lines not on this page · raw message

arch/x86/crypto/aesni-intel_glue.c +33 -275

460 lines not on this page · raw message

arch/x86/crypto/aesni-intel_glue.h new file +88 -0

88 lines not on this page · raw message

Signature · 1 line
2.17.1
#11 lore raw

RFC PATCH v2 10/11 crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions

In reply to Chang S. Bae

Key Locker (KL) is Intel's new security feature that protects the AES key at the time of data transformation. New AES SIMD instructions -- as a successor of Intel's AES-NI -- are provided to encode an AES key and reference it for the AES algorithm.

New instructions support 128/256-bit keys. While it is not desirable to receive any 192-bit key, AES-NI instructions are taken to serve this size.

New instructions are operational in both 32-/64-bit modes.

Included are methods for ECB, CBC, CTR, and XTS modes. They are not compatible with other implementations as referencing an encrypted form only.

XTS needs a separate 32-bit assembly code as the 64-bit version clobbers more than eight 128-bit registers. Support 64-bit mode only.

setkey() encodes an AES key. Users may displace the AES key right after this as encrypt()/decrypt() methods do not need the key.

Most glue code reuses the refactored functions from the AES-NI part. When enabled, it will have a bit higher priority than the AES-NI as providing key protection.

---
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  | 1181 ++++++++++++++++++++++++++++
 arch/x86/crypto/aeskl-intel_glue.c |  390 +++++++++
 crypto/Kconfig                     |   23 +
 4 files changed, 1597 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
index 30d558df67cd..6bb4e2fb2b47 100644
@@ -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 aesni-intel_asm.o aeskl-intel_glue.o aes-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 +1181 -0

1,181 lines not on this page · raw message

arch/x86/crypto/aeskl-intel_glue.c new file +390 -0

390 lines not on this page · raw message

crypto/Kconfig +23 -0
index ca3b02dcbbfa..5f3d4052fa3c 100644
@@ -1113,6 +1113,29 @@ config CRYPTO_AES_NI_INTEL
 	  ECB, CBC, LRW, XTS. The 64 bit version has additional
 	  acceleration for CTR.
 
+config CRYPTO_AES_KL
+	tristate "AES cipher algorithms (AES-KL)"
+	depends on X86_KEYLOCKER
+	depends on LD_VERSION >= 23600
+	select CRYPTO_AES_NI_INTEL
+	help
+	  Key Locker provides AES SIMD instructions (AES-KL) to protect AES
+	  keys. They are analogous to AES-NI. AES-KL supports to encode AES
+	  keys and use an encoded form to transform data instead of
+	  referencing an AES key all the way.
+
+	  The CPU-internal key is used to wrap AES keys. AES-KL relies on
+	  the x86 core code that manages the internal key.
+
+	  AES-KL supports 128-/256-bit keys. This module uses the AES-NI
+	  methods for 192-bit keys. It includes such block cipher mode
+	  support as ECB, CBC, CTR, and XTS.
+
+	  binutils version 2.36 or later is needed to assemble AES-KL.
+
+	  See the CRYPTO_AES_NI_INTEL description for more about the AES
+	  algorithm.
+
 config CRYPTO_AES_SPARC64
 	tristate "AES cipher algorithms (SPARC64)"
 	depends on SPARC64
Signature · 1 line
2.17.1
#12 lore raw

Re: [RFC PATCH v2 10/11] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions

Quoted 4 lines · Chang S. Bae

Included are methods for ECB, CBC, CTR, and XTS modes. They are not compatible with other implementations as referencing an encrypted form only.

Your code uses the standard algorithm names like cbc(aes), which implies that it is compatible with the standard cbc(aes). So which is it -- compatible or not compatible -- and if it isn't compatible, what is the expected use case?

- Eric

#13 lore raw

Re: [RFC PATCH v2 10/11] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions

Quoted 8 lines · Eric Biggers
Quoted 3 lines · Chang S. Bae

Included are methods for ECB, CBC, CTR, and XTS modes. They are not compatible with other implementations as referencing an encrypted form only.

Your code uses the standard algorithm names like cbc(aes), which implies that it is compatible with the standard cbc(aes). So which is it -- compatible or not compatible -- and if it isn't compatible, what is the expected use case?

Yes, it provides AES-CBC functionality. Well, it was intended to avoid mixed use of functions -- setkey(), decrypt(), and encrypt() -- from others. Perhaps, rewrite this as:

Each method should not be used along with other implementations'. E.g., KL’s setkey() output can’t be used to the input to the encrypt() method of AES-NI or generic implementation.

Thanks,
Chang

#14 lore raw

Re: [RFC PATCH v2 10/11] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions

Quoted 10 lines · Chang Seok
Quoted 8 lines · Eric Biggers
Quoted 3 lines · Chang S. Bae

Included are methods for ECB, CBC, CTR, and XTS modes. They are not compatible with other implementations as referencing an encrypted form only.

Your code uses the standard algorithm names like cbc(aes), which implies that it is compatible with the standard cbc(aes). So which is it -- compatible or not compatible -- and if it isn't compatible, what is the expected use case?

Quoted 8 lines

Yes, it provides AES-CBC functionality. Well, it was intended to avoid mixed use of functions -- setkey(), decrypt(), and encrypt() -- from others. Perhaps, rewrite this as:

Each method should not be used along with other implementations'. E.g., KL’s setkey() output can’t be used to the input to the encrypt() method of AES-NI or generic implementation.

Sure. But that is just the implementation, so not really as interesting as what the user sees. I think you need to do a better job explaining what this looks like from a user's perspective. It sounds like the answer is "it looks the same" -- right? What is the benefit, exactly? (Please be more specific than "it protects the AES keys".)

- Eric

#15 lore raw

Re: [RFC PATCH v2 10/11] crypto: x86/aes-kl - Support AES algorithm using Key Locker instructions

In reply to Chang S. Bae
Quoted 5 lines · Chang S. Bae

Key Locker (KL) is Intel's new security feature that protects the AES key at the time of data transformation. New AES SIMD instructions -- as a successor of Intel's AES-NI -- are provided to encode an AES key and reference it for the AES algorithm.

Quoted 8 lines

New instructions support 128/256-bit keys. While it is not desirable to receive any 192-bit key, AES-NI instructions are taken to serve this size.

New instructions are operational in both 32-/64-bit modes.

Included are methods for ECB, CBC, CTR, and XTS modes. They are not compatible with other implementations as referencing an encrypted form only.

This entire concept is severely problematic. The nineties are over -- in 2021, a responsible author of a crypto library will understand, document, and preferably prove what security properties it is supposed to have.

Even assuming that Key Locker is used properly and that the wrapping key is never compromised, the security properties of Key Locker-ified AES-CTR are weak at best. In particular, all the usual caveats of CTR apply, and KL does nothing to mitigate it. Any attacker who recovers, directly or by capture of a plaintext-ciphertext pair, enc_k(0), enc_k(1), etc can encrypt and forge any message involving those counter values.

In-kernel support for an AES Key Locker mode needs to document precisely what that mode accomplishes and when it should and should not be used.

At least AES-XTS *probably* has the property that at attacker who gets the contents of main memory once can't decrypt future disk images encrypted against the same key. AES-CTR can't even achieve that.

So far, the only compelling use case for KL that I've seen is full disk encryption. I think it would be nice for the entire process of doing that to be worked out.

--Andy

#16 lore raw

RFC PATCH v2 11/11 x86/cpu: Support the hardware randomization option for Key Locker internal key

In reply to Chang S. Bae

Hardware can load the internal key with randomization. The random.trust_cpu parameter determines the use of the CPU's random number generator. Take it to use the CPU's internal key randomization.

The feature's backup mechanism is required to distribute an internal key. This is the only way to copy it to other CPUs if using hardware randomization.

This option is disabled when hardware does not support the key backup.

---
 arch/x86/kernel/keylocker.c | 37 ++++++++++++++++++++++++++++++++-----
 drivers/char/random.c       |  6 ++++++
 include/linux/random.h      |  2 ++
 3 files changed, 40 insertions(+), 5 deletions(-)
arch/x86/kernel/keylocker.c +32 -5
index 0f60350944fa..5a784492195b 100644
@@ -7,6 +7,7 @@
 #include <linux/random.h>
 #include <linux/acpi.h>
 #include <linux/delay.h>
+#include <linux/random.h>
 
 #include <asm/cacheflush.h>
 #include <asm/fpu/api.h>
@@ -15,27 +16,34 @@
 #include <asm/tlbflush.h>
 
 static bool keybackup_available;
+static bool keyhwrand_available;
 
 /* Internal (Wrapping) Key size fits in three 128-bit registers. */
 #define KEYSIZE_128BIT	3
 
 static struct _keydata {
 	bool valid;
+	bool hwrand;
 	struct reg_128_bit value[KEYSIZE_128BIT];
 } keydata;
 
 /**
  * make_keylocker_data() - Generate the internal key.
+ * @use_hwrand:	True if use hardware randomization; otherwise, false.
  *
  * Return:	Nothing
  */
-static void make_keylocker_data(void)
+static void make_keylocker_data(bool use_hwrand)
 {
 	int i;
 
 	for (i = 0; i < KEYSIZE_128BIT; i++)
 		get_random_bytes(&keydata.value[i], sizeof(struct reg_128_bit));
 
+	keydata.hwrand = (use_hwrand && keyhwrand_available && keybackup_available);
+	if (use_hwrand && !keydata.hwrand)
+		pr_warn("x86/keylocker: hardware random key is not fully supported\n");
+
 	keydata.valid = true;
 }
 
@@ -59,6 +67,8 @@ void flush_keylocker_data(void)
 }
 
 #define KEYSRC_SWRAND		0
+#define KEYSRC_HWRAND		BIT(1)
+#define KEYSRC_HWRAND_RETRY	10
 
 /**
  * load_keylocker() - Load the internal key.
@@ -68,8 +78,16 @@ void flush_keylocker_data(void)
 static int load_keylocker(void)
 {
 	struct reg_128_bit zeros = { 0 };
-	u32 keysrc = KEYSRC_SWRAND;
-	int err;
+	int retry, err;
+	u32 keysrc;
+
+	if (keydata.hwrand) {
+		keysrc = KEYSRC_HWRAND;
+		retry = KEYSRC_HWRAND_RETRY;
+	} else {
+		keysrc = KEYSRC_SWRAND;
+		retry = 0;
+	}
 
 	kernel_fpu_begin();
 
@@ -78,13 +96,19 @@ static int load_keylocker(void)
 			 "m"(keydata.value[1]),
 			 "m"(keydata.value[2]));
 
-	err = loadiwkey(keysrc);
+	do {
+		err = loadiwkey(keysrc);
+		retry--;
+	} while (err && retry >= 0);
 
 	asm volatile ("movdqu %0, %%xmm0; movdqu %0, %%xmm1; movdqu %0, %%xmm2;"
 		      :: "m"(zeros));
 
 	kernel_fpu_end();
 
+	if (keydata.hwrand)
+		flush_keylocker_data();
+
 	return err;
 }
 
@@ -138,6 +162,7 @@ void setup_keylocker(struct cpuinfo_x86 *c)
 	cr4_set_bits(X86_CR4_KEYLOCKER);
 
 	if (c == &boot_cpu_data) {
+		bool use_hwrand = check_random_trust_cpu();
 		u32 eax, ebx, ecx, edx;
 
 		cpuid_count(KEYLOCKER_CPUID, 0, &eax, &ebx, &ecx, &edx);
@@ -156,7 +181,9 @@ void setup_keylocker(struct cpuinfo_x86 *c)
 			goto disable;
 		}
 
-		make_keylocker_data();
+		keyhwrand_available = (ecx & KEYLOCKER_CPUID_ECX_RAND);
+
+		make_keylocker_data(use_hwrand);
 
 		err = load_keylocker();
 		if (err) {
drivers/char/random.c +6 -0
index 605969ed0f96..a3ece5968497 100644
@@ -771,6 +771,12 @@ static int __init parse_trust_cpu(char *arg)
 }
 early_param("random.trust_cpu", parse_trust_cpu);
 
+bool check_random_trust_cpu(void)
+{
+	return trust_cpu;
+}
+EXPORT_SYMBOL(check_random_trust_cpu);
+
 static bool crng_init_try_arch(struct crng_state *crng)
 {
 	int		i;
include/linux/random.h +2 -0
index f45b8be3e3c4..f08f44988b13 100644
@@ -158,4 +158,6 @@ static inline bool __init arch_get_random_long_early(unsigned long *v)
 }
 #endif
 
+extern bool check_random_trust_cpu(void);
+
 #endif /* _LINUX_RANDOM_H */
Signature · 1 line
2.17.1
#17 lore raw
In reply to Chang S. Bae
Quoted 5 lines · Chang S. Bae

Key Locker [1][2] is a new security feature available in new Intel CPUs to protect data encryption keys for the Advanced Encryption Standard algorithm. The protection limits the amount of time an AES key is exposed in memory by sealing a key and referencing it with new AES instructions.

Quoted 8 lines

The new AES instruction set is a successor of Intel's AES-NI (AES New Instruction). Users may switch to the Key Locker version from crypto libraries. This series includes a new AES implementation for the Crypto API, which was validated through the crypto unit tests. The performance in the test cases was measured and found comparable to the AES-NI version.

Key Locker introduces a (CPU-)internal key to encode AES keys. The kernel needs to load it and ensure it unchanged as long as CPUs are operational.

I have high-level questions:

What is the expected use case? My personal hypothesis, based on various public Intel slides, is that the actual intended use case was internal to the ME, and that KL was ported to end-user CPUs more or less verbatim. I certainly understand how KL is valuable in a context where a verified boot process installs some KL keys that are not subsequently accessible outside the KL ISA, but Linux does not really work like this.
I'm wondering what people will use it for.

On a related note, does Intel plan to extend KL with ways to securely load keys? (E.g. the ability to, in effect, LOADIWKEY from inside an enclave? Key wrapping/unwrapping operations?) In other words, is should we look at KL the way we look at MKTME, i.e. the foundation of something neat but not necessarily very useful as is, or should we expect that KL is in its more or less final form?

What is the expected interaction between a KL-using VM guest and the host VMM? Will there be performance impacts (to context switching, for example) if a guest enables KL, even if the guest does not subsequently do anything with it? Should Linux actually enable KL if it detects that it's a VM guest? Should Linux have use a specific keying method as a guest?

--Andy

#18 lore raw
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Chang S. Bae

Key Locker [1][2] is a new security feature available in new Intel CPUs to protect data encryption keys for the Advanced Encryption Standard algorithm. The protection limits the amount of time an AES key is exposed in memory by sealing a key and referencing it with new AES instructions.

The new AES instruction set is a successor of Intel's AES-NI (AES New Instruction). Users may switch to the Key Locker version from crypto libraries. This series includes a new AES implementation for the Crypto API, which was validated through the crypto unit tests. The performance in the test cases was measured and found comparable to the AES-NI version.

Quoted 6 lines
Quoted 2 lines

Key Locker introduces a (CPU-)internal key to encode AES keys. The kernel needs to load it and ensure it unchanged as long as CPUs are operational.

I have high-level questions:

What is the expected use case?

The wrapping key here is only used for new AES instructions.

I’m aware of their potential use cases for encrypting file system or disks.

Quoted 4 lines

My personal hypothesis, based on various
public Intel slides, is that the actual intended use case was internal to the ME, and that KL was ported to end-user CPUs more or less verbatim.

No, this is a separate one. The feature has nothing to do with the firmware except that in some situations it merely helps to back up the key in its state.

Quoted 3 lines

I certainly understand how KL is valuable in a context where a verified boot process installs some KL keys that are not subsequently accessible outside the KL ISA, but Linux does not really work like this.

Do you mind elaborating on the concern? I try to understand any issue with PATCH3 [1], specifically.

Quoted 1 line

I'm wondering what people will use it for.

Mentioned above.

Quoted 6 lines

On a related note, does Intel plan to extend KL with ways to securely load keys? (E.g. the ability to, in effect, LOADIWKEY from inside an enclave? Key wrapping/unwrapping operations?) In other words, is should we look at KL the way we look at MKTME, i.e. the foundation of something neat but not necessarily very useful as is, or should we expect that KL is in its more or less final form?

All I have is pretty much in the spec. So, I think the latter is the case.

I don’t see anything about that LOADIWKEY inside an enclave in the spec. (A relevant section is A.6.1 Key Locker Usage with TEE.)

Quoted 5 lines

What is the expected interaction between a KL-using VM guest and the host VMM? Will there be performance impacts (to context switching, for example) if a guest enables KL, even if the guest does not subsequently do anything with it? Should Linux actually enable KL if it detects that it's a VM guest? Should Linux have use a specific keying method as a guest?

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

Thanks,
Chang

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/

#19 lore raw
Quoted 34 lines · Chang Seok
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Chang S. Bae

Key Locker [1][2] is a new security feature available in new Intel CPUs to protect data encryption keys for the Advanced Encryption Standard algorithm. The protection limits the amount of time an AES key is exposed in memory by sealing a key and referencing it with new AES instructions.

The new AES instruction set is a successor of Intel's AES-NI (AES New Instruction). Users may switch to the Key Locker version from crypto libraries. This series includes a new AES implementation for the Crypto API, which was validated through the crypto unit tests. The performance in the test cases was measured and found comparable to the AES-NI version.

Quoted 6 lines
Quoted 2 lines

Key Locker introduces a (CPU-)internal key to encode AES keys. The kernel needs to load it and ensure it unchanged as long as CPUs are operational.

I have high-level questions:

What is the expected use case?

The wrapping key here is only used for new AES instructions.

I’m aware of their potential use cases for encrypting file system or disks.

Quoted 4 lines

My personal hypothesis, based on various
public Intel slides, is that the actual intended use case was internal to the ME, and that KL was ported to end-user CPUs more or less verbatim.

No, this is a separate one. The feature has nothing to do with the firmware except that in some situations it merely helps to back up the key in its state.

Quoted 6 lines
Quoted 3 lines

I certainly understand how KL is valuable in a context where a verified boot process installs some KL keys that are not subsequently accessible outside the KL ISA, but Linux does not really work like this.

Do you mind elaborating on the concern? I try to understand any issue with PATCH3 [1], specifically.

If I understand Andy's concern it is the observation that the weakest link in this facility is the initial key load. Yes, KL reduces exposure after that event, but the key loading process is still vulnerable. This question is similar to the concern between the Linux "encrypted-keys" and "trusted-keys" interface. The trusted-keys interface still has an attack window where the key is unwrapped in kernel space to decrypt the sub-keys, but that exposure need not cross the user-kernel boundary and can be time-limited to a given PCR state. The encrypted-keys interface maintains the private-key material outside the kernel where it has increased exposure. KL is effectively "encrypted-keys" and Andy is questioning whether this makes KL similar to the MKTME vs SGX / TDX situation.

Quoted 4 lines
Quoted 2 lines

I'm wondering what people will use it for.

Mentioned above.

I don't think this answers Andy's question. There is a distinction between what it can be used for and what people will deploy with it in practice given the "encrypted-keys"-like exposure. Clarify the end user benefit that motivates the kernel to carry this support.

#20 lore raw
Quoted 13 lines · Dan Williams
Quoted 10 lines · Chang Seok
Quoted 5 lines · Andy Lutomirski

I certainly understand how KL is valuable in a context where a verified boot process installs some KL keys that are not subsequently accessible outside the KL ISA, but Linux does not really work like this.

Do you mind elaborating on the concern? I try to understand any issue with PATCH3 [1], specifically.

Quoted 12 lines

If I understand Andy's concern it is the observation that the weakest link in this facility is the initial key load. Yes, KL reduces exposure after that event, but the key loading process is still vulnerable. This question is similar to the concern between the Linux "encrypted-keys" and "trusted-keys" interface. The trusted-keys interface still has an attack window where the key is unwrapped in kernel space to decrypt the sub-keys, but that exposure need not cross the user-kernel boundary and can be time-limited to a given PCR state. The encrypted-keys interface maintains the private-key material outside the kernel where it has increased exposure. KL is effectively "encrypted-keys" and Andy is questioning whether this makes KL similar to the MKTME vs SGX / TDX situation.

I don’t fully grasp the MKTME vs SGX/TDX background, but LOADIWKEY provides a hardware randomization option for the initial load. Then, the internal key is unknown. Nonetheless, if one does not trust this randomization and decides not to use it, then perhaps unavoidable is the key in memory sometime during boot-time.

I think Dan just gave an example here, but FWIW, these “encrypted-keys” and “trusted-keys” are for the kernel keyring service. I wish to clarify the keyring service itself is not intended usage here. Instead, this series is intended to focus on the kernel Crypto API, as this technology protects AES keys during data transformation time.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I'm wondering what people will use it for.

Mentioned above.

I don't think this answers Andy's question. There is a distinction between what it can be used for and what people will deploy with it in practice given the "encrypted-keys"-like exposure. Clarify the end user benefit that motivates the kernel to carry this support.

The end-user of this series will benefit from key protection at data transformation time and also be provided with matched performance as AES-NI does.

Thanks,
Chang

#21 lore raw
In reply to Bae, Chang Seok
Quoted 3 lines · Chang Seok
Quoted 2 lines · Andy Lutomirski

What is the expected interaction between a KL-using VM guest and the host VMM?

Messy. :-)

Quoted 3 lines
Quoted 3 lines

Will there be performance impacts (to context switching, for example) if a guest enables KL, even if the guest does not subsequently do anything with it?

Short answer, yes. But the proposed solution is to disallow KL in KVM guests if KL is in use by the host. The problem is that, by design, the host can't restore its key via LOADIWKEY because the whole point is to throw away the real key. To restore its value, the host would need to use the platform backup/restore mechanism, which is comically slow (tens of thousands of cycles).

If KL virtualization is mutually exclusive with use in the host, then IIRC the context switching penalty is only paid by vCPUs that have executed LOADIWKEY, as other tasks can safely run with a stale/bogus key.

Quoted 1 line
Quoted 1 line

Should Linux actually enable KL if it detects that it's a VM guest?

Probably not by default. It shouldn't even be considered unless the VMM is trusted, as a malicious VMM can completely subvert KL. Even if the host is trusted, it's not clear that the tradeoffs are a net win.

Practically speaking, VMMs have to either (a) save the real key in host memory or (b) provide a single VM exclusive access to the underlying hardware.

For (a), that rules out using an ephemeral, random key, as using a truly random key prevents the VMM from saving/restoring the real key. That means the guest has to generate its own key, and the host has to also store the key in memory. There are also potential performance and live migration implications. The only benefit to using KL in the guest is that the real key is not stored in _guest_ accessible memory. So it probably reduces the attack surface, but on the other hand the VMM may store the guest's master key in a known location, which might make cross-VM attacks easier in some ways.

(b) is a fairly unlikely scenario, and certainly can't be assumed to be the default scenario for a guest.

Quoted 1 line
Quoted 1 line

Should Linux have use a specific keying method as a guest?

Could you rephrase this question? I didn't follow.

Quoted 1 line

First of all, there is an RFC series for KVM [2].

That series also fails to address the use case question.

[*] https://lore.kernel.org/kvm/YGs07I%[email protected]/

#22 lore raw
In reply to Bae, Chang Seok
Quoted 16 lines · Chang Seok
Quoted 15 lines · Andy Lutomirski
Quoted 13 lines · Chang S. Bae

Key Locker [1][2] is a new security feature available in new Intel CPUs to protect data encryption keys for the Advanced Encryption Standard algorithm. The protection limits the amount of time an AES key is exposed in memory by sealing a key and referencing it with new AES instructions.

The new AES instruction set is a successor of Intel's AES-NI (AES New Instruction). Users may switch to the Key Locker version from crypto libraries. This series includes a new AES implementation for the Crypto API, which was validated through the crypto unit tests. The performance in the test cases was measured and found comparable to the AES-NI version.

Key Locker introduces a (CPU-)internal key to encode AES keys. The kernel needs to load it and ensure it unchanged as long as CPUs are operational.

Quoted 7 lines
Quoted 3 lines

I have high-level questions:

What is the expected use case?

The wrapping key here is only used for new AES instructions.

I’m aware of their potential use cases for encrypting file system or disks.

I would like to understand what people are actually going to do with this. Give me a user story or two, please. If it turns out to be useless, I would rather not merge it.

Quoted 7 lines
Quoted 4 lines

I certainly understand how KL is valuable in a context where a verified boot process installs some KL keys that are not subsequently accessible outside the KL ISA, but Linux does not really work like this.

Do you mind elaborating on the concern? I try to understand any issue with PATCH3 [1], specifically.

My concern has nothing to do with your patches per se.

I want to understand the entire workflow that makes Key Locker safer than not using Key Locker. Something like:

Step 1: Computer is powered on.
Step 2: Boot loader loads Linux
Step 3: Linux does such-and-such
Step 4: Attacker compromises the computer in the following way

and an explanation of why this is realistic and how Key Locker helps would be nice.

Quoted 11 lines
Quoted 5 lines

What is the expected interaction between a KL-using VM guest and the host VMM? Will there be performance impacts (to context switching, for example) if a guest enables KL, even if the guest does not subsequently do anything with it? Should Linux actually enable KL if it detects that it's a VM guest? Should Linux have use a specific keying method as a guest?

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

I read that series. This is not a good solution.

I can think of at least a few reasonable ways that a host and a guest can cooperate to, potentially, make KL useful.

a) Host knows that the guest will never migrate, and guest delegates IWKEY management to the host. The host generates a random key and does not permit the guest to use LOADIWKEY. The guest shares the random key with the host. Of course, this means that a host key handle that leaks to a guest can be used within the guest.

b) Host may migrate the guest. Guest delegates IWKEY management to the host, and the host generates and remembers a key for the guest. On migration, the host forwards the key to the new host. The host can still internally any type of key, but context switches may be quite slow.

c) Guest wants to manage its own non-random key. Host lets it and context switches it.

d) Guest does not need KL and leaves CR4.KL clear. Host does whatever it wants with no overhead.

All of these have tradeoffs.

My current thought is that, if Linux is going to support Key Locker, then this all needs to be explicitly controlled. On initial boot, Linux should not initialize Key Locker. Upon explicit administrator request (via sysfs?), Linux will initialize Key Locker in the mode requested by the administrator. Modes could include:

native_random_key: Use a random key per the ISA.

native_kernel_key_remember: Use a random key but load it as a non-random key. Remember the key in kernel memory and use it for S3 resume, etc.

native_kernel_key_backup: Use a random key, put it in the backup storage, and forget it. Use the backup for resume, etc.

native_kernel_key_norestore: Use a random key. The key is lost on any power transition that forgets the key. Backup is not used.

paravirt_any: Ask the hypervisor to handle keying. Any mechanism is acceptable.

paravirt_random: Ask the hypervisor for a random key. Only succeeds if we get an actual random key.

Does this make sense?

#23 lore raw
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Chang Seok

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

I read that series. This is not a good solution.

Quoted 8 lines

I can think of at least a few reasonable ways that a host and a guest can cooperate to, potentially, make KL useful.

a) Host knows that the guest will never migrate, and guest delegates IWKEY management to the host. The host generates a random key and does not permit the guest to use LOADIWKEY. The guest shares the random key with the host. Of course, this means that a host key handle that leaks to a guest can be used within the guest.

If the guest and host share a random key, then they also share the key handle. And that handle+key would also need to be shared across all guests. I doubt this option is acceptable on the security front.

Using multiple random keys is a non-starter because they can't be restored via LOADIWKEY.

Using multiple software-defined keys will have moderate overhead because of the possibility of using KL from soft IRQ context, i.e. KVM would have to do LOADIWKEY on every VM-Enter _and_ VM-Exit. It sounds like LOADIWKEY has latency similar to WRMSR, so it's not a deal-breaker, but the added latency on top of the restrictions on how the host can use KL certainly lessen the appeal.

Quoted 4 lines

b) Host may migrate the guest. Guest delegates IWKEY management to the host, and the host generates and remembers a key for the guest. On migration, the host forwards the key to the new host. The host can still internally any type of key, but context switches may be quite slow.

Migrating is sketchy because the IWKEY has to be exposed to host userspace. But, I think the migration aspect is a secondary discussion.

Quoted 2 lines

c) Guest wants to manage its own non-random key. Host lets it and context switches it.

This is essentially a variant of (b). In both cases, the host has full control over the guest's key.

Quoted 10 lines

d) Guest does not need KL and leaves CR4.KL clear. Host does whatever it wants with no overhead.

All of these have tradeoffs.

My current thought is that, if Linux is going to support Key Locker, then this all needs to be explicitly controlled. On initial boot, Linux should not initialize Key Locker. Upon explicit administrator request (via sysfs?), Linux will initialize Key Locker in the mode requested by the administrator.

Deferring KL usage to post-boot can work, but KVM shouldn't be allowed to expose KL to a guest until KL has been explicitly configured in the host. If KVM can spawn KL guests before the host is configured, the sysfs knob would have to deal with the case where the desired configuration is incompatible with exposing KL to a guest.

Quoted 6 lines

Modes could include:

native_random_key: Use a random key per the ISA.

native_kernel_key_remember: Use a random key but load it as a non-random key. Remember the key in kernel memory and use it for S3 resume, etc.

What would be the motivation for this mode? It largely defeats the value proposition of KL, no?

Quoted 11 lines

native_kernel_key_backup: Use a random key, put it in the backup storage, and forget it. Use the backup for resume, etc.

native_kernel_key_norestore: Use a random key. The key is lost on any power transition that forgets the key. Backup is not used.

paravirt_any: Ask the hypervisor to handle keying. Any mechanism is acceptable.

paravirt_random: Ask the hypervisor for a random key. Only succeeds if we get an actual random key.

AFAIK, there's no way for the guest to verify that it got a truly random key. Hell, the guest can't even easily verify that KL is even supported. The host can lie about CPUID and CR4.KL, and intercept all KL instructions via #UD by running the guest with CR4.KL=0.

I also don't see any reason to define a paravirt interface for a truly random key. Using a random key all but requires a single guest to have exclusive access to KL, and in that case the host can simply expose KL to only that guest.

Quoted 1 line

Does this make sense?

I really want to use see concrete guest use cases before we start adding paravirt interfaces.

#24 lore raw
Quoted 19 lines · Sean Christopherson
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Chang Seok

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

I read that series. This is not a good solution.

Quoted 8 lines

I can think of at least a few reasonable ways that a host and a guest can cooperate to, potentially, make KL useful.

a) Host knows that the guest will never migrate, and guest delegates IWKEY management to the host. The host generates a random key and does not permit the guest to use LOADIWKEY. The guest shares the random key with the host. Of course, this means that a host key handle that leaks to a guest can be used within the guest.

Quoted 4 lines

If the guest and host share a random key, then they also share the key handle. And that handle+key would also need to be shared across all guests. I doubt this option is acceptable on the security front.

Indeed. Oddly, SGX has the exact same problem for any scenario in which SGX is used for HSM-like functionality, and people still use SGX.

However, I suspect that there will be use cases in which exactly one VM is permitted to use KL. Qubes might want that (any Qubes people around?)

Quoted 8 lines

Using multiple random keys is a non-starter because they can't be restored via LOADIWKEY.

Using multiple software-defined keys will have moderate overhead because of the possibility of using KL from soft IRQ context, i.e. KVM would have to do LOADIWKEY on every VM-Enter _and_ VM-Exit. It sounds like LOADIWKEY has latency similar to WRMSR, so it's not a deal-breaker, but the added latency on top of the restrictions on how the host can use KL certainly lessen the appeal.

Indeed. This stinks.

Quoted 26 lines
Quoted 5 lines

b) Host may migrate the guest. Guest delegates IWKEY management to the host, and the host generates and remembers a key for the guest. On migration, the host forwards the key to the new host. The host can still internally any type of key, but context switches may be quite slow.

Migrating is sketchy because the IWKEY has to be exposed to host userspace. But, I think the migration aspect is a secondary discussion.

Quoted 2 lines

c) Guest wants to manage its own non-random key. Host lets it and context switches it.

This is essentially a variant of (b). In both cases, the host has full control over the guest's key.

Quoted 10 lines

d) Guest does not need KL and leaves CR4.KL clear. Host does whatever it wants with no overhead.

All of these have tradeoffs.

My current thought is that, if Linux is going to support Key Locker, then this all needs to be explicitly controlled. On initial boot, Linux should not initialize Key Locker. Upon explicit administrator request (via sysfs?), Linux will initialize Key Locker in the mode requested by the administrator.

Quoted 5 lines

Deferring KL usage to post-boot can work, but KVM shouldn't be allowed to expose KL to a guest until KL has been explicitly configured in the host. If KVM can spawn KL guests before the host is configured, the sysfs knob would have to deal with the case where the desired configuration is incompatible with exposing KL to a guest.

There could be a host configuration "guest_only", perhaps.

Quoted 10 lines
Quoted 7 lines

Modes could include:

native_random_key: Use a random key per the ISA.

native_kernel_key_remember: Use a random key but load it as a non-random key. Remember the key in kernel memory and use it for S3 resume, etc.

What would be the motivation for this mode? It largely defeats the value proposition of KL, no?

It lets userspace use KL with some degree of security.

Quoted 10 lines
Quoted 10 lines

native_kernel_key_backup: Use a random key, put it in the backup storage, and forget it. Use the backup for resume, etc.

native_kernel_key_norestore: Use a random key. The key is lost on any power transition that forgets the key. Backup is not used.

paravirt_any: Ask the hypervisor to handle keying. Any mechanism is acceptable.

Quoted 7 lines
Quoted 2 lines

paravirt_random: Ask the hypervisor for a random key. Only succeeds if we get an actual random key.

AFAIK, there's no way for the guest to verify that it got a truly random key. Hell, the guest can't even easily verify that KL is even supported. The host can lie about CPUID and CR4.KL, and intercept all KL instructions via #UD by running the guest with CR4.KL=0.

The guest can use TDX. Oh wait, TDX doesn't support KL.

That being said, a host attack on the guest of this sort would be quite slow.

Quoted 10 lines

I also don't see any reason to define a paravirt interface for a truly random key. Using a random key all but requires a single guest to have exclusive access to KL, and in that case the host can simply expose KL to only that guest.

Quoted 1 line

Does this make sense?

I really want to use see concrete guest use cases before we start adding paravirt interfaces.

I want to see concrete guest use cases before we start adding *any* guest support. And this cuts both ways -- I think that, until the guest use cases are at least somewhat worked out, Linux should certainly not initialize KL by default on boot if the CPUID hypervisor bit is set.

#25 lore raw
Quoted 20 lines · Andy Lutomirski
Quoted 19 lines · Sean Christopherson
Quoted 17 lines · Andy Lutomirski
Quoted 5 lines · Chang Seok

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

I read that series. This is not a good solution.

I can think of at least a few reasonable ways that a host and a guest can cooperate to, potentially, make KL useful.

a) Host knows that the guest will never migrate, and guest delegates IWKEY management to the host. The host generates a random key and does not permit the guest to use LOADIWKEY. The guest shares the random key with the host. Of course, this means that a host key handle that leaks to a guest can be used within the guest.

Quoted 7 lines
Quoted 4 lines

If the guest and host share a random key, then they also share the key handle. And that handle+key would also need to be shared across all guests. I doubt this option is acceptable on the security front.

Indeed. Oddly, SGX has the exact same problem for any scenario in which SGX is used for HSM-like functionality, and people still use SGX.

The entire PRM/EPC shares a single key, but SGX doesn't rely on encryption to isolate enclaves from other software, including other enclaves. E.g. Intel could ship a CPU with the EPC backed entirely by on-die cache and avoid hardware encryption entirely.

Quoted 2 lines

However, I suspect that there will be use cases in which exactly one VM is permitted to use KL. Qubes might want that (any Qubes people around?)

#26 lore raw
Quoted 26 lines · Sean Christopherson
Quoted 25 lines · Andy Lutomirski
Quoted 23 lines · Sean Christopherson
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Chang Seok

First of all, there is an RFC series for KVM [2].

Each CPU has one internal key state so it needs to reload it between guest and host if both are enabled. The proposed approach enables it exclusively; expose it to guests only when disabled in a host. Then, I guess a guest may enable it.

I read that series. This is not a good solution.

Quoted 8 lines

I can think of at least a few reasonable ways that a host and a guest can cooperate to, potentially, make KL useful.

a) Host knows that the guest will never migrate, and guest delegates IWKEY management to the host. The host generates a random key and does not permit the guest to use LOADIWKEY. The guest shares the random key with the host. Of course, this means that a host key handle that leaks to a guest can be used within the guest.

If the guest and host share a random key, then they also share the key handle. And that handle+key would also need to be shared across all guests. I doubt this option is acceptable on the security front.

Quoted 7 lines
Quoted 2 lines

Indeed. Oddly, SGX has the exact same problem for any scenario in which SGX is used for HSM-like functionality, and people still use SGX.

The entire PRM/EPC shares a single key, but SGX doesn't rely on encryption to isolate enclaves from other software, including other enclaves. E.g. Intel could ship a CPU with the EPC backed entirely by on-die cache and avoid hardware encryption entirely.

Ha! I belatedly see your point: in the end, virtualized KL would also rely on a trusted entity to isolate its sensitive data via paging-like mechanisms.

The difference in my mind is that encryption is a means to an end for SGX, whereas hiding the key is the entire point of KL. E.g. the guest is already relying on the VMM to isolate its code and data, adding KL doesn't change that. Sharing an IWKEY across multiple guests would add intra-VM protection, at the cost of making cross-VM attacks easier to some degree.

#27 lore raw
In reply to Andy Lutomirski
Quoted 7 lines · Andy Lutomirski
Quoted 6 lines · Chang Seok
Quoted 4 lines · Andy Lutomirski

I have high-level questions:

What is the expected use case?

Quoted 7 lines
Quoted 3 lines

The wrapping key here is only used for new AES instructions.

I’m aware of their potential use cases for encrypting file system or disks.

I would like to understand what people are actually going to do with this. Give me a user story or two, please. If it turns out to be useless, I would rather not merge it.

Hi Andy,

V3 was posted here with both cover letter and code changes to address this:

    https://lore.kernel.org/lkml/[email protected]/

Appreciate, if you can comment on the use case at least.

Thanks,
Chang