des3_ede_glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Glue Code for assembler optimized version of 3DES
  3. *
  4. * Copyright © 2014 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. */
  22. #include <asm/processor.h>
  23. #include <crypto/des.h>
  24. #include <linux/crypto.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <crypto/algapi.h>
  29. struct des3_ede_x86_ctx {
  30. u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
  31. u32 dec_expkey[DES3_EDE_EXPKEY_WORDS];
  32. };
  33. /* regular block cipher functions */
  34. asmlinkage void des3_ede_x86_64_crypt_blk(const u32 *expkey, u8 *dst,
  35. const u8 *src);
  36. /* 3-way parallel cipher functions */
  37. asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
  38. const u8 *src);
  39. static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
  40. const u8 *src)
  41. {
  42. u32 *enc_ctx = ctx->enc_expkey;
  43. des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
  44. }
  45. static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
  46. const u8 *src)
  47. {
  48. u32 *dec_ctx = ctx->dec_expkey;
  49. des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
  50. }
  51. static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
  52. const u8 *src)
  53. {
  54. u32 *enc_ctx = ctx->enc_expkey;
  55. des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
  56. }
  57. static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
  58. const u8 *src)
  59. {
  60. u32 *dec_ctx = ctx->dec_expkey;
  61. des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
  62. }
  63. static void des3_ede_x86_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  64. {
  65. des3_ede_enc_blk(crypto_tfm_ctx(tfm), dst, src);
  66. }
  67. static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  68. {
  69. des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
  70. }
  71. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  72. const u32 *expkey)
  73. {
  74. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  75. unsigned int nbytes;
  76. int err;
  77. err = blkcipher_walk_virt(desc, walk);
  78. while ((nbytes = walk->nbytes)) {
  79. u8 *wsrc = walk->src.virt.addr;
  80. u8 *wdst = walk->dst.virt.addr;
  81. /* Process four block batch */
  82. if (nbytes >= bsize * 3) {
  83. do {
  84. des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
  85. wsrc);
  86. wsrc += bsize * 3;
  87. wdst += bsize * 3;
  88. nbytes -= bsize * 3;
  89. } while (nbytes >= bsize * 3);
  90. if (nbytes < bsize)
  91. goto done;
  92. }
  93. /* Handle leftovers */
  94. do {
  95. des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
  96. wsrc += bsize;
  97. wdst += bsize;
  98. nbytes -= bsize;
  99. } while (nbytes >= bsize);
  100. done:
  101. err = blkcipher_walk_done(desc, walk, nbytes);
  102. }
  103. return err;
  104. }
  105. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  106. struct scatterlist *src, unsigned int nbytes)
  107. {
  108. struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  109. struct blkcipher_walk walk;
  110. blkcipher_walk_init(&walk, dst, src, nbytes);
  111. return ecb_crypt(desc, &walk, ctx->enc_expkey);
  112. }
  113. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  114. struct scatterlist *src, unsigned int nbytes)
  115. {
  116. struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  117. struct blkcipher_walk walk;
  118. blkcipher_walk_init(&walk, dst, src, nbytes);
  119. return ecb_crypt(desc, &walk, ctx->dec_expkey);
  120. }
  121. static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
  122. struct blkcipher_walk *walk)
  123. {
  124. struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  125. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  126. unsigned int nbytes = walk->nbytes;
  127. u64 *src = (u64 *)walk->src.virt.addr;
  128. u64 *dst = (u64 *)walk->dst.virt.addr;
  129. u64 *iv = (u64 *)walk->iv;
  130. do {
  131. *dst = *src ^ *iv;
  132. des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
  133. iv = dst;
  134. src += 1;
  135. dst += 1;
  136. nbytes -= bsize;
  137. } while (nbytes >= bsize);
  138. *(u64 *)walk->iv = *iv;
  139. return nbytes;
  140. }
  141. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  142. struct scatterlist *src, unsigned int nbytes)
  143. {
  144. struct blkcipher_walk walk;
  145. int err;
  146. blkcipher_walk_init(&walk, dst, src, nbytes);
  147. err = blkcipher_walk_virt(desc, &walk);
  148. while ((nbytes = walk.nbytes)) {
  149. nbytes = __cbc_encrypt(desc, &walk);
  150. err = blkcipher_walk_done(desc, &walk, nbytes);
  151. }
  152. return err;
  153. }
  154. static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
  155. struct blkcipher_walk *walk)
  156. {
  157. struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  158. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  159. unsigned int nbytes = walk->nbytes;
  160. u64 *src = (u64 *)walk->src.virt.addr;
  161. u64 *dst = (u64 *)walk->dst.virt.addr;
  162. u64 ivs[3 - 1];
  163. u64 last_iv;
  164. /* Start of the last block. */
  165. src += nbytes / bsize - 1;
  166. dst += nbytes / bsize - 1;
  167. last_iv = *src;
  168. /* Process four block batch */
  169. if (nbytes >= bsize * 3) {
  170. do {
  171. nbytes -= bsize * 3 - bsize;
  172. src -= 3 - 1;
  173. dst -= 3 - 1;
  174. ivs[0] = src[0];
  175. ivs[1] = src[1];
  176. des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
  177. dst[1] ^= ivs[0];
  178. dst[2] ^= ivs[1];
  179. nbytes -= bsize;
  180. if (nbytes < bsize)
  181. goto done;
  182. *dst ^= *(src - 1);
  183. src -= 1;
  184. dst -= 1;
  185. } while (nbytes >= bsize * 3);
  186. }
  187. /* Handle leftovers */
  188. for (;;) {
  189. des3_ede_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 blkcipher_desc *desc, struct scatterlist *dst,
  203. struct scatterlist *src, unsigned int nbytes)
  204. {
  205. struct blkcipher_walk walk;
  206. int err;
  207. blkcipher_walk_init(&walk, dst, src, nbytes);
  208. err = blkcipher_walk_virt(desc, &walk);
  209. while ((nbytes = walk.nbytes)) {
  210. nbytes = __cbc_decrypt(desc, &walk);
  211. err = blkcipher_walk_done(desc, &walk, nbytes);
  212. }
  213. return err;
  214. }
  215. static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
  216. struct blkcipher_walk *walk)
  217. {
  218. u8 *ctrblk = walk->iv;
  219. u8 keystream[DES3_EDE_BLOCK_SIZE];
  220. u8 *src = walk->src.virt.addr;
  221. u8 *dst = walk->dst.virt.addr;
  222. unsigned int nbytes = walk->nbytes;
  223. des3_ede_enc_blk(ctx, keystream, ctrblk);
  224. crypto_xor(keystream, src, nbytes);
  225. memcpy(dst, keystream, nbytes);
  226. crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
  227. }
  228. static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  229. struct blkcipher_walk *walk)
  230. {
  231. struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  232. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  233. unsigned int nbytes = walk->nbytes;
  234. __be64 *src = (__be64 *)walk->src.virt.addr;
  235. __be64 *dst = (__be64 *)walk->dst.virt.addr;
  236. u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
  237. __be64 ctrblocks[3];
  238. /* Process four block batch */
  239. if (nbytes >= bsize * 3) {
  240. do {
  241. /* create ctrblks for parallel encrypt */
  242. ctrblocks[0] = cpu_to_be64(ctrblk++);
  243. ctrblocks[1] = cpu_to_be64(ctrblk++);
  244. ctrblocks[2] = cpu_to_be64(ctrblk++);
  245. des3_ede_enc_blk_3way(ctx, (u8 *)ctrblocks,
  246. (u8 *)ctrblocks);
  247. dst[0] = src[0] ^ ctrblocks[0];
  248. dst[1] = src[1] ^ ctrblocks[1];
  249. dst[2] = src[2] ^ ctrblocks[2];
  250. src += 3;
  251. dst += 3;
  252. } while ((nbytes -= bsize * 3) >= bsize * 3);
  253. if (nbytes < bsize)
  254. goto done;
  255. }
  256. /* Handle leftovers */
  257. do {
  258. ctrblocks[0] = cpu_to_be64(ctrblk++);
  259. des3_ede_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
  260. dst[0] = src[0] ^ ctrblocks[0];
  261. src += 1;
  262. dst += 1;
  263. } while ((nbytes -= bsize) >= bsize);
  264. done:
  265. *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
  266. return nbytes;
  267. }
  268. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  269. struct scatterlist *src, unsigned int nbytes)
  270. {
  271. struct blkcipher_walk walk;
  272. int err;
  273. blkcipher_walk_init(&walk, dst, src, nbytes);
  274. err = blkcipher_walk_virt_block(desc, &walk, DES3_EDE_BLOCK_SIZE);
  275. while ((nbytes = walk.nbytes) >= DES3_EDE_BLOCK_SIZE) {
  276. nbytes = __ctr_crypt(desc, &walk);
  277. err = blkcipher_walk_done(desc, &walk, nbytes);
  278. }
  279. if (walk.nbytes) {
  280. ctr_crypt_final(crypto_blkcipher_ctx(desc->tfm), &walk);
  281. err = blkcipher_walk_done(desc, &walk, 0);
  282. }
  283. return err;
  284. }
  285. static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
  286. unsigned int keylen)
  287. {
  288. struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
  289. u32 i, j, tmp;
  290. int err;
  291. /* Generate encryption context using generic implementation. */
  292. err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
  293. if (err < 0)
  294. return err;
  295. /* Fix encryption context for this implementation and form decryption
  296. * context. */
  297. j = DES3_EDE_EXPKEY_WORDS - 2;
  298. for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
  299. tmp = ror32(ctx->enc_expkey[i + 1], 4);
  300. ctx->enc_expkey[i + 1] = tmp;
  301. ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
  302. ctx->dec_expkey[j + 1] = tmp;
  303. }
  304. return 0;
  305. }
  306. static struct crypto_alg des3_ede_algs[4] = { {
  307. .cra_name = "des3_ede",
  308. .cra_driver_name = "des3_ede-asm",
  309. .cra_priority = 200,
  310. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  311. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  312. .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  313. .cra_alignmask = 0,
  314. .cra_module = THIS_MODULE,
  315. .cra_u = {
  316. .cipher = {
  317. .cia_min_keysize = DES3_EDE_KEY_SIZE,
  318. .cia_max_keysize = DES3_EDE_KEY_SIZE,
  319. .cia_setkey = des3_ede_x86_setkey,
  320. .cia_encrypt = des3_ede_x86_encrypt,
  321. .cia_decrypt = des3_ede_x86_decrypt,
  322. }
  323. }
  324. }, {
  325. .cra_name = "ecb(des3_ede)",
  326. .cra_driver_name = "ecb-des3_ede-asm",
  327. .cra_priority = 300,
  328. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  329. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  330. .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  331. .cra_alignmask = 0,
  332. .cra_type = &crypto_blkcipher_type,
  333. .cra_module = THIS_MODULE,
  334. .cra_u = {
  335. .blkcipher = {
  336. .min_keysize = DES3_EDE_KEY_SIZE,
  337. .max_keysize = DES3_EDE_KEY_SIZE,
  338. .setkey = des3_ede_x86_setkey,
  339. .encrypt = ecb_encrypt,
  340. .decrypt = ecb_decrypt,
  341. },
  342. },
  343. }, {
  344. .cra_name = "cbc(des3_ede)",
  345. .cra_driver_name = "cbc-des3_ede-asm",
  346. .cra_priority = 300,
  347. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  348. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  349. .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  350. .cra_alignmask = 0,
  351. .cra_type = &crypto_blkcipher_type,
  352. .cra_module = THIS_MODULE,
  353. .cra_u = {
  354. .blkcipher = {
  355. .min_keysize = DES3_EDE_KEY_SIZE,
  356. .max_keysize = DES3_EDE_KEY_SIZE,
  357. .ivsize = DES3_EDE_BLOCK_SIZE,
  358. .setkey = des3_ede_x86_setkey,
  359. .encrypt = cbc_encrypt,
  360. .decrypt = cbc_decrypt,
  361. },
  362. },
  363. }, {
  364. .cra_name = "ctr(des3_ede)",
  365. .cra_driver_name = "ctr-des3_ede-asm",
  366. .cra_priority = 300,
  367. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  368. .cra_blocksize = 1,
  369. .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  370. .cra_alignmask = 0,
  371. .cra_type = &crypto_blkcipher_type,
  372. .cra_module = THIS_MODULE,
  373. .cra_u = {
  374. .blkcipher = {
  375. .min_keysize = DES3_EDE_KEY_SIZE,
  376. .max_keysize = DES3_EDE_KEY_SIZE,
  377. .ivsize = DES3_EDE_BLOCK_SIZE,
  378. .setkey = des3_ede_x86_setkey,
  379. .encrypt = ctr_crypt,
  380. .decrypt = ctr_crypt,
  381. },
  382. },
  383. } };
  384. static bool is_blacklisted_cpu(void)
  385. {
  386. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  387. return false;
  388. if (boot_cpu_data.x86 == 0x0f) {
  389. /*
  390. * On Pentium 4, des3_ede-x86_64 is slower than generic C
  391. * implementation because use of 64bit rotates (which are really
  392. * slow on P4). Therefore blacklist P4s.
  393. */
  394. return true;
  395. }
  396. return false;
  397. }
  398. static int force;
  399. module_param(force, int, 0);
  400. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  401. static int __init des3_ede_x86_init(void)
  402. {
  403. if (!force && is_blacklisted_cpu()) {
  404. pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
  405. return -ENODEV;
  406. }
  407. return crypto_register_algs(des3_ede_algs, ARRAY_SIZE(des3_ede_algs));
  408. }
  409. static void __exit des3_ede_x86_fini(void)
  410. {
  411. crypto_unregister_algs(des3_ede_algs, ARRAY_SIZE(des3_ede_algs));
  412. }
  413. module_init(des3_ede_x86_init);
  414. module_exit(des3_ede_x86_fini);
  415. MODULE_LICENSE("GPL");
  416. MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
  417. MODULE_ALIAS_CRYPTO("des3_ede");
  418. MODULE_ALIAS_CRYPTO("des3_ede-asm");
  419. MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");