Skip to content

Commit 3cc2151

Browse files
ebiggersherbertx
authored andcommitted
crypto: arm/chacha20 - refactor to allow varying number of rounds
In preparation for adding XChaCha12 support, rename/refactor the NEON implementation of ChaCha20 to support different numbers of rounds. Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent d97a943 commit 3cc2151

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

arch/arm/crypto/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ obj-$(CONFIG_CRYPTO_SHA1_ARM) += sha1-arm.o
99
obj-$(CONFIG_CRYPTO_SHA1_ARM_NEON) += sha1-arm-neon.o
1010
obj-$(CONFIG_CRYPTO_SHA256_ARM) += sha256-arm.o
1111
obj-$(CONFIG_CRYPTO_SHA512_ARM) += sha512-arm.o
12-
obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o
12+
obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha-neon.o
1313

1414
ce-obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
1515
ce-obj-$(CONFIG_CRYPTO_SHA1_ARM_CE) += sha1-arm-ce.o
@@ -52,7 +52,7 @@ aes-arm-ce-y := aes-ce-core.o aes-ce-glue.o
5252
ghash-arm-ce-y := ghash-ce-core.o ghash-ce-glue.o
5353
crct10dif-arm-ce-y := crct10dif-ce-core.o crct10dif-ce-glue.o
5454
crc32-arm-ce-y:= crc32-ce-core.o crc32-ce-glue.o
55-
chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o
55+
chacha-neon-y := chacha-neon-core.o chacha-neon-glue.o
5656

5757
ifdef REGENERATE_ARM_CRYPTO
5858
quiet_cmd_perl = PERL $@

arch/arm/crypto/chacha20-neon-core.S renamed to arch/arm/crypto/chacha-neon-core.S

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ChaCha20 256-bit cipher algorithm, RFC7539, ARM NEON functions
2+
* ChaCha/XChaCha NEON helper functions
33
*
44
* Copyright (C) 2016 Linaro, Ltd. <ard.biesheuvel@linaro.org>
55
*
@@ -27,9 +27,9 @@
2727
* (d) vtbl.8 + vtbl.8 (multiple of 8 bits rotations only,
2828
* needs index vector)
2929
*
30-
* ChaCha20 has 16, 12, 8, and 7-bit rotations. For the 12 and 7-bit
31-
* rotations, the only choices are (a) and (b). We use (a) since it takes
32-
* two-thirds the cycles of (b) on both Cortex-A7 and Cortex-A53.
30+
* ChaCha has 16, 12, 8, and 7-bit rotations. For the 12 and 7-bit rotations,
31+
* the only choices are (a) and (b). We use (a) since it takes two-thirds the
32+
* cycles of (b) on both Cortex-A7 and Cortex-A53.
3333
*
3434
* For the 16-bit rotation, we use vrev32.16 since it's consistently fastest
3535
* and doesn't need a temporary register.
@@ -53,18 +53,19 @@
5353
.align 5
5454

5555
/*
56-
* chacha20_permute - permute one block
56+
* chacha_permute - permute one block
5757
*
5858
* Permute one 64-byte block where the state matrix is stored in the four NEON
5959
* registers q0-q3. It performs matrix operations on four words in parallel,
6060
* but requires shuffling to rearrange the words after each round.
6161
*
62+
* The round count is given in r3.
63+
*
6264
* Clobbers: r3, ip, q4-q5
6365
*/
64-
chacha20_permute:
66+
chacha_permute:
6567

6668
adr ip, .Lrol8_table
67-
mov r3, #10
6869
vld1.8 {d10}, [ip, :64]
6970

7071
.Ldoubleround:
@@ -128,16 +129,17 @@ chacha20_permute:
128129
// x3 = shuffle32(x3, MASK(0, 3, 2, 1))
129130
vext.8 q3, q3, q3, #4
130131

131-
subs r3, r3, #1
132+
subs r3, r3, #2
132133
bne .Ldoubleround
133134

134135
bx lr
135-
ENDPROC(chacha20_permute)
136+
ENDPROC(chacha_permute)
136137

137-
ENTRY(chacha20_block_xor_neon)
138+
ENTRY(chacha_block_xor_neon)
138139
// r0: Input state matrix, s
139140
// r1: 1 data block output, o
140141
// r2: 1 data block input, i
142+
// r3: nrounds
141143
push {lr}
142144

143145
// x0..3 = s0..3
@@ -150,7 +152,7 @@ ENTRY(chacha20_block_xor_neon)
150152
vmov q10, q2
151153
vmov q11, q3
152154

153-
bl chacha20_permute
155+
bl chacha_permute
154156

155157
add ip, r2, #0x20
156158
vld1.8 {q4-q5}, [r2]
@@ -177,30 +179,32 @@ ENTRY(chacha20_block_xor_neon)
177179
vst1.8 {q2-q3}, [ip]
178180

