mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2025-01-01 10:42:11 +00:00
crypto: chacha20 - Export common ChaCha20 helpers
As architecture specific drivers need a software fallback, export a ChaCha20 en-/decryption function together with some helpers in a header file. Signed-off-by: Martin Willi <martin@strongswan.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
parent
2dce063a31
commit
31d7247da5
@ -13,14 +13,7 @@
|
|||||||
#include <linux/crypto.h>
|
#include <linux/crypto.h>
|
||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
|
#include <crypto/chacha20.h>
|
||||||
#define CHACHA20_NONCE_SIZE 16
|
|
||||||
#define CHACHA20_KEY_SIZE 32
|
|
||||||
#define CHACHA20_BLOCK_SIZE 64
|
|
||||||
|
|
||||||
struct chacha20_ctx {
|
|
||||||
u32 key[8];
|
|
||||||
};
|
|
||||||
|
|
||||||
static inline u32 rotl32(u32 v, u8 n)
|
static inline u32 rotl32(u32 v, u8 n)
|
||||||
{
|
{
|
||||||
@ -108,7 +101,7 @@ static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
|
void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
|
||||||
{
|
{
|
||||||
static const char constant[16] = "expand 32-byte k";
|
static const char constant[16] = "expand 32-byte k";
|
||||||
|
|
||||||
@ -129,8 +122,9 @@ static void chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
|
|||||||
state[14] = le32_to_cpuvp(iv + 8);
|
state[14] = le32_to_cpuvp(iv + 8);
|
||||||
state[15] = le32_to_cpuvp(iv + 12);
|
state[15] = le32_to_cpuvp(iv + 12);
|
||||||
}
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(crypto_chacha20_init);
|
||||||
|
|
||||||
static int chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
|
int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
|
||||||
unsigned int keysize)
|
unsigned int keysize)
|
||||||
{
|
{
|
||||||
struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
|
struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
|
||||||
@ -144,8 +138,9 @@ static int chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
|
||||||
|
|
||||||
static int chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
struct blkcipher_walk walk;
|
||||||
@ -155,7 +150,7 @@ static int chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
|||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
blkcipher_walk_init(&walk, dst, src, nbytes);
|
||||||
err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
|
err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
|
||||||
|
|
||||||
chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
|
crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
|
||||||
|
|
||||||
while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
|
while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
|
||||||
chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
|
chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
|
||||||
@ -172,6 +167,7 @@ static int chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
|||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
|
||||||
|
|
||||||
static struct crypto_alg alg = {
|
static struct crypto_alg alg = {
|
||||||
.cra_name = "chacha20",
|
.cra_name = "chacha20",
|
||||||
@ -187,11 +183,11 @@ static struct crypto_alg alg = {
|
|||||||
.blkcipher = {
|
.blkcipher = {
|
||||||
.min_keysize = CHACHA20_KEY_SIZE,
|
.min_keysize = CHACHA20_KEY_SIZE,
|
||||||
.max_keysize = CHACHA20_KEY_SIZE,
|
.max_keysize = CHACHA20_KEY_SIZE,
|
||||||
.ivsize = CHACHA20_NONCE_SIZE,
|
.ivsize = CHACHA20_IV_SIZE,
|
||||||
.geniv = "seqiv",
|
.geniv = "seqiv",
|
||||||
.setkey = chacha20_setkey,
|
.setkey = crypto_chacha20_setkey,
|
||||||
.encrypt = chacha20_crypt,
|
.encrypt = crypto_chacha20_crypt,
|
||||||
.decrypt = chacha20_crypt,
|
.decrypt = crypto_chacha20_crypt,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <crypto/internal/hash.h>
|
#include <crypto/internal/hash.h>
|
||||||
#include <crypto/internal/skcipher.h>
|
#include <crypto/internal/skcipher.h>
|
||||||
#include <crypto/scatterwalk.h>
|
#include <crypto/scatterwalk.h>
|
||||||
|
#include <crypto/chacha20.h>
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
#include <linux/init.h>
|
#include <linux/init.h>
|
||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
@ -23,8 +24,6 @@
|
|||||||
#define POLY1305_BLOCK_SIZE 16
|
#define POLY1305_BLOCK_SIZE 16
|
||||||
#define POLY1305_DIGEST_SIZE 16
|
#define POLY1305_DIGEST_SIZE 16
|
||||||
#define POLY1305_KEY_SIZE 32
|
#define POLY1305_KEY_SIZE 32
|
||||||
#define CHACHA20_KEY_SIZE 32
|
|
||||||
#define CHACHA20_IV_SIZE 16
|
|
||||||
#define CHACHAPOLY_IV_SIZE 12
|
#define CHACHAPOLY_IV_SIZE 12
|
||||||
|
|
||||||
struct chachapoly_instance_ctx {
|
struct chachapoly_instance_ctx {
|
||||||
|
25
include/crypto/chacha20.h
Normal file
25
include/crypto/chacha20.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Common values for the ChaCha20 algorithm
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CRYPTO_CHACHA20_H
|
||||||
|
#define _CRYPTO_CHACHA20_H
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include <linux/crypto.h>
|
||||||
|
|
||||||
|
#define CHACHA20_IV_SIZE 16
|
||||||
|
#define CHACHA20_KEY_SIZE 32
|
||||||
|
#define CHACHA20_BLOCK_SIZE 64
|
||||||
|
|
||||||
|
struct chacha20_ctx {
|
||||||
|
u32 key[8];
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
|
struct scatterlist *src, unsigned int nbytes);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user