skcipher.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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/aead.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/bug.h>
  20. #include <linux/cryptouser.h>
  21. #include <linux/compiler.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/seq_file.h>
  26. #include <net/netlink.h>
  27. #include "internal.h"
  28. enum {
  29. SKCIPHER_WALK_PHYS = 1 << 0,
  30. SKCIPHER_WALK_SLOW = 1 << 1,
  31. SKCIPHER_WALK_COPY = 1 << 2,
  32. SKCIPHER_WALK_DIFF = 1 << 3,
  33. SKCIPHER_WALK_SLEEP = 1 << 4,
  34. };
  35. struct skcipher_walk_buffer {
  36. struct list_head entry;
  37. struct scatter_walk dst;
  38. unsigned int len;
  39. u8 *data;
  40. u8 buffer[];
  41. };
  42. static int skcipher_walk_next(struct skcipher_walk *walk);
  43. static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
  44. {
  45. if (PageHighMem(scatterwalk_page(walk)))
  46. kunmap_atomic(vaddr);
  47. }
  48. static inline void *skcipher_map(struct scatter_walk *walk)
  49. {
  50. struct page *page = scatterwalk_page(walk);
  51. return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
  52. offset_in_page(walk->offset);
  53. }
  54. static inline void skcipher_map_src(struct skcipher_walk *walk)
  55. {
  56. walk->src.virt.addr = skcipher_map(&walk->in);
  57. }
  58. static inline void skcipher_map_dst(struct skcipher_walk *walk)
  59. {
  60. walk->dst.virt.addr = skcipher_map(&walk->out);
  61. }
  62. static inline void skcipher_unmap_src(struct skcipher_walk *walk)
  63. {
  64. skcipher_unmap(&walk->in, walk->src.virt.addr);
  65. }
  66. static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
  67. {
  68. skcipher_unmap(&walk->out, walk->dst.virt.addr);
  69. }
  70. static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
  71. {
  72. return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  73. }
  74. /* Get a spot of the specified length that does not straddle a page.
  75. * The caller needs to ensure that there is enough space for this operation.
  76. */
  77. static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
  78. {
  79. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  80. return max(start, end_page);
  81. }
  82. static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
  83. {
  84. u8 *addr;
  85. addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  86. addr = skcipher_get_spot(addr, bsize);
  87. scatterwalk_copychunks(addr, &walk->out, bsize,
  88. (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
  89. return 0;
  90. }
  91. int skcipher_walk_done(struct skcipher_walk *walk, int err)
  92. {
  93. unsigned int n = walk->nbytes - err;
  94. unsigned int nbytes;
  95. nbytes = walk->total - n;
  96. if (unlikely(err < 0)) {
  97. nbytes = 0;
  98. n = 0;
  99. } else if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
  100. SKCIPHER_WALK_SLOW |
  101. SKCIPHER_WALK_COPY |
  102. SKCIPHER_WALK_DIFF)))) {
  103. unmap_src:
  104. skcipher_unmap_src(walk);
  105. } else if (walk->flags & SKCIPHER_WALK_DIFF) {
  106. skcipher_unmap_dst(walk);
  107. goto unmap_src;
  108. } else if (walk->flags & SKCIPHER_WALK_COPY) {
  109. skcipher_map_dst(walk);
  110. memcpy(walk->dst.virt.addr, walk->page, n);
  111. skcipher_unmap_dst(walk);
  112. } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
  113. if (WARN_ON(err)) {
  114. err = -EINVAL;
  115. nbytes = 0;
  116. } else
  117. n = skcipher_done_slow(walk, n);
  118. }
  119. if (err > 0)
  120. err = 0;
  121. walk->total = nbytes;
  122. walk->nbytes = nbytes;
  123. scatterwalk_advance(&walk->in, n);
  124. scatterwalk_advance(&walk->out, n);
  125. scatterwalk_done(&walk->in, 0, nbytes);
  126. scatterwalk_done(&walk->out, 1, nbytes);
  127. if (nbytes) {
  128. crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
  129. CRYPTO_TFM_REQ_MAY_SLEEP : 0);
  130. return skcipher_walk_next(walk);
  131. }
  132. /* Short-circuit for the common/fast path. */
  133. if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
  134. goto out;
  135. if (walk->flags & SKCIPHER_WALK_PHYS)
  136. goto out;
  137. if (walk->iv != walk->oiv)
  138. memcpy(walk->oiv, walk->iv, walk->ivsize);
  139. if (walk->buffer != walk->page)
  140. kfree(walk->buffer);
  141. if (walk->page)
  142. free_page((unsigned long)walk->page);
  143. out:
  144. return err;
  145. }
  146. EXPORT_SYMBOL_GPL(skcipher_walk_done);
  147. void skcipher_walk_complete(struct skcipher_walk *walk, int err)
  148. {
  149. struct skcipher_walk_buffer *p, *tmp;
  150. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  151. u8 *data;
  152. if (err)
  153. goto done;
  154. data = p->data;
  155. if (!data) {
  156. data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
  157. data = skcipher_get_spot(data, walk->stride);
  158. }
  159. scatterwalk_copychunks(data, &p->dst, p->len, 1);
  160. if (offset_in_page(p->data) + p->len + walk->stride >
  161. PAGE_SIZE)
  162. free_page((unsigned long)p->data);
  163. done:
  164. list_del(&p->entry);
  165. kfree(p);
  166. }
  167. if (!err && walk->iv != walk->oiv)
  168. memcpy(walk->oiv, walk->iv, walk->ivsize);
  169. if (walk->buffer != walk->page)
  170. kfree(walk->buffer);
  171. if (walk->page)
  172. free_page((unsigned long)walk->page);
  173. }
  174. EXPORT_SYMBOL_GPL(skcipher_walk_complete);
  175. static void skcipher_queue_write(struct skcipher_walk *walk,
  176. struct skcipher_walk_buffer *p)
  177. {
  178. p->dst = walk->out;
  179. list_add_tail(&p->entry, &walk->buffers);
  180. }
  181. static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
  182. {
  183. bool phys = walk->flags & SKCIPHER_WALK_PHYS;
  184. unsigned alignmask = walk->alignmask;
  185. struct skcipher_walk_buffer *p;
  186. unsigned a;
  187. unsigned n;
  188. u8 *buffer;
  189. void *v;
  190. if (!phys) {
  191. if (!walk->buffer)
  192. walk->buffer = walk->page;
  193. buffer = walk->buffer;
  194. if (buffer)
  195. goto ok;
  196. }
  197. /* Start with the minimum alignment of kmalloc. */
  198. a = crypto_tfm_ctx_alignment() - 1;
  199. n = bsize;
  200. if (phys) {
  201. /* Calculate the minimum alignment of p->buffer. */
  202. a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
  203. n += sizeof(*p);
  204. }
  205. /* Minimum size to align p->buffer by alignmask. */
  206. n += alignmask & ~a;
  207. /* Minimum size to ensure p->buffer does not straddle a page. */
  208. n += (bsize - 1) & ~(alignmask | a);
  209. v = kzalloc(n, skcipher_walk_gfp(walk));
  210. if (!v)
  211. return skcipher_walk_done(walk, -ENOMEM);
  212. if (phys) {
  213. p = v;
  214. p->len = bsize;
  215. skcipher_queue_write(walk, p);
  216. buffer = p->buffer;
  217. } else {
  218. walk->buffer = v;
  219. buffer = v;
  220. }
  221. ok:
  222. walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
  223. walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
  224. walk->src.virt.addr = walk->dst.virt.addr;
  225. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  226. walk->nbytes = bsize;
  227. walk->flags |= SKCIPHER_WALK_SLOW;
  228. return 0;
  229. }
  230. static int skcipher_next_copy(struct skcipher_walk *walk)
  231. {
  232. struct skcipher_walk_buffer *p;
  233. u8 *tmp = walk->page;
  234. skcipher_map_src(walk);
  235. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  236. skcipher_unmap_src(walk);
  237. walk->src.virt.addr = tmp;
  238. walk->dst.virt.addr = tmp;
  239. if (!(walk->flags & SKCIPHER_WALK_PHYS))
  240. return 0;
  241. p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
  242. if (!p)
  243. return -ENOMEM;
  244. p->data = walk->page;
  245. p->len = walk->nbytes;
  246. skcipher_queue_write(walk, p);
  247. if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
  248. PAGE_SIZE)
  249. walk->page = NULL;
  250. else
  251. walk->page += walk->nbytes;
  252. return 0;
  253. }
  254. static int skcipher_next_fast(struct skcipher_walk *walk)
  255. {
  256. unsigned long diff;
  257. walk->src.phys.page = scatterwalk_page(&walk->in);
  258. walk->src.phys.offset = offset_in_page(walk->in.offset);
  259. walk->dst.phys.page = scatterwalk_page(&walk->out);
  260. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  261. if (walk->flags & SKCIPHER_WALK_PHYS)
  262. return 0;
  263. diff = walk->src.phys.offset - walk->dst.phys.offset;
  264. diff |= walk->src.virt.page - walk->dst.virt.page;
  265. skcipher_map_src(walk);
  266. walk->dst.virt.addr = walk->src.virt.addr;
  267. if (diff) {
  268. walk->flags |= SKCIPHER_WALK_DIFF;
  269. skcipher_map_dst(walk);
  270. }
  271. return 0;
  272. }
  273. static int skcipher_walk_next(struct skcipher_walk *walk)
  274. {
  275. unsigned int bsize;
  276. unsigned int n;
  277. int err;
  278. walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
  279. SKCIPHER_WALK_DIFF);
  280. n = walk->total;
  281. bsize = min(walk->stride, max(n, walk->blocksize));
  282. n = scatterwalk_clamp(&walk->in, n);
  283. n = scatterwalk_clamp(&walk->out, n);
  284. if (unlikely(n < bsize)) {
  285. if (unlikely(walk->total < walk->blocksize))
  286. return skcipher_walk_done(walk, -EINVAL);
  287. slow_path:
  288. err = skcipher_next_slow(walk, bsize);
  289. goto set_phys_lowmem;
  290. }
  291. if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
  292. if (!walk->page) {
  293. gfp_t gfp = skcipher_walk_gfp(walk);
  294. walk->page = (void *)__get_free_page(gfp);
  295. if (!walk->page)
  296. goto slow_path;
  297. }
  298. walk->nbytes = min_t(unsigned, n,
  299. PAGE_SIZE - offset_in_page(walk->page));
  300. walk->flags |= SKCIPHER_WALK_COPY;
  301. err = skcipher_next_copy(walk);
  302. goto set_phys_lowmem;
  303. }
  304. walk->nbytes = n;
  305. return skcipher_next_fast(walk);
  306. set_phys_lowmem:
  307. if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
  308. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  309. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  310. walk->src.phys.offset &= PAGE_SIZE - 1;
  311. walk->dst.phys.offset &= PAGE_SIZE - 1;
  312. }
  313. return err;
  314. }
  315. EXPORT_SYMBOL_GPL(skcipher_walk_next);
  316. static int skcipher_copy_iv(struct skcipher_walk *walk)
  317. {
  318. unsigned a = crypto_tfm_ctx_alignment() - 1;
  319. unsigned alignmask = walk->alignmask;
  320. unsigned ivsize = walk->ivsize;
  321. unsigned bs = walk->stride;
  322. unsigned aligned_bs;
  323. unsigned size;
  324. u8 *iv;
  325. aligned_bs = ALIGN(bs, alignmask);
  326. /* Minimum size to align buffer by alignmask. */
  327. size = alignmask & ~a;
  328. if (walk->flags & SKCIPHER_WALK_PHYS)
  329. size += ivsize;
  330. else {
  331. size += aligned_bs + ivsize;
  332. /* Minimum size to ensure buffer does not straddle a page. */
  333. size += (bs - 1) & ~(alignmask | a);
  334. }
  335. walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
  336. if (!walk->buffer)
  337. return -ENOMEM;
  338. iv = PTR_ALIGN(walk->buffer, alignmask + 1);
  339. iv = skcipher_get_spot(iv, bs) + aligned_bs;
  340. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  341. return 0;
  342. }
  343. static int skcipher_walk_first(struct skcipher_walk *walk)
  344. {
  345. walk->nbytes = 0;
  346. if (WARN_ON_ONCE(in_irq()))
  347. return -EDEADLK;
  348. if (unlikely(!walk->total))
  349. return 0;
  350. walk->buffer = NULL;
  351. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  352. int err = skcipher_copy_iv(walk);
  353. if (err)
  354. return err;
  355. }
  356. walk->page = NULL;
  357. walk->nbytes = walk->total;
  358. return skcipher_walk_next(walk);
  359. }
  360. static int skcipher_walk_skcipher(struct skcipher_walk *walk,
  361. struct skcipher_request *req)
  362. {
  363. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  364. scatterwalk_start(&walk->in, req->src);
  365. scatterwalk_start(&walk->out, req->dst);
  366. walk->total = req->cryptlen;
  367. walk->iv = req->iv;
  368. walk->oiv = req->iv;
  369. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  370. walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  371. SKCIPHER_WALK_SLEEP : 0;
  372. walk->blocksize = crypto_skcipher_blocksize(tfm);
  373. walk->stride = crypto_skcipher_walksize(tfm);
  374. walk->ivsize = crypto_skcipher_ivsize(tfm);
  375. walk->alignmask = crypto_skcipher_alignmask(tfm);
  376. return skcipher_walk_first(walk);
  377. }
  378. int skcipher_walk_virt(struct skcipher_walk *walk,
  379. struct skcipher_request *req, bool atomic)
  380. {
  381. int err;
  382. walk->flags &= ~SKCIPHER_WALK_PHYS;
  383. err = skcipher_walk_skcipher(walk, req);
  384. walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
  385. return err;
  386. }
  387. EXPORT_SYMBOL_GPL(skcipher_walk_virt);
  388. void skcipher_walk_atomise(struct skcipher_walk *walk)
  389. {
  390. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  391. }
  392. EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
  393. int skcipher_walk_async(struct skcipher_walk *walk,
  394. struct skcipher_request *req)
  395. {
  396. walk->flags |= SKCIPHER_WALK_PHYS;
  397. INIT_LIST_HEAD(&walk->buffers);
  398. return skcipher_walk_skcipher(walk, req);
  399. }
  400. EXPORT_SYMBOL_GPL(skcipher_walk_async);
  401. static int skcipher_walk_aead_common(struct skcipher_walk *walk,
  402. struct aead_request *req, bool atomic)
  403. {
  404. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  405. int err;
  406. walk->flags &= ~SKCIPHER_WALK_PHYS;
  407. scatterwalk_start(&walk->in, req->src);
  408. scatterwalk_start(&walk->out, req->dst);
  409. scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
  410. scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
  411. walk->iv = req->iv;
  412. walk->oiv = req->iv;
  413. if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  414. walk->flags |= SKCIPHER_WALK_SLEEP;
  415. else
  416. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  417. walk->blocksize = crypto_aead_blocksize(tfm);
  418. walk->stride = crypto_aead_chunksize(tfm);
  419. walk->ivsize = crypto_aead_ivsize(tfm);
  420. walk->alignmask = crypto_aead_alignmask(tfm);
  421. err = skcipher_walk_first(walk);
  422. if (atomic)
  423. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  424. return err;
  425. }
  426. int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
  427. bool atomic)
  428. {
  429. walk->total = req->cryptlen;
  430. return skcipher_walk_aead_common(walk, req, atomic);
  431. }
  432. EXPORT_SYMBOL_GPL(skcipher_walk_aead);
  433. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  434. struct aead_request *req, bool atomic)
  435. {
  436. walk->total = req->cryptlen;
  437. return skcipher_walk_aead_common(walk, req, atomic);
  438. }
  439. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  440. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  441. struct aead_request *req, bool atomic)
  442. {
  443. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  444. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  445. return skcipher_walk_aead_common(walk, req, atomic);
  446. }
  447. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  448. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  449. {
  450. if (alg->cra_type == &crypto_blkcipher_type)
  451. return sizeof(struct crypto_blkcipher *);
  452. if (alg->cra_type == &crypto_ablkcipher_type ||
  453. alg->cra_type == &crypto_givcipher_type)
  454. return sizeof(struct crypto_ablkcipher *);
  455. return crypto_alg_extsize(alg);
  456. }
  457. static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
  458. const u8 *key, unsigned int keylen)
  459. {
  460. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  461. struct crypto_blkcipher *blkcipher = *ctx;
  462. int err;
  463. crypto_blkcipher_clear_flags(blkcipher, ~0);
  464. crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
  465. CRYPTO_TFM_REQ_MASK);
  466. err = crypto_blkcipher_setkey(blkcipher, key, keylen);
  467. crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
  468. CRYPTO_TFM_RES_MASK);
  469. return err;
  470. }
  471. static int skcipher_crypt_blkcipher(struct skcipher_request *req,
  472. int (*crypt)(struct blkcipher_desc *,
  473. struct scatterlist *,
  474. struct scatterlist *,
  475. unsigned int))
  476. {
  477. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  478. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  479. struct blkcipher_desc desc = {
  480. .tfm = *ctx,
  481. .info = req->iv,
  482. .flags = req->base.flags,
  483. };
  484. return crypt(&desc, req->dst, req->src, req->cryptlen);
  485. }
  486. static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
  487. {
  488. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  489. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  490. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  491. return skcipher_crypt_blkcipher(req, alg->encrypt);
  492. }
  493. static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
  494. {
  495. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  496. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  497. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  498. return skcipher_crypt_blkcipher(req, alg->decrypt);
  499. }
  500. static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  501. {
  502. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  503. crypto_free_blkcipher(*ctx);
  504. }
  505. static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  506. {
  507. struct crypto_alg *calg = tfm->__crt_alg;
  508. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  509. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  510. struct crypto_blkcipher *blkcipher;
  511. struct crypto_tfm *btfm;
  512. if (!crypto_mod_get(calg))
  513. return -EAGAIN;
  514. btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
  515. CRYPTO_ALG_TYPE_MASK);
  516. if (IS_ERR(btfm)) {
  517. crypto_mod_put(calg);
  518. return PTR_ERR(btfm);
  519. }
  520. blkcipher = __crypto_blkcipher_cast(btfm);
  521. *ctx = blkcipher;
  522. tfm->exit = crypto_exit_skcipher_ops_blkcipher;
  523. skcipher->setkey = skcipher_setkey_blkcipher;
  524. skcipher->encrypt = skcipher_encrypt_blkcipher;
  525. skcipher->decrypt = skcipher_decrypt_blkcipher;
  526. skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
  527. skcipher->keysize = calg->cra_blkcipher.max_keysize;
  528. return 0;
  529. }
  530. static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
  531. const u8 *key, unsigned int keylen)
  532. {
  533. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  534. struct crypto_ablkcipher *ablkcipher = *ctx;
  535. int err;
  536. crypto_ablkcipher_clear_flags(ablkcipher, ~0);
  537. crypto_ablkcipher_set_flags(ablkcipher,
  538. crypto_skcipher_get_flags(tfm) &
  539. CRYPTO_TFM_REQ_MASK);
  540. err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
  541. crypto_skcipher_set_flags(tfm,
  542. crypto_ablkcipher_get_flags(ablkcipher) &
  543. CRYPTO_TFM_RES_MASK);
  544. return err;
  545. }
  546. static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
  547. int (*crypt)(struct ablkcipher_request *))
  548. {
  549. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  550. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  551. struct ablkcipher_request *subreq = skcipher_request_ctx(req);
  552. ablkcipher_request_set_tfm(subreq, *ctx);
  553. ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
  554. req->base.complete, req->base.data);
  555. ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  556. req->iv);
  557. return crypt(subreq);
  558. }
  559. static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
  560. {
  561. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  562. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  563. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  564. return skcipher_crypt_ablkcipher(req, alg->encrypt);
  565. }
  566. static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
  567. {
  568. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  569. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  570. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  571. return skcipher_crypt_ablkcipher(req, alg->decrypt);
  572. }
  573. static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  574. {
  575. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  576. crypto_free_ablkcipher(*ctx);
  577. }
  578. static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  579. {
  580. struct crypto_alg *calg = tfm->__crt_alg;
  581. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  582. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  583. struct crypto_ablkcipher *ablkcipher;
  584. struct crypto_tfm *abtfm;
  585. if (!crypto_mod_get(calg))
  586. return -EAGAIN;
  587. abtfm = __crypto_alloc_tfm(calg, 0, 0);
  588. if (IS_ERR(abtfm)) {
  589. crypto_mod_put(calg);
  590. return PTR_ERR(abtfm);
  591. }
  592. ablkcipher = __crypto_ablkcipher_cast(abtfm);
  593. *ctx = ablkcipher;
  594. tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
  595. skcipher->setkey = skcipher_setkey_ablkcipher;
  596. skcipher->encrypt = skcipher_encrypt_ablkcipher;
  597. skcipher->decrypt = skcipher_decrypt_ablkcipher;
  598. skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  599. skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
  600. sizeof(struct ablkcipher_request);
  601. skcipher->keysize = calg->cra_ablkcipher.max_keysize;
  602. return 0;
  603. }
  604. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  605. {
  606. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  607. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  608. alg->exit(skcipher);
  609. }
  610. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  611. {
  612. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  613. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  614. if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  615. return crypto_init_skcipher_ops_blkcipher(tfm);
  616. if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  617. tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  618. return crypto_init_skcipher_ops_ablkcipher(tfm);
  619. skcipher->setkey = alg->setkey;
  620. skcipher->encrypt = alg->encrypt;
  621. skcipher->decrypt = alg->decrypt;
  622. skcipher->ivsize = alg->ivsize;
  623. skcipher->keysize = alg->max_keysize;
  624. if (alg->exit)
  625. skcipher->base.exit = crypto_skcipher_exit_tfm;
  626. if (alg->init)
  627. return alg->init(skcipher);
  628. return 0;
  629. }
  630. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  631. {
  632. struct skcipher_instance *skcipher =
  633. container_of(inst, struct skcipher_instance, s.base);
  634. skcipher->free(skcipher);
  635. }
  636. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  637. __maybe_unused;
  638. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  639. {
  640. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  641. base);
  642. seq_printf(m, "type : skcipher\n");
  643. seq_printf(m, "async : %s\n",
  644. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  645. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  646. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  647. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  648. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  649. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  650. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  651. }
  652. #ifdef CONFIG_NET
  653. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  654. {
  655. struct crypto_report_blkcipher rblkcipher;
  656. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  657. base);
  658. strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  659. strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  660. rblkcipher.blocksize = alg->cra_blocksize;
  661. rblkcipher.min_keysize = skcipher->min_keysize;
  662. rblkcipher.max_keysize = skcipher->max_keysize;
  663. rblkcipher.ivsize = skcipher->ivsize;
  664. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  665. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  666. goto nla_put_failure;
  667. return 0;
  668. nla_put_failure:
  669. return -EMSGSIZE;
  670. }
  671. #else
  672. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  673. {
  674. return -ENOSYS;
  675. }
  676. #endif
  677. static const struct crypto_type crypto_skcipher_type2 = {
  678. .extsize = crypto_skcipher_extsize,
  679. .init_tfm = crypto_skcipher_init_tfm,
  680. .free = crypto_skcipher_free_instance,
  681. #ifdef CONFIG_PROC_FS
  682. .show = crypto_skcipher_show,
  683. #endif
  684. .report = crypto_skcipher_report,
  685. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  686. .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  687. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  688. .tfmsize = offsetof(struct crypto_skcipher, base),
  689. };
  690. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  691. const char *name, u32 type, u32 mask)
  692. {
  693. spawn->base.frontend = &crypto_skcipher_type2;
  694. return crypto_grab_spawn(&spawn->base, name, type, mask);
  695. }
  696. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  697. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  698. u32 type, u32 mask)
  699. {
  700. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  701. }
  702. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  703. int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  704. {
  705. return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  706. type, mask);
  707. }
  708. EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  709. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  710. {
  711. struct crypto_alg *base = &alg->base;
  712. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  713. alg->walksize > PAGE_SIZE / 8)
  714. return -EINVAL;
  715. if (!alg->chunksize)
  716. alg->chunksize = base->cra_blocksize;
  717. if (!alg->walksize)
  718. alg->walksize = alg->chunksize;
  719. base->cra_type = &crypto_skcipher_type2;
  720. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  721. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  722. return 0;
  723. }
  724. int crypto_register_skcipher(struct skcipher_alg *alg)
  725. {
  726. struct crypto_alg *base = &alg->base;
  727. int err;
  728. err = skcipher_prepare_alg(alg);
  729. if (err)
  730. return err;
  731. return crypto_register_alg(base);
  732. }
  733. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  734. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  735. {
  736. crypto_unregister_alg(&alg->base);
  737. }
  738. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  739. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  740. {
  741. int i, ret;
  742. for (i = 0; i < count; i++) {
  743. ret = crypto_register_skcipher(&algs[i]);
  744. if (ret)
  745. goto err;
  746. }
  747. return 0;
  748. err:
  749. for (--i; i >= 0; --i)
  750. crypto_unregister_skcipher(&algs[i]);
  751. return ret;
  752. }
  753. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  754. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  755. {
  756. int i;
  757. for (i = count - 1; i >= 0; --i)
  758. crypto_unregister_skcipher(&algs[i]);
  759. }
  760. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  761. int skcipher_register_instance(struct crypto_template *tmpl,
  762. struct skcipher_instance *inst)
  763. {
  764. int err;
  765. err = skcipher_prepare_alg(&inst->alg);
  766. if (err)
  767. return err;
  768. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  769. }
  770. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  771. MODULE_LICENSE("GPL");
  772. MODULE_DESCRIPTION("Symmetric key cipher type");