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