skcipher.c 27 KB

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