aes_glue.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Glue Code for the asm optimized version of the AES Cipher Algorithm
  3. */
  4. #include <linux/module.h>
  5. #include <linux/crypto.h>
  6. #include <crypto/aes.h>
  7. #include "aes_glue.h"
  8. EXPORT_SYMBOL(AES_encrypt);
  9. EXPORT_SYMBOL(AES_decrypt);
  10. EXPORT_SYMBOL(private_AES_set_encrypt_key);
  11. EXPORT_SYMBOL(private_AES_set_decrypt_key);
  12. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  13. {
  14. struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
  15. AES_encrypt(src, dst, &ctx->enc_key);
  16. }
  17. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  18. {
  19. struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
  20. AES_decrypt(src, dst, &ctx->dec_key);
  21. }
  22. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  23. unsigned int key_len)
  24. {
  25. struct AES_CTX *ctx = crypto_tfm_ctx(tfm);
  26. switch (key_len) {
  27. case AES_KEYSIZE_128:
  28. key_len = 128;
  29. break;
  30. case AES_KEYSIZE_192:
  31. key_len = 192;
  32. break;
  33. case AES_KEYSIZE_256:
  34. key_len = 256;
  35. break;
  36. default:
  37. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  38. return -EINVAL;
  39. }
  40. if (private_AES_set_encrypt_key(in_key, key_len, &ctx->enc_key) == -1) {
  41. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  42. return -EINVAL;
  43. }
  44. /* private_AES_set_decrypt_key expects an encryption key as input */
  45. ctx->dec_key = ctx->enc_key;
  46. if (private_AES_set_decrypt_key(in_key, key_len, &ctx->dec_key) == -1) {
  47. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  48. return -EINVAL;
  49. }
  50. return 0;
  51. }
  52. static struct crypto_alg aes_alg = {
  53. .cra_name = "aes",
  54. .cra_driver_name = "aes-asm",
  55. .cra_priority = 200,
  56. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  57. .cra_blocksize = AES_BLOCK_SIZE,
  58. .cra_ctxsize = sizeof(struct AES_CTX),
  59. .cra_module = THIS_MODULE,
  60. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  61. .cra_u = {
  62. .cipher = {
  63. .cia_min_keysize = AES_MIN_KEY_SIZE,
  64. .cia_max_keysize = AES_MAX_KEY_SIZE,
  65. .cia_setkey = aes_set_key,
  66. .cia_encrypt = aes_encrypt,
  67. .cia_decrypt = aes_decrypt
  68. }
  69. }
  70. };
  71. static int __init aes_init(void)
  72. {
  73. return crypto_register_alg(&aes_alg);
  74. }
  75. static void __exit aes_fini(void)
  76. {
  77. crypto_unregister_alg(&aes_alg);
  78. }
  79. module_init(aes_init);
  80. module_exit(aes_fini);
  81. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm (ASM)");
  82. MODULE_LICENSE("GPL");
  83. MODULE_ALIAS_CRYPTO("aes");
  84. MODULE_ALIAS_CRYPTO("aes-asm");
  85. MODULE_AUTHOR("David McCullough <ucdevel@gmail.com>");