From patchwork Fri Dec 8 11:55:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10102401 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 B7951605BA for ; Fri, 8 Dec 2017 11:55:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B49BD28AA8 for ; Fri, 8 Dec 2017 11:55:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A915D28BB4; Fri, 8 Dec 2017 11:55:18 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 F073D28B7D for ; Fri, 8 Dec 2017 11:55:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753082AbdLHLzP (ORCPT ); Fri, 8 Dec 2017 06:55:15 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:43783 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752989AbdLHLzN (ORCPT ); Fri, 8 Dec 2017 06:55:13 -0500 Received: by mail-wr0-f195.google.com with SMTP id z34so10555909wrz.10 for ; Fri, 08 Dec 2017 03:55:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=MAkj+1Hbc3gdJL1OingPAE4YW1crGN/Ss4+8MX4UTnk=; b=QVNhw17+TUh/SYe4f/4UEr9kNuGkXrgefay5Fe/sHpBS4uId0+9YnuhD4p7jf9gNje tdrj48Mzryv5sU0Hj66KtGMqzLgN8RaQ9ix3rzq90UxBI9yTpwwLlHcAu/nDYFO7lnZY 5N6kE1DfTbgK5ZJGv6mdAYVbuxKgr+V2BkoTI= 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; bh=MAkj+1Hbc3gdJL1OingPAE4YW1crGN/Ss4+8MX4UTnk=; b=YbGn75G4nRmdPxeJR7mRExJtVniugiIIjBQv+qGF2NVKdV5qwOQzb/xTlQiIjQLoDX CPiDR/JpzTnYBx6w7dKYprLEFL0Kx/etNA82eIigxZFUiQs7wk1rjSFP+9LQPzP4t0nw 6dDIG9JBAFamfmLEL/mheIQLg6ue0oh7eIc04XFxkm+AhXnqvDkHMgMpCgvJv8BVVSGt LeB5jwLpH6Lqs8zJo67Xgp3H6EUaGZzC7RDFeUbGDk7KLspvrztLVkcfcav6TW8K/pfz F6bLOSfmgnHl0sNbWe91j0Qtkxj14Nj/GS+Mi6sJiJbRgXY2ktFekreH/e4Mlg2Vrlc/ Za+g== X-Gm-Message-State: AJaThX4TqdxZBC7KPysjHTuhfXAazUA2d82DV7WuJ6l3rBXIhHS/dxhP yOFWKbAA92BtwvDglAFLtRKJ0A== X-Google-Smtp-Source: AGs4zMY4mCGHa+BvR0wnzQNrVUYA2Tj054RQDnNDnaE0LvDk+HSf7c0PE4wyYVZn1fgPpmEoXnT6mQ== X-Received: by 10.223.156.133 with SMTP id d5mr28115513wre.43.1512734112030; Fri, 08 Dec 2017 03:55:12 -0800 (PST) Received: from localhost.localdomain ([160.171.158.223]) by smtp.gmail.com with ESMTPSA id h12sm7137196wre.52.2017.12.08.03.55.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Dec 2017 03:55:10 -0800 (PST) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, Ard Biesheuvel , Eric Biggers , linux-fscrypt@vger.kernel.org, Theodore Ts'o , linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org, Jaegeuk Kim , Michael Halcrow , Paul Crowley , Martin Willi , David Gstir , "Jason A . Donenfeld" , Stephan Mueller Subject: [RFC PATCH] crypto: chacha20 - add implementation using 96-bit nonce Date: Fri, 8 Dec 2017 11:55:02 +0000 Message-Id: <20171208115502.21775-1-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As pointed out by Eric [0], the way RFC7539 was interpreted when creating our implementation of ChaCha20 creates a risk of IV reuse when using a little endian counter as the IV generator. The reason is that the low end bits of the counter get mapped onto the ChaCha20 block counter, which advances every 64 bytes. This means that the counter value that gets selected as IV for the next input block will collide with the ChaCha20 block counter of the previous block, basically recreating the same keystream but shifted by 64 bytes. RFC7539 describes the inputs of the algorithm as follows: The inputs to ChaCha20 are: o A 256-bit key o A 32-bit initial counter. This can be set to any number, but will usually be zero or one. It makes sense to use one if we use the zero block for something else, such as generating a one-time authenticator key as part of an AEAD algorithm. o A 96-bit nonce. In some protocols, this is known as the Initialization Vector. o An arbitrary-length plaintext The solution is to use a fixed value of 0 for the initial counter, and only expose a 96-bit IV to the upper layers of the crypto API. So introduce a new ChaCha20 flavor called chacha20-iv96, which takes the above into account, and should become the preferred ChaCha20 implementation going forward for general use. [0] https://marc.info/?l=linux-crypto-vger&m=151269722430848&w=2 Cc: Eric Biggers Cc: linux-fscrypt@vger.kernel.org Cc: Theodore Ts'o Cc: linux-ext4@vger.kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net Cc: linux-mtd@lists.infradead.org Cc: linux-fsdevel@vger.kernel.org Cc: linux-crypto@vger.kernel.org Cc: Jaegeuk Kim Cc: Michael Halcrow Cc: Paul Crowley Cc: Martin Willi Cc: David Gstir Cc: "Jason A . Donenfeld" Cc: Stephan Mueller Signed-off-by: Ard Biesheuvel --- arch/arm64/crypto/chacha20-neon-glue.c | 27 +++++++++++--- arch/x86/crypto/chacha20_glue.c | 19 +++++++++- crypto/chacha20_generic.c | 38 ++++++++++++++------ crypto/testmgr.c | 9 +++++ crypto/testmgr.h | 2 +- include/crypto/chacha20.h | 4 ++- 6 files changed, 81 insertions(+), 18 deletions(-) diff --git a/arch/arm64/crypto/chacha20-neon-glue.c b/arch/arm64/crypto/chacha20-neon-glue.c index cbdb75d15cd0..76a570058cc8 100644 --- a/arch/arm64/crypto/chacha20-neon-glue.c +++ b/arch/arm64/crypto/chacha20-neon-glue.c @@ -70,7 +70,7 @@ static int chacha20_neon(struct skcipher_request *req) err = skcipher_walk_virt(&walk, req, true); - crypto_chacha20_init(state, ctx, walk.iv); + crypto_chacha20_init(state, ctx, walk.iv, crypto_skcipher_ivsize(tfm)); kernel_neon_begin(); while (walk.nbytes > 0) { @@ -88,7 +88,7 @@ static int chacha20_neon(struct skcipher_request *req) return err; } -static struct skcipher_alg alg = { +static struct skcipher_alg alg[] = {{ .base.cra_name = "chacha20", .base.cra_driver_name = "chacha20-neon", .base.cra_priority = 300, @@ -104,19 +104,35 @@ static struct skcipher_alg alg = { .setkey = crypto_chacha20_setkey, .encrypt = chacha20_neon, .decrypt = chacha20_neon, -}; +}, { + .base.cra_name = "chacha20-iv96", + .base.cra_driver_name = "chacha20-neon", + .base.cra_priority = 300, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct chacha20_ctx), + .base.cra_module = THIS_MODULE, + + .min_keysize = CHACHA20_KEY_SIZE, + .max_keysize = CHACHA20_KEY_SIZE, + .ivsize = CHACHA20_NONCE_SIZE, + .chunksize = CHACHA20_BLOCK_SIZE, + .walksize = 4 * CHACHA20_BLOCK_SIZE, + .setkey = crypto_chacha20_setkey, + .encrypt = chacha20_neon, + .decrypt = chacha20_neon, +}}; static int __init chacha20_simd_mod_init(void) { if (!(elf_hwcap & HWCAP_ASIMD)) return -ENODEV; - return crypto_register_skcipher(&alg); + return crypto_register_skciphers(alg, ARRAY_SIZE(alg)); } static void __exit chacha20_simd_mod_fini(void) { - crypto_unregister_skcipher(&alg); + crypto_unregister_skciphers(alg, ARRAY_SIZE(alg)); } module_init(chacha20_simd_mod_init); @@ -125,3 +141,4 @@ module_exit(chacha20_simd_mod_fini); MODULE_AUTHOR("Ard Biesheuvel "); MODULE_LICENSE("GPL v2"); MODULE_ALIAS_CRYPTO("chacha20"); +MODULE_ALIAS_CRYPTO("chacha20-iv96"); diff --git a/arch/x86/crypto/chacha20_glue.c b/arch/x86/crypto/chacha20_glue.c index dce7c5d39c2f..44c7fe376a1d 100644 --- a/arch/x86/crypto/chacha20_glue.c +++ b/arch/x86/crypto/chacha20_glue.c @@ -79,7 +79,7 @@ static int chacha20_simd(struct skcipher_request *req) err = skcipher_walk_virt(&walk, req, true); - crypto_chacha20_init(state, ctx, walk.iv); + crypto_chacha20_init(state, ctx, walk.iv, crypto_skcipher_ivsize(tfm)); kernel_fpu_begin(); @@ -116,6 +116,22 @@ static struct skcipher_alg alg = { .setkey = crypto_chacha20_setkey, .encrypt = chacha20_simd, .decrypt = chacha20_simd, +}, { + .base.cra_name = "chacha20-iv96", + .base.cra_driver_name = "chacha20-simd", + .base.cra_priority = 300, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct chacha20_ctx), + .base.cra_alignmask = sizeof(u32) - 1, + .base.cra_module = THIS_MODULE, + + .min_keysize = CHACHA20_KEY_SIZE, + .max_keysize = CHACHA20_KEY_SIZE, + .ivsize = CHACHA20_NONCE_SIZE, + .chunksize = CHACHA20_BLOCK_SIZE, + .setkey = crypto_chacha20_setkey, + .encrypt = chacha20_simd, + .decrypt = chacha20_simd, }; static int __init chacha20_simd_mod_init(void) @@ -143,4 +159,5 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Martin Willi "); MODULE_DESCRIPTION("chacha20 cipher algorithm, SIMD accelerated"); MODULE_ALIAS_CRYPTO("chacha20"); +MODULE_ALIAS_CRYPTO("chacha20-iv96"); MODULE_ALIAS_CRYPTO("chacha20-simd"); diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c index e451c3cb6a56..943acb92857e 100644 --- a/crypto/chacha20_generic.c +++ b/crypto/chacha20_generic.c @@ -35,7 +35,8 @@ static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, } } -void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv) +void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv, + int ivsize) { state[0] = 0x61707865; /* "expa" */ state[1] = 0x3320646e; /* "nd 3" */ @@ -49,10 +50,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] = 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); + state[12] = (ivsize > CHACHA20_NONCE_SIZE) ? get_unaligned_le32(iv) : 0; + state[13] = get_unaligned_le32(iv + ivsize - 12); + state[14] = get_unaligned_le32(iv + ivsize - 8); + state[15] = get_unaligned_le32(iv + ivsize - 4); } EXPORT_SYMBOL_GPL(crypto_chacha20_init); @@ -82,7 +83,7 @@ int crypto_chacha20_crypt(struct skcipher_request *req) err = skcipher_walk_virt(&walk, req, true); - crypto_chacha20_init(state, ctx, walk.iv); + crypto_chacha20_init(state, ctx, walk.iv, crypto_skcipher_ivsize(tfm)); while (walk.nbytes > 0) { unsigned int nbytes = walk.nbytes; @@ -99,7 +100,7 @@ int crypto_chacha20_crypt(struct skcipher_request *req) } EXPORT_SYMBOL_GPL(crypto_chacha20_crypt); -static struct skcipher_alg alg = { +static struct skcipher_alg alg[] = {{ .base.cra_name = "chacha20", .base.cra_driver_name = "chacha20-generic", .base.cra_priority = 100, @@ -114,16 +115,32 @@ static struct skcipher_alg alg = { .setkey = crypto_chacha20_setkey, .encrypt = crypto_chacha20_crypt, .decrypt = crypto_chacha20_crypt, -}; +}, { + .base.cra_name = "chacha20-iv96", + .base.cra_driver_name = "chacha20-generic", + .base.cra_priority = 100, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct chacha20_ctx), + .base.cra_alignmask = sizeof(u32) - 1, + .base.cra_module = THIS_MODULE, + + .min_keysize = CHACHA20_KEY_SIZE, + .max_keysize = CHACHA20_KEY_SIZE, + .ivsize = CHACHA20_NONCE_SIZE, + .chunksize = CHACHA20_BLOCK_SIZE, + .setkey = crypto_chacha20_setkey, + .encrypt = crypto_chacha20_crypt, + .decrypt = crypto_chacha20_crypt, +}}; static int __init chacha20_generic_mod_init(void) { - return crypto_register_skcipher(&alg); + return crypto_register_skciphers(alg, ARRAY_SIZE(alg)); } static void __exit chacha20_generic_mod_fini(void) { - crypto_unregister_skcipher(&alg); + crypto_unregister_skciphers(alg, ARRAY_SIZE(alg)); } module_init(chacha20_generic_mod_init); @@ -133,4 +150,5 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Martin Willi "); MODULE_DESCRIPTION("chacha20 cipher algorithm"); MODULE_ALIAS_CRYPTO("chacha20"); +MODULE_ALIAS_CRYPTO("chacha20-iv96"); MODULE_ALIAS_CRYPTO("chacha20-generic"); diff --git a/crypto/testmgr.c b/crypto/testmgr.c index 29d7020b8826..ce87cc4f81b1 100644 --- a/crypto/testmgr.c +++ b/crypto/testmgr.c @@ -2563,6 +2563,15 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "chacha20-iv96", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { .vecs = chacha20_enc_tv_template, .count = 1 }, + .dec = { .vecs = chacha20_enc_tv_template, .count = 1 }, + } + } + }, { .alg = "cmac(aes)", .fips_allowed = 1, .test = alg_test_hash, diff --git a/crypto/testmgr.h b/crypto/testmgr.h index a714b6293959..4a5e5411e12c 100644 --- a/crypto/testmgr.h +++ b/crypto/testmgr.h @@ -32612,7 +32612,7 @@ static const struct cipher_testvec salsa20_stream_enc_tv_template[] = { }; static const struct cipher_testvec chacha20_enc_tv_template[] = { - { /* RFC7539 A.2. Test Vector #1 */ + { /* RFC7539 A.2. Test Vector #1 (shared with chacha20-iv96) */ .key = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00" diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h index b83d66073db0..5db09213fe67 100644 --- a/include/crypto/chacha20.h +++ b/include/crypto/chacha20.h @@ -10,6 +10,7 @@ #include #include +#define CHACHA20_NONCE_SIZE 12 #define CHACHA20_IV_SIZE 16 #define CHACHA20_KEY_SIZE 32 #define CHACHA20_BLOCK_SIZE 64 @@ -20,7 +21,8 @@ struct chacha20_ctx { }; void chacha20_block(u32 *state, u32 *stream); -void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv); +void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv, + int ivsize); int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keysize); int crypto_chacha20_crypt(struct skcipher_request *req);