skcipher.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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/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_irq()))
  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. void skcipher_walk_atomise(struct skcipher_walk *walk)
  393. {
  394. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  395. }
  396. EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
  397. int skcipher_walk_async(struct skcipher_walk *walk,
  398. struct skcipher_request *req)
  399. {
  400. walk->flags |= SKCIPHER_WALK_PHYS;
  401. INIT_LIST_HEAD(&walk->buffers);
  402. return skcipher_walk_skcipher(walk, req);
  403. }
  404. EXPORT_SYMBOL_GPL(skcipher_walk_async);
  405. static int skcipher_walk_aead_common(struct skcipher_walk *walk,
  406. struct aead_request *req, bool atomic)
  407. {
  408. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  409. int err;
  410. walk->nbytes = 0;
  411. walk->iv = req->iv;
  412. walk->oiv = req->iv;
  413. if (unlikely(!walk->total))
  414. return 0;
  415. walk->flags &= ~SKCIPHER_WALK_PHYS;
  416. scatterwalk_start(&walk->in, req->src);
  417. scatterwalk_start(&walk->out, req->dst);
  418. scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
  419. scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
  420. scatterwalk_done(&walk->in, 0, walk->total);
  421. scatterwalk_done(&walk->out, 0, walk->total);
  422. if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  423. walk->flags |= SKCIPHER_WALK_SLEEP;
  424. else
  425. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  426. walk->blocksize = crypto_aead_blocksize(tfm);
  427. walk->stride = crypto_aead_chunksize(tfm);
  428. walk->ivsize = crypto_aead_ivsize(tfm);
  429. walk->alignmask = crypto_aead_alignmask(tfm);
  430. err = skcipher_walk_first(walk);
  431. if (atomic)
  432. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  433. return err;
  434. }
  435. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  436. struct aead_request *req, bool atomic)
  437. {
  438. walk->total = req->cryptlen;
  439. return skcipher_walk_aead_common(walk, req, atomic);
  440. }
  441. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  442. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  443. struct aead_request *req, bool atomic)
  444. {
  445. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  446. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  447. return skcipher_walk_aead_common(walk, req, atomic);
  448. }
  449. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  450. static void skcipher_set_needkey(struct crypto_skcipher *tfm)
  451. {
  452. if (crypto_skcipher_max_keysize(tfm) != 0)
  453. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  454. }
  455. static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  456. const u8 *key, unsigned int keylen)
  457. {
  458. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  459. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  460. u8 *buffer, *alignbuffer;
  461. unsigned long absize;
  462. int ret;
  463. absize = keylen + alignmask;
  464. buffer = kmalloc(absize, GFP_ATOMIC);
  465. if (!buffer)
  466. return -ENOMEM;
  467. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  468. memcpy(alignbuffer, key, keylen);
  469. ret = cipher->setkey(tfm, alignbuffer, keylen);
  470. kfree_sensitive(buffer);
  471. return ret;
  472. }
  473. int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  474. unsigned int keylen)
  475. {
  476. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  477. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  478. int err;
  479. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
  480. return -EINVAL;
  481. if ((unsigned long)key & alignmask)
  482. err = skcipher_setkey_unaligned(tfm, key, keylen);
  483. else
  484. err = cipher->setkey(tfm, key, keylen);
  485. if (unlikely(err)) {
  486. skcipher_set_needkey(tfm);
  487. return err;
  488. }
  489. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
  493. int crypto_skcipher_encrypt(struct skcipher_request *req)
  494. {
  495. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  496. struct crypto_alg *alg = tfm->base.__crt_alg;
  497. unsigned int cryptlen = req->cryptlen;
  498. int ret;
  499. crypto_stats_get(alg);
  500. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  501. ret = -ENOKEY;
  502. else
  503. ret = crypto_skcipher_alg(tfm)->encrypt(req);
  504. crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
  505. return ret;
  506. }
  507. EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
  508. int crypto_skcipher_decrypt(struct skcipher_request *req)
  509. {
  510. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  511. struct crypto_alg *alg = tfm->base.__crt_alg;
  512. unsigned int cryptlen = req->cryptlen;
  513. int ret;
  514. crypto_stats_get(alg);
  515. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  516. ret = -ENOKEY;
  517. else
  518. ret = crypto_skcipher_alg(tfm)->decrypt(req);
  519. crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
  520. return ret;
  521. }
  522. EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
  523. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  524. {
  525. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  526. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  527. alg->exit(skcipher);
  528. }
  529. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  530. {
  531. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  532. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  533. skcipher_set_needkey(skcipher);
  534. if (alg->exit)
  535. skcipher->base.exit = crypto_skcipher_exit_tfm;
  536. if (alg->init)
  537. return alg->init(skcipher);
  538. return 0;
  539. }
  540. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  541. {
  542. struct skcipher_instance *skcipher =
  543. container_of(inst, struct skcipher_instance, s.base);
  544. skcipher->free(skcipher);
  545. }
  546. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  547. __maybe_unused;
  548. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  549. {
  550. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  551. base);
  552. seq_printf(m, "type : skcipher\n");
  553. seq_printf(m, "async : %s\n",
  554. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  555. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  556. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  557. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  558. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  559. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  560. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  561. }
  562. #ifdef CONFIG_NET
  563. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  564. {
  565. struct crypto_report_blkcipher rblkcipher;
  566. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  567. base);
  568. memset(&rblkcipher, 0, sizeof(rblkcipher));
  569. strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  570. strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  571. rblkcipher.blocksize = alg->cra_blocksize;
  572. rblkcipher.min_keysize = skcipher->min_keysize;
  573. rblkcipher.max_keysize = skcipher->max_keysize;
  574. rblkcipher.ivsize = skcipher->ivsize;
  575. return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  576. sizeof(rblkcipher), &rblkcipher);
  577. }
  578. #else
  579. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  580. {
  581. return -ENOSYS;
  582. }
  583. #endif
  584. static const struct crypto_type crypto_skcipher_type = {
  585. .extsize = crypto_alg_extsize,
  586. .init_tfm = crypto_skcipher_init_tfm,
  587. .free = crypto_skcipher_free_instance,
  588. #ifdef CONFIG_PROC_FS
  589. .show = crypto_skcipher_show,
  590. #endif
  591. .report = crypto_skcipher_report,
  592. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  593. .maskset = CRYPTO_ALG_TYPE_MASK,
  594. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  595. .tfmsize = offsetof(struct crypto_skcipher, base),
  596. };
  597. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  598. struct crypto_instance *inst,
  599. const char *name, u32 type, u32 mask)
  600. {
  601. spawn->base.frontend = &crypto_skcipher_type;
  602. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  603. }
  604. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  605. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  606. u32 type, u32 mask)
  607. {
  608. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  609. }
  610. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  611. struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
  612. const char *alg_name, u32 type, u32 mask)
  613. {
  614. struct crypto_skcipher *tfm;
  615. /* Only sync algorithms allowed. */
  616. mask |= CRYPTO_ALG_ASYNC;
  617. tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  618. /*
  619. * Make sure we do not allocate something that might get used with
  620. * an on-stack request: check the request size.
  621. */
  622. if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
  623. MAX_SYNC_SKCIPHER_REQSIZE)) {
  624. crypto_free_skcipher(tfm);
  625. return ERR_PTR(-EINVAL);
  626. }
  627. return (struct crypto_sync_skcipher *)tfm;
  628. }
  629. EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
  630. int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
  631. {
  632. return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
  633. }
  634. EXPORT_SYMBOL_GPL(crypto_has_skcipher);
  635. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  636. {
  637. struct crypto_alg *base = &alg->base;
  638. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  639. alg->walksize > PAGE_SIZE / 8)
  640. return -EINVAL;
  641. if (!alg->chunksize)
  642. alg->chunksize = base->cra_blocksize;
  643. if (!alg->walksize)
  644. alg->walksize = alg->chunksize;
  645. base->cra_type = &crypto_skcipher_type;
  646. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  647. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  648. return 0;
  649. }
  650. int crypto_register_skcipher(struct skcipher_alg *alg)
  651. {
  652. struct crypto_alg *base = &alg->base;
  653. int err;
  654. err = skcipher_prepare_alg(alg);
  655. if (err)
  656. return err;
  657. return crypto_register_alg(base);
  658. }
  659. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  660. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  661. {
  662. crypto_unregister_alg(&alg->base);
  663. }
  664. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  665. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  666. {
  667. int i, ret;
  668. for (i = 0; i < count; i++) {
  669. ret = crypto_register_skcipher(&algs[i]);
  670. if (ret)
  671. goto err;
  672. }
  673. return 0;
  674. err:
  675. for (--i; i >= 0; --i)
  676. crypto_unregister_skcipher(&algs[i]);
  677. return ret;
  678. }
  679. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  680. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  681. {
  682. int i;
  683. for (i = count - 1; i >= 0; --i)
  684. crypto_unregister_skcipher(&algs[i]);
  685. }
  686. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  687. int skcipher_register_instance(struct crypto_template *tmpl,
  688. struct skcipher_instance *inst)
  689. {
  690. int err;
  691. if (WARN_ON(!inst->free))
  692. return -EINVAL;
  693. err = skcipher_prepare_alg(&inst->alg);
  694. if (err)
  695. return err;
  696. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  697. }
  698. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  699. static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
  700. unsigned int keylen)
  701. {
  702. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  703. crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
  704. crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
  705. CRYPTO_TFM_REQ_MASK);
  706. return crypto_cipher_setkey(cipher, key, keylen);
  707. }
  708. static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
  709. {
  710. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  711. struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
  712. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  713. struct crypto_cipher *cipher;
  714. cipher = crypto_spawn_cipher(spawn);
  715. if (IS_ERR(cipher))
  716. return PTR_ERR(cipher);
  717. ctx->cipher = cipher;
  718. return 0;
  719. }
  720. static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
  721. {
  722. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  723. crypto_free_cipher(ctx->cipher);
  724. }
  725. static void skcipher_free_instance_simple(struct skcipher_instance *inst)
  726. {
  727. crypto_drop_cipher(skcipher_instance_ctx(inst));
  728. kfree(inst);
  729. }
  730. /**
  731. * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
  732. *
  733. * Allocate an skcipher_instance for a simple block cipher mode of operation,
  734. * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
  735. * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
  736. * alignmask, and priority are set from the underlying cipher but can be
  737. * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and
  738. * default ->setkey(), ->init(), and ->exit() methods are installed.
  739. *
  740. * @tmpl: the template being instantiated
  741. * @tb: the template parameters
  742. *
  743. * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
  744. * needs to register the instance.
  745. */
  746. struct skcipher_instance *skcipher_alloc_instance_simple(
  747. struct crypto_template *tmpl, struct rtattr **tb)
  748. {
  749. u32 mask;
  750. struct skcipher_instance *inst;
  751. struct crypto_cipher_spawn *spawn;
  752. struct crypto_alg *cipher_alg;
  753. int err;
  754. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
  755. if (err)
  756. return ERR_PTR(err);
  757. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  758. if (!inst)
  759. return ERR_PTR(-ENOMEM);
  760. spawn = skcipher_instance_ctx(inst);
  761. err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
  762. crypto_attr_alg_name(tb[1]), 0, mask);
  763. if (err)
  764. goto err_free_inst;
  765. cipher_alg = crypto_spawn_cipher_alg(spawn);
  766. err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
  767. cipher_alg);
  768. if (err)
  769. goto err_free_inst;
  770. inst->free = skcipher_free_instance_simple;
  771. /* Default algorithm properties, can be overridden */
  772. inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
  773. inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
  774. inst->alg.base.cra_priority = cipher_alg->cra_priority;
  775. inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
  776. inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
  777. inst->alg.ivsize = cipher_alg->cra_blocksize;
  778. /* Use skcipher_ctx_simple by default, can be overridden */
  779. inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
  780. inst->alg.setkey = skcipher_setkey_simple;
  781. inst->alg.init = skcipher_init_tfm_simple;
  782. inst->alg.exit = skcipher_exit_tfm_simple;
  783. return inst;
  784. err_free_inst:
  785. skcipher_free_instance_simple(inst);
  786. return ERR_PTR(err);
  787. }
  788. EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
  789. MODULE_LICENSE("GPL");
  790. MODULE_DESCRIPTION("Symmetric key cipher type");
  791. MODULE_IMPORT_NS(CRYPTO_INTERNAL);