Skip to content

Commit 5fb8ef2

Browse files
ardbiesheuvelherbertx
authored andcommitted
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>
1 parent 746b2e0 commit 5fb8ef2

12 files changed

Lines changed: 199 additions & 86 deletions

File tree

‎arch/arm/crypto/chacha-neon-glue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include <crypto/algapi.h>
23-
#include <crypto/chacha.h>
23+
#include <crypto/internal/chacha.h>
2424
#include <crypto/internal/simd.h>
2525
#include <crypto/internal/skcipher.h>
2626
#include <linux/kernel.h>

‎arch/arm64/crypto/chacha-neon-glue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include <crypto/algapi.h>
23-
#include <crypto/chacha.h>
23+
#include <crypto/internal/chacha.h>
2424
#include <crypto/internal/simd.h>
2525
#include <crypto/internal/skcipher.h>
2626
#include <linux/kernel.h>

‎arch/x86/crypto/chacha_glue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
#include <crypto/algapi.h>
10-
#include <crypto/chacha.h>
10+
#include <crypto/internal/chacha.h>
1111
#include <crypto/internal/simd.h>
1212
#include <crypto/internal/skcipher.h>
1313
#include <linux/kernel.h>

‎crypto/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ config CRYPTO_SALSA20
14121412

14131413
config CRYPTO_CHACHA20
14141414
tristate "ChaCha stream cipher algorithms"
1415+
select CRYPTO_LIB_CHACHA_GENERIC
14151416
select CRYPTO_SKCIPHER
14161417
help
14171418
The ChaCha20, XChaCha20, and XChaCha12 stream cipher algorithms.

‎crypto/chacha_generic.c‎

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,10 @@
88

99
#include <asm/unaligned.h>
1010
#include <crypto/algapi.h>
11-
#include <crypto/chacha.h>
11+
#include <crypto/internal/chacha.h>
1212
#include <crypto/internal/skcipher.h>
1313
#include <linux/module.h>
1414

15-
static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src,
16-
unsigned int bytes, int nrounds)
17-
{
18-
/* aligned to potentially speed up crypto_xor() */
19-
u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
20-
21-
while (bytes >= CHACHA_BLOCK_SIZE) {
22-
chacha_block(state, stream, nrounds);
23-
crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE);
24-
bytes -= CHACHA_BLOCK_SIZE;
25-
dst += CHACHA_BLOCK_SIZE;
26-
src += CHACHA_BLOCK_SIZE;
27-
}
28-
if (bytes) {
29-
chacha_block(state, stream, nrounds);
30-
crypto_xor_cpy(dst, src, stream, bytes);
31-
}
32-
}
33-
3415
static int chacha_stream_xor(struct skcipher_request *req,
3516
const struct chacha_ctx *ctx, const u8 *iv)
3617
{
@@ -48,8 +29,8 @@ static int chacha_stream_xor(struct skcipher_request *req,
4829
if (nbytes < walk.total)
4930
nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE);
5031

51-
chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
52-
nbytes, ctx->nrounds);
32+
chacha_crypt_generic(state, walk.dst.virt.addr,
33+
walk.src.virt.addr, nbytes, ctx->nrounds);
5334
err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
5435
}
5536

@@ -58,41 +39,10 @@ static int chacha_stream_xor(struct skcipher_request *req,
5839

5940
void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv)
6041
{
61-
state[0] = 0x61707865; /* "expa" */
62-
state[1] = 0x3320646e; /* "nd 3" */
63-
state[2] = 0x79622d32; /* "2-by" */
64-
state[3] = 0x6b206574; /* "te k" */
65-
state[4] = ctx->key[0];
66-
state[5] = ctx->key[1];
67-
state[6] = ctx->key[2];
68-
state[7] = ctx->key[3];
69-
state[8] = ctx->key[4];
70-
state[9] = ctx->key[5];
71-
state[10] = ctx->key[6];
72-
state[11] = ctx->key[7];
73-
state[12] = get_unaligned_le32(iv + 0);
74-
state[13] = get_unaligned_le32(iv + 4);
75-
state[14] = get_unaligned_le32(iv + 8);
76-
state[15] = get_unaligned_le32(iv + 12);
42+
chacha_init_generic(state, ctx->key, iv);
7743
}
7844
EXPORT_SYMBOL_GPL(crypto_chacha_init);
7945

