speck-neon-glue.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NEON-accelerated implementation of Speck128-XTS and Speck64-XTS
  4. *
  5. * Copyright (c) 2018 Google, Inc
  6. *
  7. * Note: the NIST recommendation for XTS only specifies a 128-bit block size,
  8. * but a 64-bit version (needed for Speck64) is fairly straightforward; the math
  9. * is just done in GF(2^64) instead of GF(2^128), with the reducing polynomial
  10. * x^64 + x^4 + x^3 + x + 1 from the original XEX paper (Rogaway, 2004:
  11. * "Efficient Instantiations of Tweakable Blockciphers and Refinements to Modes
  12. * OCB and PMAC"), represented as 0x1B.
  13. */
  14. #include <asm/hwcap.h>
  15. #include <asm/neon.h>
  16. #include <asm/simd.h>
  17. #include <crypto/algapi.h>
  18. #include <crypto/gf128mul.h>
  19. #include <crypto/internal/skcipher.h>
  20. #include <crypto/speck.h>
  21. #include <crypto/xts.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. /* The assembly functions only handle multiples of 128 bytes */
  25. #define SPECK_NEON_CHUNK_SIZE 128
  26. /* Speck128 */
  27. struct speck128_xts_tfm_ctx {
  28. struct speck128_tfm_ctx main_key;
  29. struct speck128_tfm_ctx tweak_key;
  30. };
  31. asmlinkage void speck128_xts_encrypt_neon(const u64 *round_keys, int nrounds,
  32. void *dst, const void *src,
  33. unsigned int nbytes, void *tweak);
  34. asmlinkage void speck128_xts_decrypt_neon(const u64 *round_keys, int nrounds,
  35. void *dst, const void *src,
  36. unsigned int nbytes, void *tweak);
  37. typedef void (*speck128_crypt_one_t)(const struct speck128_tfm_ctx *,
  38. u8 *, const u8 *);
  39. typedef void (*speck128_xts_crypt_many_t)(const u64 *, int, void *,
  40. const void *, unsigned int, void *);
  41. static __always_inline int
  42. __speck128_xts_crypt(struct skcipher_request *req,
  43. speck128_crypt_one_t crypt_one,
  44. speck128_xts_crypt_many_t crypt_many)
  45. {
  46. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  47. const struct speck128_xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
  48. struct skcipher_walk walk;
  49. le128 tweak;
  50. int err;
  51. err = skcipher_walk_virt(&walk, req, true);
  52. crypto_speck128_encrypt(&ctx->tweak_key, (u8 *)&tweak, walk.iv);
  53. while (walk.nbytes > 0) {
  54. unsigned int nbytes = walk.nbytes;
  55. u8 *dst = walk.dst.virt.addr;
  56. const u8 *src = walk.src.virt.addr;
  57. if (nbytes >= SPECK_NEON_CHUNK_SIZE && may_use_simd()) {
  58. unsigned int count;
  59. count = round_down(nbytes, SPECK_NEON_CHUNK_SIZE);
  60. kernel_neon_begin();
  61. (*crypt_many)(ctx->main_key.round_keys,
  62. ctx->main_key.nrounds,
  63. dst, src, count, &tweak);
  64. kernel_neon_end();
  65. dst += count;
  66. src += count;
  67. nbytes -= count;
  68. }
  69. /* Handle any remainder with generic code */
  70. while (nbytes >= sizeof(tweak)) {
  71. le128_xor((le128 *)dst, (const le128 *)src, &tweak);
  72. (*crypt_one)(&ctx->main_key, dst, dst);
  73. le128_xor((le128 *)dst, (const le128 *)dst, &tweak);
  74. gf128mul_x_ble(&tweak, &tweak);
  75. dst += sizeof(tweak);
  76. src += sizeof(tweak);
  77. nbytes -= sizeof(tweak);
  78. }
  79. err = skcipher_walk_done(&walk, nbytes);
  80. }
  81. return err;
  82. }
  83. static int speck128_xts_encrypt(struct skcipher_request *req)
  84. {
  85. return __speck128_xts_crypt(req, crypto_speck128_encrypt,
  86. speck128_xts_encrypt_neon);
  87. }
  88. static int speck128_xts_decrypt(struct skcipher_request *req)
  89. {
  90. return __speck128_xts_crypt(req, crypto_speck128_decrypt,
  91. speck128_xts_decrypt_neon);
  92. }
  93. static int speck128_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
  94. unsigned int keylen)
  95. {
  96. struct speck128_xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
  97. int err;
  98. err = xts_verify_key(tfm, key, keylen);
  99. if (err)
  100. return err;
  101. keylen /= 2;
  102. err = crypto_speck128_setkey(&ctx->main_key, key, keylen);
  103. if (err)
  104. return err;
  105. return crypto_speck128_setkey(&ctx->tweak_key, key + keylen, keylen);
  106. }
  107. /* Speck64 */
  108. struct speck64_xts_tfm_ctx {
  109. struct speck64_tfm_ctx main_key;
  110. struct speck64_tfm_ctx tweak_key;
  111. };
  112. asmlinkage void speck64_xts_encrypt_neon(const u32 *round_keys, int nrounds,
  113. void *dst, const void *src,
  114. unsigned int nbytes, void *tweak);
  115. asmlinkage void speck64_xts_decrypt_neon(const u32 *round_keys, int nrounds,
  116. void *dst, const void *src,
  117. unsigned int nbytes, void *tweak);
  118. typedef void (*speck64_crypt_one_t)(const struct speck64_tfm_ctx *,
  119. u8 *, const u8 *);
  120. typedef void (*speck64_xts_crypt_many_t)(const u32 *, int, void *,
  121. const void *, unsigned int, void *);
  122. static __always_inline int
  123. __speck64_xts_crypt(struct skcipher_request *req, speck64_crypt_one_t crypt_one,
  124. speck64_xts_crypt_many_t crypt_many)
  125. {
  126. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  127. const struct speck64_xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
  128. struct skcipher_walk walk;
  129. __le64 tweak;
  130. int err;
  131. err = skcipher_walk_virt(&walk, req, true);
  132. crypto_speck64_encrypt(&ctx->tweak_key, (u8 *)&tweak, walk.iv);
  133. while (walk.nbytes > 0) {
  134. unsigned int nbytes = walk.nbytes;
  135. u8 *dst = walk.dst.virt.addr;
  136. const u8 *src = walk.src.virt.addr;
  137. if (nbytes >= SPECK_NEON_CHUNK_SIZE && may_use_simd()) {
  138. unsigned int count;
  139. count = round_down(nbytes, SPECK_NEON_CHUNK_SIZE);
  140. kernel_neon_begin();
  141. (*crypt_many)(ctx->main_key.round_keys,
  142. ctx->main_key.nrounds,
  143. dst, src, count, &tweak);
  144. kernel_neon_end();
  145. dst += count;
  146. src += count;
  147. nbytes -= count;
  148. }
  149. /* Handle any remainder with generic code */
  150. while (nbytes >= sizeof(tweak)) {
  151. *(__le64 *)dst = *(__le64 *)src ^ tweak;
  152. (*crypt_one)(&ctx->main_key, dst, dst);
  153. *(__le64 *)dst ^= tweak;
  154. tweak = cpu_to_le64((le64_to_cpu(tweak) << 1) ^
  155. ((tweak & cpu_to_le64(1ULL << 63)) ?
  156. 0x1B : 0));
  157. dst += sizeof(tweak);
  158. src += sizeof(tweak);
  159. nbytes -= sizeof(tweak);
  160. }
  161. err = skcipher_walk_done(&walk, nbytes);
  162. }
  163. return err;
  164. }
  165. static int speck64_xts_encrypt(struct skcipher_request *req)
  166. {
  167. return __speck64_xts_crypt(req, crypto_speck64_encrypt,
  168. speck64_xts_encrypt_neon);
  169. }
  170. static int speck64_xts_decrypt(struct skcipher_request *req)
  171. {
  172. return __speck64_xts_crypt(req, crypto_speck64_decrypt,
  173. speck64_xts_decrypt_neon);
  174. }
  175. static int speck64_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
  176. unsigned int keylen)
  177. {
  178. struct speck64_xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
  179. int err;
  180. err = xts_verify_key(tfm, key, keylen);
  181. if (err)
  182. return err;
  183. keylen /= 2;
  184. err = crypto_speck64_setkey(&ctx->main_key, key, keylen);
  185. if (err)
  186. return err;
  187. return crypto_speck64_setkey(&ctx->tweak_key, key + keylen, keylen);
  188. }
  189. static struct skcipher_alg speck_algs[] = {
  190. {
  191. .base.cra_name = "xts(speck128)",
  192. .base.cra_driver_name = "xts-speck128-neon",
  193. .base.cra_priority = 300,
  194. .base.cra_blocksize = SPECK128_BLOCK_SIZE,
  195. .base.cra_ctxsize = sizeof(struct speck128_xts_tfm_ctx),
  196. .base.cra_alignmask = 7,
  197. .base.cra_module = THIS_MODULE,
  198. .min_keysize = 2 * SPECK128_128_KEY_SIZE,
  199. .max_keysize = 2 * SPECK128_256_KEY_SIZE,
  200. .ivsize = SPECK128_BLOCK_SIZE,
  201. .walksize = SPECK_NEON_CHUNK_SIZE,
  202. .setkey = speck128_xts_setkey,
  203. .encrypt = speck128_xts_encrypt,
  204. .decrypt = speck128_xts_decrypt,
  205. }, {
  206. .base.cra_name = "xts(speck64)",
  207. .base.cra_driver_name = "xts-speck64-neon",
  208. .base.cra_priority = 300,
  209. .base.cra_blocksize = SPECK64_BLOCK_SIZE,
  210. .base.cra_ctxsize = sizeof(struct speck64_xts_tfm_ctx),
  211. .base.cra_alignmask = 7,
  212. .base.cra_module = THIS_MODULE,
  213. .min_keysize = 2 * SPECK64_96_KEY_SIZE,
  214. .max_keysize = 2 * SPECK64_128_KEY_SIZE,
  215. .ivsize = SPECK64_BLOCK_SIZE,
  216. .walksize = SPECK_NEON_CHUNK_SIZE,
  217. .setkey = speck64_xts_setkey,
  218. .encrypt = speck64_xts_encrypt,
  219. .decrypt = speck64_xts_decrypt,
  220. }
  221. };
  222. static int __init speck_neon_module_init(void)
  223. {
  224. if (!(elf_hwcap & HWCAP_NEON))
  225. return -ENODEV;
  226. return crypto_register_skciphers(speck_algs, ARRAY_SIZE(speck_algs));
  227. }
  228. static void __exit speck_neon_module_exit(void)
  229. {
  230. crypto_unregister_skciphers(speck_algs, ARRAY_SIZE(speck_algs));
  231. }
  232. module_init(speck_neon_module_init);
  233. module_exit(speck_neon_module_exit);
  234. MODULE_DESCRIPTION("Speck block cipher (NEON-accelerated)");
  235. MODULE_LICENSE("GPL");
  236. MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
  237. MODULE_ALIAS_CRYPTO("xts(speck128)");
  238. MODULE_ALIAS_CRYPTO("xts-speck128-neon");
  239. MODULE_ALIAS_CRYPTO("xts(speck64)");
  240. MODULE_ALIAS_CRYPTO("xts-speck64-neon");