Skip to content

Commit 9ae433b

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: chacha20 - convert generic and x86 versions to skcipher
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers. This ports the generic and x86 versions at the same time because the latter reuses routines of the former. Note that the skcipher_walk() API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify the encrypt() routine somewhat. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8096667 commit 9ae433b

3 files changed

Lines changed: 64 additions & 84 deletions

File tree

‎arch/x86/crypto/chacha20_glue.c‎

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <crypto/algapi.h>
1313
#include <crypto/chacha20.h>
14-
#include <linux/crypto.h>
14+
#include <crypto/internal/skcipher.h>
1515
#include <linux/kernel.h>
1616
#include <linux/module.h>
1717
#include <asm/fpu/api.h>
@@ -63,64 +63,57 @@ static void chacha20_dosimd(u32 *state, u8 *dst, const u8 *src,
6363
}
6464
}
6565

66-
static int chacha20_simd(struct blkcipher_desc *desc, struct scatterlist *dst,
67-
struct scatterlist *src, unsigned int nbytes)
66+
static int chacha20_simd(struct skcipher_request *req)
6867
{
69-
u32 *state, state_buf[16 + (CHACHA20_STATE_ALIGN / sizeof(u32)) - 1];
70-
struct blkcipher_walk walk;
68+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
69+
struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
70+
u32 state[16] __aligned(CHACHA20_STATE_ALIGN);
71+
struct skcipher_walk walk;
7172
int err;
7273

73-
if (nbytes <= CHACHA20_BLOCK_SIZE || !may_use_simd())
74-
return crypto_chacha20_crypt(desc, dst, src, nbytes);
74+
if (req->cryptlen <= CHACHA20_BLOCK_SIZE || !may_use_simd())
75+
return crypto_chacha20_crypt(req);
7576

76-
state = (u32 *)roundup((uintptr_t)state_buf, CHACHA20_STATE_ALIGN);
77+
err = skcipher_walk_virt(&walk, req, true);
7778

78-
blkcipher_walk_init(&walk, dst, src, nbytes);
79-
err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
80-
81-
crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
79+
crypto_chacha20_init(state, ctx, walk.iv);
8280

8381
kernel_fpu_begin();
8482

8583
while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
8684
chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
8785
rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
88-
err = blkcipher_walk_done(desc, &walk,
89-
walk.nbytes % CHACHA20_BLOCK_SIZE);
86+
err = skcipher_walk_done(&walk,
87+
walk.nbytes % CHACHA20_BLOCK_SIZE);
9088
}
9189

9290
if (walk.nbytes) {
9391
chacha20_dosimd(state, walk.dst.virt.addr, walk.src.virt.addr,
9492
walk.nbytes);
95-
err = blkcipher_walk_done(desc, &walk, 0);
93+
err = skcipher_walk_done(&walk, 0);
9694
}
9795

9896
kernel_fpu_end();
9997

10098
return err;
10199
}
102100

103-
static struct crypto_alg alg = {
104-
.cra_name = "chacha20",
105-
.cra_driver_name = "chacha20-simd",
106-
.cra_priority = 300,
107-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
108-
.cra_blocksize = 1,
109-
.cra_type = &crypto_blkcipher_type,
110-
.cra_ctxsize = sizeof(struct chacha20_ctx),
111-
.cra_alignmask = sizeof(u32) - 1,
112-
.cra_module = THIS_MODULE,
113-
.cra_u = {
114-
.blkcipher = {
115-
.min_keysize = CHACHA20_KEY_SIZE,
116-
.max_keysize = CHACHA20_KEY_SIZE,
117-
.ivsize = CHACHA20_IV_SIZE,
118-
.geniv = "seqiv",
119-
.setkey = crypto_chacha20_setkey,
120-
.encrypt = chacha20_simd,
121-
.decrypt = chacha20_simd,
122-
},
123-
},
101+
static struct skcipher_alg alg = {
102+
.base.cra_name = "chacha20",
103+
.base.cra_driver_name = "chacha20-simd",
104+
.base.cra_priority = 300,
105+
.base.cra_blocksize = 1,
106+
.base.cra_ctxsize = sizeof(struct chacha20_ctx),
107+
.base.cra_alignmask = sizeof(u32) - 1,
108+
.base.cra_module = THIS_MODULE,
109+
110+
.min_keysize = CHACHA20_KEY_SIZE,
111+
.max_keysize = CHACHA20_KEY_SIZE,
112+
.ivsize = CHACHA20_IV_SIZE,
113+
.chunksize = CHACHA20_BLOCK_SIZE,
114+
.setkey = crypto_chacha20_setkey,
115+
.encrypt = chacha20_simd,
116+
.decrypt = chacha20_simd,
124117
};
125118

126119
static int __init chacha20_simd_mod_init(void)
@@ -133,12 +126,12 @@ static int __init chacha20_simd_mod_init(void)
133126
boot_cpu_has(X86_FEATURE_AVX2) &&
134127
cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
135128
#endif
136-
return crypto_register_alg(&alg);
129+
return crypto_register_skcipher(&alg);
137130
}
138131

