des_glue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Glue code for DES encryption optimized for sparc64 crypto opcodes.
  3. *
  4. * Copyright (C) 2012 David S. Miller <[email protected]>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/crypto.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/types.h>
  12. #include <crypto/algapi.h>
  13. #include <crypto/internal/des.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <asm/fpumacro.h>
  16. #include <asm/pstate.h>
  17. #include <asm/elf.h>
  18. #include "opcodes.h"
  19. struct des_sparc64_ctx {
  20. u64 encrypt_expkey[DES_EXPKEY_WORDS / 2];
  21. u64 decrypt_expkey[DES_EXPKEY_WORDS / 2];
  22. };
  23. struct des3_ede_sparc64_ctx {
  24. u64 encrypt_expkey[DES3_EDE_EXPKEY_WORDS / 2];
  25. u64 decrypt_expkey[DES3_EDE_EXPKEY_WORDS / 2];
  26. };
  27. static void encrypt_to_decrypt(u64 *d, const u64 *e)
  28. {
  29. const u64 *s = e + (DES_EXPKEY_WORDS / 2) - 1;
  30. int i;
  31. for (i = 0; i < DES_EXPKEY_WORDS / 2; i++)
  32. *d++ = *s--;
  33. }
  34. extern void des_sparc64_key_expand(const u32 *input_key, u64 *key);
  35. static int des_set_key(struct crypto_tfm *tfm, const u8 *key,
  36. unsigned int keylen)
  37. {
  38. struct des_sparc64_ctx *dctx = crypto_tfm_ctx(tfm);
  39. int err;
  40. /* Even though we have special instructions for key expansion,
  41. * we call des_verify_key() so that we don't have to write our own
  42. * weak key detection code.
  43. */
  44. err = crypto_des_verify_key(tfm, key);
  45. if (err)
  46. return err;
  47. des_sparc64_key_expand((const u32 *) key, &dctx->encrypt_expkey[0]);
  48. encrypt_to_decrypt(&dctx->decrypt_expkey[0], &dctx->encrypt_expkey[0]);
  49. return 0;
  50. }
  51. static int des_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *key,
  52. unsigned int keylen)
  53. {
  54. return des_set_key(crypto_skcipher_tfm(tfm), key, keylen);
  55. }
  56. extern void des_sparc64_crypt(const u64 *key, const u64 *input,
  57. u64 *output);
  58. static void sparc_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  59. {
  60. struct des_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  61. const u64 *K = ctx->encrypt_expkey;
  62. des_sparc64_crypt(K, (const u64 *) src, (u64 *) dst);
  63. }
  64. static void sparc_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  65. {
  66. struct des_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  67. const u64 *K = ctx->decrypt_expkey;
  68. des_sparc64_crypt(K, (const u64 *) src, (u64 *) dst);
  69. }
  70. extern void des_sparc64_load_keys(const u64 *key);
  71. extern void des_sparc64_ecb_crypt(const u64 *input, u64 *output,
  72. unsigned int len);
  73. static int __ecb_crypt(struct skcipher_request *req, bool encrypt)
  74. {
  75. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  76. const struct des_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
  77. struct skcipher_walk walk;
  78. unsigned int nbytes;
  79. int err;
  80. err = skcipher_walk_virt(&walk, req, true);
  81. if (err)
  82. return err;
  83. if (encrypt)
  84. des_sparc64_load_keys(&ctx->encrypt_expkey[0]);
  85. else
  86. des_sparc64_load_keys(&ctx->decrypt_expkey[0]);
  87. while ((nbytes = walk.nbytes) != 0) {
  88. des_sparc64_ecb_crypt(walk.src.virt.addr, walk.dst.virt.addr,
  89. round_down(nbytes, DES_BLOCK_SIZE));
  90. err = skcipher_walk_done(&walk, nbytes % DES_BLOCK_SIZE);
  91. }
  92. fprs_write(0);
  93. return err;
  94. }
  95. static int ecb_encrypt(struct skcipher_request *req)
  96. {
  97. return __ecb_crypt(req, true);
  98. }
  99. static int ecb_decrypt(struct skcipher_request *req)
  100. {
  101. return __ecb_crypt(req, false);
  102. }
  103. extern void des_sparc64_cbc_encrypt(const u64 *input, u64 *output,
  104. unsigned int len, u64 *iv);
  105. extern void des_sparc64_cbc_decrypt(const u64 *input, u64 *output,
  106. unsigned int len, u64 *iv);
  107. static int __cbc_crypt(struct skcipher_request *req, bool encrypt)
  108. {
  109. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  110. const struct des_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
  111. struct skcipher_walk walk;
  112. unsigned int nbytes;
  113. int err;
  114. err = skcipher_walk_virt(&walk, req, true);
  115. if (err)
  116. return err;
  117. if (encrypt)
  118. des_sparc64_load_keys(&ctx->encrypt_expkey[0]);
  119. else
  120. des_sparc64_load_keys(&ctx->decrypt_expkey[0]);
  121. while ((nbytes = walk.nbytes) != 0) {
  122. if (encrypt)
  123. des_sparc64_cbc_encrypt(walk.src.virt.addr,
  124. walk.dst.virt.addr,
  125. round_down(nbytes,
  126. DES_BLOCK_SIZE),
  127. walk.iv);
  128. else
  129. des_sparc64_cbc_decrypt(walk.src.virt.addr,
  130. walk.dst.virt.addr,
  131. round_down(nbytes,
  132. DES_BLOCK_SIZE),
  133. walk.iv);
  134. err = skcipher_walk_done(&walk, nbytes % DES_BLOCK_SIZE);
  135. }
  136. fprs_write(0);
  137. return err;
  138. }
  139. static int cbc_encrypt(struct skcipher_request *req)
  140. {
  141. return __cbc_crypt(req, true);
  142. }
  143. static int cbc_decrypt(struct skcipher_request *req)
  144. {
  145. return __cbc_crypt(req, false);
  146. }
  147. static int des3_ede_set_key(struct crypto_tfm *tfm, const u8 *key,
  148. unsigned int keylen)
  149. {
  150. struct des3_ede_sparc64_ctx *dctx = crypto_tfm_ctx(tfm);
  151. u64 k1[DES_EXPKEY_WORDS / 2];
  152. u64 k2[DES_EXPKEY_WORDS / 2];
  153. u64 k3[DES_EXPKEY_WORDS / 2];
  154. int err;
  155. err = crypto_des3_ede_verify_key(tfm, key);
  156. if (err)
  157. return err;
  158. des_sparc64_key_expand((const u32 *)key, k1);
  159. key += DES_KEY_SIZE;
  160. des_sparc64_key_expand((const u32 *)key, k2);
  161. key += DES_KEY_SIZE;
  162. des_sparc64_key_expand((const u32 *)key, k3);
  163. memcpy(&dctx->encrypt_expkey[0], &k1[0], sizeof(k1));
  164. encrypt_to_decrypt(&dctx->encrypt_expkey[DES_EXPKEY_WORDS / 2], &k2[0]);
  165. memcpy(&dctx->encrypt_expkey[(DES_EXPKEY_WORDS / 2) * 2],
  166. &k3[0], sizeof(k3));
  167. encrypt_to_decrypt(&dctx->decrypt_expkey[0], &k3[0]);
  168. memcpy(&dctx->decrypt_expkey[DES_EXPKEY_WORDS / 2],
  169. &k2[0], sizeof(k2));
  170. encrypt_to_decrypt(&dctx->decrypt_expkey[(DES_EXPKEY_WORDS / 2) * 2],
  171. &k1[0]);
  172. return 0;
  173. }
  174. static int des3_ede_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *key,
  175. unsigned int keylen)
  176. {
  177. return des3_ede_set_key(crypto_skcipher_tfm(tfm), key, keylen);
  178. }
  179. extern void des3_ede_sparc64_crypt(const u64 *key, const u64 *input,
  180. u64 *output);
  181. static void sparc_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  182. {
  183. struct des3_ede_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  184. const u64 *K = ctx->encrypt_expkey;
  185. des3_ede_sparc64_crypt(K, (const u64 *) src, (u64 *) dst);
  186. }
  187. static void sparc_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  188. {
  189. struct des3_ede_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  190. const u64 *K = ctx->decrypt_expkey;
  191. des3_ede_sparc64_crypt(K, (const u64 *) src, (u64 *) dst);
  192. }
  193. extern void des3_ede_sparc64_load_keys(const u64 *key);
  194. extern void des3_ede_sparc64_ecb_crypt(const u64 *expkey, const u64 *input,
  195. u64 *output, unsigned int len);
  196. static int __ecb3_crypt(struct skcipher_request *req, bool encrypt)
  197. {
  198. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  199. const struct des3_ede_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
  200. struct skcipher_walk walk;
  201. const u64 *K;
  202. unsigned int nbytes;
  203. int err;
  204. err = skcipher_walk_virt(&walk, req, true);
  205. if (err)
  206. return err;
  207. if (encrypt)
  208. K = &ctx->encrypt_expkey[0];
  209. else
  210. K = &ctx->decrypt_expkey[0];
  211. des3_ede_sparc64_load_keys(K);
  212. while ((nbytes = walk.nbytes) != 0) {
  213. des3_ede_sparc64_ecb_crypt(K, walk.src.virt.addr,
  214. walk.dst.virt.addr,
  215. round_down(nbytes, DES_BLOCK_SIZE));
  216. err = skcipher_walk_done(&walk, nbytes % DES_BLOCK_SIZE);
  217. }
  218. fprs_write(0);
  219. return err;
  220. }
  221. static int ecb3_encrypt(struct skcipher_request *req)
  222. {
  223. return __ecb3_crypt(req, true);
  224. }
  225. static int ecb3_decrypt(struct skcipher_request *req)
  226. {
  227. return __ecb3_crypt(req, false);
  228. }
  229. extern void des3_ede_sparc64_cbc_encrypt(const u64 *expkey, const u64 *input,
  230. u64 *output, unsigned int len,
  231. u64 *iv);
  232. extern void des3_ede_sparc64_cbc_decrypt(const u64 *expkey, const u64 *input,
  233. u64 *output, unsigned int len,
  234. u64 *iv);
  235. static int __cbc3_crypt(struct skcipher_request *req, bool encrypt)
  236. {
  237. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  238. const struct des3_ede_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
  239. struct skcipher_walk walk;
  240. const u64 *K;
  241. unsigned int nbytes;
  242. int err;
  243. err = skcipher_walk_virt(&walk, req, true);
  244. if (err)
  245. return err;
  246. if (encrypt)
  247. K = &ctx->encrypt_expkey[0];
  248. else
  249. K = &ctx->decrypt_expkey[0];
  250. des3_ede_sparc64_load_keys(K);
  251. while ((nbytes = walk.nbytes) != 0) {
  252. if (encrypt)
  253. des3_ede_sparc64_cbc_encrypt(K, walk.src.virt.addr,
  254. walk.dst.virt.addr,
  255. round_down(nbytes,
  256. DES_BLOCK_SIZE),
  257. walk.iv);
  258. else
  259. des3_ede_sparc64_cbc_decrypt(K, walk.src.virt.addr,
  260. walk.dst.virt.addr,
  261. round_down(nbytes,
  262. DES_BLOCK_SIZE),
  263. walk.iv);
  264. err = skcipher_walk_done(&walk, nbytes % DES_BLOCK_SIZE);
  265. }
  266. fprs_write(0);
  267. return err;
  268. }
  269. static int cbc3_encrypt(struct skcipher_request *req)
  270. {
  271. return __cbc3_crypt(req, true);
  272. }
  273. static int cbc3_decrypt(struct skcipher_request *req)
  274. {
  275. return __cbc3_crypt(req, false);
  276. }
  277. static struct crypto_alg cipher_algs[] = {
  278. {
  279. .cra_name = "des",
  280. .cra_driver_name = "des-sparc64",
  281. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  282. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  283. .cra_blocksize = DES_BLOCK_SIZE,
  284. .cra_ctxsize = sizeof(struct des_sparc64_ctx),
  285. .cra_alignmask = 7,
  286. .cra_module = THIS_MODULE,
  287. .cra_u = {
  288. .cipher = {
  289. .cia_min_keysize = DES_KEY_SIZE,
  290. .cia_max_keysize = DES_KEY_SIZE,
  291. .cia_setkey = des_set_key,
  292. .cia_encrypt = sparc_des_encrypt,
  293. .cia_decrypt = sparc_des_decrypt
  294. }
  295. }
  296. }, {
  297. .cra_name = "des3_ede",
  298. .cra_driver_name = "des3_ede-sparc64",
  299. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  300. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  301. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  302. .cra_ctxsize = sizeof(struct des3_ede_sparc64_ctx),
  303. .cra_alignmask = 7,
  304. .cra_module = THIS_MODULE,
  305. .cra_u = {
  306. .cipher = {
  307. .cia_min_keysize = DES3_EDE_KEY_SIZE,
  308. .cia_max_keysize = DES3_EDE_KEY_SIZE,
  309. .cia_setkey = des3_ede_set_key,
  310. .cia_encrypt = sparc_des3_ede_encrypt,
  311. .cia_decrypt = sparc_des3_ede_decrypt
  312. }
  313. }
  314. }
  315. };
  316. static struct skcipher_alg skcipher_algs[] = {
  317. {
  318. .base.cra_name = "ecb(des)",
  319. .base.cra_driver_name = "ecb-des-sparc64",
  320. .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
  321. .base.cra_blocksize = DES_BLOCK_SIZE,
  322. .base.cra_ctxsize = sizeof(struct des_sparc64_ctx),
  323. .base.cra_alignmask = 7,
  324. .base.cra_module = THIS_MODULE,
  325. .min_keysize = DES_KEY_SIZE,
  326. .max_keysize = DES_KEY_SIZE,
  327. .setkey = des_set_key_skcipher,
  328. .encrypt = ecb_encrypt,
  329. .decrypt = ecb_decrypt,
  330. }, {
  331. .base.cra_name = "cbc(des)",
  332. .base.cra_driver_name = "cbc-des-sparc64",
  333. .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
  334. .base.cra_blocksize = DES_BLOCK_SIZE,
  335. .base.cra_ctxsize = sizeof(struct des_sparc64_ctx),
  336. .base.cra_alignmask = 7,
  337. .base.cra_module = THIS_MODULE,
  338. .min_keysize = DES_KEY_SIZE,
  339. .max_keysize = DES_KEY_SIZE,
  340. .ivsize = DES_BLOCK_SIZE,
  341. .setkey = des_set_key_skcipher,
  342. .encrypt = cbc_encrypt,
  343. .decrypt = cbc_decrypt,
  344. }, {
  345. .base.cra_name = "ecb(des3_ede)",
  346. .base.cra_driver_name = "ecb-des3_ede-sparc64",
  347. .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
  348. .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
  349. .base.cra_ctxsize = sizeof(struct des3_ede_sparc64_ctx),
  350. .base.cra_alignmask = 7,
  351. .base.cra_module = THIS_MODULE,
  352. .min_keysize = DES3_EDE_KEY_SIZE,
  353. .max_keysize = DES3_EDE_KEY_SIZE,
  354. .setkey = des3_ede_set_key_skcipher,
  355. .encrypt = ecb3_encrypt,
  356. .decrypt = ecb3_decrypt,
  357. }, {
  358. .base.cra_name = "cbc(des3_ede)",
  359. .base.cra_driver_name = "cbc-des3_ede-sparc64",
  360. .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
  361. .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
  362. .base.cra_ctxsize = sizeof(struct des3_ede_sparc64_ctx),
  363. .base.cra_alignmask = 7,
  364. .base.cra_module = THIS_MODULE,
  365. .min_keysize = DES3_EDE_KEY_SIZE,
  366. .max_keysize = DES3_EDE_KEY_SIZE,
  367. .ivsize = DES3_EDE_BLOCK_SIZE,
  368. .setkey = des3_ede_set_key_skcipher,
  369. .encrypt = cbc3_encrypt,
  370. .decrypt = cbc3_decrypt,
  371. }
  372. };
  373. static bool __init sparc64_has_des_opcode(void)
  374. {
  375. unsigned long cfr;
  376. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  377. return false;
  378. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  379. if (!(cfr & CFR_DES))
  380. return false;
  381. return true;
  382. }
  383. static int __init des_sparc64_mod_init(void)
  384. {
  385. int err;
  386. if (!sparc64_has_des_opcode()) {
  387. pr_info("sparc64 des opcodes not available.\n");
  388. return -ENODEV;
  389. }
  390. pr_info("Using sparc64 des opcodes optimized DES implementation\n");
  391. err = crypto_register_algs(cipher_algs, ARRAY_SIZE(cipher_algs));
  392. if (err)
  393. return err;
  394. err = crypto_register_skciphers(skcipher_algs,
  395. ARRAY_SIZE(skcipher_algs));
  396. if (err)
  397. crypto_unregister_algs(cipher_algs, ARRAY_SIZE(cipher_algs));
  398. return err;
  399. }
  400. static void __exit des_sparc64_mod_fini(void)
  401. {
  402. crypto_unregister_algs(cipher_algs, ARRAY_SIZE(cipher_algs));
  403. crypto_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
  404. }
  405. module_init(des_sparc64_mod_init);
  406. module_exit(des_sparc64_mod_fini);
  407. MODULE_LICENSE("GPL");
  408. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms, sparc64 des opcode accelerated");
  409. MODULE_ALIAS_CRYPTO("des");
  410. MODULE_ALIAS_CRYPTO("des3_ede");
  411. #include "crop_devid.c"