arc4.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/algapi.h>
  10. #include <crypto/arc4.h>
  11. #include <crypto/internal/skcipher.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. static int crypto_arc4_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
  17. unsigned int key_len)
  18. {
  19. struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
  20. return arc4_setkey(ctx, in_key, key_len);
  21. }
  22. static int crypto_arc4_crypt(struct skcipher_request *req)
  23. {
  24. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  25. struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm);
  26. struct skcipher_walk walk;
  27. int err;
  28. err = skcipher_walk_virt(&walk, req, false);
  29. while (walk.nbytes > 0) {
  30. arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  31. walk.nbytes);
  32. err = skcipher_walk_done(&walk, 0);
  33. }
  34. return err;
  35. }
  36. static int crypto_arc4_init(struct crypto_skcipher *tfm)
  37. {
  38. pr_warn_ratelimited("\"%s\" (%ld) uses obsolete ecb(arc4) skcipher\n",
  39. current->comm, (unsigned long)current->pid);
  40. return 0;
  41. }
  42. static struct skcipher_alg arc4_alg = {
  43. /*
  44. * For legacy reasons, this is named "ecb(arc4)", not "arc4".
  45. * Nevertheless it's actually a stream cipher, not a block cipher.
  46. */
  47. .base.cra_name = "ecb(arc4)",
  48. .base.cra_driver_name = "ecb(arc4)-generic",
  49. .base.cra_priority = 100,
  50. .base.cra_blocksize = ARC4_BLOCK_SIZE,
  51. .base.cra_ctxsize = sizeof(struct arc4_ctx),
  52. .base.cra_module = THIS_MODULE,
  53. .min_keysize = ARC4_MIN_KEY_SIZE,
  54. .max_keysize = ARC4_MAX_KEY_SIZE,
  55. .setkey = crypto_arc4_setkey,
  56. .encrypt = crypto_arc4_crypt,
  57. .decrypt = crypto_arc4_crypt,
  58. .init = crypto_arc4_init,
  59. };
  60. static int __init arc4_init(void)
  61. {
  62. return crypto_register_skcipher(&arc4_alg);
  63. }
  64. static void __exit arc4_exit(void)
  65. {
  66. crypto_unregister_skcipher(&arc4_alg);
  67. }
  68. subsys_initcall(arc4_init);
  69. module_exit(arc4_exit);
  70. MODULE_LICENSE("GPL");
  71. MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
  72. MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
  73. MODULE_ALIAS_CRYPTO("ecb(arc4)");