skcipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  181. const u8 *key, unsigned int keylen)
  182. {
  183. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  184. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  185. u8 *buffer, *alignbuffer;
  186. unsigned long absize;
  187. int ret;
  188. absize = keylen + alignmask;
  189. buffer = kmalloc(absize, GFP_ATOMIC);
  190. if (!buffer)
  191. return -ENOMEM;
  192. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  193. memcpy(alignbuffer, key, keylen);
  194. ret = cipher->setkey(tfm, alignbuffer, keylen);
  195. kzfree(buffer);
  196. return ret;
  197. }
  198. static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  199. unsigned int keylen)
  200. {
  201. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  202. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  203. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  204. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  205. return -EINVAL;
  206. }
  207. if ((unsigned long)key & alignmask)
  208. return skcipher_setkey_unaligned(tfm, key, keylen);
  209. return cipher->setkey(tfm, key, keylen);
  210. }
  211. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  212. {
  213. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  214. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  215. alg->exit(skcipher);
  216. }
  217. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  218. {
  219. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  220. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  221. if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  222. return crypto_init_skcipher_ops_blkcipher(tfm);
  223. if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  224. tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  225. return crypto_init_skcipher_ops_ablkcipher(tfm);
  226. skcipher->setkey = skcipher_setkey;
  227. skcipher->encrypt = alg->encrypt;
  228. skcipher->decrypt = alg->decrypt;
  229. skcipher->ivsize = alg->ivsize;
  230. skcipher->keysize = alg->max_keysize;
  231. if (alg->exit)
  232. skcipher->base.exit = crypto_skcipher_exit_tfm;
  233. if (alg->init)
  234. return alg->init(skcipher);
  235. return 0;
  236. }
  237. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  238. {
  239. struct skcipher_instance *skcipher =
  240. container_of(inst, struct skcipher_instance, s.base);
  241. skcipher->free(skcipher);
  242. }
  243. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  244. __attribute__ ((unused));
  245. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  246. {
  247. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  248. base);
  249. seq_printf(m, "type : skcipher\n");
  250. seq_printf(m, "async : %s\n",
  251. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  252. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  253. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  254. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  255. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  256. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  257. }
  258. #ifdef CONFIG_NET
  259. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  260. {
  261. struct crypto_report_blkcipher rblkcipher;
  262. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  263. base);
  264. strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  265. strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  266. rblkcipher.blocksize = alg->cra_blocksize;
  267. rblkcipher.min_keysize = skcipher->min_keysize;
  268. rblkcipher.max_keysize = skcipher->max_keysize;
  269. rblkcipher.ivsize = skcipher->ivsize;
  270. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  271. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  272. goto nla_put_failure;
  273. return 0;
  274. nla_put_failure:
  275. return -EMSGSIZE;
  276. }
  277. #else
  278. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  279. {
  280. return -ENOSYS;
  281. }
  282. #endif
  283. static const struct crypto_type crypto_skcipher_type2 = {
  284. .extsize = crypto_skcipher_extsize,
  285. .init_tfm = crypto_skcipher_init_tfm,
  286. .free = crypto_skcipher_free_instance,
  287. #ifdef CONFIG_PROC_FS
  288. .show = crypto_skcipher_show,
  289. #endif
  290. .report = crypto_skcipher_report,
  291. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  292. .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  293. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  294. .tfmsize = offsetof(struct crypto_skcipher, base),
  295. };
  296. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  297. const char *name, u32 type, u32 mask)
  298. {
  299. spawn->base.frontend = &crypto_skcipher_type2;
  300. return crypto_grab_spawn(&spawn->base, name, type, mask);
  301. }
  302. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  303. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  304. u32 type, u32 mask)
  305. {
  306. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  307. }
  308. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  309. int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  310. {
  311. return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  312. type, mask);
  313. }
  314. EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  315. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  316. {
  317. struct crypto_alg *base = &alg->base;
  318. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
  319. return -EINVAL;
  320. if (!alg->chunksize)
  321. alg->chunksize = base->cra_blocksize;
  322. base->cra_type = &crypto_skcipher_type2;
  323. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  324. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  325. return 0;
  326. }
  327. int crypto_register_skcipher(struct skcipher_alg *alg)
  328. {
  329. struct crypto_alg *base = &alg->base;
  330. int err;
  331. err = skcipher_prepare_alg(alg);
  332. if (err)
  333. return err;
  334. return crypto_register_alg(base);
  335. }
  336. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  337. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  338. {
  339. crypto_unregister_alg(&alg->base);
  340. }
  341. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  342. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  343. {
  344. int i, ret;
  345. for (i = 0; i < count; i++) {
  346. ret = crypto_register_skcipher(&algs[i]);
  347. if (ret)
  348. goto err;
  349. }
  350. return 0;
  351. err:
  352. for (--i; i >= 0; --i)
  353. crypto_unregister_skcipher(&algs[i]);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  357. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  358. {
  359. int i;
  360. for (i = count - 1; i >= 0; --i)
  361. crypto_unregister_skcipher(&algs[i]);
  362. }
  363. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  364. int skcipher_register_instance(struct crypto_template *tmpl,
  365. struct skcipher_instance *inst)
  366. {
  367. int err;
  368. err = skcipher_prepare_alg(&inst->alg);
  369. if (err)
  370. return err;
  371. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  372. }
  373. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  374. MODULE_LICENSE("GPL");
  375. MODULE_DESCRIPTION("Symmetric key cipher type");