skcipher.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Symmetric key cipher operations.
  3. *
  4. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  5. * multiple page boundaries by using temporary blocks. In user context,
  6. * the kernel is given a chance to schedule us once per page.
  7. *
  8. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/skcipher.h>
  17. #include <linux/bug.h>
  18. #include <linux/cryptouser.h>
  19. #include <linux/module.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/seq_file.h>
  22. #include <net/netlink.h>
  23. #include "internal.h"
  24. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  25. {
  26. if (alg->cra_type == &crypto_blkcipher_type)
  27. return sizeof(struct crypto_blkcipher *);
  28. if (alg->cra_type == &crypto_ablkcipher_type ||
  29. alg->cra_type == &crypto_givcipher_type)
  30. return sizeof(struct crypto_ablkcipher *);
  31. return crypto_alg_extsize(alg);
  32. }
  33. static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
  34. const u8 *key, unsigned int keylen)
  35. {
  36. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  37. struct crypto_blkcipher *blkcipher = *ctx;
  38. int err;
  39. crypto_blkcipher_clear_flags(blkcipher, ~0);
  40. crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
  41. CRYPTO_TFM_REQ_MASK);
  42. err = crypto_blkcipher_setkey(blkcipher, key, keylen);
  43. crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
  44. CRYPTO_TFM_RES_MASK);
  45. return err;
  46. }
  47. static int skcipher_crypt_blkcipher(struct skcipher_request *req,
  48. int (*crypt)(struct blkcipher_desc *,
  49. struct scatterlist *,
  50. struct scatterlist *,
  51. unsigned int))
  52. {
  53. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  54. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  55. struct blkcipher_desc desc = {
  56. .tfm = *ctx,
  57. .info = req->iv,
  58. .flags = req->base.flags,
  59. };
  60. return crypt(&desc, req->dst, req->src, req->cryptlen);
  61. }
  62. static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
  63. {
  64. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  65. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  66. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  67. return skcipher_crypt_blkcipher(req, alg->encrypt);
  68. }
  69. static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
  70. {
  71. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  72. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  73. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  74. return skcipher_crypt_blkcipher(req, alg->decrypt);
  75. }
  76. static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  77. {
  78. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  79. crypto_free_blkcipher(*ctx);
  80. }
  81. static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  82. {
  83. struct crypto_alg *calg = tfm->__crt_alg;
  84. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  85. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  86. struct crypto_blkcipher *blkcipher;
  87. struct crypto_tfm *btfm;
  88. if (!crypto_mod_get(calg))
  89. return -EAGAIN;
  90. btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
  91. CRYPTO_ALG_TYPE_MASK);
  92. if (IS_ERR(btfm)) {
  93. crypto_mod_put(calg);
  94. return PTR_ERR(btfm);
  95. }
  96. blkcipher = __crypto_blkcipher_cast(btfm);
  97. *ctx = blkcipher;
  98. tfm->exit = crypto_exit_skcipher_ops_blkcipher;
  99. skcipher->setkey = skcipher_setkey_blkcipher;
  100. skcipher->encrypt = skcipher_encrypt_blkcipher;
  101. skcipher->decrypt = skcipher_decrypt_blkcipher;
  102. skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
  103. skcipher->keysize = calg->cra_blkcipher.max_keysize;
  104. return 0;
  105. }
  106. static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
  107. const u8 *key, unsigned int keylen)
  108. {
  109. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  110. struct crypto_ablkcipher *ablkcipher = *ctx;
  111. int err;
  112. crypto_ablkcipher_clear_flags(ablkcipher, ~0);
  113. crypto_ablkcipher_set_flags(ablkcipher,
  114. crypto_skcipher_get_flags(tfm) &
  115. CRYPTO_TFM_REQ_MASK);
  116. err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
  117. crypto_skcipher_set_flags(tfm,
  118. crypto_ablkcipher_get_flags(ablkcipher) &
  119. CRYPTO_TFM_RES_MASK);
  120. return err;
  121. }
  122. static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
  123. int (*crypt)(struct ablkcipher_request *))
  124. {
  125. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  126. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  127. struct ablkcipher_request *subreq = skcipher_request_ctx(req);
  128. ablkcipher_request_set_tfm(subreq, *ctx);
  129. ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
  130. req->base.complete, req->base.data);
  131. ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  132. req->iv);
  133. return crypt(subreq);
  134. }
  135. static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
  136. {
  137. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  138. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  139. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  140. return skcipher_crypt_ablkcipher(req, alg->encrypt);
  141. }
  142. static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
  143. {
  144. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  145. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  146. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  147. return skcipher_crypt_ablkcipher(req, alg->decrypt);
  148. }
  149. static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  150. {
  151. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  152. crypto_free_ablkcipher(*ctx);
  153. }
  154. static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  155. {
  156. struct crypto_alg *calg = tfm->__crt_alg;
  157. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  158. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  159. struct crypto_ablkcipher *ablkcipher;
  160. struct crypto_tfm *abtfm;
  161. if (!crypto_mod_get(calg))
  162. return -EAGAIN;
  163. abtfm = __crypto_alloc_tfm(calg, 0, 0);
  164. if (IS_ERR(abtfm)) {
  165. crypto_mod_put(calg);
  166. return PTR_ERR(abtfm);
  167. }
  168. ablkcipher = __crypto_ablkcipher_cast(abtfm);
  169. *ctx = ablkcipher;
  170. tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
  171. skcipher->setkey = skcipher_setkey_ablkcipher;
  172. skcipher->encrypt = skcipher_encrypt_ablkcipher;
  173. skcipher->decrypt = skcipher_decrypt_ablkcipher;
  174. skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  175. skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
  176. sizeof(struct ablkcipher_request);
  177. skcipher->keysize = calg->cra_ablkcipher.max_keysize;
  178. return 0;
  179. }
  180. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  181. {
  182. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  183. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  184. alg->exit(skcipher);
  185. }
  186. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  187. {
  188. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  189. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  190. if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  191. return crypto_init_skcipher_ops_blkcipher(tfm);
  192. if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  193. tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  194. return crypto_init_skcipher_ops_ablkcipher(tfm);
  195. skcipher->setkey = alg->setkey;
  196. skcipher->encrypt = alg->encrypt;
  197. skcipher->decrypt = alg->decrypt;
  198. skcipher->ivsize = alg->ivsize;
  199. skcipher->keysize = alg->max_keysize;
  200. if (alg->exit)
  201. skcipher->base.exit = crypto_skcipher_exit_tfm;
  202. if (alg->init)
  203. return alg->init(skcipher);
  204. return 0;
  205. }
  206. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  207. {
  208. struct skcipher_instance *skcipher =
  209. container_of(inst, struct skcipher_instance, s.base);
  210. skcipher->free(skcipher);
  211. }
  212. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  213. __attribute__ ((unused));
  214. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  215. {
  216. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  217. base);
  218. seq_printf(m, "type : skcipher\n");
  219. seq_printf(m, "async : %s\n",
  220. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  221. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  222. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  223. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  224. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  225. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  226. }
  227. #ifdef CONFIG_NET
  228. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  229. {
  230. struct crypto_report_blkcipher rblkcipher;
  231. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  232. base);
  233. strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  234. strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  235. rblkcipher.blocksize = alg->cra_blocksize;
  236. rblkcipher.min_keysize = skcipher->min_keysize;
  237. rblkcipher.max_keysize = skcipher->max_keysize;
  238. rblkcipher.ivsize = skcipher->ivsize;
  239. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  240. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  241. goto nla_put_failure;
  242. return 0;
  243. nla_put_failure:
  244. return -EMSGSIZE;
  245. }
  246. #else
  247. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  248. {
  249. return -ENOSYS;
  250. }
  251. #endif
  252. static const struct crypto_type crypto_skcipher_type2 = {
  253. .extsize = crypto_skcipher_extsize,
  254. .init_tfm = crypto_skcipher_init_tfm,
  255. .free = crypto_skcipher_free_instance,
  256. #ifdef CONFIG_PROC_FS
  257. .show = crypto_skcipher_show,
  258. #endif
  259. .report = crypto_skcipher_report,
  260. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  261. .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  262. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  263. .tfmsize = offsetof(struct crypto_skcipher, base),
  264. };
  265. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  266. const char *name, u32 type, u32 mask)
  267. {
  268. spawn->base.frontend = &crypto_skcipher_type2;
  269. return crypto_grab_spawn(&spawn->base, name, type, mask);
  270. }
  271. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  272. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  273. u32 type, u32 mask)
  274. {
  275. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  276. }
  277. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  278. int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  279. {
  280. return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  281. type, mask);
  282. }
  283. EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  284. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  285. {
  286. struct crypto_alg *base = &alg->base;
  287. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
  288. return -EINVAL;
  289. if (!alg->chunksize)
  290. alg->chunksize = base->cra_blocksize;
  291. base->cra_type = &crypto_skcipher_type2;
  292. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  293. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  294. return 0;
  295. }
  296. int crypto_register_skcipher(struct skcipher_alg *alg)
  297. {
  298. struct crypto_alg *base = &alg->base;
  299. int err;
  300. err = skcipher_prepare_alg(alg);
  301. if (err)
  302. return err;
  303. return crypto_register_alg(base);
  304. }
  305. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  306. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  307. {
  308. crypto_unregister_alg(&alg->base);
  309. }
  310. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  311. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  312. {
  313. int i, ret;
  314. for (i = 0; i < count; i++) {
  315. ret = crypto_register_skcipher(&algs[i]);
  316. if (ret)
  317. goto err;
  318. }
  319. return 0;
  320. err:
  321. for (--i; i >= 0; --i)
  322. crypto_unregister_skcipher(&algs[i]);
  323. return ret;
  324. }
  325. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  326. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  327. {
  328. int i;
  329. for (i = count - 1; i >= 0; --i)
  330. crypto_unregister_skcipher(&algs[i]);
  331. }
  332. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  333. int skcipher_register_instance(struct crypto_template *tmpl,
  334. struct skcipher_instance *inst)
  335. {
  336. int err;
  337. err = skcipher_prepare_alg(&inst->alg);
  338. if (err)
  339. return err;
  340. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  341. }
  342. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  343. MODULE_LICENSE("GPL");
  344. MODULE_DESCRIPTION("Symmetric key cipher type");