blowfish_glue.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue Code for assembler optimized version of Blowfish
  4. *
  5. * Copyright (c) 2011 Jussi Kivilinna <[email protected]>
  6. *
  7. * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
  8. * Copyright (c) 2006 Herbert Xu <[email protected]>
  9. */
  10. #include <crypto/algapi.h>
  11. #include <crypto/blowfish.h>
  12. #include <crypto/internal/skcipher.h>
  13. #include <linux/crypto.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. /* regular block cipher functions */
  18. asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src,
  19. bool xor);
  20. asmlinkage void blowfish_dec_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
  21. /* 4-way parallel cipher functions */
  22. asmlinkage void __blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
  23. const u8 *src, bool xor);
  24. asmlinkage void blowfish_dec_blk_4way(struct bf_ctx *ctx, u8 *dst,
  25. const u8 *src);
  26. static inline void blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src)
  27. {
  28. __blowfish_enc_blk(ctx, dst, src, false);
  29. }
  30. static inline void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
  31. const u8 *src)
  32. {
  33. __blowfish_enc_blk_4way(ctx, dst, src, false);
  34. }
  35. static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  36. {
  37. blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
  38. }
  39. static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  40. {
  41. blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
  42. }
  43. static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
  44. const u8 *key, unsigned int keylen)
  45. {
  46. return blowfish_setkey(&tfm->base, key, keylen);
  47. }
  48. static int ecb_crypt(struct skcipher_request *req,
  49. void (*fn)(struct bf_ctx *, u8 *, const u8 *),
  50. void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
  51. {
  52. unsigned int bsize = BF_BLOCK_SIZE;
  53. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  54. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  55. struct skcipher_walk walk;
  56. unsigned int nbytes;
  57. int err;
  58. err = skcipher_walk_virt(&walk, req, false);
  59. while ((nbytes = walk.nbytes)) {
  60. u8 *wsrc = walk.src.virt.addr;
  61. u8 *wdst = walk.dst.virt.addr;
  62. /* Process four block batch */
  63. if (nbytes >= bsize * 4) {
  64. do {
  65. fn_4way(ctx, wdst, wsrc);
  66. wsrc += bsize * 4;
  67. wdst += bsize * 4;
  68. nbytes -= bsize * 4;
  69. } while (nbytes >= bsize * 4);
  70. if (nbytes < bsize)
  71. goto done;
  72. }
  73. /* Handle leftovers */
  74. do {
  75. fn(ctx, wdst, wsrc);
  76. wsrc += bsize;
  77. wdst += bsize;
  78. nbytes -= bsize;
  79. } while (nbytes >= bsize);
  80. done:
  81. err = skcipher_walk_done(&walk, nbytes);
  82. }
  83. return err;
  84. }
  85. static int ecb_encrypt(struct skcipher_request *req)
  86. {
  87. return ecb_crypt(req, blowfish_enc_blk, blowfish_enc_blk_4way);
  88. }
  89. static int ecb_decrypt(struct skcipher_request *req)
  90. {
  91. return ecb_crypt(req, blowfish_dec_blk, blowfish_dec_blk_4way);
  92. }
  93. static unsigned int __cbc_encrypt(struct bf_ctx *ctx,
  94. struct skcipher_walk *walk)
  95. {
  96. unsigned int bsize = BF_BLOCK_SIZE;
  97. unsigned int nbytes = walk->nbytes;
  98. u64 *src = (u64 *)walk->src.virt.addr;
  99. u64 *dst = (u64 *)walk->dst.virt.addr;
  100. u64 *iv = (u64 *)walk->iv;
  101. do {
  102. *dst = *src ^ *iv;
  103. blowfish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
  104. iv = dst;
  105. src += 1;
  106. dst += 1;
  107. nbytes -= bsize;
  108. } while (nbytes >= bsize);
  109. *(u64 *)walk->iv = *iv;
  110. return nbytes;
  111. }
  112. static int cbc_encrypt(struct skcipher_request *req)
  113. {
  114. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  115. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  116. struct skcipher_walk walk;
  117. unsigned int nbytes;
  118. int err;
  119. err = skcipher_walk_virt(&walk, req, false);
  120. while (walk.nbytes) {
  121. nbytes = __cbc_encrypt(ctx, &walk);
  122. err = skcipher_walk_done(&walk, nbytes);
  123. }
  124. return err;
  125. }
  126. static unsigned int __cbc_decrypt(struct bf_ctx *ctx,
  127. struct skcipher_walk *walk)
  128. {
  129. unsigned int bsize = BF_BLOCK_SIZE;
  130. unsigned int nbytes = walk->nbytes;
  131. u64 *src = (u64 *)walk->src.virt.addr;
  132. u64 *dst = (u64 *)walk->dst.virt.addr;
  133. u64 ivs[4 - 1];
  134. u64 last_iv;
  135. /* Start of the last block. */
  136. src += nbytes / bsize - 1;
  137. dst += nbytes / bsize - 1;
  138. last_iv = *src;
  139. /* Process four block batch */
  140. if (nbytes >= bsize * 4) {
  141. do {
  142. nbytes -= bsize * 4 - bsize;
  143. src -= 4 - 1;
  144. dst -= 4 - 1;
  145. ivs[0] = src[0];
  146. ivs[1] = src[1];
  147. ivs[2] = src[2];
  148. blowfish_dec_blk_4way(ctx, (u8 *)dst, (u8 *)src);
  149. dst[1] ^= ivs[0];
  150. dst[2] ^= ivs[1];
  151. dst[3] ^= ivs[2];
  152. nbytes -= bsize;
  153. if (nbytes < bsize)
  154. goto done;
  155. *dst ^= *(src - 1);
  156. src -= 1;
  157. dst -= 1;
  158. } while (nbytes >= bsize * 4);
  159. }
  160. /* Handle leftovers */
  161. for (;;) {
  162. blowfish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
  163. nbytes -= bsize;
  164. if (nbytes < bsize)
  165. break;
  166. *dst ^= *(src - 1);
  167. src -= 1;
  168. dst -= 1;
  169. }
  170. done:
  171. *dst ^= *(u64 *)walk->iv;
  172. *(u64 *)walk->iv = last_iv;
  173. return nbytes;
  174. }
  175. static int cbc_decrypt(struct skcipher_request *req)
  176. {
  177. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  178. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  179. struct skcipher_walk walk;
  180. unsigned int nbytes;
  181. int err;
  182. err = skcipher_walk_virt(&walk, req, false);
  183. while (walk.nbytes) {
  184. nbytes = __cbc_decrypt(ctx, &walk);
  185. err = skcipher_walk_done(&walk, nbytes);
  186. }
  187. return err;
  188. }
  189. static struct crypto_alg bf_cipher_alg = {
  190. .cra_name = "blowfish",
  191. .cra_driver_name = "blowfish-asm",
  192. .cra_priority = 200,
  193. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  194. .cra_blocksize = BF_BLOCK_SIZE,
  195. .cra_ctxsize = sizeof(struct bf_ctx),
  196. .cra_alignmask = 0,
  197. .cra_module = THIS_MODULE,
  198. .cra_u = {
  199. .cipher = {
  200. .cia_min_keysize = BF_MIN_KEY_SIZE,
  201. .cia_max_keysize = BF_MAX_KEY_SIZE,
  202. .cia_setkey = blowfish_setkey,
  203. .cia_encrypt = blowfish_encrypt,
  204. .cia_decrypt = blowfish_decrypt,
  205. }
  206. }
  207. };
  208. static struct skcipher_alg bf_skcipher_algs[] = {
  209. {
  210. .base.cra_name = "ecb(blowfish)",
  211. .base.cra_driver_name = "ecb-blowfish-asm",
  212. .base.cra_priority = 300,
  213. .base.cra_blocksize = BF_BLOCK_SIZE,
  214. .base.cra_ctxsize = sizeof(struct bf_ctx),
  215. .base.cra_module = THIS_MODULE,
  216. .min_keysize = BF_MIN_KEY_SIZE,
  217. .max_keysize = BF_MAX_KEY_SIZE,
  218. .setkey = blowfish_setkey_skcipher,
  219. .encrypt = ecb_encrypt,
  220. .decrypt = ecb_decrypt,
  221. }, {
  222. .base.cra_name = "cbc(blowfish)",
  223. .base.cra_driver_name = "cbc-blowfish-asm",
  224. .base.cra_priority = 300,
  225. .base.cra_blocksize = BF_BLOCK_SIZE,
  226. .base.cra_ctxsize = sizeof(struct bf_ctx),
  227. .base.cra_module = THIS_MODULE,
  228. .min_keysize = BF_MIN_KEY_SIZE,
  229. .max_keysize = BF_MAX_KEY_SIZE,
  230. .ivsize = BF_BLOCK_SIZE,
  231. .setkey = blowfish_setkey_skcipher,
  232. .encrypt = cbc_encrypt,
  233. .decrypt = cbc_decrypt,
  234. },
  235. };
  236. static bool is_blacklisted_cpu(void)
  237. {
  238. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  239. return false;
  240. if (boot_cpu_data.x86 == 0x0f) {
  241. /*
  242. * On Pentium 4, blowfish-x86_64 is slower than generic C
  243. * implementation because use of 64bit rotates (which are really
  244. * slow on P4). Therefore blacklist P4s.
  245. */
  246. return true;
  247. }
  248. return false;
  249. }
  250. static int force;
  251. module_param(force, int, 0);
  252. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  253. static int __init blowfish_init(void)
  254. {
  255. int err;
  256. if (!force && is_blacklisted_cpu()) {
  257. printk(KERN_INFO
  258. "blowfish-x86_64: performance on this CPU "
  259. "would be suboptimal: disabling "
  260. "blowfish-x86_64.\n");
  261. return -ENODEV;
  262. }
  263. err = crypto_register_alg(&bf_cipher_alg);
  264. if (err)
  265. return err;
  266. err = crypto_register_skciphers(bf_skcipher_algs,
  267. ARRAY_SIZE(bf_skcipher_algs));
  268. if (err)
  269. crypto_unregister_alg(&bf_cipher_alg);
  270. return err;
  271. }
  272. static void __exit blowfish_fini(void)
  273. {
  274. crypto_unregister_alg(&bf_cipher_alg);
  275. crypto_unregister_skciphers(bf_skcipher_algs,
  276. ARRAY_SIZE(bf_skcipher_algs));
  277. }
  278. module_init(blowfish_init);
  279. module_exit(blowfish_fini);
  280. MODULE_LICENSE("GPL");
  281. MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
  282. MODULE_ALIAS_CRYPTO("blowfish");
  283. MODULE_ALIAS_CRYPTO("blowfish-asm");