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: 121156 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp559527qgn; Fri, 8 Dec 2017 03:55:18 -0800 (PST) X-Google-Smtp-Source: AGs4zMbJQGNvVSDGP2iQsO3hdfle49qHlGgPzzxPUlwxlf0+oRuB/SLN/smuOWGS6RmCfdiHjQXt X-Received: by 10.84.252.19 with SMTP id x19mr6556132pll.237.1512734117977; Fri, 08 Dec 2017 03:55:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512734117; cv=none; d=google.com; s=arc-20160816; b=yULP32IVcEDuLZw/xfNpl+Al+1Y/ivafy6rYITADxblOXxYiFO5uyNOPlm6b+v+eM7 f2BQGA7JW/ESS4sb71cTZ2bI8GOKJd/gz6FPTQomZm62XGsFbA838XoXxLW5Veuf2pcU GFdVrCYjLLwR3iWolavleoS/wTmb3eDxhhUxB+M5kmcaElsiq7apJZtAJEx/0HZflZTa JiijZgD0pIbEIuhyUeBkpcQsQ+fQLborE2t/3HW05kGjGfh7fCXTpSGbciI376+gjIE/ NHVqtRfaCZF+Xe1tyY+RcckssbLAPZQxCBFvm3J2zLN3+oYItA26pxByHzZeyunvSy7V 3rOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=MAkj+1Hbc3gdJL1OingPAE4YW1crGN/Ss4+8MX4UTnk=; b=UUertXyz5a6Ms0TRWct6YY3Elpmtg/hv5LlQJvF1usVoH7+XBrKEYOG+8YQvcq0BDJ 0OdKFCZvqwlvaTGRHSI5fO6d8cx57i7SEw/65+wUxVfzkm+/G4ChFPh4y0SrCQ3WFM1q pbpLUGTb/ryJ3noyzqD163jVYSKKEmWdxZb+di905zakK8kR+/trJA/Yd55K/wLKnXBD +geKYzw8HM4cwCO9s749PYl0e8IDtH+PzMlyFULhgX4RyEPDMHSXG7XmfJUAwumWY6Z+ s5v1tIx7RrgnkJz16hY3HA2g2w+yCj8VU4AHqDG8toSjBqSZ7XKuVjd56Cyqud6zsbaR Pi4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QVNhw17+; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m37si5415332plg.444.2017.12.08.03.55.17; Fri, 08 Dec 2017 03:55:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-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=@linaro.org header.s=google header.b=QVNhw17+; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752974AbdLHLzP (ORCPT + 1 other); Fri, 8 Dec 2017 06:55:15 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:42161 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752990AbdLHLzN (ORCPT ); Fri, 8 Dec 2017 06:55:13 -0500 Received: by mail-wr0-f195.google.com with SMTP id s66so10578938wrc.9 for ; Fri, 08 Dec 2017 03:55:13 -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=q2Z9bfCb0Nt4tp4+0uw1nMljMvSQpC1nxwTBxBfidX1/9V3pydV3e4yghm6KXKIIjf 9Ms3Jx5l11FcajxOiUpEq+ugSRl4lmHu024P9ern5rP7/7DMww5OoJCavZy02fPo5Lg+ 3BCDOOcsf4/g2c4U/hVTn1HVLcP9G3AjI5bPUOJdSzf30VU8xFbIcrnzGD0LmINepCe1 QahbTQ4gJpngdq8CGxtecNeNFgsxmA/cYAjNZ5yo1sVC8sdR6OzLO2yL2w408Vx0/6O6 /R/mUyIxd9by+cxsK/GWTzjTLkPTSwqixzZ46E6yaY6xOCiRVaFfTXJ05OTovXttCE6w xWKw== X-Gm-Message-State: AJaThX7NMq4gtThPqKx8QzRuga12MW+XOOwxN3WppvBsHq1n7EevAHfL cA/Yc/XUaH8wwHXkQUkEqQZRj64/7Vg= 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-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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(-) -- 2.11.0 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);