From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org, Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-fscrypt@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org,
Paul Crowley <paulcrowley@google.com>,
Greg Kaiser <gkaiser@google.com>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Samuel Neves <samuel.c.p.neves@gmail.com>,
Tomer Ashur <tomer.ashur@esat.kuleuven.be>,
Eric Biggers <ebiggers@google.com>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Martin Willi <martin@strongswan.org>
Subject: [PATCH v4 01/14] crypto: chacha20-generic - add HChaCha20 library function
Date: Fri, 16 Nov 2018 17:26:18 -0800 [thread overview]
Message-ID: <20181117012631.23528-2-ebiggers@kernel.org> (raw)
In-Reply-To: <20181117012631.23528-1-ebiggers@kernel.org>
From: Eric Biggers <ebiggers@google.com>
Refactor the unkeyed permutation part of chacha20_block() into its own
function, then add hchacha20_block() which is the ChaCha equivalent of
HSalsa20 and is an intermediate step towards XChaCha20 (see
https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha20 skips the
final addition of the initial state, and outputs only certain words of
the state. It should not be used for streaming directly.
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
include/crypto/chacha20.h | 2 ++
lib/chacha20.c | 50 ++++++++++++++++++++++++++++++++++-----
2 files changed, 46 insertions(+), 6 deletions(-)
diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h
index 2d3129442a52..56073814eef0 100644
--- a/include/crypto/chacha20.h
+++ b/include/crypto/chacha20.h
@@ -20,6 +20,8 @@ struct chacha20_ctx {
};
void chacha20_block(u32 *state, u8 *stream);
+void hchacha20_block(const u32 *in, u32 *out);
+
void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv);
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
unsigned int keysize);
diff --git a/lib/chacha20.c b/lib/chacha20.c
index d907fec6a9ed..6a484e16171d 100644
--- a/lib/chacha20.c
+++ b/lib/chacha20.c
@@ -1,5 +1,5 @@
/*
- * ChaCha20 256-bit cipher algorithm, RFC7539
+ * The "hash function" used as the core of the ChaCha20 stream cipher (RFC7539)
*
* Copyright (C) 2015 Martin Willi
*
@@ -16,14 +16,10 @@
#include <asm/unaligned.h>
#include <crypto/chacha20.h>
-void chacha20_block(u32 *state, u8 *stream)
+static void chacha20_permute(u32 *x)
{
- u32 x[16];
int i;
- for (i = 0; i < ARRAY_SIZE(x); i++)
- x[i] = state[i];
-
for (i = 0; i < 20; i += 2) {
x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16);
x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16);
@@ -65,6 +61,25 @@ void chacha20_block(u32 *state, u8 *stream)
x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7);
x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7);
}
+}
+
+/**
+ * chacha20_block - generate one keystream block and increment block counter
+ * @state: input state matrix (16 32-bit words)
+ * @stream: output keystream block (64 bytes)
+ *
+ * This is the ChaCha20 core, a function from 64-byte strings to 64-byte
+ * strings. The caller has already converted the endianness of the input. This
+ * function also handles incrementing the block counter in the input matrix.
+ */
+void chacha20_block(u32 *state, u8 *stream)
+{
+ u32 x[16];
+ int i;
+
+ memcpy(x, state, 64);
+
+ chacha20_permute(x);
for (i = 0; i < ARRAY_SIZE(x); i++)
put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
@@ -72,3 +87,26 @@ void chacha20_block(u32 *state, u8 *stream)
state[12]++;
}
EXPORT_SYMBOL(chacha20_block);
+
+/**
+ * hchacha20_block - abbreviated ChaCha20 core, for XChaCha20
+ * @in: input state matrix (16 32-bit words)
+ * @out: output (8 32-bit words)
+ *
+ * HChaCha20 is the ChaCha equivalent of HSalsa20 and is an intermediate step
+ * towards XChaCha20 (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf).
+ * HChaCha20 skips the final addition of the initial state, and outputs only
+ * certain words of the state. It should not be used for streaming directly.
+ */
+void hchacha20_block(const u32 *in, u32 *out)
+{
+ u32 x[16];
+
+ memcpy(x, in, 64);
+
+ chacha20_permute(x);
+
+ memcpy(&out[0], &x[0], 16);
+ memcpy(&out[4], &x[12], 16);
+}
+EXPORT_SYMBOL(hchacha20_block);
--
2.19.1.1215.g8438c0b245-goog
WARNING: multiple messages have this Message-ID (diff)
From: ebiggers@kernel.org (Eric Biggers)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4 01/14] crypto: chacha20-generic - add HChaCha20 library function
Date: Fri, 16 Nov 2018 17:26:18 -0800 [thread overview]
Message-ID: <20181117012631.23528-2-ebiggers@kernel.org> (raw)
In-Reply-To: <20181117012631.23528-1-ebiggers@kernel.org>
From: Eric Biggers <ebiggers@google.com>
Refactor the unkeyed permutation part of chacha20_block() into its own
function, then add hchacha20_block() which is the ChaCha equivalent of
HSalsa20 and is an intermediate step towards XChaCha20 (see
https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha20 skips the
final addition of the initial state, and outputs only certain words of
the state. It should not be used for streaming directly.
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
include/crypto/chacha20.h | 2 ++
lib/chacha20.c | 50 ++++++++++++++++++++++++++++++++++-----
2 files changed, 46 insertions(+), 6 deletions(-)
diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h
index 2d3129442a52..56073814eef0 100644
--- a/include/crypto/chacha20.h
+++ b/include/crypto/chacha20.h
@@ -20,6 +20,8 @@ struct chacha20_ctx {
};
void chacha20_block(u32 *state, u8 *stream);
+void hchacha20_block(const u32 *in, u32 *out);
+
void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv);
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
unsigned int keysize);
diff --git a/lib/chacha20.c b/lib/chacha20.c
index d907fec6a9ed..6a484e16171d 100644
--- a/lib/chacha20.c
+++ b/lib/chacha20.c
@@ -1,5 +1,5 @@
/*
- * ChaCha20 256-bit cipher algorithm, RFC7539
+ * The "hash function" used as the core of the ChaCha20 stream cipher (RFC7539)
*
* Copyright (C) 2015 Martin Willi
*
@@ -16,14 +16,10 @@
#include <asm/unaligned.h>
#include <crypto/chacha20.h>
-void chacha20_block(u32 *state, u8 *stream)
+static void chacha20_permute(u32 *x)
{
- u32 x[16];
int i;
- for (i = 0; i < ARRAY_SIZE(x); i++)
- x[i] = state[i];
-
for (i = 0; i < 20; i += 2) {
x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16);
x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16);
@@ -65,6 +61,25 @@ void chacha20_block(u32 *state, u8 *stream)
x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7);
x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7);
}
+}
+
+/**
+ * chacha20_block - generate one keystream block and increment block counter
+ * @state: input state matrix (16 32-bit words)
+ * @stream: output keystream block (64 bytes)
+ *
+ * This is the ChaCha20 core, a function from 64-byte strings to 64-byte
+ * strings. The caller has already converted the endianness of the input. This
+ * function also handles incrementing the block counter in the input matrix.
+ */
+void chacha20_block(u32 *state, u8 *stream)
+{
+ u32 x[16];
+ int i;
+
+ memcpy(x, state, 64);
+
+ chacha20_permute(x);
for (i = 0; i < ARRAY_SIZE(x); i++)
put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
@@ -72,3 +87,26 @@ void chacha20_block(u32 *state, u8 *stream)
state[12]++;
}
EXPORT_SYMBOL(chacha20_block);
+
+/**
+ * hchacha20_block - abbreviated ChaCha20 core, for XChaCha20
+ * @in: input state matrix (16 32-bit words)
+ * @out: output (8 32-bit words)
+ *
+ * HChaCha20 is the ChaCha equivalent of HSalsa20 and is an intermediate step
+ * towards XChaCha20 (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf).
+ * HChaCha20 skips the final addition of the initial state, and outputs only
+ * certain words of the state. It should not be used for streaming directly.
+ */
+void hchacha20_block(const u32 *in, u32 *out)
+{
+ u32 x[16];
+
+ memcpy(x, in, 64);
+
+ chacha20_permute(x);
+
+ memcpy(&out[0], &x[0], 16);
+ memcpy(&out[4], &x[12], 16);
+}
+EXPORT_SYMBOL(hchacha20_block);
--
2.19.1.1215.g8438c0b245-goog
next prev parent reply other threads:[~2018-11-17 11:43 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-17 1:26 [PATCH v4 00/14] crypto: Adiantum support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` Eric Biggers [this message]
2018-11-17 1:26 ` [PATCH v4 01/14] crypto: chacha20-generic - add HChaCha20 library function Eric Biggers
2018-11-17 1:26 ` [PATCH v4 02/14] crypto: chacha20-generic - don't unnecessarily use atomic walk Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 03/14] crypto: chacha20-generic - add XChaCha20 support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 04/14] crypto: chacha20-generic - refactor to allow varying number of rounds Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 05/14] crypto: chacha - add XChaCha12 support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 06/14] crypto: arm/chacha20 - limit the preemption-disabled section Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 07/14] crypto: arm/chacha20 - add XChaCha20 support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 08/14] crypto: arm/chacha20 - refactor to allow varying number of rounds Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 09/14] crypto: arm/chacha - add XChaCha12 support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:26 ` [PATCH v4 10/14] crypto: poly1305 - use structures for key and accumulator Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:43 ` Ard Biesheuvel
2018-11-17 1:43 ` Ard Biesheuvel
2018-11-17 1:26 ` [PATCH v4 11/14] crypto: poly1305 - add Poly1305 core API Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:50 ` Ard Biesheuvel
2018-11-17 1:50 ` Ard Biesheuvel
2018-11-17 1:26 ` [PATCH v4 12/14] crypto: nhpoly1305 - add NHPoly1305 support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 1:52 ` Ard Biesheuvel
2018-11-17 1:52 ` Ard Biesheuvel
2018-11-17 1:26 ` [PATCH v4 13/14] crypto: arm/nhpoly1305 - add NEON-accelerated NHPoly1305 Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 2:00 ` Ard Biesheuvel
2018-11-17 2:00 ` Ard Biesheuvel
2018-11-17 1:26 ` [PATCH v4 14/14] crypto: adiantum - add Adiantum support Eric Biggers
2018-11-17 1:26 ` Eric Biggers
2018-11-17 2:03 ` Ard Biesheuvel
2018-11-17 2:03 ` Ard Biesheuvel
2018-11-20 6:33 ` [PATCH v4 00/14] crypto: " Herbert Xu
2018-11-20 6:33 ` Herbert Xu
2018-11-30 17:58 ` Eric Biggers
2018-11-30 17:58 ` Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181117012631.23528-2-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ard.biesheuvel@linaro.org \
--cc=ebiggers@google.com \
--cc=gkaiser@google.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin@strongswan.org \
--cc=paulcrowley@google.com \
--cc=samuel.c.p.neves@gmail.com \
--cc=tomer.ashur@esat.kuleuven.be \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.