aes_ti.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Scalar fixed time AES core transform
  4. *
  5. * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. */
  7. #include <crypto/aes.h>
  8. #include <linux/crypto.h>
  9. #include <linux/module.h>
  10. static int aesti_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  11. unsigned int key_len)
  12. {
  13. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  14. return aes_expandkey(ctx, in_key, key_len);
  15. }
  16. static void aesti_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  17. {
  18. const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  19. unsigned long flags;
  20. /*
  21. * Temporarily disable interrupts to avoid races where cachelines are
  22. * evicted when the CPU is interrupted to do something else.
  23. */
  24. local_irq_save(flags);
  25. aes_encrypt(ctx, out, in);
  26. local_irq_restore(flags);
  27. }
  28. static void aesti_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  29. {
  30. const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  31. unsigned long flags;
  32. /*
  33. * Temporarily disable interrupts to avoid races where cachelines are
  34. * evicted when the CPU is interrupted to do something else.
  35. */
  36. local_irq_save(flags);
  37. aes_decrypt(ctx, out, in);
  38. local_irq_restore(flags);
  39. }
  40. static struct crypto_alg aes_alg = {
  41. .cra_name = "aes",
  42. .cra_driver_name = "aes-fixed-time",
  43. .cra_priority = 100 + 1,
  44. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  45. .cra_blocksize = AES_BLOCK_SIZE,
  46. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  47. .cra_module = THIS_MODULE,
  48. .cra_cipher.cia_min_keysize = AES_MIN_KEY_SIZE,
  49. .cra_cipher.cia_max_keysize = AES_MAX_KEY_SIZE,
  50. .cra_cipher.cia_setkey = aesti_set_key,
  51. .cra_cipher.cia_encrypt = aesti_encrypt,
  52. .cra_cipher.cia_decrypt = aesti_decrypt
  53. };
  54. static int __init aes_init(void)
  55. {
  56. return crypto_register_alg(&aes_alg);
  57. }
  58. static void __exit aes_fini(void)
  59. {
  60. crypto_unregister_alg(&aes_alg);
  61. }
  62. module_init(aes_init);
  63. module_exit(aes_fini);
  64. MODULE_DESCRIPTION("Generic fixed time AES");
  65. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  66. MODULE_LICENSE("GPL v2");