chacha.h 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _CRYPTO_INTERNAL_CHACHA_H
  3. #define _CRYPTO_INTERNAL_CHACHA_H
  4. #include <crypto/chacha.h>
  5. #include <crypto/internal/skcipher.h>
  6. #include <linux/crypto.h>
  7. struct chacha_ctx {
  8. u32 key[8];
  9. int nrounds;
  10. };
  11. static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
  12. unsigned int keysize, int nrounds)
  13. {
  14. struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
  15. int i;
  16. if (keysize != CHACHA_KEY_SIZE)
  17. return -EINVAL;
  18. for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
  19. ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
  20. ctx->nrounds = nrounds;
  21. return 0;
  22. }
  23. static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
  24. unsigned int keysize)
  25. {
  26. return chacha_setkey(tfm, key, keysize, 20);
  27. }
  28. static inline int chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
  29. unsigned int keysize)
  30. {
  31. return chacha_setkey(tfm, key, keysize, 12);
  32. }
  33. #endif /* _CRYPTO_CHACHA_H */