80-
static int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
81-
unsigned int keysize, int nrounds)
82-
{
83-
struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
84-
int i;
85-
86-
if (keysize != CHACHA_KEY_SIZE)
87-
return -EINVAL;
88-
89-
for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
90-
ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
91-
92-
ctx->nrounds = nrounds;
93-
return 0;
94-
}
95-
9646
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
9747
unsigned int keysize)
9848
{
@@ -126,7 +76,7 @@ int crypto_xchacha_crypt(struct skcipher_request *req)
12676

12777
/* Compute the subkey given the original key and first 128 nonce bits */
12878
crypto_chacha_init(state, ctx, req->iv);
129-
hchacha_block(state, subctx.key, ctx->nrounds);
79+
hchacha_block_generic(state, subctx.key, ctx->nrounds);
13080
subctx.nrounds = ctx->nrounds;
13181

13282
/* Build the real IV */

‎include/crypto/chacha.h‎

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
#ifndef _CRYPTO_CHACHA_H
1616
#define _CRYPTO_CHACHA_H
1717

18-
#include <crypto/skcipher.h>
18+
#include <asm/unaligned.h>
1919
#include <linux/types.h>
20-
#include <linux/crypto.h>
2120

2221
/* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
2322
#define CHACHA_IV_SIZE 16
@@ -29,26 +28,70 @@
2928
/* 192-bit nonce, then 64-bit stream position */
3029
#define XCHACHA_IV_SIZE 32
3130

32-
struct chacha_ctx {
33-
u32 key[8];
34-
int nrounds;
35-
};
36-
37-
void chacha_block(u32 *state, u8 *stream, int nrounds);
31+
void chacha_block_generic(u32 *state, u8 *stream, int nrounds);
3832
static inline void chacha20_block(u32 *state, u8 *stream)
3933
{
40-
chacha_block(state, stream, 20);
34+
chacha_block_generic(state, stream, 20);
4135
}
42-
void hchacha_block(const u32 *in, u32 *out, int nrounds);
4336

44-
void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);
37+
void hchacha_block_arch(const u32 *state, u32 *out, int nrounds);
38+
void hchacha_block_generic(const u32 *state, u32 *out, int nrounds);
39+
40+
static inline void hchacha_block(const u32 *state, u32 *out, int nrounds)
41+
{
42+
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
43+
hchacha_block_arch(state, out, nrounds);
44+
else
45+
hchacha_block_generic(state, out, nrounds);
46+
}
4547

46-
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
47-
unsigned int keysize);
48-
int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
49-
unsigned int keysize);
48+
void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv);
49+
static inline void chacha_init_generic(u32 *state, const u32 *key, const u8 *iv)
50+
{
51+
state[0] = 0x61707865; /* "expa" */
52+
state[1] = 0x3320646e; /* "nd 3" */
53+
state[2] = 0x79622d32; /* "2-by" */
54+
state[3] = 0x6b206574; /* "te k" */
55+
state[4] = key[0];
56+
state[5] = key[1];
57+
state[6] = key[2];
58+
state[7] = key[3];
59+
state[8] = key[4];
60+
state[9] = key[5];
61+
state[10] = key[6];
62+
state[11] = key[7];
63+
state[12] = get_unaligned_le32(iv + 0);
64+
state[13] = get_unaligned_le32(iv + 4);
65+
state[14] = get_unaligned_le32(iv + 8);
66+
state[15] = get_unaligned_le32(iv + 12);
67+
}
5068

51-
int crypto_chacha_crypt(struct skcipher_request *req);
52-
int crypto_xchacha_crypt(struct skcipher_request *req);
69+
static inline void chacha_init(u32 *state, const u32 *key, const u8 *iv)
70+
{
71+
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
72+
chacha_init_arch(state, key, iv);
73+
else
74+
chacha_init_generic(state, key, iv);
75+
}
76+
77+
void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
78+
unsigned int bytes, int nrounds);
79+
void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
80+
unsigned int bytes, int nrounds);
81+
82+
static inline void chacha_crypt(u32 *state, u8 *dst, const u8 *src,
83+
unsigned int bytes, int nrounds)
84+
{
85+
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
86+
chacha_crypt_arch(state, dst, src, bytes, nrounds);
87+
else
88+
chacha_crypt_generic(state, dst, src, bytes, nrounds);
89+
}
90+
91+
static inline void chacha20_crypt(u32 *state, u8 *dst, const u8 *src,
92+
unsigned int bytes)
93+
{
94+
chacha_crypt(state, dst, src, bytes, 20);
95+
}
5396

