UPSTREAM: crypto: chacha - move existing library code into lib/crypto
Currently, our generic ChaCha implementation consists of a permute function in lib/chacha.c that operates on the 64-byte ChaCha state directly [and which is always included into the core kernel since it is used by the /dev/random driver], and the crypto API plumbing to expose it as a skcipher. In order to support in-kernel users that need the ChaCha streamcipher but have no need [or tolerance] for going through the abstractions of the crypto API, let's expose the streamcipher bits via a library API as well, in a way that permits the implementation to be superseded by an architecture specific one if provided. So move the streamcipher code into a separate module in lib/crypto, and expose the init() and crypt() routines to users of the library. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> (cherry picked from commit 5fb8ef25803ef33e2eb60b626435828b937bed75) Bug: 152722841 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I7fe321d1fcbaea1dc3f9f65dec74a6f40da2d489
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
c099f33354
commit
2aa92dfe28
@@ -31,7 +31,7 @@ endif
|
||||
lib-y := ctype.o string.o vsprintf.o cmdline.o \
|
||||
rbtree.o radix-tree.o timerqueue.o\
|
||||
idr.o int_sqrt.o extable.o \
|
||||
sha1.o chacha.o irq_regs.o argv_split.o \
|
||||
sha1.o irq_regs.o argv_split.o \
|
||||
flex_proportions.o ratelimit.o show_mem.o \
|
||||
is_single_threaded.o plist.o decompress.o kobject_uevent.o \
|
||||
earlycpio.o seq_buf.o siphash.o dec_and_lock.o \
|
||||
|
||||
@@ -1,3 +1,29 @@
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
comment "Crypto library routines"
|
||||
|
||||
config CRYPTO_ARCH_HAVE_LIB_CHACHA
|
||||
tristate
|
||||
help
|
||||
Declares whether the architecture provides an arch-specific
|
||||
accelerated implementation of the ChaCha library interface,
|
||||
either builtin or as a module.
|
||||
|
||||
config CRYPTO_LIB_CHACHA_GENERIC
|
||||
tristate
|
||||
select CRYPTO_ALGAPI
|
||||
help
|
||||
This symbol can be depended upon by arch implementations of the
|
||||
ChaCha library interface that require the generic code as a
|
||||
fallback, e.g., for SIMD implementations. If no arch specific
|
||||
implementation is enabled, this implementation serves the users
|
||||
of CRYPTO_LIB_CHACHA.
|
||||
|
||||
config CRYPTO_LIB_CHACHA
|
||||
tristate "ChaCha library interface"
|
||||
depends on CRYPTO_ARCH_HAVE_LIB_CHACHA || !CRYPTO_ARCH_HAVE_LIB_CHACHA
|
||||
select CRYPTO_LIB_CHACHA_GENERIC if CRYPTO_ARCH_HAVE_LIB_CHACHA=n
|
||||
help
|
||||
Enable the ChaCha library interface. This interface may be fulfilled
|
||||
by either the generic implementation or an arch-specific one, if one
|
||||
is available and enabled.
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
# chacha is used by the /dev/random driver which is always builtin
|
||||
obj-y += chacha.o
|
||||
obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC) += libchacha.o
|
||||
|
||||
@@ -9,9 +9,11 @@
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <linux/bug.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/export.h>
|
||||
#include <linux/bitops.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/cryptohash.h>
|
||||
#include <asm/unaligned.h>
|
||||
#include <crypto/chacha.h>
|
||||
@@ -76,7 +78,7 @@ static void chacha_permute(u32 *x, int nrounds)
|
||||
* The caller has already converted the endianness of the input. This function
|
||||
* also handles incrementing the block counter in the input matrix.
|
||||
*/
|
||||
void chacha_block(u32 *state, u8 *stream, int nrounds)
|
||||
void chacha_block_generic(u32 *state, u8 *stream, int nrounds)
|
||||
{
|
||||
u32 x[16];
|
||||
int i;
|
||||
@@ -90,11 +92,11 @@ void chacha_block(u32 *state, u8 *stream, int nrounds)
|
||||
|
||||
state[12]++;
|
||||
}
|
||||
EXPORT_SYMBOL(chacha_block);
|
||||
EXPORT_SYMBOL(chacha_block_generic);
|
||||
|
||||
/**
|
||||
* hchacha_block - abbreviated ChaCha core, for XChaCha
|
||||
* @in: input state matrix (16 32-bit words)
|
||||
* hchacha_block_generic - abbreviated ChaCha core, for XChaCha
|
||||
* @state: input state matrix (16 32-bit words)
|
||||
* @out: output (8 32-bit words)
|
||||
* @nrounds: number of rounds (20 or 12; 20 is recommended)
|
||||
*
|
||||
@@ -103,15 +105,15 @@ EXPORT_SYMBOL(chacha_block);
|
||||
* 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 hchacha_block(const u32 *in, u32 *out, int nrounds)
|
||||
void hchacha_block_generic(const u32 *state, u32 *stream, int nrounds)
|
||||
{
|
||||
u32 x[16];
|
||||
|
||||
memcpy(x, in, 64);
|
||||
memcpy(x, state, 64);
|
||||
|
||||
chacha_permute(x, nrounds);
|
||||
|
||||
memcpy(&out[0], &x[0], 16);
|
||||
memcpy(&out[4], &x[12], 16);
|
||||
memcpy(&stream[0], &x[0], 16);
|
||||
memcpy(&stream[4], &x[12], 16);
|
||||
}
|
||||
EXPORT_SYMBOL(hchacha_block);
|
||||
EXPORT_SYMBOL(hchacha_block_generic);
|
||||
35
lib/crypto/libchacha.c
Normal file
35
lib/crypto/libchacha.c
Normal file
@@ -0,0 +1,35 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* The ChaCha stream cipher (RFC7539)
|
||||
*
|
||||
* Copyright (C) 2015 Martin Willi
|
||||
*/
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/export.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
#include <crypto/algapi.h> // for crypto_xor_cpy
|
||||
#include <crypto/chacha.h>
|
||||
|
||||
void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
|
||||
unsigned int bytes, int nrounds)
|
||||
{
|
||||
/* aligned to potentially speed up crypto_xor() */
|
||||
u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
|
||||
|
||||
while (bytes >= CHACHA_BLOCK_SIZE) {
|
||||
chacha_block_generic(state, stream, nrounds);
|
||||
crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE);
|
||||
bytes -= CHACHA_BLOCK_SIZE;
|
||||
dst += CHACHA_BLOCK_SIZE;
|
||||
src += CHACHA_BLOCK_SIZE;
|
||||
}
|
||||
if (bytes) {
|
||||
chacha_block_generic(state, stream, nrounds);
|
||||
crypto_xor_cpy(dst, src, stream, bytes);
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(chacha_crypt_generic);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
Reference in New Issue
Block a user