Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp293254pxm; Tue, 22 Feb 2022 23:56:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJwY5An8OcbvKF5T5MRKqHQhtOFpstmuFsnpIIQuM+4RmPByiiEPmmxLrzkGhYkbRtoCWtzq X-Received: by 2002:a17:906:2811:b0:6ce:eacf:5210 with SMTP id r17-20020a170906281100b006ceeacf5210mr23022289ejc.618.1645603007033; Tue, 22 Feb 2022 23:56:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645603007; cv=none; d=google.com; s=arc-20160816; b=CmjeQhhsGh6uE62ElsuDV1rsVxr7ANFwIULtwde73TLkTxj7QarqNrnR9AFq0tLlc6 R34yonZZUt4vQ0OUg1GHS9oqP8Yohi0TdiNknV86HcqE9QesqbtyNxGhnFWREOe14DyQ FvTRZc1g1g5NO5EGcxUDUVbdqK63kkmonCSy3tiDsojEhNskKME1joFlNs0FjC47XfF5 wYI16aq2hC3w4Gpvez1d5opNOjZtP5xjrAYqP5vEUJvmBc+3F668SpaUzN7eiumfyhtW 8Fl/hteUEYVcnVBznqPcqbT3Iq4O8RDAf91kWqfhpW8LVNEV0M40hZgqhUYNrGS7EQEv Ay1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=qYkDytURAXNMHGMPP8OQ9zhn4VwiNiCEZi6sdYFlMXo=; b=k++AEF1hJAiySLwDLxT6gUqT1uiZfH9H6ie7H4KwxNAGUjQUO2LbByZx9tOcoV9v8L hC1mvXB7T+N7MDg2T6yNtUxEXvOybrv/rpXI1MmIQ4eX8rp2wA7iu9Zbg99Zcey/Qglp ZR8HJVWx3Jb/bUSb523FHv0bPWCeajy+57UmzELO2AYBzJT/72PiM9kjLHfN4jbQ4yLk uWialdvvtCCX1uh1R34GJ7V2/UijUPejS1WVANIStunG2fJwnnta3kYPjoN0dU76clRD u9teVe8U18mYG+PDU6hZGFSHfEiSdl1cYNSbCwjvcVz6LxPDt+nKsLD0GHZguXroL6uG Vz8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UGD+m+9y; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dd2si14964307ejc.334.2022.02.22.23.56.23; Tue, 22 Feb 2022 23:56:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UGD+m+9y; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238097AbiBWHHj (ORCPT + 99 others); Wed, 23 Feb 2022 02:07:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238533AbiBWHHj (ORCPT ); Wed, 23 Feb 2022 02:07:39 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38508A1B6 for ; Tue, 22 Feb 2022 23:07:12 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D289B614CC for ; Wed, 23 Feb 2022 07:07:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AC5BC340E7; Wed, 23 Feb 2022 07:07:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1645600031; bh=JRTomZkeDAUGX5ynEJbc79c4T54YfaZMZeCXKOwpl04=; h=From:To:Cc:Subject:Date:From; b=UGD+m+9ys6vYs+O69ec838zXBUKQPgvlrWUEzUBGeZ2yNodEPuk5ydkCXCQJa4tPX xNwk7Li/1mscATLbIElcxQJpw8ykrOwT35NWGLXS2FFi0AX8buuoJ04zRWNEjaMn28 KjbbchY10gGj3Vrq6in9kBs3hmu02trhtgyUWZoz8J71R20KUWpsN7q94S4q071PWO iDhCqMFXuy5oSmhcUmL4Y76sEUseSP6Gvyj45a4Y/Gn8/e/pzBBMlVtbFjft4eCNov 8bIUcC+S6d9GIGLsfIuiisMdqmSJRUDsbe4bnABokt6RYqVVKFWzetuoTbwIUOVaS1 mKRl+dSEpR5/g== From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@kernel.org, Ard Biesheuvel Subject: [PATCH v2] crypto: crypto_xor - use helpers for unaligned accesses Date: Wed, 23 Feb 2022 08:07:01 +0100 Message-Id: <20220223070701.1457542-1-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3600; h=from:subject; bh=JRTomZkeDAUGX5ynEJbc79c4T54YfaZMZeCXKOwpl04=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBiFd0U4Tb9euK9PgHy2S/6vYudag7p/Ij573eBaFnl ZkvCJwuJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYhXdFAAKCRDDTyI5ktmPJBiTC/ wNEoRQDmhh47aJYN0SgqB3KtvZOAiyQZlG69DAFxv6sO7pgl9GCO04VuK/+MtifRSCnEmDKDh/hPGV zx+CFl1jUWJisQjot4iA217kliuhnL7+pARE1Q8VuEHFBMtxbq39VLQNcYuPOgZ0LgAYICvSsHY1LC ErkLSjgnd+L0QSzr5l6ko8bfT2Q8IAub6MKk8GeaIdm5mdpaeg5H6Lq2tPUodZmnT0I7SvNcJ5XANI Wwse6ArIOPp+Uiv+UP7WODF8CqNYwIZTBOjILS5ndaLujh7miVbmwG/hSKdiVbiGVx9eYQsJN2aJfw sAnH4uVo7IoxmySNVlsbmuYuL3GZTJwuIdEsbQqHvZvyjmrBwgWTrpETAXcG+PLkiLYeI4v3bwxAsw qxUkHG8NiS6AgJpGvJqbcGpinmL9mcqpql3cEdpQnHegdHKwDB1CFj9IfgxIPO6ITOG4UUBmKbYT5M 5132JcgZ3lrgYNMv3FOW788Ti+08GbUszjXWljGwNntCg= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Dereferencing a misaligned pointer is undefined behavior in C, and may result in codegen on architectures such as ARM that trigger alignments traps and expensive fixups in software. Instead, use the get_aligned()/put_aligned() accessors, which are cheap or even completely free when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y. In the converse case, the prior alignment checks ensure that the casts are safe, and so no unaligned accessors are necessary. Signed-off-by: Ard Biesheuvel --- v2: fix issue in crypto_xor_cpy() crypto/algapi.c | 24 +++++++++++++++++--- include/crypto/algapi.h | 10 ++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index 9f15e11f5d73..a6a10f2ee0dc 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -1008,7 +1008,13 @@ void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) } while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) { - *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2; + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + u64 l = get_unaligned((u64 *)src1) ^ + get_unaligned((u64 *)src2); + put_unaligned(l, (u64 *)dst); + } else { + *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2; + } dst += 8; src1 += 8; src2 += 8; @@ -1016,7 +1022,13 @@ void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) } while (len >= 4 && !(relalign & 3)) { - *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2; + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + u32 l = get_unaligned((u32 *)src1) ^ + get_unaligned((u32 *)src2); + put_unaligned(l, (u32 *)dst); + } else { + *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2; + } dst += 4; src1 += 4; src2 += 4; @@ -1024,7 +1036,13 @@ void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) } while (len >= 2 && !(relalign & 1)) { - *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2; + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + u16 l = get_unaligned((u16 *)src1) ^ + get_unaligned((u16 *)src2); + put_unaligned(l, (u16 *)dst); + } else { + *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2; + } dst += 2; src1 += 2; src2 += 2; diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index f76ec723ceae..f50c5d1725da 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -13,6 +13,8 @@ #include #include +#include + /* * Maximum values for blocksize and alignmask, used to allocate * static buffers that are big enough for any combination of @@ -154,9 +156,11 @@ static inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size) (size % sizeof(unsigned long)) == 0) { unsigned long *d = (unsigned long *)dst; unsigned long *s = (unsigned long *)src; + unsigned long l; while (size > 0) { - *d++ ^= *s++; + l = get_unaligned(d) ^ get_unaligned(s++); + put_unaligned(l, d++); size -= sizeof(unsigned long); } } else { @@ -173,9 +177,11 @@ static inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2, unsigned long *d = (unsigned long *)dst; unsigned long *s1 = (unsigned long *)src1; unsigned long *s2 = (unsigned long *)src2; + unsigned long l; while (size > 0) { - *d++ = *s1++ ^ *s2++; + l = get_unaligned(s1++) ^ get_unaligned(s2++); + put_unaligned(l, d++); size -= sizeof(unsigned long); } } else { -- 2.30.2