179181
pop {pc}
180-
ENDPROC(chacha20_block_xor_neon)
182+
ENDPROC(chacha_block_xor_neon)
181183

182-
ENTRY(hchacha20_block_neon)
184+
ENTRY(hchacha_block_neon)
183185
// r0: Input state matrix, s
184186
// r1: output (8 32-bit words)
187+
// r2: nrounds
185188
push {lr}
186189

187190
vld1.32 {q0-q1}, [r0]!
188191
vld1.32 {q2-q3}, [r0]
189192

190-
bl chacha20_permute
193+
mov r3, r2
194+
bl chacha_permute
191195

192196
vst1.32 {q0}, [r1]!
193197
vst1.32 {q3}, [r1]
194198

195199
pop {pc}
196-
ENDPROC(hchacha20_block_neon)
200+
ENDPROC(hchacha_block_neon)
197201

198202
.align 4
199203
.Lctrinc: .word 0, 1, 2, 3
200204
.Lrol8_table: .byte 3, 0, 1, 2, 7, 4, 5, 6
201205

202206
.align 5
203-
ENTRY(chacha20_4block_xor_neon)
207+
ENTRY(chacha_4block_xor_neon)
204208
push {r4-r5}
205209
mov r4, sp // preserve the stack pointer
206210
sub ip, sp, #0x20 // allocate a 32 byte buffer
@@ -210,9 +214,10 @@ ENTRY(chacha20_4block_xor_neon)
210214
// r0: Input state matrix, s
211215
// r1: 4 data blocks output, o
212216
// r2: 4 data blocks input, i
217+
// r3: nrounds
213218

214219
//
215-
// This function encrypts four consecutive ChaCha20 blocks by loading
220+
// This function encrypts four consecutive ChaCha blocks by loading
216221
// the state matrix in NEON registers four times. The algorithm performs
217222
// each operation on the corresponding word of each state matrix, hence
218223
// requires no word shuffling. The words are re-interleaved before the
@@ -245,7 +250,6 @@ ENTRY(chacha20_4block_xor_neon)
245250
vdup.32 q0, d0[0]
246251

247252
adr ip, .Lrol8_table
248-
mov r3, #10
249253
b 1f
250254

251255
.Ldoubleround4:
@@ -443,7 +447,7 @@ ENTRY(chacha20_4block_xor_neon)
443447
vsri.u32 q5, q8, #25
444448
vsri.u32 q6, q9, #25
445449

446-
subs r3, r3, #1
450+
subs r3, r3, #2
447451
bne .Ldoubleround4
448452

449453
// x0..7[0-3] are in q0-q7, x10..15[0-3] are in q10-q15.
@@ -553,4 +557,4 @@ ENTRY(chacha20_4block_xor_neon)
553557

554558
pop {r4-r5}
555559
bx lr
556-
ENDPROC(chacha20_4block_xor_neon)
560+
ENDPROC(chacha_4block_xor_neon)

arch/arm/crypto/chacha20-neon-glue.c renamed to arch/arm/crypto/chacha-neon-glue.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,40 @@
2828
#include <asm/neon.h>
2929
#include <asm/simd.h>
3030

31-
asmlinkage void chacha20_block_xor_neon(u32 *state, u8 *dst, const u8 *src);
32-
asmlinkage void chacha20_4block_xor_neon(u32 *state, u8 *dst, const u8 *src);
33-
asmlinkage void hchacha20_block_neon(const u32 *state, u32 *out);
34-
35-
static void chacha20_doneon(u32 *state, u8 *dst, const u8 *src,
36-
unsigned int bytes)
31+
asmlinkage void chacha_block_xor_neon(const u32 *state, u8 *dst, const u8 *src,
32+
int nrounds);
33+
asmlinkage void chacha_4block_xor_neon(const u32 *state, u8 *dst, const u8 *src,
34+
int nrounds);
35+
asmlinkage void hchacha_block_neon(const u32 *state, u32 *out, int nrounds);
36+
37+
static void chacha_doneon(u32 *state, u8 *dst, const u8 *src,
38+
unsigned int bytes, int nrounds)
3739
{
3840
u8 buf[CHACHA_BLOCK_SIZE];
3941

4042
while (bytes >= CHACHA_BLOCK_SIZE * 4) {
41-
chacha20_4block_xor_neon(state, dst, src);
43+
chacha_4block_xor_neon(state, dst, src, nrounds);
4244
bytes -= CHACHA_BLOCK_SIZE * 4;
4345
src += CHACHA_BLOCK_SIZE * 4;
4446
dst += CHACHA_BLOCK_SIZE * 4;
4547
state[12] += 4;
4648
}
4749
while (bytes >= CHACHA_BLOCK_SIZE) {
48-
chacha20_block_xor_neon(state, dst, src);
50+
chacha_block_xor_neon(state, dst, src, nrounds);
4951
bytes -= CHACHA_BLOCK_SIZE;
5052
src += CHACHA_BLOCK_SIZE;
5153
dst += CHACHA_BLOCK_SIZE;
5254
state[12]++;
5355
}
5456
if (bytes) {
5557
memcpy(buf, src, bytes);
56-
chacha20_block_xor_neon(state, buf, buf);
58+
chacha_block_xor_neon(state, buf, buf, nrounds);
5759
memcpy(dst, buf, bytes);
5860
}
5961
}
6062

