libchacha.c 896 B

1234567891011121314151617181920212223242526272829303132333435
  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. #include <linux/kernel.h>
  8. #include <linux/export.h>
  9. #include <linux/module.h>
  10. #include <crypto/algapi.h> // for crypto_xor_cpy
  11. #include <crypto/chacha.h>
  12. void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
  13. unsigned int bytes, int nrounds)
  14. {
  15. /* aligned to potentially speed up crypto_xor() */
  16. u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
  17. while (bytes >= CHACHA_BLOCK_SIZE) {
  18. chacha_block_generic(state, stream, nrounds);
  19. crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE);
  20. bytes -= CHACHA_BLOCK_SIZE;
  21. dst += CHACHA_BLOCK_SIZE;
  22. src += CHACHA_BLOCK_SIZE;
  23. }
  24. if (bytes) {
  25. chacha_block_generic(state, stream, nrounds);
  26. crypto_xor_cpy(dst, src, stream, bytes);
  27. }
  28. }
  29. EXPORT_SYMBOL(chacha_crypt_generic);
  30. MODULE_LICENSE("GPL");