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
@ -15,9 +15,8 @@
|
||||
#ifndef _CRYPTO_CHACHA_H
|
||||
#define _CRYPTO_CHACHA_H
|
||||
|
||||
#include <crypto/skcipher.h>
|
||||
#include <asm/unaligned.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/crypto.h>
|
||||
|
||||
/* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
|
||||
#define CHACHA_IV_SIZE 16
|
||||
@ -28,26 +27,70 @@
|
||||
/* 192-bit nonce, then 64-bit stream position */
|
||||
#define XCHACHA_IV_SIZE 32
|
||||
|
||||
struct chacha_ctx {
|
||||
u32 key[8];
|
||||
int nrounds;
|
||||
};
|
||||
|
||||
void chacha_block(u32 *state, u8 *stream, int nrounds);
|
||||
void chacha_block_generic(u32 *state, u8 *stream, int nrounds);
|
||||
static inline void chacha20_block(u32 *state, u8 *stream)
|
||||
{
|
||||
chacha_block(state, stream, 20);
|
||||
chacha_block_generic(state, stream, 20);
|
||||
}
|
||||
void hchacha_block(const u32 *in, u32 *out, int nrounds);
|
||||
|
||||
void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);
|
||||
void hchacha_block_arch(const u32 *state, u32 *out, int nrounds);
|
||||
void hchacha_block_generic(const u32 *state, u32 *out, int nrounds);
|
||||
|
||||
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize);
|
||||
int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize);
|
||||
static inline void hchacha_block(const u32 *state, u32 *out, int nrounds)
|
||||
{
|
||||
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
|
||||
hchacha_block_arch(state, out, nrounds);
|
||||
else
|
||||
hchacha_block_generic(state, out, nrounds);
|
||||
}
|
||||
|
||||
int crypto_chacha_crypt(struct skcipher_request *req);
|
||||
int crypto_xchacha_crypt(struct skcipher_request *req);
|
||||
void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv);
|
||||
static inline void chacha_init_generic(u32 *state, const u32 *key, const u8 *iv)
|
||||
{
|
||||
state[0] = 0x61707865; /* "expa" */
|
||||
state[1] = 0x3320646e; /* "nd 3" */
|
||||
state[2] = 0x79622d32; /* "2-by" */
|
||||
state[3] = 0x6b206574; /* "te k" */
|
||||
state[4] = key[0];
|
||||
state[5] = key[1];
|
||||
state[6] = key[2];
|
||||
state[7] = key[3];
|
||||
state[8] = key[4];
|
||||
state[9] = key[5];
|
||||
state[10] = key[6];
|
||||
state[11] = 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);
|
||||
}
|
||||
|
||||
static inline void chacha_init(u32 *state, const u32 *key, const u8 *iv)
|
||||
{
|
||||
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
|
||||
chacha_init_arch(state, key, iv);
|
||||
else
|
||||
chacha_init_generic(state, key, iv);
|
||||
}
|
||||
|
||||
void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
|
||||
unsigned int bytes, int nrounds);
|
||||
void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
|
||||
unsigned int bytes, int nrounds);
|
||||
|
||||
static inline void chacha_crypt(u32 *state, u8 *dst, const u8 *src,
|
||||
unsigned int bytes, int nrounds)
|
||||
{
|
||||
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
|
||||
chacha_crypt_arch(state, dst, src, bytes, nrounds);
|
||||
else
|
||||
chacha_crypt_generic(state, dst, src, bytes, nrounds);
|
||||
}
|
||||
|
||||
static inline void chacha20_crypt(u32 *state, u8 *dst, const u8 *src,
|
||||
unsigned int bytes)
|
||||
{
|
||||
chacha_crypt(state, dst, src, bytes, 20);
|
||||
}
|
||||
|
||||
#endif /* _CRYPTO_CHACHA_H */
|
||||
|
||||
53
include/crypto/internal/chacha.h
Normal file
53
include/crypto/internal/chacha.h
Normal file
@ -0,0 +1,53 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
#ifndef _CRYPTO_INTERNAL_CHACHA_H
|
||||
#define _CRYPTO_INTERNAL_CHACHA_H
|
||||
|
||||
#include <crypto/chacha.h>
|
||||
#include <crypto/internal/skcipher.h>
|
||||
#include <linux/crypto.h>
|
||||
|
||||
struct chacha_ctx {
|
||||
u32 key[8];
|
||||
int nrounds;
|
||||
};
|
||||
|
||||
void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);
|
||||
|
||||
static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize, int nrounds)
|
||||
{
|
||||
struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
|
||||
int i;
|
||||
|
||||
if (keysize != CHACHA_KEY_SIZE)
|
||||
return -EINVAL;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
|
||||
ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
|
||||
|
||||
ctx->nrounds = nrounds;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize)
|
||||
{
|
||||
return chacha_setkey(tfm, key, keysize, 20);
|
||||
}
|
||||
|
||||
static int inline chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize)
|
||||
{
|
||||
return chacha_setkey(tfm, key, keysize, 12);
|
||||
}
|
||||
|
||||
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize);
|
||||
int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
||||
unsigned int keysize);
|
||||
|
||||
int crypto_chacha_crypt(struct skcipher_request *req);
|
||||
int crypto_xchacha_crypt(struct skcipher_request *req);
|
||||
|
||||
#endif /* _CRYPTO_CHACHA_H */
|
||||
Reference in New Issue
Block a user