61-
static int chacha20_neon_stream_xor(struct skcipher_request *req,
62-
struct chacha_ctx *ctx, u8 *iv)
63+
static int chacha_neon_stream_xor(struct skcipher_request *req,
64+
struct chacha_ctx *ctx, u8 *iv)
6365
{
6466
struct skcipher_walk walk;
6567
u32 state[16];
@@ -76,27 +78,27 @@ static int chacha20_neon_stream_xor(struct skcipher_request *req,
7678
nbytes = round_down(nbytes, walk.stride);
7779

7880
kernel_neon_begin();
79-
chacha20_doneon(state, walk.dst.virt.addr, walk.src.virt.addr,
80-
nbytes);
81+
chacha_doneon(state, walk.dst.virt.addr, walk.src.virt.addr,
82+
nbytes, ctx->nrounds);
8183
kernel_neon_end();
8284
err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
8385
}
8486

8587
return err;
8688
}
8789

88-
static int chacha20_neon(struct skcipher_request *req)
90+
static int chacha_neon(struct skcipher_request *req)
8991
{
9092
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
9193
struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
9294

9395
if (req->cryptlen <= CHACHA_BLOCK_SIZE || !may_use_simd())
9496
return crypto_chacha_crypt(req);
9597

96-
return chacha20_neon_stream_xor(req, ctx, req->iv);
98+
return chacha_neon_stream_xor(req, ctx, req->iv);
9799
}
98100

99-
static int xchacha20_neon(struct skcipher_request *req)
101+
static int xchacha_neon(struct skcipher_request *req)
100102
{
101103
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
102104
struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
@@ -110,12 +112,13 @@ static int xchacha20_neon(struct skcipher_request *req)
110112
crypto_chacha_init(state, ctx, req->iv);
111113

112114
kernel_neon_begin();
113-
hchacha20_block_neon(state, subctx.key);
115+
hchacha_block_neon(state, subctx.key, ctx->nrounds);
114116
kernel_neon_end();
117+
subctx.nrounds = ctx->nrounds;
115118

116119
memcpy(&real_iv[0], req->iv + 24, 8);
117120
memcpy(&real_iv[8], req->iv + 16, 8);
118-
return chacha20_neon_stream_xor(req, &subctx, real_iv);
121+
return chacha_neon_stream_xor(req, &subctx, real_iv);
119122
}
120123

121124
static struct skcipher_alg algs[] = {
@@ -133,8 +136,8 @@ static struct skcipher_alg algs[] = {
133136
.chunksize = CHACHA_BLOCK_SIZE,
134137
.walksize = 4 * CHACHA_BLOCK_SIZE,
135138
.setkey = crypto_chacha20_setkey,
136-
.encrypt = chacha20_neon,
137-
.decrypt = chacha20_neon,
139+
.encrypt = chacha_neon,
140+
.decrypt = chacha_neon,
138141
}, {
139142
.base.cra_name = "xchacha20",
140143
.base.cra_driver_name = "xchacha20-neon",
@@ -149,27 +152,28 @@ static struct skcipher_alg algs[] = {
149152
.chunksize = CHACHA_BLOCK_SIZE,
150153
.walksize = 4 * CHACHA_BLOCK_SIZE,
151154
.setkey = crypto_chacha20_setkey,
152-
.encrypt = xchacha20_neon,
153-
.decrypt = xchacha20_neon,
155+
.encrypt = xchacha_neon,
156+
.decrypt = xchacha_neon,
154157
}
155158
};
156159

157-
static int __init chacha20_simd_mod_init(void)
160+
static int __init chacha_simd_mod_init(void)
158161
{
159162
if (!(elf_hwcap & HWCAP_NEON))
160163
return -ENODEV;
161164

162165
return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
163166
}
164167

165-
static void __exit chacha20_simd_mod_fini(void)
168+
static void __exit chacha_simd_mod_fini(void)
166169
{
167170
crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
168171
}
169172

170-
module_init(chacha20_simd_mod_init);
171-
module_exit(chacha20_simd_mod_fini);
173+
module_init(chacha_simd_mod_init);
174+
module_exit(chacha_simd_mod_fini);
172175

176+
MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (NEON accelerated)");
173177
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
174178
MODULE_LICENSE("GPL v2");
175179
MODULE_ALIAS_CRYPTO("chacha20");

0 commit comments

Comments
 (0)