aes-ce-glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2013 - 2017 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 <asm/simd.h>
  12. #include <asm/unaligned.h>
  13. #include <crypto/aes.h>
  14. #include <linux/cpufeature.h>
  15. #include <linux/crypto.h>
  16. #include <linux/module.h>
  17. #include "aes-ce-setkey.h"
  18. MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
  19. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  20. MODULE_LICENSE("GPL v2");
  21. asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  22. asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  23. struct aes_block {
  24. u8 b[AES_BLOCK_SIZE];
  25. };
  26. asmlinkage void __aes_ce_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  27. asmlinkage void __aes_ce_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  28. asmlinkage u32 __aes_ce_sub(u32 l);
  29. asmlinkage void __aes_ce_invert(struct aes_block *out,
  30. const struct aes_block *in);
  31. static int num_rounds(struct crypto_aes_ctx *ctx)
  32. {
  33. /*
  34. * # of rounds specified by AES:
  35. * 128 bit key 10 rounds
  36. * 192 bit key 12 rounds
  37. * 256 bit key 14 rounds
  38. * => n byte key => 6 + (n/4) rounds
  39. */
  40. return 6 + ctx->key_length / 4;
  41. }
  42. static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
  43. {
  44. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  45. if (!may_use_simd()) {
  46. __aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
  47. return;
  48. }
  49. kernel_neon_begin();
  50. __aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
  51. kernel_neon_end();
  52. }
  53. static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
  54. {
  55. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  56. if (!may_use_simd()) {
  57. __aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
  58. return;
  59. }
  60. kernel_neon_begin();
  61. __aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
  62. kernel_neon_end();
  63. }
  64. int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
  65. unsigned int key_len)
  66. {
  67. /*
  68. * The AES key schedule round constants
  69. */
  70. static u8 const rcon[] = {
  71. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
  72. };
  73. u32 kwords = key_len / sizeof(u32);
  74. struct aes_block *key_enc, *key_dec;
  75. int i, j;
  76. if (key_len != AES_KEYSIZE_128 &&
  77. key_len != AES_KEYSIZE_192 &&
  78. key_len != AES_KEYSIZE_256)
  79. return -EINVAL;
  80. ctx->key_length = key_len;
  81. for (i = 0; i < kwords; i++)
  82. ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
  83. kernel_neon_begin();
  84. for (i = 0; i < sizeof(rcon); i++) {
  85. u32 *rki = ctx->key_enc + (i * kwords);
  86. u32 *rko = rki + kwords;
  87. rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
  88. rko[1] = rko[0] ^ rki[1];
  89. rko[2] = rko[1] ^ rki[2];
  90. rko[3] = rko[2] ^ rki[3];
  91. if (key_len == AES_KEYSIZE_192) {
  92. if (i >= 7)
  93. break;
  94. rko[4] = rko[3] ^ rki[4];
  95. rko[5] = rko[4] ^ rki[5];
  96. } else if (key_len == AES_KEYSIZE_256) {
  97. if (i >= 6)
  98. break;
  99. rko[4] = __aes_ce_sub(rko[3]) ^ rki[4];
  100. rko[5] = rko[4] ^ rki[5];
  101. rko[6] = rko[5] ^ rki[6];
  102. rko[7] = rko[6] ^ rki[7];
  103. }
  104. }
  105. /*
  106. * Generate the decryption keys for the Equivalent Inverse Cipher.
  107. * This involves reversing the order of the round keys, and applying
  108. * the Inverse Mix Columns transformation on all but the first and
  109. * the last one.
  110. */
  111. key_enc = (struct aes_block *)ctx->key_enc;
  112. key_dec = (struct aes_block *)ctx->key_dec;
  113. j = num_rounds(ctx);
  114. key_dec[0] = key_enc[j];
  115. for (i = 1, j--; j > 0; i++, j--)
  116. __aes_ce_invert(key_dec + i, key_enc + j);
  117. key_dec[i] = key_enc[0];
  118. kernel_neon_end();
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(ce_aes_expandkey);
  122. int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  123. unsigned int key_len)
  124. {
  125. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  126. int ret;
  127. ret = ce_aes_expandkey(ctx, in_key, key_len);
  128. if (!ret)
  129. return 0;
  130. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  131. return -EINVAL;
  132. }
  133. EXPORT_SYMBOL(ce_aes_setkey);
  134. static struct crypto_alg aes_alg = {
  135. .cra_name = "aes",
  136. .cra_driver_name = "aes-ce",
  137. .cra_priority = 250,
  138. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  139. .cra_blocksize = AES_BLOCK_SIZE,
  140. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  141. .cra_module = THIS_MODULE,
  142. .cra_cipher = {
  143. .cia_min_keysize = AES_MIN_KEY_SIZE,
  144. .cia_max_keysize = AES_MAX_KEY_SIZE,
  145. .cia_setkey = ce_aes_setkey,
  146. .cia_encrypt = aes_cipher_encrypt,
  147. .cia_decrypt = aes_cipher_decrypt
  148. }
  149. };
  150. static int __init aes_mod_init(void)
  151. {
  152. return crypto_register_alg(&aes_alg);
  153. }
  154. static void __exit aes_mod_exit(void)
  155. {
  156. crypto_unregister_alg(&aes_alg);
  157. }
  158. module_cpu_feature_match(AES, aes_mod_init);
  159. module_exit(aes_mod_exit);