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 <crypto/algapi.h>
  23. #include <crypto/des.h>
  24. #include <crypto/internal/skcipher.h>
  25. #include <linux/crypto.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/types.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 skcipher_request *req, const u32 *expkey)
  72. {
  73. const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  74. struct skcipher_walk walk;
  75. unsigned int nbytes;
  76. int err;
  77. err = skcipher_walk_virt(&walk, req, false);
  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 = skcipher_walk_done(&walk, nbytes);
  102. }
  103. return err;
  104. }
  105. static int ecb_encrypt(struct skcipher_request *req)
  106. {
  107. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  108. struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
  109. return ecb_crypt(req, ctx->enc_expkey);
  110. }
  111. static int ecb_decrypt(struct skcipher_request *req)
  112. {
  113. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  114. struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
  115. return ecb_crypt(req, ctx->dec_expkey);
  116. }
  117. static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
  118. struct skcipher_walk *walk)
  119. {
  120. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  121. unsigned int nbytes = walk->nbytes;
  122. u64 *src = (u64 *)walk->src.virt.addr;
  123. u64 *dst = (u64 *)walk->dst.virt.addr;
  124. u64 *iv = (u64 *)walk->iv;
  125. do {
  126. *dst = *src ^ *iv;
  127. des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
  128. iv = dst;
  129. src += 1;
  130. dst += 1;
  131. nbytes -= bsize;
  132. } while (nbytes >= bsize);
  133. *(u64 *)walk->iv = *iv;
  134. return nbytes;
  135. }
  136. static int cbc_encrypt(struct skcipher_request *req)
  137. {
  138. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  139. struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
  140. struct skcipher_walk walk;
  141. unsigned int nbytes;
  142. int err;
  143. err = skcipher_walk_virt(&walk, req, false);
  144. while ((nbytes = walk.nbytes)) {
  145. nbytes = __cbc_encrypt(ctx, &walk);
  146. err = skcipher_walk_done(&walk, nbytes);
  147. }
  148. return err;
  149. }
  150. static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx *ctx,
  151. struct skcipher_walk *walk)
  152. {
  153. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  154. unsigned int nbytes = walk->nbytes;
  155. u64 *src = (u64 *)walk->src.virt.addr;
  156. u64 *dst = (u64 *)walk->dst.virt.addr;
  157. u64 ivs[3 - 1];
  158. u64 last_iv;
  159. /* Start of the last block. */
  160. src += nbytes / bsize - 1;
  161. dst += nbytes / bsize - 1;
  162. last_iv = *src;
  163. /* Process four block batch */
  164. if (nbytes >= bsize * 3) {
  165. do {
  166. nbytes -= bsize * 3 - bsize;
  167. src -= 3 - 1;
  168. dst -= 3 - 1;
  169. ivs[0] = src[0];
  170. ivs[1] = src[1];
  171. des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
  172. dst[1] ^= ivs[0];
  173. dst[2] ^= ivs[1];
  174. nbytes -= bsize;
  175. if (nbytes < bsize)
  176. goto done;
  177. *dst ^= *(src - 1);
  178. src -= 1;
  179. dst -= 1;
  180. } while (nbytes >= bsize * 3);
  181. }
  182. /* Handle leftovers */
  183. for (;;) {
  184. des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
  185. nbytes -= bsize;
  186. if (nbytes < bsize)
  187. break;
  188. *dst ^= *(src - 1);
  189. src -= 1;
  190. dst -= 1;
  191. }
  192. done:
  193. *dst ^= *(u64 *)walk->iv;
  194. *(u64 *)walk->iv = last_iv;
  195. return nbytes;
  196. }
  197. static int cbc_decrypt(struct skcipher_request *req)
  198. {
  199. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  200. struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
  201. struct skcipher_walk walk;
  202. unsigned int nbytes;
  203. int err;
  204. err = skcipher_walk_virt(&walk, req, false);
  205. while ((nbytes = walk.nbytes)) {
  206. nbytes = __cbc_decrypt(ctx, &walk);
  207. err = skcipher_walk_done(&walk, nbytes);
  208. }
  209. return err;
  210. }
  211. static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
  212. struct skcipher_walk *walk)
  213. {
  214. u8 *ctrblk = walk->iv;
  215. u8 keystream[DES3_EDE_BLOCK_SIZE];
  216. u8 *src = walk->src.virt.addr;
  217. u8 *dst = walk->dst.virt.addr;
  218. unsigned int nbytes = walk->nbytes;
  219. des3_ede_enc_blk(ctx, keystream, ctrblk);
  220. crypto_xor_cpy(dst, keystream, src, nbytes);
  221. crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
  222. }
  223. static unsigned int __ctr_crypt(struct des3_ede_x86_ctx *ctx,
  224. struct skcipher_walk *walk)
  225. {
  226. unsigned int bsize = DES3_EDE_BLOCK_SIZE;
  227. unsigned int nbytes = walk->nbytes;
  228. __be64 *src = (__be64 *)walk->src.virt.addr;
  229. __be64 *dst = (__be64 *)walk->dst.virt.addr;
  230. u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
  231. __be64 ctrblocks[3];
  232. /* Process four block batch */
  233. if (nbytes >= bsize * 3) {
  234. do {
  235. /* create ctrblks for parallel encrypt */
  236. ctrblocks[0] = cpu_to_be64(ctrblk++);
  237. ctrblocks[1] = cpu_to_be64(ctrblk++);
  238. ctrblocks[2] = cpu_to_be64(ctrblk++);
  239. des3_ede_enc_blk_3way(ctx, (u8 *)ctrblocks,
  240. (u8 *)ctrblocks);
  241. dst[0] = src[0] ^ ctrblocks[0];
  242. dst[1] = src[1] ^ ctrblocks[1];
  243. dst[2] = src[2] ^ ctrblocks[2];
  244. src += 3;
  245. dst += 3;
  246. } while ((nbytes -= bsize * 3) >= bsize * 3);
  247. if (nbytes < bsize)
  248. goto done;
  249. }
  250. /* Handle leftovers */
  251. do {
  252. ctrblocks[0] = cpu_to_be64(ctrblk++);
  253. des3_ede_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
  254. dst[0] = src[0] ^ ctrblocks[0];
  255. src += 1;
  256. dst += 1;
  257. } while ((nbytes -= bsize) >= bsize);
  258. done:
  259. *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
  260. return nbytes;
  261. }
  262. static int ctr_crypt(struct skcipher_request *req)
  263. {
  264. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  265. struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
  266. struct skcipher_walk walk;
  267. unsigned int nbytes;
  268. int err;
  269. err = skcipher_walk_virt(&walk, req, false);
  270. while ((nbytes = walk.nbytes) >= DES3_EDE_BLOCK_SIZE) {
  271. nbytes = __ctr_crypt(ctx, &walk);
  272. err = skcipher_walk_done(&walk, nbytes);
  273. }
  274. if (nbytes) {
  275. ctr_crypt_final(ctx, &walk);
  276. err = skcipher_walk_done(&walk, 0);
  277. }
  278. return err;
  279. }
  280. static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
  281. unsigned int keylen)
  282. {
  283. struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
  284. u32 i, j, tmp;
  285. int err;
  286. /* Generate encryption context using generic implementation. */
  287. err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
  288. if (err < 0)
  289. return err;
  290. /* Fix encryption context for this implementation and form decryption
  291. * context. */
  292. j = DES3_EDE_EXPKEY_WORDS - 2;
  293. for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
  294. tmp = ror32(ctx->enc_expkey[i + 1], 4);
  295. ctx->enc_expkey[i + 1] = tmp;
  296. ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
  297. ctx->dec_expkey[j + 1] = tmp;
  298. }
  299. return 0;
  300. }
  301. static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
  302. const u8 *key,
  303. unsigned int keylen)
  304. {
  305. return des3_ede_x86_setkey(&tfm->base, key, keylen);
  306. }
  307. static struct crypto_alg des3_ede_cipher = {
  308. .cra_name = "des3_ede",
  309. .cra_driver_name = "des3_ede-asm",
  310. .cra_priority = 200,
  311. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  312. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  313. .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  314. .cra_alignmask = 0,
  315. .cra_module = THIS_MODULE,
  316. .cra_u = {
  317. .cipher = {
  318. .cia_min_keysize = DES3_EDE_KEY_SIZE,
  319. .cia_max_keysize = DES3_EDE_KEY_SIZE,
  320. .cia_setkey = des3_ede_x86_setkey,
  321. .cia_encrypt = des3_ede_x86_encrypt,
  322. .cia_decrypt = des3_ede_x86_decrypt,
  323. }
  324. }
  325. };
  326. static struct skcipher_alg des3_ede_skciphers[] = {
  327. {
  328. .base.cra_name = "ecb(des3_ede)",
  329. .base.cra_driver_name = "ecb-des3_ede-asm",
  330. .base.cra_priority = 300,
  331. .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
  332. .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  333. .base.cra_module = THIS_MODULE,
  334. .min_keysize = DES3_EDE_KEY_SIZE,
  335. .max_keysize = DES3_EDE_KEY_SIZE,
  336. .setkey = des3_ede_x86_setkey_skcipher,
  337. .encrypt = ecb_encrypt,
  338. .decrypt = ecb_decrypt,
  339. }, {
  340. .base.cra_name = "cbc(des3_ede)",
  341. .base.cra_driver_name = "cbc-des3_ede-asm",
  342. .base.cra_priority = 300,
  343. .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
  344. .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  345. .base.cra_module = THIS_MODULE,
  346. .min_keysize = DES3_EDE_KEY_SIZE,
  347. .max_keysize = DES3_EDE_KEY_SIZE,
  348. .ivsize = DES3_EDE_BLOCK_SIZE,
  349. .setkey = des3_ede_x86_setkey_skcipher,
  350. .encrypt = cbc_encrypt,
  351. .decrypt = cbc_decrypt,
  352. }, {
  353. .base.cra_name = "ctr(des3_ede)",
  354. .base.cra_driver_name = "ctr-des3_ede-asm",
  355. .base.cra_priority = 300,
  356. .base.cra_blocksize = 1,
  357. .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
  358. .base.cra_module = THIS_MODULE,
  359. .min_keysize = DES3_EDE_KEY_SIZE,
  360. .max_keysize = DES3_EDE_KEY_SIZE,
  361. .ivsize = DES3_EDE_BLOCK_SIZE,
  362. .chunksize = DES3_EDE_BLOCK_SIZE,
  363. .setkey = des3_ede_x86_setkey_skcipher,
  364. .encrypt = ctr_crypt,
  365. .decrypt = ctr_crypt,
  366. }
  367. };
  368. static bool is_blacklisted_cpu(void)
  369. {
  370. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  371. return false;
  372. if (boot_cpu_data.x86 == 0x0f) {
  373. /*
  374. * On Pentium 4, des3_ede-x86_64 is slower than generic C
  375. * implementation because use of 64bit rotates (which are really
  376. * slow on P4). Therefore blacklist P4s.
  377. */
  378. return true;
  379. }
  380. return false;
  381. }
  382. static int force;
  383. module_param(force, int, 0);
  384. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  385. static int __init des3_ede_x86_init(void)
  386. {
  387. int err;
  388. if (!force && is_blacklisted_cpu()) {
  389. pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
  390. return -ENODEV;
  391. }
  392. err = crypto_register_alg(&des3_ede_cipher);
  393. if (err)
  394. return err;
  395. err = crypto_register_skciphers(des3_ede_skciphers,
  396. ARRAY_SIZE(des3_ede_skciphers));
  397. if (err)
  398. crypto_unregister_alg(&des3_ede_cipher);
  399. return err;
  400. }
  401. static void __exit des3_ede_x86_fini(void)
  402. {
  403. crypto_unregister_alg(&des3_ede_cipher);
  404. crypto_unregister_skciphers(des3_ede_skciphers,
  405. ARRAY_SIZE(des3_ede_skciphers));
  406. }
  407. module_init(des3_ede_x86_init);
  408. module_exit(des3_ede_x86_fini);
  409. MODULE_LICENSE("GPL");
  410. MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
  411. MODULE_ALIAS_CRYPTO("des3_ede");
  412. MODULE_ALIAS_CRYPTO("des3_ede-asm");
  413. MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");