blowfish_glue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Glue Code for assembler optimized version of Blowfish
  3. *
  4. * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. * CTR part based on code (crypto/ctr.c) by:
  9. * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  24. * USA
  25. *
  26. */
  27. #include <crypto/algapi.h>
  28. #include <crypto/blowfish.h>
  29. #include <crypto/internal/skcipher.h>
  30. #include <linux/crypto.h>
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/types.h>
  34. /* regular block cipher functions */
  35. asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src,
  36. bool xor);
  37. asmlinkage void blowfish_dec_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
  38. /* 4-way parallel cipher functions */
  39. asmlinkage void __blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
  40. const u8 *src, bool xor);
  41. asmlinkage void blowfish_dec_blk_4way(struct bf_ctx *ctx, u8 *dst,
  42. const u8 *src);
  43. static inline void blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src)
  44. {
  45. __blowfish_enc_blk(ctx, dst, src, false);
  46. }
  47. static inline void blowfish_enc_blk_xor(struct bf_ctx *ctx, u8 *dst,
  48. const u8 *src)
  49. {
  50. __blowfish_enc_blk(ctx, dst, src, true);
  51. }
  52. static inline void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
  53. const u8 *src)
  54. {
  55. __blowfish_enc_blk_4way(ctx, dst, src, false);
  56. }
  57. static inline void blowfish_enc_blk_xor_4way(struct bf_ctx *ctx, u8 *dst,
  58. const u8 *src)
  59. {
  60. __blowfish_enc_blk_4way(ctx, dst, src, true);
  61. }
  62. static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  63. {
  64. blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
  65. }
  66. static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  67. {
  68. blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
  69. }
  70. static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
  71. const u8 *key, unsigned int keylen)
  72. {
  73. return blowfish_setkey(&tfm->base, key, keylen);
  74. }
  75. static int ecb_crypt(struct skcipher_request *req,
  76. void (*fn)(struct bf_ctx *, u8 *, const u8 *),
  77. void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
  78. {
  79. unsigned int bsize = BF_BLOCK_SIZE;
  80. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  81. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  82. struct skcipher_walk walk;
  83. unsigned int nbytes;
  84. int err;
  85. err = skcipher_walk_virt(&walk, req, false);
  86. while ((nbytes = walk.nbytes)) {
  87. u8 *wsrc = walk.src.virt.addr;
  88. u8 *wdst = walk.dst.virt.addr;
  89. /* Process four block batch */
  90. if (nbytes >= bsize * 4) {
  91. do {
  92. fn_4way(ctx, wdst, wsrc);
  93. wsrc += bsize * 4;
  94. wdst += bsize * 4;
  95. nbytes -= bsize * 4;
  96. } while (nbytes >= bsize * 4);
  97. if (nbytes < bsize)
  98. goto done;
  99. }
  100. /* Handle leftovers */
  101. do {
  102. fn(ctx, wdst, wsrc);
  103. wsrc += bsize;
  104. wdst += bsize;
  105. nbytes -= bsize;
  106. } while (nbytes >= bsize);
  107. done:
  108. err = skcipher_walk_done(&walk, nbytes);
  109. }
  110. return err;
  111. }
  112. static int ecb_encrypt(struct skcipher_request *req)
  113. {
  114. return ecb_crypt(req, blowfish_enc_blk, blowfish_enc_blk_4way);
  115. }
  116. static int ecb_decrypt(struct skcipher_request *req)
  117. {
  118. return ecb_crypt(req, blowfish_dec_blk, blowfish_dec_blk_4way);
  119. }
  120. static unsigned int __cbc_encrypt(struct bf_ctx *ctx,
  121. struct skcipher_walk *walk)
  122. {
  123. unsigned int bsize = BF_BLOCK_SIZE;
  124. unsigned int nbytes = walk->nbytes;
  125. u64 *src = (u64 *)walk->src.virt.addr;
  126. u64 *dst = (u64 *)walk->dst.virt.addr;
  127. u64 *iv = (u64 *)walk->iv;
  128. do {
  129. *dst = *src ^ *iv;
  130. blowfish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
  131. iv = dst;
  132. src += 1;
  133. dst += 1;
  134. nbytes -= bsize;
  135. } while (nbytes >= bsize);
  136. *(u64 *)walk->iv = *iv;
  137. return nbytes;
  138. }
  139. static int cbc_encrypt(struct skcipher_request *req)
  140. {
  141. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  142. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  143. struct skcipher_walk walk;
  144. unsigned int nbytes;
  145. int err;
  146. err = skcipher_walk_virt(&walk, req, false);
  147. while ((nbytes = walk.nbytes)) {
  148. nbytes = __cbc_encrypt(ctx, &walk);
  149. err = skcipher_walk_done(&walk, nbytes);
  150. }
  151. return err;
  152. }
  153. static unsigned int __cbc_decrypt(struct bf_ctx *ctx,
  154. struct skcipher_walk *walk)
  155. {
  156. unsigned int bsize = BF_BLOCK_SIZE;
  157. unsigned int nbytes = walk->nbytes;
  158. u64 *src = (u64 *)walk->src.virt.addr;
  159. u64 *dst = (u64 *)walk->dst.virt.addr;
  160. u64 ivs[4 - 1];
  161. u64 last_iv;
  162. /* Start of the last block. */
  163. src += nbytes / bsize - 1;
  164. dst += nbytes / bsize - 1;
  165. last_iv = *src;
  166. /* Process four block batch */
  167. if (nbytes >= bsize * 4) {
  168. do {
  169. nbytes -= bsize * 4 - bsize;
  170. src -= 4 - 1;
  171. dst -= 4 - 1;
  172. ivs[0] = src[0];
  173. ivs[1] = src[1];
  174. ivs[2] = src[2];
  175. blowfish_dec_blk_4way(ctx, (u8 *)dst, (u8 *)src);
  176. dst[1] ^= ivs[0];
  177. dst[2] ^= ivs[1];
  178. dst[3] ^= ivs[2];
  179. nbytes -= bsize;
  180. if (nbytes < bsize)
  181. goto done;
  182. *dst ^= *(src - 1);
  183. src -= 1;
  184. dst -= 1;
  185. } while (nbytes >= bsize * 4);
  186. }
  187. /* Handle leftovers */
  188. for (;;) {
  189. blowfish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
  190. nbytes -= bsize;
  191. if (nbytes < bsize)
  192. break;
  193. *dst ^= *(src - 1);
  194. src -= 1;
  195. dst -= 1;
  196. }
  197. done:
  198. *dst ^= *(u64 *)walk->iv;
  199. *(u64 *)walk->iv = last_iv;
  200. return nbytes;
  201. }
  202. static int cbc_decrypt(struct skcipher_request *req)
  203. {
  204. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  205. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  206. struct skcipher_walk walk;
  207. unsigned int nbytes;
  208. int err;
  209. err = skcipher_walk_virt(&walk, req, false);
  210. while ((nbytes = walk.nbytes)) {
  211. nbytes = __cbc_decrypt(ctx, &walk);
  212. err = skcipher_walk_done(&walk, nbytes);
  213. }
  214. return err;
  215. }
  216. static void ctr_crypt_final(struct bf_ctx *ctx, struct skcipher_walk *walk)
  217. {
  218. u8 *ctrblk = walk->iv;
  219. u8 keystream[BF_BLOCK_SIZE];
  220. u8 *src = walk->src.virt.addr;
  221. u8 *dst = walk->dst.virt.addr;
  222. unsigned int nbytes = walk->nbytes;
  223. blowfish_enc_blk(ctx, keystream, ctrblk);
  224. crypto_xor_cpy(dst, keystream, src, nbytes);
  225. crypto_inc(ctrblk, BF_BLOCK_SIZE);
  226. }
  227. static unsigned int __ctr_crypt(struct bf_ctx *ctx, struct skcipher_walk *walk)
  228. {
  229. unsigned int bsize = BF_BLOCK_SIZE;
  230. unsigned int nbytes = walk->nbytes;
  231. u64 *src = (u64 *)walk->src.virt.addr;
  232. u64 *dst = (u64 *)walk->dst.virt.addr;
  233. u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
  234. __be64 ctrblocks[4];
  235. /* Process four block batch */
  236. if (nbytes >= bsize * 4) {
  237. do {
  238. if (dst != src) {
  239. dst[0] = src[0];
  240. dst[1] = src[1];
  241. dst[2] = src[2];
  242. dst[3] = src[3];
  243. }
  244. /* create ctrblks for parallel encrypt */
  245. ctrblocks[0] = cpu_to_be64(ctrblk++);
  246. ctrblocks[1] = cpu_to_be64(ctrblk++);
  247. ctrblocks[2] = cpu_to_be64(ctrblk++);
  248. ctrblocks[3] = cpu_to_be64(ctrblk++);
  249. blowfish_enc_blk_xor_4way(ctx, (u8 *)dst,
  250. (u8 *)ctrblocks);
  251. src += 4;
  252. dst += 4;
  253. } while ((nbytes -= bsize * 4) >= bsize * 4);
  254. if (nbytes < bsize)
  255. goto done;
  256. }
  257. /* Handle leftovers */
  258. do {
  259. if (dst != src)
  260. *dst = *src;
  261. ctrblocks[0] = cpu_to_be64(ctrblk++);
  262. blowfish_enc_blk_xor(ctx, (u8 *)dst, (u8 *)ctrblocks);
  263. src += 1;
  264. dst += 1;
  265. } while ((nbytes -= bsize) >= bsize);
  266. done:
  267. *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
  268. return nbytes;
  269. }
  270. static int ctr_crypt(struct skcipher_request *req)
  271. {
  272. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  273. struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
  274. struct skcipher_walk walk;
  275. unsigned int nbytes;
  276. int err;
  277. err = skcipher_walk_virt(&walk, req, false);
  278. while ((nbytes = walk.nbytes) >= BF_BLOCK_SIZE) {
  279. nbytes = __ctr_crypt(ctx, &walk);
  280. err = skcipher_walk_done(&walk, nbytes);
  281. }
  282. if (nbytes) {
  283. ctr_crypt_final(ctx, &walk);
  284. err = skcipher_walk_done(&walk, 0);
  285. }
  286. return err;
  287. }
  288. static struct crypto_alg bf_cipher_alg = {
  289. .cra_name = "blowfish",
  290. .cra_driver_name = "blowfish-asm",
  291. .cra_priority = 200,
  292. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  293. .cra_blocksize = BF_BLOCK_SIZE,
  294. .cra_ctxsize = sizeof(struct bf_ctx),
  295. .cra_alignmask = 0,
  296. .cra_module = THIS_MODULE,
  297. .cra_u = {
  298. .cipher = {
  299. .cia_min_keysize = BF_MIN_KEY_SIZE,
  300. .cia_max_keysize = BF_MAX_KEY_SIZE,
  301. .cia_setkey = blowfish_setkey,
  302. .cia_encrypt = blowfish_encrypt,
  303. .cia_decrypt = blowfish_decrypt,
  304. }
  305. }
  306. };
  307. static struct skcipher_alg bf_skcipher_algs[] = {
  308. {
  309. .base.cra_name = "ecb(blowfish)",
  310. .base.cra_driver_name = "ecb-blowfish-asm",
  311. .base.cra_priority = 300,
  312. .base.cra_blocksize = BF_BLOCK_SIZE,
  313. .base.cra_ctxsize = sizeof(struct bf_ctx),
  314. .base.cra_module = THIS_MODULE,
  315. .min_keysize = BF_MIN_KEY_SIZE,
  316. .max_keysize = BF_MAX_KEY_SIZE,
  317. .setkey = blowfish_setkey_skcipher,
  318. .encrypt = ecb_encrypt,
  319. .decrypt = ecb_decrypt,
  320. }, {
  321. .base.cra_name = "cbc(blowfish)",
  322. .base.cra_driver_name = "cbc-blowfish-asm",
  323. .base.cra_priority = 300,
  324. .base.cra_blocksize = BF_BLOCK_SIZE,
  325. .base.cra_ctxsize = sizeof(struct bf_ctx),
  326. .base.cra_module = THIS_MODULE,
  327. .min_keysize = BF_MIN_KEY_SIZE,
  328. .max_keysize = BF_MAX_KEY_SIZE,
  329. .ivsize = BF_BLOCK_SIZE,
  330. .setkey = blowfish_setkey_skcipher,
  331. .encrypt = cbc_encrypt,
  332. .decrypt = cbc_decrypt,
  333. }, {
  334. .base.cra_name = "ctr(blowfish)",
  335. .base.cra_driver_name = "ctr-blowfish-asm",
  336. .base.cra_priority = 300,
  337. .base.cra_blocksize = 1,
  338. .base.cra_ctxsize = sizeof(struct bf_ctx),
  339. .base.cra_module = THIS_MODULE,
  340. .min_keysize = BF_MIN_KEY_SIZE,
  341. .max_keysize = BF_MAX_KEY_SIZE,
  342. .ivsize = BF_BLOCK_SIZE,
  343. .chunksize = BF_BLOCK_SIZE,
  344. .setkey = blowfish_setkey_skcipher,
  345. .encrypt = ctr_crypt,
  346. .decrypt = ctr_crypt,
  347. },
  348. };
  349. static bool is_blacklisted_cpu(void)
  350. {
  351. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  352. return false;
  353. if (boot_cpu_data.x86 == 0x0f) {
  354. /*
  355. * On Pentium 4, blowfish-x86_64 is slower than generic C
  356. * implementation because use of 64bit rotates (which are really
  357. * slow on P4). Therefore blacklist P4s.
  358. */
  359. return true;
  360. }
  361. return false;
  362. }
  363. static int force;
  364. module_param(force, int, 0);
  365. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  366. static int __init init(void)
  367. {
  368. int err;
  369. if (!force && is_blacklisted_cpu()) {
  370. printk(KERN_INFO
  371. "blowfish-x86_64: performance on this CPU "
  372. "would be suboptimal: disabling "
  373. "blowfish-x86_64.\n");
  374. return -ENODEV;
  375. }
  376. err = crypto_register_alg(&bf_cipher_alg);
  377. if (err)
  378. return err;
  379. err = crypto_register_skciphers(bf_skcipher_algs,
  380. ARRAY_SIZE(bf_skcipher_algs));
  381. if (err)
  382. crypto_unregister_alg(&bf_cipher_alg);
  383. return err;
  384. }
  385. static void __exit fini(void)
  386. {
  387. crypto_unregister_alg(&bf_cipher_alg);
  388. crypto_unregister_skciphers(bf_skcipher_algs,
  389. ARRAY_SIZE(bf_skcipher_algs));
  390. }
  391. module_init(init);
  392. module_exit(fini);
  393. MODULE_LICENSE("GPL");
  394. MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
  395. MODULE_ALIAS_CRYPTO("blowfish");
  396. MODULE_ALIAS_CRYPTO("blowfish-asm");