skcipher.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. if (WARN_ON_ONCE(in_irq()))
  346. return -EDEADLK;
  347. walk->buffer = NULL;
  348. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  349. int err = skcipher_copy_iv(walk);
  350. if (err)
  351. return err;
  352. }
  353. walk->page = NULL;
  354. walk->nbytes = walk->total;
  355. return skcipher_walk_next(walk);
  356. }
  357. static int skcipher_walk_skcipher(struct skcipher_walk *walk,
  358. struct skcipher_request *req)
  359. {
  360. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  361. walk->total = req->cryptlen;
  362. walk->nbytes = 0;
  363. if (unlikely(!walk->total))
  364. return 0;
  365. scatterwalk_start(&walk->in, req->src);
  366. scatterwalk_start(&walk->out, req->dst);
  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->nbytes = 0;
  407. if (unlikely(!walk->total))
  408. return 0;
  409. walk->flags &= ~SKCIPHER_WALK_PHYS;
  410. scatterwalk_start(&walk->in, req->src);
  411. scatterwalk_start(&walk->out, req->dst);
  412. scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
  413. scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
  414. walk->iv = req->iv;
  415. walk->oiv = req->iv;
  416. if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  417. walk->flags |= SKCIPHER_WALK_SLEEP;
  418. else
  419. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  420. walk->blocksize = crypto_aead_blocksize(tfm);
  421. walk->stride = crypto_aead_chunksize(tfm);
  422. walk->ivsize = crypto_aead_ivsize(tfm);
  423. walk->alignmask = crypto_aead_alignmask(tfm);
  424. err = skcipher_walk_first(walk);
  425. if (atomic)
  426. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  427. return err;
  428. }
  429. int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
  430. bool atomic)
  431. {
  432. walk->total = req->cryptlen;
  433. return skcipher_walk_aead_common(walk, req, atomic);
  434. }
  435. EXPORT_SYMBOL_GPL(skcipher_walk_aead);
  436. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  437. struct aead_request *req, bool atomic)
  438. {
  439. walk->total = req->cryptlen;
  440. return skcipher_walk_aead_common(walk, req, atomic);
  441. }
  442. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  443. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  444. struct aead_request *req, bool atomic)
  445. {
  446. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  447. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  448. return skcipher_walk_aead_common(walk, req, atomic);
  449. }
  450. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  451. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  452. {
  453. if (alg->cra_type == &crypto_blkcipher_type)
  454. return sizeof(struct crypto_blkcipher *);
  455. if (alg->cra_type == &crypto_ablkcipher_type ||
  456. alg->cra_type == &crypto_givcipher_type)
  457. return sizeof(struct crypto_ablkcipher *);
  458. return crypto_alg_extsize(alg);
  459. }
  460. static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
  461. const u8 *key, unsigned int keylen)
  462. {
  463. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  464. struct crypto_blkcipher *blkcipher = *ctx;
  465. int err;
  466. crypto_blkcipher_clear_flags(blkcipher, ~0);
  467. crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
  468. CRYPTO_TFM_REQ_MASK);
  469. err = crypto_blkcipher_setkey(blkcipher, key, keylen);
  470. crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
  471. CRYPTO_TFM_RES_MASK);
  472. return err;
  473. }
  474. static int skcipher_crypt_blkcipher(struct skcipher_request *req,
  475. int (*crypt)(struct blkcipher_desc *,
  476. struct scatterlist *,
  477. struct scatterlist *,
  478. unsigned int))
  479. {
  480. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  481. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  482. struct blkcipher_desc desc = {
  483. .tfm = *ctx,
  484. .info = req->iv,
  485. .flags = req->base.flags,
  486. };
  487. return crypt(&desc, req->dst, req->src, req->cryptlen);
  488. }
  489. static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
  490. {
  491. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  492. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  493. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  494. return skcipher_crypt_blkcipher(req, alg->encrypt);
  495. }
  496. static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
  497. {
  498. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  499. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  500. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  501. return skcipher_crypt_blkcipher(req, alg->decrypt);
  502. }
  503. static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  504. {
  505. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  506. crypto_free_blkcipher(*ctx);
  507. }
  508. static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  509. {
  510. struct crypto_alg *calg = tfm->__crt_alg;
  511. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  512. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  513. struct crypto_blkcipher *blkcipher;
  514. struct crypto_tfm *btfm;
  515. if (!crypto_mod_get(calg))
  516. return -EAGAIN;
  517. btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
  518. CRYPTO_ALG_TYPE_MASK);
  519. if (IS_ERR(btfm)) {
  520. crypto_mod_put(calg);
  521. return PTR_ERR(btfm);
  522. }
  523. blkcipher = __crypto_blkcipher_cast(btfm);
  524. *ctx = blkcipher;
  525. tfm->exit = crypto_exit_skcipher_ops_blkcipher;
  526. skcipher->setkey = skcipher_setkey_blkcipher;
  527. skcipher->encrypt = skcipher_encrypt_blkcipher;
  528. skcipher->decrypt = skcipher_decrypt_blkcipher;
  529. skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
  530. skcipher->keysize = calg->cra_blkcipher.max_keysize;
  531. return 0;
  532. }
  533. static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
  534. const u8 *key, unsigned int keylen)
  535. {
  536. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  537. struct crypto_ablkcipher *ablkcipher = *ctx;
  538. int err;
  539. crypto_ablkcipher_clear_flags(ablkcipher, ~0);
  540. crypto_ablkcipher_set_flags(ablkcipher,
  541. crypto_skcipher_get_flags(tfm) &
  542. CRYPTO_TFM_REQ_MASK);
  543. err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
  544. crypto_skcipher_set_flags(tfm,
  545. crypto_ablkcipher_get_flags(ablkcipher) &
  546. CRYPTO_TFM_RES_MASK);
  547. return err;
  548. }
  549. static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
  550. int (*crypt)(struct ablkcipher_request *))
  551. {
  552. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  553. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  554. struct ablkcipher_request *subreq = skcipher_request_ctx(req);
  555. ablkcipher_request_set_tfm(subreq, *ctx);
  556. ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
  557. req->base.complete, req->base.data);
  558. ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  559. req->iv);
  560. return crypt(subreq);
  561. }
  562. static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
  563. {
  564. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  565. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  566. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  567. return skcipher_crypt_ablkcipher(req, alg->encrypt);
  568. }
  569. static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
  570. {
  571. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  572. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  573. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  574. return skcipher_crypt_ablkcipher(req, alg->decrypt);
  575. }
  576. static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  577. {
  578. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  579. crypto_free_ablkcipher(*ctx);
  580. }
  581. static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  582. {
  583. struct crypto_alg *calg = tfm->__crt_alg;
  584. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  585. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  586. struct crypto_ablkcipher *ablkcipher;
  587. struct crypto_tfm *abtfm;
  588. if (!crypto_mod_get(calg))
  589. return -EAGAIN;
  590. abtfm = __crypto_alloc_tfm(calg, 0, 0);
  591. if (IS_ERR(abtfm)) {
  592. crypto_mod_put(calg);
  593. return PTR_ERR(abtfm);
  594. }
  595. ablkcipher = __crypto_ablkcipher_cast(abtfm);
  596. *ctx = ablkcipher;
  597. tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
  598. skcipher->setkey = skcipher_setkey_ablkcipher;
  599. skcipher->encrypt = skcipher_encrypt_ablkcipher;
  600. skcipher->decrypt = skcipher_decrypt_ablkcipher;
  601. skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  602. skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
  603. sizeof(struct ablkcipher_request);
  604. skcipher->keysize = calg->cra_ablkcipher.max_keysize;
  605. return 0;
  606. }
  607. static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  608. const u8 *key, unsigned int keylen)
  609. {
  610. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  611. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  612. u8 *buffer, *alignbuffer;
  613. unsigned long absize;
  614. int ret;
  615. absize = keylen + alignmask;
  616. buffer = kmalloc(absize, GFP_ATOMIC);
  617. if (!buffer)
  618. return -ENOMEM;
  619. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  620. memcpy(alignbuffer, key, keylen);
  621. ret = cipher->setkey(tfm, alignbuffer, keylen);
  622. kzfree(buffer);
  623. return ret;
  624. }
  625. static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  626. unsigned int keylen)
  627. {
  628. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  629. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  630. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  631. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  632. return -EINVAL;
  633. }
  634. if ((unsigned long)key & alignmask)
  635. return skcipher_setkey_unaligned(tfm, key, keylen);
  636. return cipher->setkey(tfm, key, keylen);
  637. }
  638. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  639. {
  640. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  641. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  642. alg->exit(skcipher);
  643. }
  644. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  645. {
  646. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  647. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  648. if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  649. return crypto_init_skcipher_ops_blkcipher(tfm);
  650. if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  651. tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  652. return crypto_init_skcipher_ops_ablkcipher(tfm);
  653. skcipher->setkey = skcipher_setkey;
  654. skcipher->encrypt = alg->encrypt;
  655. skcipher->decrypt = alg->decrypt;
  656. skcipher->ivsize = alg->ivsize;
  657. skcipher->keysize = alg->max_keysize;
  658. if (alg->exit)
  659. skcipher->base.exit = crypto_skcipher_exit_tfm;
  660. if (alg->init)
  661. return alg->init(skcipher);
  662. return 0;
  663. }
  664. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  665. {
  666. struct skcipher_instance *skcipher =
  667. container_of(inst, struct skcipher_instance, s.base);
  668. skcipher->free(skcipher);
  669. }
  670. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  671. __maybe_unused;
  672. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  673. {
  674. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  675. base);
  676. seq_printf(m, "type : skcipher\n");
  677. seq_printf(m, "async : %s\n",
  678. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  679. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  680. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  681. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  682. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  683. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  684. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  685. }
  686. #ifdef CONFIG_NET
  687. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  688. {
  689. struct crypto_report_blkcipher rblkcipher;
  690. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  691. base);
  692. strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  693. strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  694. rblkcipher.blocksize = alg->cra_blocksize;
  695. rblkcipher.min_keysize = skcipher->min_keysize;
  696. rblkcipher.max_keysize = skcipher->max_keysize;
  697. rblkcipher.ivsize = skcipher->ivsize;
  698. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  699. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  700. goto nla_put_failure;
  701. return 0;
  702. nla_put_failure:
  703. return -EMSGSIZE;
  704. }
  705. #else
  706. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  707. {
  708. return -ENOSYS;
  709. }
  710. #endif
  711. static const struct crypto_type crypto_skcipher_type2 = {
  712. .extsize = crypto_skcipher_extsize,
  713. .init_tfm = crypto_skcipher_init_tfm,
  714. .free = crypto_skcipher_free_instance,
  715. #ifdef CONFIG_PROC_FS
  716. .show = crypto_skcipher_show,
  717. #endif
  718. .report = crypto_skcipher_report,
  719. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  720. .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  721. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  722. .tfmsize = offsetof(struct crypto_skcipher, base),
  723. };
  724. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  725. const char *name, u32 type, u32 mask)
  726. {
  727. spawn->base.frontend = &crypto_skcipher_type2;
  728. return crypto_grab_spawn(&spawn->base, name, type, mask);
  729. }
  730. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  731. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  732. u32 type, u32 mask)
  733. {
  734. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  735. }
  736. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  737. int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  738. {
  739. return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  740. type, mask);
  741. }
  742. EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  743. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  744. {
  745. struct crypto_alg *base = &alg->base;
  746. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  747. alg->walksize > PAGE_SIZE / 8)
  748. return -EINVAL;
  749. if (!alg->chunksize)
  750. alg->chunksize = base->cra_blocksize;
  751. if (!alg->walksize)
  752. alg->walksize = alg->chunksize;
  753. base->cra_type = &crypto_skcipher_type2;
  754. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  755. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  756. return 0;
  757. }
  758. int crypto_register_skcipher(struct skcipher_alg *alg)
  759. {
  760. struct crypto_alg *base = &alg->base;
  761. int err;
  762. err = skcipher_prepare_alg(alg);
  763. if (err)
  764. return err;
  765. return crypto_register_alg(base);
  766. }
  767. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  768. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  769. {
  770. crypto_unregister_alg(&alg->base);
  771. }
  772. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  773. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  774. {
  775. int i, ret;
  776. for (i = 0; i < count; i++) {
  777. ret = crypto_register_skcipher(&algs[i]);
  778. if (ret)
  779. goto err;
  780. }
  781. return 0;
  782. err:
  783. for (--i; i >= 0; --i)
  784. crypto_unregister_skcipher(&algs[i]);
  785. return ret;
  786. }
  787. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  788. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  789. {
  790. int i;
  791. for (i = count - 1; i >= 0; --i)
  792. crypto_unregister_skcipher(&algs[i]);
  793. }
  794. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  795. int skcipher_register_instance(struct crypto_template *tmpl,
  796. struct skcipher_instance *inst)
  797. {
  798. int err;
  799. err = skcipher_prepare_alg(&inst->alg);
  800. if (err)
  801. return err;
  802. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  803. }
  804. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  805. MODULE_LICENSE("GPL");
  806. MODULE_DESCRIPTION("Symmetric key cipher type");