skcipher.c 25 KB

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