bpf_crypto_skcipher.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2024 Meta, Inc */
  3. #include <linux/types.h>
  4. #include <linux/module.h>
  5. #include <linux/bpf_crypto.h>
  6. #include <crypto/skcipher.h>
  7. static void *bpf_crypto_lskcipher_alloc_tfm(const char *algo)
  8. {
  9. return crypto_alloc_lskcipher(algo, 0, 0);
  10. }
  11. static void bpf_crypto_lskcipher_free_tfm(void *tfm)
  12. {
  13. crypto_free_lskcipher(tfm);
  14. }
  15. static int bpf_crypto_lskcipher_has_algo(const char *algo)
  16. {
  17. return crypto_has_skcipher(algo, CRYPTO_ALG_TYPE_LSKCIPHER, CRYPTO_ALG_TYPE_MASK);
  18. }
  19. static int bpf_crypto_lskcipher_setkey(void *tfm, const u8 *key, unsigned int keylen)
  20. {
  21. return crypto_lskcipher_setkey(tfm, key, keylen);
  22. }
  23. static u32 bpf_crypto_lskcipher_get_flags(void *tfm)
  24. {
  25. return crypto_lskcipher_get_flags(tfm);
  26. }
  27. static unsigned int bpf_crypto_lskcipher_ivsize(void *tfm)
  28. {
  29. return crypto_lskcipher_ivsize(tfm);
  30. }
  31. static unsigned int bpf_crypto_lskcipher_statesize(void *tfm)
  32. {
  33. return crypto_lskcipher_statesize(tfm);
  34. }
  35. static int bpf_crypto_lskcipher_encrypt(void *tfm, const u8 *src, u8 *dst,
  36. unsigned int len, u8 *siv)
  37. {
  38. return crypto_lskcipher_encrypt(tfm, src, dst, len, siv);
  39. }
  40. static int bpf_crypto_lskcipher_decrypt(void *tfm, const u8 *src, u8 *dst,
  41. unsigned int len, u8 *siv)
  42. {
  43. return crypto_lskcipher_decrypt(tfm, src, dst, len, siv);
  44. }
  45. static const struct bpf_crypto_type bpf_crypto_lskcipher_type = {
  46. .alloc_tfm = bpf_crypto_lskcipher_alloc_tfm,
  47. .free_tfm = bpf_crypto_lskcipher_free_tfm,
  48. .has_algo = bpf_crypto_lskcipher_has_algo,
  49. .setkey = bpf_crypto_lskcipher_setkey,
  50. .encrypt = bpf_crypto_lskcipher_encrypt,
  51. .decrypt = bpf_crypto_lskcipher_decrypt,
  52. .ivsize = bpf_crypto_lskcipher_ivsize,
  53. .statesize = bpf_crypto_lskcipher_statesize,
  54. .get_flags = bpf_crypto_lskcipher_get_flags,
  55. .owner = THIS_MODULE,
  56. .name = "skcipher",
  57. };
  58. static int __init bpf_crypto_skcipher_init(void)
  59. {
  60. return bpf_crypto_register_type(&bpf_crypto_lskcipher_type);
  61. }
  62. static void __exit bpf_crypto_skcipher_exit(void)
  63. {
  64. int err = bpf_crypto_unregister_type(&bpf_crypto_lskcipher_type);
  65. WARN_ON_ONCE(err);
  66. }
  67. module_init(bpf_crypto_skcipher_init);
  68. module_exit(bpf_crypto_skcipher_exit);
  69. MODULE_LICENSE("GPL");