1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * The ChaCha stream cipher (RFC7539)
4 *
5 * Copyright (C) 2015 Martin Willi
6 */
7
8 #include <crypto/algapi.h> // for crypto_xor_cpy
9 #include <crypto/chacha.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13
chacha_crypt_generic(struct chacha_state * state,u8 * dst,const u8 * src,unsigned int bytes,int nrounds)14 void chacha_crypt_generic(struct chacha_state *state, u8 *dst, const u8 *src,
15 unsigned int bytes, int nrounds)
16 {
17 /* aligned to potentially speed up crypto_xor() */
18 u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
19
20 while (bytes >= CHACHA_BLOCK_SIZE) {
21 chacha_block_generic(state, stream, nrounds);
22 crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE);
23 bytes -= CHACHA_BLOCK_SIZE;
24 dst += CHACHA_BLOCK_SIZE;
25 src += CHACHA_BLOCK_SIZE;
26 }
27 if (bytes) {
28 chacha_block_generic(state, stream, nrounds);
29 crypto_xor_cpy(dst, src, stream, bytes);
30 }
31 }
32 EXPORT_SYMBOL(chacha_crypt_generic);
33
34 MODULE_DESCRIPTION("ChaCha stream cipher (RFC7539)");
35 MODULE_LICENSE("GPL");
36