| 1 | |
| 2 | |
| 3 | * The ChaCha stream cipher (RFC7539)
|
| 4 | *
|
| 5 | * Copyright (C) 2015 Martin Willi
|
| 6 |
|
| 7 |
|
| 8 | #include <crypto/chacha.h>
|
| 9 | #include <crypto/utils.h>
|
| 10 | #include <linux/export.h>
|
| 11 | #include <linux/kernel.h>
|
| 12 | #include <linux/module.h>
|
| 13 |
|
| 14 | static void __maybe_unused chacha_crypt_generic(struct chacha_state *state,
|
| 15 | u8 *dst, const u8 *src,
|
| 16 | size_t bytes, int nrounds)
|
| 17 | {
|
| 18 |
|
| 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 |
|
| 34 | #ifdef CONFIG_CRYPTO_LIB_CHACHA_ARCH
|
| 35 | #include "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 | size_t 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");
|