xref: /openbmc/linux/lib/crypto/libchacha.c (revision 234489ac)
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 <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/module.h>
11 
12 #include <crypto/algapi.h> // for crypto_xor_cpy
13 #include <crypto/chacha.h>
14 
15 void chacha_crypt_generic(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_generic(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_generic(state, stream, nrounds);
30 		crypto_xor_cpy(dst, src, stream, bytes);
31 	}
32 }
33 EXPORT_SYMBOL(chacha_crypt_generic);
34 
35 MODULE_LICENSE("GPL");
36