From patchwork Wed Nov 22 19:51:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10070887 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.web.codeaurora.org (Postfix) with ESMTP id D0FF560595 for ; Wed, 22 Nov 2017 19:53:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C609F29E29 for ; Wed, 22 Nov 2017 19:53:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BB13129E2E; Wed, 22 Nov 2017 19:53:23 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable 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 177ED29E29 for ; Wed, 22 Nov 2017 19:53:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751249AbdKVTxH (ORCPT ); Wed, 22 Nov 2017 14:53:07 -0500 Received: from mail-io0-f195.google.com ([209.85.223.195]:46980 "EHLO mail-io0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751260AbdKVTwL (ORCPT ); Wed, 22 Nov 2017 14:52:11 -0500 Received: by mail-io0-f195.google.com with SMTP id q64so11536521iof.13; Wed, 22 Nov 2017 11:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nEN7B7i/FcV90zoJllQyuDcIGBaywFGZOXv0nnlyrNA=; b=Le29/NLnwtUxfFXkbsl9WwmrBruG8vMez57OUYTgmAKfJ+nbYqlkiHrcmYjA1dJ5KV ONF3lcNNYchUGdDrPzeYESJqItQaGRVycAx/aovNqxaW78pnpTYhQgpAFl498Ol8kOJK 1JkFfe44VdDQ4+bb729S4hTmgkTEaN73HoIGwZ3+m7K1YZ5NQtJTr5zwWSsN9DVHC1bY nJ4Ac2QU+1bYzc+X1ZhJH/VSOI5FnkNqmbEfZ+ZYF5fhX9n0WSJISj9G3GeMG3Kfi3oF gA+1nuoQ9JKgCkcjW//CbWDKWY84k5mtISK4ubh9A3V9f6vfX4vtBD1Inn5d3oJVwfUa x7bg== 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; bh=nEN7B7i/FcV90zoJllQyuDcIGBaywFGZOXv0nnlyrNA=; b=UDXPUPSSs/hNToFq3ZR9cR12nvp880nk1M8ONjuixfAtwAq7dm7j7aBNLm/7DupsXy LAuap/dIFMr9r7pywRjV16oN2x6MjXvCZPvn9M/K/jpv7a7A4SPuqTHv2qO7sEyWTAjg AjJdZUXMABLpseQeypG7kY/Oq2N8X+Qn5fFwgis7HT9sB+u5P7wRgqlY9sWIVmbrV3+n 5m2yt7ZYoR3EE9VgzbSDpTf/YpJLCX1KqUtP04ZNSdqy3Y3eoKaYKVEZDGoMynOWrmOv vC9KZSx1m7/5KqXSDVahCsZJ7SrZczWeT+iXHHOjG72Ztgwt0wk6QxmQ79amXzGB3tYP y90w== X-Gm-Message-State: AJaThX6GDbhNtZ7ihdjIn53i8K9T6Agjs6m5XPiFtVQ/+jwuajX4Q//6 ET94LxldRrk0+1SbPtLl93gCW+Bf X-Google-Smtp-Source: AGs4zMZRZDRa2p8NfrEVv69nTXsxfMmU7U73BFdHHAiwEy4RtIZyDBSXONdrvdpOl8FaxCDI2nLmrQ== X-Received: by 10.107.183.76 with SMTP id h73mr24443545iof.154.1511380330277; Wed, 22 Nov 2017 11:52:10 -0800 (PST) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.175.88]) by smtp.gmail.com with ESMTPSA id f193sm2281542ita.27.2017.11.22.11.52.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 22 Nov 2017 11:52:09 -0800 (PST) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Theodore Ts'o , "Jason A . Donenfeld" , Martin Willi , Ard Biesheuvel , linux-kernel@vger.kernel.org, Eric Biggers Subject: [PATCH 2/5] crypto: chacha20 - Use unaligned access macros when loading key and IV Date: Wed, 22 Nov 2017 11:51:36 -0800 Message-Id: <20171122195139.121269-3-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.0.448.gf294e3d99a-goog In-Reply-To: <20171122195139.121269-1-ebiggers3@gmail.com> References: <20171122195139.121269-1-ebiggers3@gmail.com> 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 From: Eric Biggers The generic ChaCha20 implementation has a cra_alignmask of 3, which ensures that the key passed into crypto_chacha20_setkey() and the IV passed into crypto_chacha20_init() are 4-byte aligned. However, these functions are also called from the ARM and ARM64 implementations of ChaCha20, which intentionally do not have a cra_alignmask set. This is broken because 32-bit words are being loaded from potentially-unaligned buffers without the unaligned access macros. Fix it by using the unaligned access macros when loading the key and IV. Signed-off-by: Eric Biggers Acked-by: Ard Biesheuvel --- crypto/chacha20_generic.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c index ec84e7837aac..b5a10ebf1b82 100644 --- a/crypto/chacha20_generic.c +++ b/crypto/chacha20_generic.c @@ -9,16 +9,12 @@ * (at your option) any later version. */ +#include #include #include #include #include -static inline u32 le32_to_cpuvp(const void *p) -{ - return le32_to_cpup(p); -} - static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, unsigned int bytes) { @@ -53,10 +49,10 @@ void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv) state[9] = ctx->key[5]; state[10] = ctx->key[6]; state[11] = ctx->key[7]; - state[12] = le32_to_cpuvp(iv + 0); - state[13] = le32_to_cpuvp(iv + 4); - state[14] = le32_to_cpuvp(iv + 8); - state[15] = le32_to_cpuvp(iv + 12); + state[12] = get_unaligned_le32(iv + 0); + state[13] = get_unaligned_le32(iv + 4); + state[14] = get_unaligned_le32(iv + 8); + state[15] = get_unaligned_le32(iv + 12); } EXPORT_SYMBOL_GPL(crypto_chacha20_init); @@ -70,7 +66,7 @@ int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, return -EINVAL; for (i = 0; i < ARRAY_SIZE(ctx->key); i++) - ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32)); + ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32)); return 0; }