skcipher.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Symmetric key cipher operations.
  4. *
  5. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  6. * multiple page boundaries by using temporary blocks. In user context,
  7. * the kernel is given a chance to schedule us once per page.
  8. *
  9. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  10. */
  11. #include <crypto/internal/aead.h>
  12. #include <crypto/internal/cipher.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/bug.h>
  16. #include <linux/cryptouser.h>
  17. #include <linux/err.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <net/netlink.h>
  26. #include "skcipher.h"
  27. #define CRYPTO_ALG_TYPE_SKCIPHER_MASK 0x0000000e
  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 const struct crypto_type crypto_skcipher_type;
  43. static int skcipher_walk_next(struct skcipher_walk *walk);
  44. static inline void skcipher_map_src(struct skcipher_walk *walk)
  45. {
  46. walk->src.virt.addr = scatterwalk_map(&walk->in);
  47. }
  48. static inline void skcipher_map_dst(struct skcipher_walk *walk)
  49. {
  50. walk->dst.virt.addr = scatterwalk_map(&walk->out);
  51. }
  52. static inline void skcipher_unmap_src(struct skcipher_walk *walk)
  53. {
  54. scatterwalk_unmap(walk->src.virt.addr);
  55. }
  56. static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
  57. {
  58. scatterwalk_unmap(walk->dst.virt.addr);
  59. }
  60. static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
  61. {
  62. return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  63. }
  64. /* Get a spot of the specified length that does not straddle a page.
  65. * The caller needs to ensure that there is enough space for this operation.
  66. */
  67. static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
  68. {
  69. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  70. return max(start, end_page);
  71. }
  72. static inline struct skcipher_alg *__crypto_skcipher_alg(
  73. struct crypto_alg *alg)
  74. {
  75. return container_of(alg, struct skcipher_alg, base);
  76. }
  77. static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
  78. {
  79. u8 *addr;
  80. addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  81. addr = skcipher_get_spot(addr, bsize);
  82. scatterwalk_copychunks(addr, &walk->out, bsize,
  83. (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
  84. return 0;
  85. }
  86. int skcipher_walk_done(struct skcipher_walk *walk, int err)
  87. {
  88. unsigned int n = walk->nbytes;
  89. unsigned int nbytes = 0;
  90. if (!n)
  91. goto finish;
  92. if (likely(err >= 0)) {
  93. n -= err;
  94. nbytes = walk->total - n;
  95. }
  96. if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
  97. SKCIPHER_WALK_SLOW |
  98. SKCIPHER_WALK_COPY |
  99. SKCIPHER_WALK_DIFF)))) {
  100. unmap_src:
  101. skcipher_unmap_src(walk);
  102. } else if (walk->flags & SKCIPHER_WALK_DIFF) {
  103. skcipher_unmap_dst(walk);
  104. goto unmap_src;
  105. } else if (walk->flags & SKCIPHER_WALK_COPY) {
  106. skcipher_map_dst(walk);
  107. memcpy(walk->dst.virt.addr, walk->page, n);
  108. skcipher_unmap_dst(walk);
  109. } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
  110. if (err > 0) {
  111. /*
  112. * Didn't process all bytes. Either the algorithm is
  113. * broken, or this was the last step and it turned out
  114. * the message wasn't evenly divisible into blocks but
  115. * the algorithm requires it.
  116. */
  117. err = -EINVAL;
  118. nbytes = 0;
  119. } else
  120. n = skcipher_done_slow(walk, n);
  121. }
  122. if (err > 0)
  123. err = 0;
  124. walk->total = nbytes;
  125. walk->nbytes = 0;
  126. scatterwalk_advance(&walk->in, n);
  127. scatterwalk_advance(&walk->out, n);
  128. scatterwalk_done(&walk->in, 0, nbytes);
  129. scatterwalk_done(&walk->out, 1, nbytes);
  130. if (nbytes) {
  131. crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
  132. CRYPTO_TFM_REQ_MAY_SLEEP : 0);
  133. return skcipher_walk_next(walk);
  134. }
  135. finish:
  136. /* Short-circuit for the common/fast path. */
  137. if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
  138. goto out;
  139. if (walk->flags & SKCIPHER_WALK_PHYS)
  140. goto out;
  141. if (walk->iv != walk->oiv)
  142. memcpy(walk->oiv, walk->iv, walk->ivsize);
  143. if (walk->buffer != walk->page)
  144. kfree(walk->buffer);
  145. if (walk->page)
  146. free_page((unsigned long)walk->page);
  147. out:
  148. return err;
  149. }
  150. EXPORT_SYMBOL_GPL(skcipher_walk_done);
  151. void skcipher_walk_complete(struct skcipher_walk *walk, int err)
  152. {
  153. struct skcipher_walk_buffer *p, *tmp;
  154. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  155. u8 *data;
  156. if (err)
  157. goto done;
  158. data = p->data;
  159. if (!data) {
  160. data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
  161. data = skcipher_get_spot(data, walk->stride);
  162. }
  163. scatterwalk_copychunks(data, &p->dst, p->len, 1);
  164. if (offset_in_page(p->data) + p->len + walk->stride >
  165. PAGE_SIZE)
  166. free_page((unsigned long)p->data);
  167. done:
  168. list_del(&p->entry);
  169. kfree(p);
  170. }
  171. if (!err && walk->iv != walk->oiv)
  172. memcpy(walk->oiv, walk->iv, walk->ivsize);
  173. if (walk->buffer != walk->page)
  174. kfree(walk->buffer);
  175. if (walk->page)
  176. free_page((unsigned long)walk->page);
  177. }
  178. EXPORT_SYMBOL_GPL(skcipher_walk_complete);
  179. static void skcipher_queue_write(struct skcipher_walk *walk,
  180. struct skcipher_walk_buffer *p)
  181. {
  182. p->dst = walk->out;
  183. list_add_tail(&p->entry, &walk->buffers);
  184. }
  185. static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
  186. {
  187. bool phys = walk->flags & SKCIPHER_WALK_PHYS;
  188. unsigned alignmask = walk->alignmask;
  189. struct skcipher_walk_buffer *p;
  190. unsigned a;
  191. unsigned n;
  192. u8 *buffer;
  193. void *v;
  194. if (!phys) {
  195. if (!walk->buffer)
  196. walk->buffer = walk->page;
  197. buffer = walk->buffer;
  198. if (buffer)
  199. goto ok;
  200. }
  201. /* Start with the minimum alignment of kmalloc. */
  202. a = crypto_tfm_ctx_alignment() - 1;
  203. n = bsize;
  204. if (phys) {
  205. /* Calculate the minimum alignment of p->buffer. */
  206. a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
  207. n += sizeof(*p);
  208. }
  209. /* Minimum size to align p->buffer by alignmask. */
  210. n += alignmask & ~a;
  211. /* Minimum size to ensure p->buffer does not straddle a page. */
  212. n += (bsize - 1) & ~(alignmask | a);
  213. v = kzalloc(n, skcipher_walk_gfp(walk));
  214. if (!v)
  215. return skcipher_walk_done(walk, -ENOMEM);
  216. if (phys) {
  217. p = v;
  218. p->len = bsize;
  219. skcipher_queue_write(walk, p);
  220. buffer = p->buffer;
  221. } else {
  222. walk->buffer = v;
  223. buffer = v;
  224. }
  225. ok:
  226. walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
  227. walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
  228. walk->src.virt.addr = walk->dst.virt.addr;
  229. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  230. walk->nbytes = bsize;
  231. walk->flags |= SKCIPHER_WALK_SLOW;
  232. return 0;
  233. }
  234. static int skcipher_next_copy(struct skcipher_walk *walk)
  235. {
  236. struct skcipher_walk_buffer *p;
  237. u8 *tmp = walk->page;
  238. skcipher_map_src(walk);
  239. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  240. skcipher_unmap_src(walk);
  241. walk->src.virt.addr = tmp;
  242. walk->dst.virt.addr = tmp;
  243. if (!(walk->flags & SKCIPHER_WALK_PHYS))
  244. return 0;
  245. p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
  246. if (!p)
  247. return -ENOMEM;
  248. p->data = walk->page;
  249. p->len = walk->nbytes;
  250. skcipher_queue_write(walk, p);
  251. if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
  252. PAGE_SIZE)
  253. walk->page = NULL;
  254. else
  255. walk->page += walk->nbytes;
  256. return 0;
  257. }
  258. static int skcipher_next_fast(struct skcipher_walk *walk)
  259. {
  260. unsigned long diff;
  261. walk->src.phys.page = scatterwalk_page(&walk->in);
  262. walk->src.phys.offset = offset_in_page(walk->in.offset);
  263. walk->dst.phys.page = scatterwalk_page(&walk->out);
  264. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  265. if (walk->flags & SKCIPHER_WALK_PHYS)
  266. return 0;
  267. diff = walk->src.phys.offset - walk->dst.phys.offset;
  268. diff |= walk->src.virt.page - walk->dst.virt.page;
  269. skcipher_map_src(walk);
  270. walk->dst.virt.addr = walk->src.virt.addr;
  271. if (diff) {
  272. walk->flags |= SKCIPHER_WALK_DIFF;
  273. skcipher_map_dst(walk);
  274. }
  275. return 0;
  276. }
  277. static int skcipher_walk_next(struct skcipher_walk *walk)
  278. {
  279. unsigned int bsize;
  280. unsigned int n;
  281. int err;
  282. walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
  283. SKCIPHER_WALK_DIFF);
  284. n = walk->total;
  285. bsize = min(walk->stride, max(n, walk->blocksize));
  286. n = scatterwalk_clamp(&walk->in, n);
  287. n = scatterwalk_clamp(&walk->out, n);
  288. if (unlikely(n < bsize)) {
  289. if (unlikely(walk->total < walk->blocksize))
  290. return skcipher_walk_done(walk, -EINVAL);
  291. slow_path:
  292. err = skcipher_next_slow(walk, bsize);
  293. goto set_phys_lowmem;
  294. }
  295. if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
  296. if (!walk->page) {
  297. gfp_t gfp = skcipher_walk_gfp(walk);
  298. walk->page = (void *)__get_free_page(gfp);
  299. if (!walk->page)
  300. goto slow_path;
  301. }
  302. walk->nbytes = min_t(unsigned, n,
  303. PAGE_SIZE - offset_in_page(walk->page));
  304. walk->flags |= SKCIPHER_WALK_COPY;
  305. err = skcipher_next_copy(walk);
  306. goto set_phys_lowmem;
  307. }
  308. walk->nbytes = n;
  309. return skcipher_next_fast(walk);
  310. set_phys_lowmem:
  311. if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
  312. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  313. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  314. walk->src.phys.offset &= PAGE_SIZE - 1;
  315. walk->dst.phys.offset &= PAGE_SIZE - 1;
  316. }
  317. return err;
  318. }
  319. static int skcipher_copy_iv(struct skcipher_walk *walk)
  320. {
  321. unsigned a = crypto_tfm_ctx_alignment() - 1;
  322. unsigned alignmask = walk->alignmask;
  323. unsigned ivsize = walk->ivsize;
  324. unsigned bs = walk->stride;
  325. unsigned aligned_bs;
  326. unsigned size;
  327. u8 *iv;
  328. aligned_bs = ALIGN(bs, alignmask + 1);
  329. /* Minimum size to align buffer by alignmask. */
  330. size = alignmask & ~a;
  331. if (walk->flags & SKCIPHER_WALK_PHYS)
  332. size += ivsize;
  333. else {
  334. size += aligned_bs + ivsize;
  335. /* Minimum size to ensure buffer does not straddle a page. */
  336. size += (bs - 1) & ~(alignmask | a);
  337. }
  338. walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
  339. if (!walk->buffer)
  340. return -ENOMEM;
  341. iv = PTR_ALIGN(walk->buffer, alignmask + 1);
  342. iv = skcipher_get_spot(iv, bs) + aligned_bs;
  343. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  344. return 0;
  345. }
  346. static int skcipher_walk_first(struct skcipher_walk *walk)
  347. {
  348. if (WARN_ON_ONCE(in_hardirq()))
  349. return -EDEADLK;
  350. walk->buffer = NULL;
  351. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  352. int err = skcipher_copy_iv(walk);
  353. if (err)
  354. return err;
  355. }
  356. walk->page = NULL;
  357. 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. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  364. walk->total = req->cryptlen;
  365. walk->nbytes = 0;
  366. walk->iv = req->iv;
  367. walk->oiv = req->iv;
  368. if (unlikely(!walk->total))
  369. return 0;
  370. scatterwalk_start(&walk->in, req->src);
  371. scatterwalk_start(&walk->out, req->dst);
  372. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  373. walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  374. SKCIPHER_WALK_SLEEP : 0;
  375. walk->blocksize = crypto_skcipher_blocksize(tfm);
  376. walk->ivsize = crypto_skcipher_ivsize(tfm);
  377. walk->alignmask = crypto_skcipher_alignmask(tfm);
  378. if (alg->co.base.cra_type != &crypto_skcipher_type)
  379. walk->stride = alg->co.chunksize;
  380. else
  381. walk->stride = alg->walksize;
  382. return skcipher_walk_first(walk);
  383. }
  384. int skcipher_walk_virt(struct skcipher_walk *walk,
  385. struct skcipher_request *req, bool atomic)
  386. {
  387. int err;
  388. might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  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. int skcipher_walk_async(struct skcipher_walk *walk,
  396. struct skcipher_request *req)
  397. {
  398. walk->flags |= SKCIPHER_WALK_PHYS;
  399. INIT_LIST_HEAD(&walk->buffers);
  400. return skcipher_walk_skcipher(walk, req);
  401. }
  402. EXPORT_SYMBOL_GPL(skcipher_walk_async);
  403. static int skcipher_walk_aead_common(struct skcipher_walk *walk,
  404. struct aead_request *req, bool atomic)
  405. {
  406. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  407. int err;
  408. walk->nbytes = 0;
  409. walk->iv = req->iv;
  410. walk->oiv = req->iv;
  411. if (unlikely(!walk->total))
  412. return 0;
  413. walk->flags &= ~SKCIPHER_WALK_PHYS;
  414. scatterwalk_start(&walk->in, req->src);
  415. scatterwalk_start(&walk->out, req->dst);
  416. scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
  417. scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
  418. scatterwalk_done(&walk->in, 0, walk->total);
  419. scatterwalk_done(&walk->out, 0, walk->total);
  420. if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  421. walk->flags |= SKCIPHER_WALK_SLEEP;
  422. else
  423. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  424. walk->blocksize = crypto_aead_blocksize(tfm);
  425. walk->stride = crypto_aead_chunksize(tfm);
  426. walk->ivsize = crypto_aead_ivsize(tfm);
  427. walk->alignmask = crypto_aead_alignmask(tfm);
  428. err = skcipher_walk_first(walk);
  429. if (atomic)
  430. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  431. return err;
  432. }
  433. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  434. struct aead_request *req, bool atomic)
  435. {
  436. walk->total = req->cryptlen;
  437. return skcipher_walk_aead_common(walk, req, atomic);
  438. }
  439. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  440. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  441. struct aead_request *req, bool atomic)
  442. {
  443. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  444. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  445. return skcipher_walk_aead_common(walk, req, atomic);
  446. }
  447. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  448. static void skcipher_set_needkey(struct crypto_skcipher *tfm)
  449. {
  450. if (crypto_skcipher_max_keysize(tfm) != 0)
  451. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  452. }
  453. static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  454. const u8 *key, unsigned int keylen)
  455. {
  456. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  457. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  458. u8 *buffer, *alignbuffer;
  459. unsigned long absize;
  460. int ret;
  461. absize = keylen + alignmask;
  462. buffer = kmalloc(absize, GFP_ATOMIC);
  463. if (!buffer)
  464. return -ENOMEM;
  465. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  466. memcpy(alignbuffer, key, keylen);
  467. ret = cipher->setkey(tfm, alignbuffer, keylen);
  468. kfree_sensitive(buffer);
  469. return ret;
  470. }
  471. int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  472. unsigned int keylen)
  473. {
  474. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  475. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  476. int err;
  477. if (cipher->co.base.cra_type != &crypto_skcipher_type) {
  478. struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm);
  479. crypto_lskcipher_clear_flags(*ctx, CRYPTO_TFM_REQ_MASK);
  480. crypto_lskcipher_set_flags(*ctx,
  481. crypto_skcipher_get_flags(tfm) &
  482. CRYPTO_TFM_REQ_MASK);
  483. err = crypto_lskcipher_setkey(*ctx, key, keylen);
  484. goto out;
  485. }
  486. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
  487. return -EINVAL;
  488. if ((unsigned long)key & alignmask)
  489. err = skcipher_setkey_unaligned(tfm, key, keylen);
  490. else
  491. err = cipher->setkey(tfm, key, keylen);
  492. out:
  493. if (unlikely(err)) {
  494. skcipher_set_needkey(tfm);
  495. return err;
  496. }
  497. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
  501. int crypto_skcipher_encrypt(struct skcipher_request *req)
  502. {
  503. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  504. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  505. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  506. return -ENOKEY;
  507. if (alg->co.base.cra_type != &crypto_skcipher_type)
  508. return crypto_lskcipher_encrypt_sg(req);
  509. return alg->encrypt(req);
  510. }
  511. EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
  512. int crypto_skcipher_decrypt(struct skcipher_request *req)
  513. {
  514. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  515. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  516. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  517. return -ENOKEY;
  518. if (alg->co.base.cra_type != &crypto_skcipher_type)
  519. return crypto_lskcipher_decrypt_sg(req);
  520. return alg->decrypt(req);
  521. }
  522. EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
  523. static int crypto_lskcipher_export(struct skcipher_request *req, void *out)
  524. {
  525. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  526. u8 *ivs = skcipher_request_ctx(req);
  527. ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1);
  528. memcpy(out, ivs + crypto_skcipher_ivsize(tfm),
  529. crypto_skcipher_statesize(tfm));
  530. return 0;
  531. }
  532. static int crypto_lskcipher_import(struct skcipher_request *req, const void *in)
  533. {
  534. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  535. u8 *ivs = skcipher_request_ctx(req);
  536. ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1);
  537. memcpy(ivs + crypto_skcipher_ivsize(tfm), in,
  538. crypto_skcipher_statesize(tfm));
  539. return 0;
  540. }
  541. static int skcipher_noexport(struct skcipher_request *req, void *out)
  542. {
  543. return 0;
  544. }
  545. static int skcipher_noimport(struct skcipher_request *req, const void *in)
  546. {
  547. return 0;
  548. }
  549. int crypto_skcipher_export(struct skcipher_request *req, void *out)
  550. {
  551. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  552. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  553. if (alg->co.base.cra_type != &crypto_skcipher_type)
  554. return crypto_lskcipher_export(req, out);
  555. return alg->export(req, out);
  556. }
  557. EXPORT_SYMBOL_GPL(crypto_skcipher_export);
  558. int crypto_skcipher_import(struct skcipher_request *req, const void *in)
  559. {
  560. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  561. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  562. if (alg->co.base.cra_type != &crypto_skcipher_type)
  563. return crypto_lskcipher_import(req, in);
  564. return alg->import(req, in);
  565. }
  566. EXPORT_SYMBOL_GPL(crypto_skcipher_import);
  567. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  568. {
  569. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  570. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  571. alg->exit(skcipher);
  572. }
  573. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  574. {
  575. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  576. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  577. skcipher_set_needkey(skcipher);
  578. if (tfm->__crt_alg->cra_type != &crypto_skcipher_type) {
  579. unsigned am = crypto_skcipher_alignmask(skcipher);
  580. unsigned reqsize;
  581. reqsize = am & ~(crypto_tfm_ctx_alignment() - 1);
  582. reqsize += crypto_skcipher_ivsize(skcipher);
  583. reqsize += crypto_skcipher_statesize(skcipher);
  584. crypto_skcipher_set_reqsize(skcipher, reqsize);
  585. return crypto_init_lskcipher_ops_sg(tfm);
  586. }
  587. if (alg->exit)
  588. skcipher->base.exit = crypto_skcipher_exit_tfm;
  589. if (alg->init)
  590. return alg->init(skcipher);
  591. return 0;
  592. }
  593. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  594. {
  595. if (alg->cra_type != &crypto_skcipher_type)
  596. return sizeof(struct crypto_lskcipher *);
  597. return crypto_alg_extsize(alg);
  598. }
  599. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  600. {
  601. struct skcipher_instance *skcipher =
  602. container_of(inst, struct skcipher_instance, s.base);
  603. skcipher->free(skcipher);
  604. }
  605. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  606. __maybe_unused;
  607. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  608. {
  609. struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg);
  610. seq_printf(m, "type : skcipher\n");
  611. seq_printf(m, "async : %s\n",
  612. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  613. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  614. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  615. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  616. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  617. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  618. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  619. seq_printf(m, "statesize : %u\n", skcipher->statesize);
  620. }
  621. static int __maybe_unused crypto_skcipher_report(
  622. struct sk_buff *skb, struct crypto_alg *alg)
  623. {
  624. struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg);
  625. struct crypto_report_blkcipher rblkcipher;
  626. memset(&rblkcipher, 0, sizeof(rblkcipher));
  627. strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  628. strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  629. rblkcipher.blocksize = alg->cra_blocksize;
  630. rblkcipher.min_keysize = skcipher->min_keysize;
  631. rblkcipher.max_keysize = skcipher->max_keysize;
  632. rblkcipher.ivsize = skcipher->ivsize;
  633. return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  634. sizeof(rblkcipher), &rblkcipher);
  635. }
  636. static const struct crypto_type crypto_skcipher_type = {
  637. .extsize = crypto_skcipher_extsize,
  638. .init_tfm = crypto_skcipher_init_tfm,
  639. .free = crypto_skcipher_free_instance,
  640. #ifdef CONFIG_PROC_FS
  641. .show = crypto_skcipher_show,
  642. #endif
  643. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  644. .report = crypto_skcipher_report,
  645. #endif
  646. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  647. .maskset = CRYPTO_ALG_TYPE_SKCIPHER_MASK,
  648. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  649. .tfmsize = offsetof(struct crypto_skcipher, base),
  650. };
  651. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  652. struct crypto_instance *inst,
  653. const char *name, u32 type, u32 mask)
  654. {
  655. spawn->base.frontend = &crypto_skcipher_type;
  656. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  657. }
  658. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  659. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  660. u32 type, u32 mask)
  661. {
  662. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  663. }
  664. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  665. struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
  666. const char *alg_name, u32 type, u32 mask)
  667. {
  668. struct crypto_skcipher *tfm;
  669. /* Only sync algorithms allowed. */
  670. mask |= CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE;
  671. tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  672. /*
  673. * Make sure we do not allocate something that might get used with
  674. * an on-stack request: check the request size.
  675. */
  676. if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
  677. MAX_SYNC_SKCIPHER_REQSIZE)) {
  678. crypto_free_skcipher(tfm);
  679. return ERR_PTR(-EINVAL);
  680. }
  681. return (struct crypto_sync_skcipher *)tfm;
  682. }
  683. EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
  684. int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
  685. {
  686. return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
  687. }
  688. EXPORT_SYMBOL_GPL(crypto_has_skcipher);
  689. int skcipher_prepare_alg_common(struct skcipher_alg_common *alg)
  690. {
  691. struct crypto_alg *base = &alg->base;
  692. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  693. alg->statesize > PAGE_SIZE / 2 ||
  694. (alg->ivsize + alg->statesize) > PAGE_SIZE / 2)
  695. return -EINVAL;
  696. if (!alg->chunksize)
  697. alg->chunksize = base->cra_blocksize;
  698. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  699. return 0;
  700. }
  701. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  702. {
  703. struct crypto_alg *base = &alg->base;
  704. int err;
  705. err = skcipher_prepare_alg_common(&alg->co);
  706. if (err)
  707. return err;
  708. if (alg->walksize > PAGE_SIZE / 8)
  709. return -EINVAL;
  710. if (!alg->walksize)
  711. alg->walksize = alg->chunksize;
  712. if (!alg->statesize) {
  713. alg->import = skcipher_noimport;
  714. alg->export = skcipher_noexport;
  715. } else if (!(alg->import && alg->export))
  716. return -EINVAL;
  717. base->cra_type = &crypto_skcipher_type;
  718. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  719. return 0;
  720. }
  721. int crypto_register_skcipher(struct skcipher_alg *alg)
  722. {
  723. struct crypto_alg *base = &alg->base;
  724. int err;
  725. err = skcipher_prepare_alg(alg);
  726. if (err)
  727. return err;
  728. return crypto_register_alg(base);
  729. }
  730. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  731. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  732. {
  733. crypto_unregister_alg(&alg->base);
  734. }
  735. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  736. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  737. {
  738. int i, ret;
  739. for (i = 0; i < count; i++) {
  740. ret = crypto_register_skcipher(&algs[i]);
  741. if (ret)
  742. goto err;
  743. }
  744. return 0;
  745. err:
  746. for (--i; i >= 0; --i)
  747. crypto_unregister_skcipher(&algs[i]);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  751. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  752. {
  753. int i;
  754. for (i = count - 1; i >= 0; --i)
  755. crypto_unregister_skcipher(&algs[i]);
  756. }
  757. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  758. int skcipher_register_instance(struct crypto_template *tmpl,
  759. struct skcipher_instance *inst)
  760. {
  761. int err;
  762. if (WARN_ON(!inst->free))
  763. return -EINVAL;
  764. err = skcipher_prepare_alg(&inst->alg);
  765. if (err)
  766. return err;
  767. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  768. }
  769. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  770. static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
  771. unsigned int keylen)
  772. {
  773. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  774. crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
  775. crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
  776. CRYPTO_TFM_REQ_MASK);
  777. return crypto_cipher_setkey(cipher, key, keylen);
  778. }
  779. static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
  780. {
  781. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  782. struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
  783. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  784. struct crypto_cipher *cipher;
  785. cipher = crypto_spawn_cipher(spawn);
  786. if (IS_ERR(cipher))
  787. return PTR_ERR(cipher);
  788. ctx->cipher = cipher;
  789. return 0;
  790. }
  791. static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
  792. {
  793. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  794. crypto_free_cipher(ctx->cipher);
  795. }
  796. static void skcipher_free_instance_simple(struct skcipher_instance *inst)
  797. {
  798. crypto_drop_cipher(skcipher_instance_ctx(inst));
  799. kfree(inst);
  800. }
  801. /**
  802. * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
  803. *
  804. * Allocate an skcipher_instance for a simple block cipher mode of operation,
  805. * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
  806. * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
  807. * alignmask, and priority are set from the underlying cipher but can be
  808. * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and
  809. * default ->setkey(), ->init(), and ->exit() methods are installed.
  810. *
  811. * @tmpl: the template being instantiated
  812. * @tb: the template parameters
  813. *
  814. * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
  815. * needs to register the instance.
  816. */
  817. struct skcipher_instance *skcipher_alloc_instance_simple(
  818. struct crypto_template *tmpl, struct rtattr **tb)
  819. {
  820. u32 mask;
  821. struct skcipher_instance *inst;
  822. struct crypto_cipher_spawn *spawn;
  823. struct crypto_alg *cipher_alg;
  824. int err;
  825. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
  826. if (err)
  827. return ERR_PTR(err);
  828. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  829. if (!inst)
  830. return ERR_PTR(-ENOMEM);
  831. spawn = skcipher_instance_ctx(inst);
  832. err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
  833. crypto_attr_alg_name(tb[1]), 0, mask);
  834. if (err)
  835. goto err_free_inst;
  836. cipher_alg = crypto_spawn_cipher_alg(spawn);
  837. err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
  838. cipher_alg);
  839. if (err)
  840. goto err_free_inst;
  841. inst->free = skcipher_free_instance_simple;
  842. /* Default algorithm properties, can be overridden */
  843. inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
  844. inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
  845. inst->alg.base.cra_priority = cipher_alg->cra_priority;
  846. inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
  847. inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
  848. inst->alg.ivsize = cipher_alg->cra_blocksize;
  849. /* Use skcipher_ctx_simple by default, can be overridden */
  850. inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
  851. inst->alg.setkey = skcipher_setkey_simple;
  852. inst->alg.init = skcipher_init_tfm_simple;
  853. inst->alg.exit = skcipher_exit_tfm_simple;
  854. return inst;
  855. err_free_inst:
  856. skcipher_free_instance_simple(inst);
  857. return ERR_PTR(err);
  858. }
  859. EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
  860. MODULE_LICENSE("GPL");
  861. MODULE_DESCRIPTION("Symmetric key cipher type");
  862. MODULE_IMPORT_NS(CRYPTO_INTERNAL);