5497
#endif /* _CRYPTO_CHACHA_H */

‎include/crypto/internal/chacha.h‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef _CRYPTO_INTERNAL_CHACHA_H
4+
#define _CRYPTO_INTERNAL_CHACHA_H
5+
6+
#include <crypto/chacha.h>
7+
#include <crypto/internal/skcipher.h>
8+
#include <linux/crypto.h>
9+
10+
struct chacha_ctx {
11+
u32 key[8];
12+
int nrounds;
13+
};
14+
15+
void crypto_chacha_init(u32 *state, const struct chacha_ctx *ctx, const u8 *iv);
16+
17+
static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
18+
unsigned int keysize, int nrounds)
19+
{
20+
struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
21+
int i;
22+
23+
if (keysize != CHACHA_KEY_SIZE)
24+
return -EINVAL;
25+
26+
for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
27+
ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
28+
29+
ctx->nrounds = nrounds;
30+
return 0;
31+
}
32+
33+
static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
34+
unsigned int keysize)
35+
{
36+
return chacha_setkey(tfm, key, keysize, 20);
37+
}
38+
39+
static int inline chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
40+
unsigned int keysize)
41+
{
42+
return chacha_setkey(tfm, key, keysize, 12);
43+
}
44+
45+
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
46+
unsigned int keysize);
47+
int crypto_chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
48+
unsigned int keysize);
49+
50+
int crypto_chacha_crypt(struct skcipher_request *req);
51+
int crypto_xchacha_crypt(struct skcipher_request *req);
52+
53+
#endif /* _CRYPTO_CHACHA_H */

‎lib/Makefile‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ endif
2626

2727
lib-y := ctype.o string.o vsprintf.o cmdline.o \
2828
rbtree.o radix-tree.o timerqueue.o xarray.o \
29-
idr.o extable.o \
30-
sha1.o chacha.o irq_regs.o argv_split.o \
29+
idr.o extable.o sha1.o irq_regs.o argv_split.o \
3130
flex_proportions.o ratelimit.o show_mem.o \
3231
is_single_threaded.o plist.o decompress.o kobject_uevent.o \
3332
earlycpio.o seq_buf.o siphash.o dec_and_lock.o \

‎lib/crypto/Kconfig‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ config CRYPTO_LIB_AES
88
config CRYPTO_LIB_ARC4
99
tristate
1010

11+
config CRYPTO_ARCH_HAVE_LIB_CHACHA
12+
tristate
13+
help
14+
Declares whether the architecture provides an arch-specific
15+
accelerated implementation of the ChaCha library interface,
16+
either builtin or as a module.
17+
18+
config CRYPTO_LIB_CHACHA_GENERIC
19+
tristate
20+
select CRYPTO_ALGAPI
21+
help
22+
This symbol can be depended upon by arch implementations of the
23+
ChaCha library interface that require the generic code as a
24+
fallback, e.g., for SIMD implementations. If no arch specific
25+
implementation is enabled, this implementation serves the users
26+
of CRYPTO_LIB_CHACHA.
27+
28+
config CRYPTO_LIB_CHACHA
29+
tristate "ChaCha library interface"
30+
depends on CRYPTO_ARCH_HAVE_LIB_CHACHA || !CRYPTO_ARCH_HAVE_LIB_CHACHA
31+
select CRYPTO_LIB_CHACHA_GENERIC if CRYPTO_ARCH_HAVE_LIB_CHACHA=n
32+
help
33+
Enable the ChaCha library interface. This interface may be fulfilled
34+
by either the generic implementation or an arch-specific one, if one
35+
is available and enabled.
36+
1137
config CRYPTO_LIB_DES
1238
tristate
1339

‎lib/crypto/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
# chacha is used by the /dev/random driver which is always builtin
4+
obj-y += chacha.o
5+
obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC) += libchacha.o
6+
37
obj-$(CONFIG_CRYPTO_LIB_AES) += libaes.o
48
libaes-y := aes.o
59

0 commit comments

Comments
 (0)