arc4.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API
  4. *
  5. * ARC4 Cipher Algorithm
  6. *
  7. * Jon Oberheide <jon@oberheide.org>
  8. */
  9. #include <crypto/arc4.h>
  10. #include <crypto/internal/skcipher.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #define ARC4_ALIGN __alignof__(struct arc4_ctx)
  16. static int crypto_arc4_setkey(struct crypto_lskcipher *tfm, const u8 *in_key,
  17. unsigned int key_len)
  18. {
  19. struct arc4_ctx *ctx = crypto_lskcipher_ctx(tfm);
  20. return arc4_setkey(ctx, in_key, key_len);
  21. }
  22. static int crypto_arc4_crypt(struct crypto_lskcipher *tfm, const u8 *src,
  23. u8 *dst, unsigned nbytes, u8 *siv, u32 flags)
  24. {
  25. struct arc4_ctx *ctx = crypto_lskcipher_ctx(tfm);
  26. if (!(flags & CRYPTO_LSKCIPHER_FLAG_CONT))
  27. memcpy(siv, ctx, sizeof(*ctx));
  28. ctx = (struct arc4_ctx *)siv;
  29. arc4_crypt(ctx, dst, src, nbytes);
  30. return 0;
  31. }
  32. static int crypto_arc4_init(struct crypto_lskcipher *tfm)
  33. {
  34. pr_warn_ratelimited("\"%s\" (%ld) uses obsolete ecb(arc4) skcipher\n",
  35. current->comm, (unsigned long)current->pid);
  36. return 0;
  37. }
  38. static struct lskcipher_alg arc4_alg = {
  39. .co.base.cra_name = "arc4",
  40. .co.base.cra_driver_name = "arc4-generic",
  41. .co.base.cra_priority = 100,
  42. .co.base.cra_blocksize = ARC4_BLOCK_SIZE,
  43. .co.base.cra_ctxsize = sizeof(struct arc4_ctx),
  44. .co.base.cra_alignmask = ARC4_ALIGN - 1,
  45. .co.base.cra_module = THIS_MODULE,
  46. .co.min_keysize = ARC4_MIN_KEY_SIZE,
  47. .co.max_keysize = ARC4_MAX_KEY_SIZE,
  48. .co.statesize = sizeof(struct arc4_ctx),
  49. .setkey = crypto_arc4_setkey,
  50. .encrypt = crypto_arc4_crypt,
  51. .decrypt = crypto_arc4_crypt,
  52. .init = crypto_arc4_init,
  53. };
  54. static int __init arc4_init(void)
  55. {
  56. return crypto_register_lskcipher(&arc4_alg);
  57. }
  58. static void __exit arc4_exit(void)
  59. {
  60. crypto_unregister_lskcipher(&arc4_alg);
  61. }
  62. module_init(arc4_init);
  63. module_exit(arc4_exit);
  64. MODULE_LICENSE("GPL");
  65. MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
  66. MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
  67. MODULE_ALIAS_CRYPTO("ecb(arc4)");