aes.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API support for AES block cipher
  4. *
  5. * Copyright 2026 Google LLC
  6. */
  7. #include <crypto/aes.h>
  8. #include <crypto/algapi.h>
  9. #include <linux/module.h>
  10. static_assert(__alignof__(struct aes_key) <= CRYPTO_MINALIGN);
  11. static int crypto_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  12. unsigned int key_len)
  13. {
  14. struct aes_key *key = crypto_tfm_ctx(tfm);
  15. return aes_preparekey(key, in_key, key_len);
  16. }
  17. static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  18. {
  19. const struct aes_key *key = crypto_tfm_ctx(tfm);
  20. aes_encrypt(key, out, in);
  21. }
  22. static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  23. {
  24. const struct aes_key *key = crypto_tfm_ctx(tfm);
  25. aes_decrypt(key, out, in);
  26. }
  27. static struct crypto_alg alg = {
  28. .cra_name = "aes",
  29. .cra_driver_name = "aes-lib",
  30. .cra_priority = 100,
  31. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  32. .cra_blocksize = AES_BLOCK_SIZE,
  33. .cra_ctxsize = sizeof(struct aes_key),
  34. .cra_module = THIS_MODULE,
  35. .cra_u = { .cipher = { .cia_min_keysize = AES_MIN_KEY_SIZE,
  36. .cia_max_keysize = AES_MAX_KEY_SIZE,
  37. .cia_setkey = crypto_aes_setkey,
  38. .cia_encrypt = crypto_aes_encrypt,
  39. .cia_decrypt = crypto_aes_decrypt } }
  40. };
  41. static int __init crypto_aes_mod_init(void)
  42. {
  43. return crypto_register_alg(&alg);
  44. }
  45. module_init(crypto_aes_mod_init);
  46. static void __exit crypto_aes_mod_exit(void)
  47. {
  48. crypto_unregister_alg(&alg);
  49. }
  50. module_exit(crypto_aes_mod_exit);
  51. MODULE_DESCRIPTION("Crypto API support for AES block cipher");
  52. MODULE_LICENSE("GPL");
  53. MODULE_ALIAS_CRYPTO("aes");
  54. MODULE_ALIAS_CRYPTO("aes-lib");