Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2649392imj; Mon, 11 Feb 2019 06:27:44 -0800 (PST) X-Google-Smtp-Source: AHgI3IbDn2lNfDcINoNB/HU63coUZJWQ2drziuLoliqK738ZbBlV/tCgiwgWgQHVOrTtgCCuwf3N X-Received: by 2002:a63:da45:: with SMTP id l5mr33995419pgj.111.1549895264180; Mon, 11 Feb 2019 06:27:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549895264; cv=none; d=google.com; s=arc-20160816; b=FlE4LGfxMYLoxiDau7Of3Kmjr3XfzUaAiFnTi9G/2v8xmxgMcUvMKfHGeUPOB4EiE1 7fMkBvK9KxrSSHJLzGDcQy53rmkFz1hODvO9fl73f5eUzIO4FhBwkNOIbpw86LOf5oKB 7rvSUi2NcYCqBPyufzLOt0TPWSODDXGaGohTZweNaTkn0HuRmW27De7k4KWcyZgiahJd jE3qhfAUchSJ5+r+uBCXAtdJ114rbpL64KZrHvvyTEHCoFoWU+fZshoI3UBsTLFgVqlk QxCzhEjLSAc0JkD8h4XomFnlAU5268oG+jM1VFjaju1NiUyqF/x9ANmlo561OQQ4heDP kZVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=e6VgQxsrStc83oRUmGBgmQupxuT3EA7R3MblX1k3nqc=; b=TEZ2defUxl6T0PuXyfLZ7VrxwAcCrcKmFeKwG1UR0dsn3WwPOw+QIot9FmCMlOHRLG 70J14DXg6n4wEc0Ifr1EAJqLn+1d05YpLGIn0YCKPpN0DAdYr7XLd1/D6JlqCl0WOVuP 741KrWiDoqgyOKUXzMcwmx/BmnWqMCldBnF4P0sKKtKrFwFRFSsMTRyVIW7RMtndFbWw E1CrwRFW3YpqLjM2c+UL/9zIZC9mfAqhiYndwB4ExVcgHiPDzEU8rZCX1EHoyUR5adEh 8fFV/eKqU1CZUokR+GCW0Yk58cHS8bOgWF+LP+LnjWo+5Gy5tsckhoXk0bdhUxeTkVer BEMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bhvTnlL2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n14si1545935plp.257.2019.02.11.06.27.28; Mon, 11 Feb 2019 06:27:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bhvTnlL2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728612AbfBKOVw (ORCPT + 99 others); Mon, 11 Feb 2019 09:21:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:54632 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728606AbfBKOVt (ORCPT ); Mon, 11 Feb 2019 09:21:49 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C8BE3222AA; Mon, 11 Feb 2019 14:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549894908; bh=2PAryEP6tKHojVcLy3vRribLfeLAENPs4f3gpanAJVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bhvTnlL2g67fL3vJ8wDnuWFUhsJ2Rr3DChASoeR3pPBRZr+2Ba76lFvmKTuIkhfrG bSb7OPAiI6Qn+KRiJC7or/3gRZu7M4xE0NWHdhhYUMKyz3QFc62xthWVejkHEETZeb gpcU89ieWOlj162FA9PzQVflgXe3JfMED+GH3M+k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ard Biesheuvel , Eric Biggers , Herbert Xu , Sasha Levin Subject: [PATCH 4.20 029/352] crypto: aes_ti - disable interrupts while accessing S-box Date: Mon, 11 Feb 2019 15:14:16 +0100 Message-Id: <20190211141848.192803545@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 0a6a40c2a8c184a2fb467efacfb1cd338d719e0b ] In the "aes-fixed-time" AES implementation, disable interrupts while accessing the S-box, in order to make cache-timing attacks more difficult. Previously it was possible for the CPU to be interrupted while the S-box was loaded into L1 cache, potentially evicting the cachelines and causing later table lookups to be time-variant. In tests I did on x86 and ARM, this doesn't affect performance significantly. Responsiveness is potentially a concern, but interrupts are only disabled for a single AES block. Note that even after this change, the implementation still isn't necessarily guaranteed to be constant-time; see https://cr.yp.to/antiforgery/cachetiming-20050414.pdf for a discussion of the many difficulties involved in writing truly constant-time AES software. But it's valuable to make such attacks more difficult. Reviewed-by: Ard Biesheuvel Signed-off-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/Kconfig | 3 ++- crypto/aes_ti.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 05c91eb10ca1..4f24acac3d48 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1006,7 +1006,8 @@ config CRYPTO_AES_TI 8 for decryption), this implementation only uses just two S-boxes of 256 bytes each, and attempts to eliminate data dependent latencies by prefetching the entire table into the cache at the start of each - block. + block. Interrupts are also disabled to avoid races where cachelines + are evicted when the CPU is interrupted to do something else. config CRYPTO_AES_586 tristate "AES cipher algorithms (i586)" diff --git a/crypto/aes_ti.c b/crypto/aes_ti.c index 03023b2290e8..1ff9785b30f5 100644 --- a/crypto/aes_ti.c +++ b/crypto/aes_ti.c @@ -269,6 +269,7 @@ static void aesti_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) const u32 *rkp = ctx->key_enc + 4; int rounds = 6 + ctx->key_length / 4; u32 st0[4], st1[4]; + unsigned long flags; int round; st0[0] = ctx->key_enc[0] ^ get_unaligned_le32(in); @@ -276,6 +277,12 @@ static void aesti_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) st0[2] = ctx->key_enc[2] ^ get_unaligned_le32(in + 8); st0[3] = ctx->key_enc[3] ^ get_unaligned_le32(in + 12); + /* + * Temporarily disable interrupts to avoid races where cachelines are + * evicted when the CPU is interrupted to do something else. + */ + local_irq_save(flags); + st0[0] ^= __aesti_sbox[ 0] ^ __aesti_sbox[128]; st0[1] ^= __aesti_sbox[32] ^ __aesti_sbox[160]; st0[2] ^= __aesti_sbox[64] ^ __aesti_sbox[192]; @@ -300,6 +307,8 @@ static void aesti_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) put_unaligned_le32(subshift(st1, 1) ^ rkp[5], out + 4); put_unaligned_le32(subshift(st1, 2) ^ rkp[6], out + 8); put_unaligned_le32(subshift(st1, 3) ^ rkp[7], out + 12); + + local_irq_restore(flags); } static void aesti_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) @@ -308,6 +317,7 @@ static void aesti_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) const u32 *rkp = ctx->key_dec + 4; int rounds = 6 + ctx->key_length / 4; u32 st0[4], st1[4]; + unsigned long flags; int round; st0[0] = ctx->key_dec[0] ^ get_unaligned_le32(in); @@ -315,6 +325,12 @@ static void aesti_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) st0[2] = ctx->key_dec[2] ^ get_unaligned_le32(in + 8); st0[3] = ctx->key_dec[3] ^ get_unaligned_le32(in + 12); + /* + * Temporarily disable interrupts to avoid races where cachelines are + * evicted when the CPU is interrupted to do something else. + */ + local_irq_save(flags); + st0[0] ^= __aesti_inv_sbox[ 0] ^ __aesti_inv_sbox[128]; st0[1] ^= __aesti_inv_sbox[32] ^ __aesti_inv_sbox[160]; st0[2] ^= __aesti_inv_sbox[64] ^ __aesti_inv_sbox[192]; @@ -339,6 +355,8 @@ static void aesti_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) put_unaligned_le32(inv_subshift(st1, 1) ^ rkp[5], out + 4); put_unaligned_le32(inv_subshift(st1, 2) ^ rkp[6], out + 8); put_unaligned_le32(inv_subshift(st1, 3) ^ rkp[7], out + 12); + + local_irq_restore(flags); } static struct crypto_alg aes_alg = { -- 2.19.1