aes-ce-cipher.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2013 - 2014 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <crypto/aes.h>
  12. #include <linux/cpufeature.h>
  13. #include <linux/crypto.h>
  14. #include <linux/module.h>
  15. MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
  16. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  17. MODULE_LICENSE("GPL v2");
  18. struct aes_block {
  19. u8 b[AES_BLOCK_SIZE];
  20. };
  21. static int num_rounds(struct crypto_aes_ctx *ctx)
  22. {
  23. /*
  24. * # of rounds specified by AES:
  25. * 128 bit key 10 rounds
  26. * 192 bit key 12 rounds
  27. * 256 bit key 14 rounds
  28. * => n byte key => 6 + (n/4) rounds
  29. */
  30. return 6 + ctx->key_length / 4;
  31. }
  32. static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
  33. {
  34. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  35. struct aes_block *out = (struct aes_block *)dst;
  36. struct aes_block const *in = (struct aes_block *)src;
  37. void *dummy0;
  38. int dummy1;
  39. kernel_neon_begin_partial(4);
  40. __asm__(" ld1 {v0.16b}, %[in] ;"
  41. " ld1 {v1.2d}, [%[key]], #16 ;"
  42. " cmp %w[rounds], #10 ;"
  43. " bmi 0f ;"
  44. " bne 3f ;"
  45. " mov v3.16b, v1.16b ;"
  46. " b 2f ;"
  47. "0: mov v2.16b, v1.16b ;"
  48. " ld1 {v3.2d}, [%[key]], #16 ;"
  49. "1: aese v0.16b, v2.16b ;"
  50. " aesmc v0.16b, v0.16b ;"
  51. "2: ld1 {v1.2d}, [%[key]], #16 ;"
  52. " aese v0.16b, v3.16b ;"
  53. " aesmc v0.16b, v0.16b ;"
  54. "3: ld1 {v2.2d}, [%[key]], #16 ;"
  55. " subs %w[rounds], %w[rounds], #3 ;"
  56. " aese v0.16b, v1.16b ;"
  57. " aesmc v0.16b, v0.16b ;"
  58. " ld1 {v3.2d}, [%[key]], #16 ;"
  59. " bpl 1b ;"
  60. " aese v0.16b, v2.16b ;"
  61. " eor v0.16b, v0.16b, v3.16b ;"
  62. " st1 {v0.16b}, %[out] ;"
  63. : [out] "=Q"(*out),
  64. [key] "=r"(dummy0),
  65. [rounds] "=r"(dummy1)
  66. : [in] "Q"(*in),
  67. "1"(ctx->key_enc),
  68. "2"(num_rounds(ctx) - 2)
  69. : "cc");
  70. kernel_neon_end();
  71. }
  72. static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
  73. {
  74. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  75. struct aes_block *out = (struct aes_block *)dst;
  76. struct aes_block const *in = (struct aes_block *)src;
  77. void *dummy0;
  78. int dummy1;
  79. kernel_neon_begin_partial(4);
  80. __asm__(" ld1 {v0.16b}, %[in] ;"
  81. " ld1 {v1.2d}, [%[key]], #16 ;"
  82. " cmp %w[rounds], #10 ;"
  83. " bmi 0f ;"
  84. " bne 3f ;"
  85. " mov v3.16b, v1.16b ;"
  86. " b 2f ;"
  87. "0: mov v2.16b, v1.16b ;"
  88. " ld1 {v3.2d}, [%[key]], #16 ;"
  89. "1: aesd v0.16b, v2.16b ;"
  90. " aesimc v0.16b, v0.16b ;"
  91. "2: ld1 {v1.2d}, [%[key]], #16 ;"
  92. " aesd v0.16b, v3.16b ;"
  93. " aesimc v0.16b, v0.16b ;"
  94. "3: ld1 {v2.2d}, [%[key]], #16 ;"
  95. " subs %w[rounds], %w[rounds], #3 ;"
  96. " aesd v0.16b, v1.16b ;"
  97. " aesimc v0.16b, v0.16b ;"
  98. " ld1 {v3.2d}, [%[key]], #16 ;"
  99. " bpl 1b ;"
  100. " aesd v0.16b, v2.16b ;"
  101. " eor v0.16b, v0.16b, v3.16b ;"
  102. " st1 {v0.16b}, %[out] ;"
  103. : [out] "=Q"(*out),
  104. [key] "=r"(dummy0),
  105. [rounds] "=r"(dummy1)
  106. : [in] "Q"(*in),
  107. "1"(ctx->key_dec),
  108. "2"(num_rounds(ctx) - 2)
  109. : "cc");
  110. kernel_neon_end();
  111. }
  112. static struct crypto_alg aes_alg = {
  113. .cra_name = "aes",
  114. .cra_driver_name = "aes-ce",
  115. .cra_priority = 300,
  116. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  117. .cra_blocksize = AES_BLOCK_SIZE,
  118. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  119. .cra_module = THIS_MODULE,
  120. .cra_cipher = {
  121. .cia_min_keysize = AES_MIN_KEY_SIZE,
  122. .cia_max_keysize = AES_MAX_KEY_SIZE,
  123. .cia_setkey = crypto_aes_set_key,
  124. .cia_encrypt = aes_cipher_encrypt,
  125. .cia_decrypt = aes_cipher_decrypt
  126. }
  127. };
  128. static int __init aes_mod_init(void)
  129. {
  130. return crypto_register_alg(&aes_alg);
  131. }
  132. static void __exit aes_mod_exit(void)
  133. {
  134. crypto_unregister_alg(&aes_alg);
  135. }
  136. module_cpu_feature_match(AES, aes_mod_init);
  137. module_exit(aes_mod_exit);