| 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 | |
| 14 | static void __maybe_unused |
| 15 | chacha_crypt_generic(struct chacha_state *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, out: stream, nrounds); |
| 23 | crypto_xor_cpy(dst, src1: src, src2: 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, out: stream, nrounds); |
| 30 | crypto_xor_cpy(dst, src1: src, src2: stream, size: bytes); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #ifdef CONFIG_CRYPTO_LIB_CHACHA_ARCH |
| 35 | #include "chacha.h" /* $(SRCARCH)/chacha.h */ |
| 36 | #else |
| 37 | #define chacha_crypt_arch chacha_crypt_generic |
| 38 | #define hchacha_block_arch hchacha_block_generic |
| 39 | #endif |
| 40 | |
| 41 | void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src, |
| 42 | unsigned int bytes, int nrounds) |
| 43 | { |
| 44 | chacha_crypt_arch(state, dst, src, bytes, nrounds); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(chacha_crypt); |
| 47 | |
| 48 | void hchacha_block(const struct chacha_state *state, |
| 49 | u32 out[HCHACHA_OUT_WORDS], int nrounds) |
| 50 | { |
| 51 | hchacha_block_arch(state, out, nrounds); |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(hchacha_block); |
| 54 | |
| 55 | #ifdef chacha_mod_init_arch |
| 56 | static int __init chacha_mod_init(void) |
| 57 | { |
| 58 | chacha_mod_init_arch(); |
| 59 | return 0; |
| 60 | } |
| 61 | subsys_initcall(chacha_mod_init); |
| 62 | |
| 63 | static void __exit chacha_mod_exit(void) |
| 64 | { |
| 65 | } |
| 66 | module_exit(chacha_mod_exit); |
| 67 | #endif |
| 68 | |
| 69 | MODULE_DESCRIPTION("ChaCha stream cipher (RFC7539)" ); |
| 70 | MODULE_LICENSE("GPL" ); |
| 71 | |