From patchwork Sun Sep 30 08:58:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10621337 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 93DE11895 for ; Sun, 30 Sep 2018 08:59:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 84FD02962D for ; Sun, 30 Sep 2018 08:59:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 76A2329633; Sun, 30 Sep 2018 08:59:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 01F4329635 for ; Sun, 30 Sep 2018 08:59:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727829AbeI3PbW (ORCPT ); Sun, 30 Sep 2018 11:31:22 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:39917 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727818AbeI3PbW (ORCPT ); Sun, 30 Sep 2018 11:31:22 -0400 Received: by mail-wm1-f65.google.com with SMTP id q8-v6so5800159wmq.4 for ; Sun, 30 Sep 2018 01:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hL3qJaZqPoF49D2S/c5KQsR5E6Cld1xrmPIJV/R9eiM=; b=dBRGLB55DPEvghfqx2eNuxcY4I9CFgEDt+PP5W0Hs1GsDacI+wGAY/Nmj5iOTBLT69 0CRuQ1P4IFENNUy3JYEfrYpOVCMcrqUIiAPGgFs5wGTxyije29xqU2OWiO1HEVRGzQFQ pS1E5v8f91iW6yBqq8ejxIY8VIcHJuVwj4OoQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hL3qJaZqPoF49D2S/c5KQsR5E6Cld1xrmPIJV/R9eiM=; b=db45m3sFCnVXNk50omW/ARnywzw6y7IA5hEnEufgGcMGwFXpH90MfgpIFiuV08dYOe gLd2zBItXbn0NKhXi398WfbVLUf0gUuRe5EDv87EQ+v7ZeKuHtVkvSEdv3bU8C4/mMkU kpN0C0cMNy/akFgNv2G837j5LBbxMJGqWTTbIkATdgWc0OLjMeEt3T7jyHAO+quVTZxG gm0mbZFnW1lXvlBMFXlvRznrxDTtEBevSUsDQT4iouqhjvXuwhW4CMew/+1VGa/gJ47r IPwSQ7+8jNUCkuw3UMhEZg8MpLGvepKlJXIldD7I7i8YTglTZ+aCY7t5eCxS1uF37IbS FYbA== X-Gm-Message-State: ABuFfojkZib/4VNd/RbzUQTbGk65jHnyb66kt9/rI+RfF4ppoTTYt1q9 VI8TWk4WpGXUJr/siwjMcdl4sv/aqh0= X-Google-Smtp-Source: ACcGV61FywHyu/fhn/I3laoq92tlmmdEqcDM3GPdVPdK+DpAPBStmUak58YyHyu02jCvsTbGjxdceg== X-Received: by 2002:a1c:f11a:: with SMTP id p26-v6mr5360192wmh.92.1538297951866; Sun, 30 Sep 2018 01:59:11 -0700 (PDT) Received: from rev03.home ([2a01:cb1d:112:6f00:4507:1640:20db:cc08]) by smtp.gmail.com with ESMTPSA id l140-v6sm10816540wmb.24.2018.09.30.01.59.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Sep 2018 01:59:10 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, omosnace@redhat.com, Ard Biesheuvel Subject: [PATCH 2/2] crypto: aegis/generic - fix for big endian systems Date: Sun, 30 Sep 2018 10:58:59 +0200 Message-Id: <20180930085859.15038-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180930085859.15038-1-ard.biesheuvel@linaro.org> References: <20180930085859.15038-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use the correct __le32 annotation and accessors to perform the single round of AES encryption performed inside the AEGIS transform. Otherwise, tcrypt reports: alg: aead: Test 1 failed on encryption for aegis128-generic 00000000: 6c 25 25 4a 3c 10 1d 27 2b c1 d4 84 9a ef 7f 6e alg: aead: Test 1 failed on encryption for aegis128l-generic 00000000: cd c6 e3 b8 a0 70 9d 8e c2 4f 6f fe 71 42 df 28 alg: aead: Test 1 failed on encryption for aegis256-generic 00000000: aa ed 07 b1 96 1d e9 e6 f2 ed b5 8e 1c 5f dc 1c While at it, let's refer to the first precomputed table only, and derive the other ones by rotation. This reduces the D-cache footprint by 75%, and shouldn't be too costly or free on load/store architectures (and X86 has its own AES-NI based implementation) Fixes: f606a88e5823 ("crypto: aegis - Add generic AEGIS AEAD implementations") Cc: # v4.18+ Signed-off-by: Ard Biesheuvel --- crypto/aegis.h | 23 +++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/crypto/aegis.h b/crypto/aegis.h index f1c6900ddb80..84d3e07a3c33 100644 --- a/crypto/aegis.h +++ b/crypto/aegis.h @@ -21,7 +21,7 @@ union aegis_block { __le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)]; - u32 words32[AEGIS_BLOCK_SIZE / sizeof(u32)]; + __le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)]; u8 bytes[AEGIS_BLOCK_SIZE]; }; @@ -59,22 +59,19 @@ static void crypto_aegis_aesenc(union aegis_block *dst, { u32 *d = dst->words32; const u8 *s = src->bytes; - const u32 *k = key->words32; + const __le32 *k = key->words32; const u32 *t0 = crypto_ft_tab[0]; - const u32 *t1 = crypto_ft_tab[1]; - const u32 *t2 = crypto_ft_tab[2]; - const u32 *t3 = crypto_ft_tab[3]; u32 d0, d1, d2, d3; - d0 = t0[s[ 0]] ^ t1[s[ 5]] ^ t2[s[10]] ^ t3[s[15]] ^ k[0]; - d1 = t0[s[ 4]] ^ t1[s[ 9]] ^ t2[s[14]] ^ t3[s[ 3]] ^ k[1]; - d2 = t0[s[ 8]] ^ t1[s[13]] ^ t2[s[ 2]] ^ t3[s[ 7]] ^ k[2]; - d3 = t0[s[12]] ^ t1[s[ 1]] ^ t2[s[ 6]] ^ t3[s[11]] ^ k[3]; + d0 = t0[s[ 0]] ^ rol32(t0[s[ 5]], 8) ^ rol32(t0[s[10]], 16) ^ rol32(t0[s[15]], 24); + d1 = t0[s[ 4]] ^ rol32(t0[s[ 9]], 8) ^ rol32(t0[s[14]], 16) ^ rol32(t0[s[ 3]], 24); + d2 = t0[s[ 8]] ^ rol32(t0[s[13]], 8) ^ rol32(t0[s[ 2]], 16) ^ rol32(t0[s[ 7]], 24); + d3 = t0[s[12]] ^ rol32(t0[s[ 1]], 8) ^ rol32(t0[s[ 6]], 16) ^ rol32(t0[s[11]], 24); - d[0] = d0; - d[1] = d1; - d[2] = d2; - d[3] = d3; + d[0] = cpu_to_le32(d0 ^ le32_to_cpu(k[0])); + d[1] = cpu_to_le32(d1 ^ le32_to_cpu(k[1])); + d[2] = cpu_to_le32(d2 ^ le32_to_cpu(k[2])); + d[3] = cpu_to_le32(d3 ^ le32_to_cpu(k[3])); } #endif /* _CRYPTO_AEGIS_H */