speck-neon-glue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/speck.h>
  20. #include <crypto/xts.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. /* The assembly functions only handle multiples of 128 bytes */
  24. #define SPECK_NEON_CHUNK_SIZE 128
  25. /* Speck128 */
  26. struct speck128_xts_tfm_ctx {
  27. struct speck128_tfm_ctx main_key;
  28. struct speck128_tfm_ctx tweak_key;
  29. };
  30. asmlinkage void speck128_xts_encrypt_neon(const u64 *round_keys, int nrounds,
  31. void *dst, const void *src,
  32. unsigned int nbytes, void *tweak);
  33. asmlinkage void speck128_xts_decrypt_neon(const u64 *round_keys, int nrounds,
  34. void *dst, const void *src,
  35. unsigned int nbytes, void *tweak);
  36. typedef void (*speck128_crypt_one_t)(const struct speck128_tfm_ctx *,
  37. u8 *, const u8 *);
  38. typedef void (*speck128_xts_crypt_many_t)(const u64 *, int, void *,
  39. const void *, unsigned int, void *);
  40. static __always_inline int
  41. __speck128_xts_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  42. struct scatterlist *src, unsigned int nbytes,
  43. speck128_crypt_one_t crypt_one,
  44. speck128_xts_crypt_many_t crypt_many)
  45. {
  46. struct crypto_blkcipher *tfm = desc->tfm;
  47. const struct speck128_xts_tfm_ctx *ctx = crypto_blkcipher_ctx(tfm);
  48. struct blkcipher_walk walk;
  49. le128 tweak;
  50. int err;
  51. blkcipher_walk_init(&walk, dst, src, nbytes);
  52. err = blkcipher_walk_virt_block(desc, &walk, SPECK_NEON_CHUNK_SIZE);
  53. crypto_speck128_encrypt(&ctx->tweak_key, (u8 *)&tweak, walk.iv);
  54. while (walk.nbytes > 0) {
  55. unsigned int nbytes = walk.nbytes;
  56. u8 *dst = walk.dst.virt.addr;
  57. const u8 *src = walk.src.virt.addr;
  58. if (nbytes >= SPECK_NEON_CHUNK_SIZE && may_use_simd()) {
  59. unsigned int count;
  60. count = round_down(nbytes, SPECK_NEON_CHUNK_SIZE);
  61. kernel_neon_begin();
  62. (*crypt_many)(ctx->main_key.round_keys,
  63. ctx->main_key.nrounds,
  64. dst, src, count, &tweak);
  65. kernel_neon_end();
  66. dst += count;
  67. src += count;
  68. nbytes -= count;
  69. }
  70. /* Handle any remainder with generic code */
  71. while (nbytes >= sizeof(tweak)) {
  72. le128_xor((le128 *)dst, (const le128 *)src, &tweak);
  73. (*crypt_one)(&ctx->main_key, dst, dst);
  74. le128_xor((le128 *)dst, (const le128 *)dst, &tweak);
  75. gf128mul_x_ble((be128 *)&tweak, (const be128 *)&tweak);
  76. dst += sizeof(tweak);
  77. src += sizeof(tweak);
  78. nbytes -= sizeof(tweak);
  79. }
  80. err = blkcipher_walk_done(desc, &walk, nbytes);
  81. }
  82. return err;
  83. }
  84. static int speck128_xts_encrypt(struct blkcipher_desc *desc,
  85. struct scatterlist *dst,
  86. struct scatterlist *src,
  87. unsigned int nbytes)
  88. {
  89. return __speck128_xts_crypt(desc, dst, src, nbytes,
  90. crypto_speck128_encrypt,
  91. speck128_xts_encrypt_neon);
  92. }
  93. static int speck128_xts_decrypt(struct blkcipher_desc *desc,
  94. struct scatterlist *dst,
  95. struct scatterlist *src,
  96. unsigned int nbytes)
  97. {
  98. return __speck128_xts_crypt(desc, dst, src, nbytes,
  99. crypto_speck128_decrypt,
  100. speck128_xts_decrypt_neon);
  101. }
  102. static int speck128_xts_setkey(struct crypto_tfm *tfm, const u8 *key,
  103. unsigned int keylen)
  104. {
  105. struct speck128_xts_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  106. int err;
  107. if (keylen % 2)
  108. return -EINVAL;
  109. keylen /= 2;
  110. err = crypto_speck128_setkey(&ctx->main_key, key, keylen);
  111. if (err)
  112. return err;
  113. return crypto_speck128_setkey(&ctx->tweak_key, key + keylen, keylen);
  114. }
  115. /* Speck64 */
  116. struct speck64_xts_tfm_ctx {
  117. struct speck64_tfm_ctx main_key;
  118. struct speck64_tfm_ctx tweak_key;
  119. };
  120. asmlinkage void speck64_xts_encrypt_neon(const u32 *round_keys, int nrounds,
  121. void *dst, const void *src,
  122. unsigned int nbytes, void *tweak);
  123. asmlinkage void speck64_xts_decrypt_neon(const u32 *round_keys, int nrounds,
  124. void *dst, const void *src,
  125. unsigned int nbytes, void *tweak);
  126. typedef void (*speck64_crypt_one_t)(const struct speck64_tfm_ctx *,
  127. u8 *, const u8 *);
  128. typedef void (*speck64_xts_crypt_many_t)(const u32 *, int, void *,
  129. const void *, unsigned int, void *);
  130. static __always_inline int
  131. __speck64_xts_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  132. struct scatterlist *src, unsigned int nbytes,
  133. speck64_crypt_one_t crypt_one,
  134. speck64_xts_crypt_many_t crypt_many)
  135. {
  136. struct crypto_blkcipher *tfm = desc->tfm;
  137. const struct speck64_xts_tfm_ctx *ctx = crypto_blkcipher_ctx(tfm);
  138. struct blkcipher_walk walk;
  139. __le64 tweak;
  140. int err;
  141. blkcipher_walk_init(&walk, dst, src, nbytes);
  142. err = blkcipher_walk_virt_block(desc, &walk, SPECK_NEON_CHUNK_SIZE);
  143. crypto_speck64_encrypt(&ctx->tweak_key, (u8 *)&tweak, walk.iv);
  144. while (walk.nbytes > 0) {
  145. unsigned int nbytes = walk.nbytes;
  146. u8 *dst = walk.dst.virt.addr;
  147. const u8 *src = walk.src.virt.addr;
  148. if (nbytes >= SPECK_NEON_CHUNK_SIZE && may_use_simd()) {
  149. unsigned int count;
  150. count = round_down(nbytes, SPECK_NEON_CHUNK_SIZE);
  151. kernel_neon_begin();
  152. (*crypt_many)(ctx->main_key.round_keys,
  153. ctx->main_key.nrounds,
  154. dst, src, count, &tweak);
  155. kernel_neon_end();
  156. dst += count;
  157. src += count;
  158. nbytes -= count;
  159. }
  160. /* Handle any remainder with generic code */
  161. while (nbytes >= sizeof(tweak)) {
  162. *(__le64 *)dst = *(__le64 *)src ^ tweak;
  163. (*crypt_one)(&ctx->main_key, dst, dst);
  164. *(__le64 *)dst ^= tweak;
  165. tweak = cpu_to_le64((le64_to_cpu(tweak) << 1) ^
  166. ((tweak & cpu_to_le64(1ULL << 63)) ?
  167. 0x1B : 0));
  168. dst += sizeof(tweak);
  169. src += sizeof(tweak);
  170. nbytes -= sizeof(tweak);
  171. }
  172. err = blkcipher_walk_done(desc, &walk, nbytes);
  173. }
  174. return err;
  175. }
  176. static int speck64_xts_encrypt(struct blkcipher_desc *desc,
  177. struct scatterlist *dst, struct scatterlist *src,
  178. unsigned int nbytes)
  179. {
  180. return __speck64_xts_crypt(desc, dst, src, nbytes,
  181. crypto_speck64_encrypt,
  182. speck64_xts_encrypt_neon);
  183. }
  184. static int speck64_xts_decrypt(struct blkcipher_desc *desc,
  185. struct scatterlist *dst, struct scatterlist *src,
  186. unsigned int nbytes)
  187. {
  188. return __speck64_xts_crypt(desc, dst, src, nbytes,
  189. crypto_speck64_decrypt,
  190. speck64_xts_decrypt_neon);
  191. }
  192. static int speck64_xts_setkey(struct crypto_tfm *tfm, const u8 *key,
  193. unsigned int keylen)
  194. {
  195. struct speck64_xts_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  196. int err;
  197. if (keylen % 2)
  198. return -EINVAL;
  199. keylen /= 2;
  200. err = crypto_speck64_setkey(&ctx->main_key, key, keylen);
  201. if (err)
  202. return err;
  203. return crypto_speck64_setkey(&ctx->tweak_key, key + keylen, keylen);
  204. }
  205. static struct crypto_alg speck_algs[] = {
  206. {
  207. .cra_name = "xts(speck128)",
  208. .cra_driver_name = "xts-speck128-neon",
  209. .cra_priority = 300,
  210. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  211. .cra_blocksize = SPECK128_BLOCK_SIZE,
  212. .cra_type = &crypto_blkcipher_type,
  213. .cra_ctxsize = sizeof(struct speck128_xts_tfm_ctx),
  214. .cra_alignmask = 7,
  215. .cra_module = THIS_MODULE,
  216. .cra_u = {
  217. .blkcipher = {
  218. .min_keysize = 2 * SPECK128_128_KEY_SIZE,
  219. .max_keysize = 2 * SPECK128_256_KEY_SIZE,
  220. .ivsize = SPECK128_BLOCK_SIZE,
  221. .setkey = speck128_xts_setkey,
  222. .encrypt = speck128_xts_encrypt,
  223. .decrypt = speck128_xts_decrypt,
  224. }
  225. }
  226. }, {
  227. .cra_name = "xts(speck64)",
  228. .cra_driver_name = "xts-speck64-neon",
  229. .cra_priority = 300,
  230. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  231. .cra_blocksize = SPECK64_BLOCK_SIZE,
  232. .cra_type = &crypto_blkcipher_type,
  233. .cra_ctxsize = sizeof(struct speck64_xts_tfm_ctx),
  234. .cra_alignmask = 7,
  235. .cra_module = THIS_MODULE,
  236. .cra_u = {
  237. .blkcipher = {
  238. .min_keysize = 2 * SPECK64_96_KEY_SIZE,
  239. .max_keysize = 2 * SPECK64_128_KEY_SIZE,
  240. .ivsize = SPECK64_BLOCK_SIZE,
  241. .setkey = speck64_xts_setkey,
  242. .encrypt = speck64_xts_encrypt,
  243. .decrypt = speck64_xts_decrypt,
  244. }
  245. }
  246. }
  247. };
  248. static int __init speck_neon_module_init(void)
  249. {
  250. if (!(elf_hwcap & HWCAP_NEON))
  251. return -ENODEV;
  252. return crypto_register_algs(speck_algs, ARRAY_SIZE(speck_algs));
  253. }
  254. static void __exit speck_neon_module_exit(void)
  255. {
  256. crypto_unregister_algs(speck_algs, ARRAY_SIZE(speck_algs));
  257. }
  258. module_init(speck_neon_module_init);
  259. module_exit(speck_neon_module_exit);
  260. MODULE_DESCRIPTION("Speck block cipher (NEON-accelerated)");
  261. MODULE_LICENSE("GPL");
  262. MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
  263. MODULE_ALIAS_CRYPTO("xts(speck128)");
  264. MODULE_ALIAS_CRYPTO("xts-speck128-neon");
  265. MODULE_ALIAS_CRYPTO("xts(speck64)");
  266. MODULE_ALIAS_CRYPTO("xts-speck64-neon");