skcipher.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Symmetric key cipher operations.
  4. *
  5. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  6. * multiple page boundaries by using temporary blocks. In user context,
  7. * the kernel is given a chance to schedule us once per page.
  8. *
  9. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  10. */
  11. #include <crypto/internal/aead.h>
  12. #include <crypto/internal/cipher.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/bug.h>
  16. #include <linux/cryptouser.h>
  17. #include <linux/err.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/string_choices.h>
  25. #include <net/netlink.h>
  26. #include "skcipher.h"
  27. #define CRYPTO_ALG_TYPE_SKCIPHER_MASK 0x0000000e
  28. enum {
  29. SKCIPHER_WALK_SLOW = 1 << 0,
  30. SKCIPHER_WALK_COPY = 1 << 1,
  31. SKCIPHER_WALK_DIFF = 1 << 2,
  32. SKCIPHER_WALK_SLEEP = 1 << 3,
  33. };
  34. static const struct crypto_type crypto_skcipher_type;
  35. static int skcipher_walk_next(struct skcipher_walk *walk);
  36. static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
  37. {
  38. return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  39. }
  40. static inline struct skcipher_alg *__crypto_skcipher_alg(
  41. struct crypto_alg *alg)
  42. {
  43. return container_of(alg, struct skcipher_alg, base);
  44. }
  45. /**
  46. * skcipher_walk_done() - finish one step of a skcipher_walk
  47. * @walk: the skcipher_walk
  48. * @res: number of bytes *not* processed (>= 0) from walk->nbytes,
  49. * or a -errno value to terminate the walk due to an error
  50. *
  51. * This function cleans up after one step of walking through the source and
  52. * destination scatterlists, and advances to the next step if applicable.
  53. * walk->nbytes is set to the number of bytes available in the next step,
  54. * walk->total is set to the new total number of bytes remaining, and
  55. * walk->{src,dst}.virt.addr is set to the next pair of data pointers. If there
  56. * is no more data, or if an error occurred (i.e. -errno return), then
  57. * walk->nbytes and walk->total are set to 0 and all resources owned by the
  58. * skcipher_walk are freed.
  59. *
  60. * Return: 0 or a -errno value. If @res was a -errno value then it will be
  61. * returned, but other errors may occur too.
  62. */
  63. int skcipher_walk_done(struct skcipher_walk *walk, int res)
  64. {
  65. unsigned int n = walk->nbytes; /* num bytes processed this step */
  66. unsigned int total = 0; /* new total remaining */
  67. if (!n)
  68. goto finish;
  69. if (likely(res >= 0)) {
  70. n -= res; /* subtract num bytes *not* processed */
  71. total = walk->total - n;
  72. }
  73. if (likely(!(walk->flags & (SKCIPHER_WALK_SLOW |
  74. SKCIPHER_WALK_COPY |
  75. SKCIPHER_WALK_DIFF)))) {
  76. scatterwalk_advance(&walk->in, n);
  77. } else if (walk->flags & SKCIPHER_WALK_DIFF) {
  78. scatterwalk_done_src(&walk->in, n);
  79. } else if (walk->flags & SKCIPHER_WALK_COPY) {
  80. scatterwalk_advance(&walk->in, n);
  81. scatterwalk_map(&walk->out);
  82. memcpy(walk->out.addr, walk->page, n);
  83. } else { /* SKCIPHER_WALK_SLOW */
  84. if (res > 0) {
  85. /*
  86. * Didn't process all bytes. Either the algorithm is
  87. * broken, or this was the last step and it turned out
  88. * the message wasn't evenly divisible into blocks but
  89. * the algorithm requires it.
  90. */
  91. res = -EINVAL;
  92. total = 0;
  93. } else
  94. memcpy_to_scatterwalk(&walk->out, walk->out.addr, n);
  95. goto dst_done;
  96. }
  97. scatterwalk_done_dst(&walk->out, n);
  98. dst_done:
  99. if (res > 0)
  100. res = 0;
  101. walk->total = total;
  102. walk->nbytes = 0;
  103. if (total) {
  104. if (walk->flags & SKCIPHER_WALK_SLEEP)
  105. cond_resched();
  106. walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
  107. SKCIPHER_WALK_DIFF);
  108. return skcipher_walk_next(walk);
  109. }
  110. finish:
  111. /* Short-circuit for the common/fast path. */
  112. if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
  113. goto out;
  114. if (walk->iv != walk->oiv)
  115. memcpy(walk->oiv, walk->iv, walk->ivsize);
  116. if (walk->buffer != walk->page)
  117. kfree(walk->buffer);
  118. if (walk->page)
  119. free_page((unsigned long)walk->page);
  120. out:
  121. return res;
  122. }
  123. EXPORT_SYMBOL_GPL(skcipher_walk_done);
  124. static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
  125. {
  126. unsigned alignmask = walk->alignmask;
  127. unsigned n;
  128. void *buffer;
  129. if (!walk->buffer)
  130. walk->buffer = walk->page;
  131. buffer = walk->buffer;
  132. if (!buffer) {
  133. /* Min size for a buffer of bsize bytes aligned to alignmask */
  134. n = bsize + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  135. buffer = kzalloc(n, skcipher_walk_gfp(walk));
  136. if (!buffer)
  137. return skcipher_walk_done(walk, -ENOMEM);
  138. walk->buffer = buffer;
  139. }
  140. buffer = PTR_ALIGN(buffer, alignmask + 1);
  141. memcpy_from_scatterwalk(buffer, &walk->in, bsize);
  142. walk->out.__addr = buffer;
  143. walk->in.__addr = walk->out.addr;
  144. walk->nbytes = bsize;
  145. walk->flags |= SKCIPHER_WALK_SLOW;
  146. return 0;
  147. }
  148. static int skcipher_next_copy(struct skcipher_walk *walk)
  149. {
  150. void *tmp = walk->page;
  151. scatterwalk_map(&walk->in);
  152. memcpy(tmp, walk->in.addr, walk->nbytes);
  153. scatterwalk_unmap(&walk->in);
  154. /*
  155. * walk->in is advanced later when the number of bytes actually
  156. * processed (which might be less than walk->nbytes) is known.
  157. */
  158. walk->in.__addr = tmp;
  159. walk->out.__addr = tmp;
  160. return 0;
  161. }
  162. static int skcipher_next_fast(struct skcipher_walk *walk)
  163. {
  164. unsigned long diff;
  165. diff = offset_in_page(walk->in.offset) -
  166. offset_in_page(walk->out.offset);
  167. diff |= (u8 *)(sg_page(walk->in.sg) + (walk->in.offset >> PAGE_SHIFT)) -
  168. (u8 *)(sg_page(walk->out.sg) + (walk->out.offset >> PAGE_SHIFT));
  169. scatterwalk_map(&walk->out);
  170. walk->in.__addr = walk->out.__addr;
  171. if (diff) {
  172. walk->flags |= SKCIPHER_WALK_DIFF;
  173. scatterwalk_map(&walk->in);
  174. }
  175. return 0;
  176. }
  177. static int skcipher_walk_next(struct skcipher_walk *walk)
  178. {
  179. unsigned int bsize;
  180. unsigned int n;
  181. n = walk->total;
  182. bsize = min(walk->stride, max(n, walk->blocksize));
  183. n = scatterwalk_clamp(&walk->in, n);
  184. n = scatterwalk_clamp(&walk->out, n);
  185. if (unlikely(n < bsize)) {
  186. if (unlikely(walk->total < walk->blocksize))
  187. return skcipher_walk_done(walk, -EINVAL);
  188. slow_path:
  189. return skcipher_next_slow(walk, bsize);
  190. }
  191. walk->nbytes = n;
  192. if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
  193. if (!walk->page) {
  194. gfp_t gfp = skcipher_walk_gfp(walk);
  195. walk->page = (void *)__get_free_page(gfp);
  196. if (!walk->page)
  197. goto slow_path;
  198. }
  199. walk->flags |= SKCIPHER_WALK_COPY;
  200. return skcipher_next_copy(walk);
  201. }
  202. return skcipher_next_fast(walk);
  203. }
  204. static int skcipher_copy_iv(struct skcipher_walk *walk)
  205. {
  206. unsigned alignmask = walk->alignmask;
  207. unsigned ivsize = walk->ivsize;
  208. unsigned aligned_stride = ALIGN(walk->stride, alignmask + 1);
  209. unsigned size;
  210. u8 *iv;
  211. /* Min size for a buffer of stride + ivsize, aligned to alignmask */
  212. size = aligned_stride + ivsize +
  213. (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  214. walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
  215. if (!walk->buffer)
  216. return -ENOMEM;
  217. iv = PTR_ALIGN(walk->buffer, alignmask + 1) + aligned_stride;
  218. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  219. return 0;
  220. }
  221. static int skcipher_walk_first(struct skcipher_walk *walk)
  222. {
  223. if (WARN_ON_ONCE(in_hardirq()))
  224. return -EDEADLK;
  225. walk->buffer = NULL;
  226. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  227. int err = skcipher_copy_iv(walk);
  228. if (err)
  229. return err;
  230. }
  231. walk->page = NULL;
  232. return skcipher_walk_next(walk);
  233. }
  234. int skcipher_walk_virt(struct skcipher_walk *__restrict walk,
  235. struct skcipher_request *__restrict req, bool atomic)
  236. {
  237. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  238. struct skcipher_alg *alg;
  239. might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  240. alg = crypto_skcipher_alg(tfm);
  241. walk->total = req->cryptlen;
  242. walk->nbytes = 0;
  243. walk->iv = req->iv;
  244. walk->oiv = req->iv;
  245. if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic)
  246. walk->flags = SKCIPHER_WALK_SLEEP;
  247. else
  248. walk->flags = 0;
  249. if (unlikely(!walk->total))
  250. return 0;
  251. scatterwalk_start(&walk->in, req->src);
  252. scatterwalk_start(&walk->out, req->dst);
  253. walk->blocksize = crypto_skcipher_blocksize(tfm);
  254. walk->ivsize = crypto_skcipher_ivsize(tfm);
  255. walk->alignmask = crypto_skcipher_alignmask(tfm);
  256. if (alg->co.base.cra_type != &crypto_skcipher_type)
  257. walk->stride = alg->co.chunksize;
  258. else
  259. walk->stride = alg->walksize;
  260. return skcipher_walk_first(walk);
  261. }
  262. EXPORT_SYMBOL_GPL(skcipher_walk_virt);
  263. static int skcipher_walk_aead_common(struct skcipher_walk *__restrict walk,
  264. struct aead_request *__restrict req,
  265. bool atomic)
  266. {
  267. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  268. walk->nbytes = 0;
  269. walk->iv = req->iv;
  270. walk->oiv = req->iv;
  271. if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic)
  272. walk->flags = SKCIPHER_WALK_SLEEP;
  273. else
  274. walk->flags = 0;
  275. if (unlikely(!walk->total))
  276. return 0;
  277. scatterwalk_start_at_pos(&walk->in, req->src, req->assoclen);
  278. scatterwalk_start_at_pos(&walk->out, req->dst, req->assoclen);
  279. walk->blocksize = crypto_aead_blocksize(tfm);
  280. walk->stride = crypto_aead_chunksize(tfm);
  281. walk->ivsize = crypto_aead_ivsize(tfm);
  282. walk->alignmask = crypto_aead_alignmask(tfm);
  283. return skcipher_walk_first(walk);
  284. }
  285. int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk,
  286. struct aead_request *__restrict req,
  287. bool atomic)
  288. {
  289. walk->total = req->cryptlen;
  290. return skcipher_walk_aead_common(walk, req, atomic);
  291. }
  292. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  293. int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk,
  294. struct aead_request *__restrict req,
  295. bool atomic)
  296. {
  297. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  298. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  299. return skcipher_walk_aead_common(walk, req, atomic);
  300. }
  301. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  302. static void skcipher_set_needkey(struct crypto_skcipher *tfm)
  303. {
  304. if (crypto_skcipher_max_keysize(tfm) != 0)
  305. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  306. }
  307. static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  308. const u8 *key, unsigned int keylen)
  309. {
  310. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  311. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  312. u8 *buffer, *alignbuffer;
  313. unsigned long absize;
  314. int ret;
  315. absize = keylen + alignmask;
  316. buffer = kmalloc(absize, GFP_ATOMIC);
  317. if (!buffer)
  318. return -ENOMEM;
  319. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  320. memcpy(alignbuffer, key, keylen);
  321. ret = cipher->setkey(tfm, alignbuffer, keylen);
  322. kfree_sensitive(buffer);
  323. return ret;
  324. }
  325. int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  326. unsigned int keylen)
  327. {
  328. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  329. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  330. int err;
  331. if (cipher->co.base.cra_type != &crypto_skcipher_type) {
  332. struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm);
  333. crypto_lskcipher_clear_flags(*ctx, CRYPTO_TFM_REQ_MASK);
  334. crypto_lskcipher_set_flags(*ctx,
  335. crypto_skcipher_get_flags(tfm) &
  336. CRYPTO_TFM_REQ_MASK);
  337. err = crypto_lskcipher_setkey(*ctx, key, keylen);
  338. goto out;
  339. }
  340. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
  341. return -EINVAL;
  342. if ((unsigned long)key & alignmask)
  343. err = skcipher_setkey_unaligned(tfm, key, keylen);
  344. else
  345. err = cipher->setkey(tfm, key, keylen);
  346. out:
  347. if (unlikely(err)) {
  348. skcipher_set_needkey(tfm);
  349. return err;
  350. }
  351. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
  355. int crypto_skcipher_encrypt(struct skcipher_request *req)
  356. {
  357. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  358. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  359. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  360. return -ENOKEY;
  361. if (alg->co.base.cra_type != &crypto_skcipher_type)
  362. return crypto_lskcipher_encrypt_sg(req);
  363. return alg->encrypt(req);
  364. }
  365. EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
  366. int crypto_skcipher_decrypt(struct skcipher_request *req)
  367. {
  368. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  369. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  370. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  371. return -ENOKEY;
  372. if (alg->co.base.cra_type != &crypto_skcipher_type)
  373. return crypto_lskcipher_decrypt_sg(req);
  374. return alg->decrypt(req);
  375. }
  376. EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
  377. static int crypto_lskcipher_export(struct skcipher_request *req, void *out)
  378. {
  379. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  380. u8 *ivs = skcipher_request_ctx(req);
  381. ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1);
  382. memcpy(out, ivs + crypto_skcipher_ivsize(tfm),
  383. crypto_skcipher_statesize(tfm));
  384. return 0;
  385. }
  386. static int crypto_lskcipher_import(struct skcipher_request *req, const void *in)
  387. {
  388. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  389. u8 *ivs = skcipher_request_ctx(req);
  390. ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1);
  391. memcpy(ivs + crypto_skcipher_ivsize(tfm), in,
  392. crypto_skcipher_statesize(tfm));
  393. return 0;
  394. }
  395. static int skcipher_noexport(struct skcipher_request *req, void *out)
  396. {
  397. return 0;
  398. }
  399. static int skcipher_noimport(struct skcipher_request *req, const void *in)
  400. {
  401. return 0;
  402. }
  403. int crypto_skcipher_export(struct skcipher_request *req, void *out)
  404. {
  405. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  406. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  407. if (alg->co.base.cra_type != &crypto_skcipher_type)
  408. return crypto_lskcipher_export(req, out);
  409. return alg->export(req, out);
  410. }
  411. EXPORT_SYMBOL_GPL(crypto_skcipher_export);
  412. int crypto_skcipher_import(struct skcipher_request *req, const void *in)
  413. {
  414. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  415. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  416. if (alg->co.base.cra_type != &crypto_skcipher_type)
  417. return crypto_lskcipher_import(req, in);
  418. return alg->import(req, in);
  419. }
  420. EXPORT_SYMBOL_GPL(crypto_skcipher_import);
  421. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  422. {
  423. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  424. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  425. alg->exit(skcipher);
  426. }
  427. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  428. {
  429. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  430. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  431. skcipher_set_needkey(skcipher);
  432. if (tfm->__crt_alg->cra_type != &crypto_skcipher_type) {
  433. unsigned am = crypto_skcipher_alignmask(skcipher);
  434. unsigned reqsize;
  435. reqsize = am & ~(crypto_tfm_ctx_alignment() - 1);
  436. reqsize += crypto_skcipher_ivsize(skcipher);
  437. reqsize += crypto_skcipher_statesize(skcipher);
  438. crypto_skcipher_set_reqsize(skcipher, reqsize);
  439. return crypto_init_lskcipher_ops_sg(tfm);
  440. }
  441. crypto_skcipher_set_reqsize(skcipher, crypto_tfm_alg_reqsize(tfm));
  442. if (alg->exit)
  443. skcipher->base.exit = crypto_skcipher_exit_tfm;
  444. if (alg->init)
  445. return alg->init(skcipher);
  446. return 0;
  447. }
  448. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  449. {
  450. if (alg->cra_type != &crypto_skcipher_type)
  451. return sizeof(struct crypto_lskcipher *);
  452. return crypto_alg_extsize(alg);
  453. }
  454. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  455. {
  456. struct skcipher_instance *skcipher =
  457. container_of(inst, struct skcipher_instance, s.base);
  458. skcipher->free(skcipher);
  459. }
  460. static void __maybe_unused crypto_skcipher_show(struct seq_file *m,
  461. struct crypto_alg *alg)
  462. {
  463. struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg);
  464. seq_printf(m, "type : skcipher\n");
  465. seq_printf(m, "async : %s\n",
  466. str_yes_no(alg->cra_flags & CRYPTO_ALG_ASYNC));
  467. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  468. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  469. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  470. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  471. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  472. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  473. seq_printf(m, "statesize : %u\n", skcipher->statesize);
  474. }
  475. static int __maybe_unused crypto_skcipher_report(
  476. struct sk_buff *skb, struct crypto_alg *alg)
  477. {
  478. struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg);
  479. struct crypto_report_blkcipher rblkcipher;
  480. memset(&rblkcipher, 0, sizeof(rblkcipher));
  481. strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  482. strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  483. rblkcipher.blocksize = alg->cra_blocksize;
  484. rblkcipher.min_keysize = skcipher->min_keysize;
  485. rblkcipher.max_keysize = skcipher->max_keysize;
  486. rblkcipher.ivsize = skcipher->ivsize;
  487. return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  488. sizeof(rblkcipher), &rblkcipher);
  489. }
  490. static const struct crypto_type crypto_skcipher_type = {
  491. .extsize = crypto_skcipher_extsize,
  492. .init_tfm = crypto_skcipher_init_tfm,
  493. .free = crypto_skcipher_free_instance,
  494. #ifdef CONFIG_PROC_FS
  495. .show = crypto_skcipher_show,
  496. #endif
  497. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  498. .report = crypto_skcipher_report,
  499. #endif
  500. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  501. .maskset = CRYPTO_ALG_TYPE_SKCIPHER_MASK,
  502. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  503. .tfmsize = offsetof(struct crypto_skcipher, base),
  504. .algsize = offsetof(struct skcipher_alg, base),
  505. };
  506. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  507. struct crypto_instance *inst,
  508. const char *name, u32 type, u32 mask)
  509. {
  510. spawn->base.frontend = &crypto_skcipher_type;
  511. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  512. }
  513. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  514. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  515. u32 type, u32 mask)
  516. {
  517. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  518. }
  519. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  520. struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
  521. const char *alg_name, u32 type, u32 mask)
  522. {
  523. struct crypto_skcipher *tfm;
  524. /* Only sync algorithms allowed. */
  525. mask |= CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE;
  526. type &= ~(CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE);
  527. tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  528. /*
  529. * Make sure we do not allocate something that might get used with
  530. * an on-stack request: check the request size.
  531. */
  532. if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
  533. MAX_SYNC_SKCIPHER_REQSIZE)) {
  534. crypto_free_skcipher(tfm);
  535. return ERR_PTR(-EINVAL);
  536. }
  537. return (struct crypto_sync_skcipher *)tfm;
  538. }
  539. EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
  540. int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
  541. {
  542. return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
  543. }
  544. EXPORT_SYMBOL_GPL(crypto_has_skcipher);
  545. int skcipher_prepare_alg_common(struct skcipher_alg_common *alg)
  546. {
  547. struct crypto_alg *base = &alg->base;
  548. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  549. alg->statesize > PAGE_SIZE / 2 ||
  550. (alg->ivsize + alg->statesize) > PAGE_SIZE / 2)
  551. return -EINVAL;
  552. if (!alg->chunksize)
  553. alg->chunksize = base->cra_blocksize;
  554. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  555. return 0;
  556. }
  557. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  558. {
  559. struct crypto_alg *base = &alg->base;
  560. int err;
  561. err = skcipher_prepare_alg_common(&alg->co);
  562. if (err)
  563. return err;
  564. if (alg->walksize > PAGE_SIZE / 8)
  565. return -EINVAL;
  566. if (!alg->walksize)
  567. alg->walksize = alg->chunksize;
  568. if (!alg->statesize) {
  569. alg->import = skcipher_noimport;
  570. alg->export = skcipher_noexport;
  571. } else if (!(alg->import && alg->export))
  572. return -EINVAL;
  573. base->cra_type = &crypto_skcipher_type;
  574. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  575. return 0;
  576. }
  577. int crypto_register_skcipher(struct skcipher_alg *alg)
  578. {
  579. struct crypto_alg *base = &alg->base;
  580. int err;
  581. err = skcipher_prepare_alg(alg);
  582. if (err)
  583. return err;
  584. return crypto_register_alg(base);
  585. }
  586. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  587. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  588. {
  589. crypto_unregister_alg(&alg->base);
  590. }
  591. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  592. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  593. {
  594. int i, ret;
  595. for (i = 0; i < count; i++) {
  596. ret = crypto_register_skcipher(&algs[i]);
  597. if (ret) {
  598. crypto_unregister_skciphers(algs, i);
  599. return ret;
  600. }
  601. }
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  605. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  606. {
  607. int i;
  608. for (i = count - 1; i >= 0; --i)
  609. crypto_unregister_skcipher(&algs[i]);
  610. }
  611. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  612. int skcipher_register_instance(struct crypto_template *tmpl,
  613. struct skcipher_instance *inst)
  614. {
  615. int err;
  616. if (WARN_ON(!inst->free))
  617. return -EINVAL;
  618. err = skcipher_prepare_alg(&inst->alg);
  619. if (err)
  620. return err;
  621. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  622. }
  623. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  624. static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
  625. unsigned int keylen)
  626. {
  627. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  628. crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
  629. crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
  630. CRYPTO_TFM_REQ_MASK);
  631. return crypto_cipher_setkey(cipher, key, keylen);
  632. }
  633. static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
  634. {
  635. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  636. struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
  637. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  638. struct crypto_cipher *cipher;
  639. cipher = crypto_spawn_cipher(spawn);
  640. if (IS_ERR(cipher))
  641. return PTR_ERR(cipher);
  642. ctx->cipher = cipher;
  643. return 0;
  644. }
  645. static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
  646. {
  647. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  648. crypto_free_cipher(ctx->cipher);
  649. }
  650. static void skcipher_free_instance_simple(struct skcipher_instance *inst)
  651. {
  652. crypto_drop_cipher(skcipher_instance_ctx(inst));
  653. kfree(inst);
  654. }
  655. /**
  656. * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
  657. *
  658. * Allocate an skcipher_instance for a simple block cipher mode of operation,
  659. * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
  660. * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
  661. * alignmask, and priority are set from the underlying cipher but can be
  662. * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and
  663. * default ->setkey(), ->init(), and ->exit() methods are installed.
  664. *
  665. * @tmpl: the template being instantiated
  666. * @tb: the template parameters
  667. *
  668. * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
  669. * needs to register the instance.
  670. */
  671. struct skcipher_instance *skcipher_alloc_instance_simple(
  672. struct crypto_template *tmpl, struct rtattr **tb)
  673. {
  674. u32 mask;
  675. struct skcipher_instance *inst;
  676. struct crypto_cipher_spawn *spawn;
  677. struct crypto_alg *cipher_alg;
  678. int err;
  679. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
  680. if (err)
  681. return ERR_PTR(err);
  682. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  683. if (!inst)
  684. return ERR_PTR(-ENOMEM);
  685. spawn = skcipher_instance_ctx(inst);
  686. err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
  687. crypto_attr_alg_name(tb[1]), 0, mask);
  688. if (err)
  689. goto err_free_inst;
  690. cipher_alg = crypto_spawn_cipher_alg(spawn);
  691. err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
  692. cipher_alg);
  693. if (err)
  694. goto err_free_inst;
  695. inst->free = skcipher_free_instance_simple;
  696. /* Default algorithm properties, can be overridden */
  697. inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
  698. inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
  699. inst->alg.base.cra_priority = cipher_alg->cra_priority;
  700. inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
  701. inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
  702. inst->alg.ivsize = cipher_alg->cra_blocksize;
  703. /* Use skcipher_ctx_simple by default, can be overridden */
  704. inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
  705. inst->alg.setkey = skcipher_setkey_simple;
  706. inst->alg.init = skcipher_init_tfm_simple;
  707. inst->alg.exit = skcipher_exit_tfm_simple;
  708. return inst;
  709. err_free_inst:
  710. skcipher_free_instance_simple(inst);
  711. return ERR_PTR(err);
  712. }
  713. EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
  714. MODULE_LICENSE("GPL");
  715. MODULE_DESCRIPTION("Symmetric key cipher type");
  716. MODULE_IMPORT_NS("CRYPTO_INTERNAL");