cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 1629595c7ee3cc336b5685c9c3630e7f9a11648c
parent dc737cafcbc0d030d797b66a45ccf9fe92d09700
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon,  6 Feb 2023 15:15:44 -0600

Only do vmmcall in aes_generic if dummy key detected

Diffstat:
March/x86/kvm/mmu/mmu.c | 3---
Mcrypto/aes_generic.c | 57+++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c @@ -4025,9 +4025,6 @@ static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu, break; case CPC_TRACK_PAGES: BUG_ON(modes[i] != KVM_PAGE_TRACK_EXEC); - /* future readers: ik this part is messy, but handling - * instructions on page boundaries has many cases - * when optimizing for the common case (not on boundary) */ if (!inst_fetch || !fault->present) return false; diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c @@ -1140,6 +1140,8 @@ int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, { struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + CPC_WARN("Set key\n"); + return aes_expandkey(ctx, in_key, key_len); } EXPORT_SYMBOL_GPL(crypto_aes_set_key); @@ -1177,13 +1179,6 @@ EXPORT_SYMBOL_GPL(crypto_aes_set_key); static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) { - int cpu; - - CPC_WARN("Running AES-Generic!"); - - cpu = get_cpu(); - CPC_DO_VMMCALL(SIGNAL, CPC_GUEST_START_TRACK, 0); - const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); u32 b0[4], b1[4]; const u32 *kp = ctx->key_enc + 4; @@ -1204,24 +1199,46 @@ static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) f_nround(b0, b1, kp); } - f_nround(b1, b0, kp); - f_nround(b0, b1, kp); - f_nround(b1, b0, kp); - f_nround(b0, b1, kp); - f_nround(b1, b0, kp); - f_nround(b0, b1, kp); - f_nround(b1, b0, kp); - f_nround(b0, b1, kp); - f_nround(b1, b0, kp); - f_lround(b0, b1, kp); + CPC_WARN("Running AES-Generic! %08x %08x %08x %08x\n", + kp[0], kp[1], kp[2], kp[3]); + + const u8 cpc_key[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + if (1 || !memcmp(kp, cpc_key, 16)) { + spinlock_t lock; + spin_lock_init(&lock); + spin_lock_irq(&lock); + CPC_DO_VMMCALL(KVM_HC_CPC_VMMCALL_SIGNAL, CPC_GUEST_START_TRACK, 0); + + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_lround(b0, b1, kp); + + CPC_DO_VMMCALL(KVM_HC_CPC_VMMCALL_SIGNAL, CPC_GUEST_STOP_TRACK, 0); + spin_unlock_irq(&lock); + } else { + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_nround(b0, b1, kp); + f_nround(b1, b0, kp); + f_lround(b0, b1, kp); + } put_unaligned_le32(b0[0], out); put_unaligned_le32(b0[1], out + 4); put_unaligned_le32(b0[2], out + 8); put_unaligned_le32(b0[3], out + 12); - - CPC_DO_VMMCALL(KVM_HC_CPC_VMMCALL_SIGNAL, CPC_GUEST_STOP_TRACK, 0); - put_cpu(); } /* decrypt a block of text */