139132
static void __exit chacha20_simd_mod_fini(void)
140133
{
141-
crypto_unregister_alg(&alg);
134+
crypto_unregister_skcipher(&alg);
142135
}
143136

144137
module_init(chacha20_simd_mod_init);

‎crypto/chacha20_generic.c‎

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
*/
1111

1212
#include <crypto/algapi.h>
13-
#include <linux/crypto.h>
14-
#include <linux/kernel.h>
15-
#include <linux/module.h>
1613
#include <crypto/chacha20.h>
14+
#include <crypto/internal/skcipher.h>
15+
#include <linux/module.h>
1716

1817
static inline u32 le32_to_cpuvp(const void *p)
1918
{
@@ -63,10 +62,10 @@ void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
6362
}
6463
EXPORT_SYMBOL_GPL(crypto_chacha20_init);
6564

66-
int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
65+
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
6766
unsigned int keysize)
6867
{
69-
struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
68+
struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
7069
int i;
7170

7271
if (keysize != CHACHA20_KEY_SIZE)
@@ -79,66 +78,54 @@ int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
7978
}
8079
EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
8180

82-
int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
83-
struct scatterlist *src, unsigned int nbytes)
81+
int crypto_chacha20_crypt(struct skcipher_request *req)
8482
{
85-
struct blkcipher_walk walk;
83+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
84+
struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
85+
struct skcipher_walk walk;
8686
u32 state[16];
8787
int err;
8888

89-
blkcipher_walk_init(&walk, dst, src, nbytes);
90-
err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
91-
92-
crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
89+
err = skcipher_walk_virt(&walk, req, true);
9390

94-
while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
95-
chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
96-
rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
97-
err = blkcipher_walk_done(desc, &walk,
98-
walk.nbytes % CHACHA20_BLOCK_SIZE);
99-
}
91+
crypto_chacha20_init(state, ctx, walk.iv);
10092

101-
if (walk.nbytes) {
93+
while (walk.nbytes > 0) {
10294
chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
10395
walk.nbytes);
104-
err = blkcipher_walk_done(desc, &walk, 0);
96+
err = skcipher_walk_done(&walk, 0);
10597
}
10698

10799
return err;
108100
}
109101
EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
110102

111-
static struct crypto_alg alg = {
112-
.cra_name = "chacha20",
113-
.cra_driver_name = "chacha20-generic",
114-
.cra_priority = 100,
115-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
116-
.cra_blocksize = 1,
117-
.cra_type = &crypto_blkcipher_type,
118-
.cra_ctxsize = sizeof(struct chacha20_ctx),
119-
.cra_alignmask = sizeof(u32) - 1,
120-
.cra_module = THIS_MODULE,
121-
.cra_u = {
122-
.blkcipher = {
123-
.min_keysize = CHACHA20_KEY_SIZE,
124-
.max_keysize = CHACHA20_KEY_SIZE,
125-
.ivsize = CHACHA20_IV_SIZE,
126-
.geniv = "seqiv",
127-
.setkey = crypto_chacha20_setkey,
128-
.encrypt = crypto_chacha20_crypt,
129-
.decrypt = crypto_chacha20_crypt,
130-
},
131-
},
103+
static struct skcipher_alg alg = {
104+
.base.cra_name = "chacha20",
105+
.base.cra_driver_name = "chacha20-generic",
106+
.base.cra_priority = 100,
107+
.base.cra_blocksize = 1,
108+
.base.cra_ctxsize = sizeof(struct chacha20_ctx),
109+
.base.cra_alignmask = sizeof(u32) - 1,
110+
.base.cra_module = THIS_MODULE,
111+
112+
.min_keysize = CHACHA20_KEY_SIZE,
113+
.max_keysize = CHACHA20_KEY_SIZE,
114+
.ivsize = CHACHA20_IV_SIZE,
115+
.chunksize = CHACHA20_BLOCK_SIZE,
116+
.setkey = crypto_chacha20_setkey,
117+
.encrypt = crypto_chacha20_crypt,
118+
.decrypt = crypto_chacha20_crypt,
132119
};
133120

134121
static int __init chacha20_generic_mod_init(void)
135122
{
136-
return crypto_register_alg(&alg);
123+
return crypto_register_skcipher(&alg);
137124
}
138125

139126
static void __exit chacha20_generic_mod_fini(void)
140127
{
141-
crypto_unregister_alg(&alg);
128+
crypto_unregister_skcipher(&alg);
142129
}
143130

144131
module_init(chacha20_generic_mod_init);

‎include/crypto/chacha20.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef _CRYPTO_CHACHA20_H
66
#define _CRYPTO_CHACHA20_H
77

8+
#include <crypto/skcipher.h>
89
#include <linux/types.h>
910
#include <linux/crypto.h>
1011

@@ -18,9 +19,8 @@ struct chacha20_ctx {
1819

1920
void chacha20_block(u32 *state, void *stream);
2021
void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv);
21-
int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
22+
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
2223
unsigned int keysize);
23-
int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
24-
struct scatterlist *src, unsigned int nbytes);
24+
int crypto_chacha20_crypt(struct skcipher_request *req);
2525

2626
#endif

0 commit comments

Comments
 (0)