skcipher.c 25 KB

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