Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc41a67c authored by Eric Biggers's avatar Eric Biggers
Browse files

FROMGIT: crypto: chacha20-generic - add HChaCha20 library function

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: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: default avatarMartin Willi <martin@strongswan.org>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>

(cherry picked from commit dd333449d0fb667c5250c42488a7e90470e16c77
 https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git

 master)
Bug: 112008522
Test: As series, see Ic61c13b53facfd2173065be715a7ee5f3af8760b
Change-Id: I5b7e92b39ada49343cbdf21e4c6d7c1aa1adf183
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
parent 910bbfcb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,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_tfm *tfm, const u8 *key,
			   unsigned int keysize);
+44 −6
Original line number Diff line number Diff line
/*
 * 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);