skcipher.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. type &= ~(CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE);
  672. tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
  673. /*
  674. * Make sure we do not allocate something that might get used with
  675. * an on-stack request: check the request size.
  676. */
  677. if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
  678. MAX_SYNC_SKCIPHER_REQSIZE)) {
  679. crypto_free_skcipher(tfm);
  680. return ERR_PTR(-EINVAL);
  681. }
  682. return (struct crypto_sync_skcipher *)tfm;
  683. }
  684. EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
  685. int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
  686. {
  687. return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
  688. }
  689. EXPORT_SYMBOL_GPL(crypto_has_skcipher);
  690. int skcipher_prepare_alg_common(struct skcipher_alg_common *alg)
  691. {
  692. struct crypto_alg *base = &alg->base;
  693. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  694. alg->statesize > PAGE_SIZE / 2 ||
  695. (alg->ivsize + alg->statesize) > PAGE_SIZE / 2)
  696. return -EINVAL;
  697. if (!alg->chunksize)
  698. alg->chunksize = base->cra_blocksize;
  699. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  700. return 0;
  701. }
  702. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  703. {
  704. struct crypto_alg *base = &alg->base;
  705. int err;
  706. err = skcipher_prepare_alg_common(&alg->co);
  707. if (err)
  708. return err;
  709. if (alg->walksize > PAGE_SIZE / 8)
  710. return -EINVAL;
  711. if (!alg->walksize)
  712. alg->walksize = alg->chunksize;
  713. if (!alg->statesize) {
  714. alg->import = skcipher_noimport;
  715. alg->export = skcipher_noexport;
  716. } else if (!(alg->import && alg->export))
  717. return -EINVAL;
  718. base->cra_type = &crypto_skcipher_type;
  719. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  720. return 0;
  721. }
  722. int crypto_register_skcipher(struct skcipher_alg *alg)
  723. {
  724. struct crypto_alg *base = &alg->base;
  725. int err;
  726. err = skcipher_prepare_alg(alg);
  727. if (err)
  728. return err;
  729. return crypto_register_alg(base);
  730. }
  731. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  732. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  733. {
  734. crypto_unregister_alg(&alg->base);
  735. }
  736. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  737. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  738. {
  739. int i, ret;
  740. for (i = 0; i < count; i++) {
  741. ret = crypto_register_skcipher(&algs[i]);
  742. if (ret)
  743. goto err;
  744. }
  745. return 0;
  746. err:
  747. for (--i; i >= 0; --i)
  748. crypto_unregister_skcipher(&algs[i]);
  749. return ret;
  750. }
  751. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  752. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  753. {
  754. int i;
  755. for (i = count - 1; i >= 0; --i)
  756. crypto_unregister_skcipher(&algs[i]);
  757. }
  758. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  759. int skcipher_register_instance(struct crypto_template *tmpl,
  760. struct skcipher_instance *inst)
  761. {
  762. int err;
  763. if (WARN_ON(!inst->free))
  764. return -EINVAL;
  765. err = skcipher_prepare_alg(&inst->alg);
  766. if (err)
  767. return err;
  768. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  769. }
  770. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  771. static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
  772. unsigned int keylen)
  773. {
  774. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  775. crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
  776. crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
  777. CRYPTO_TFM_REQ_MASK);
  778. return crypto_cipher_setkey(cipher, key, keylen);
  779. }
  780. static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
  781. {
  782. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  783. struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
  784. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  785. struct crypto_cipher *cipher;
  786. cipher = crypto_spawn_cipher(spawn);
  787. if (IS_ERR(cipher))
  788. return PTR_ERR(cipher);
  789. ctx->cipher = cipher;
  790. return 0;
  791. }
  792. static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
  793. {
  794. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  795. crypto_free_cipher(ctx->cipher);
  796. }
  797. static void skcipher_free_instance_simple(struct skcipher_instance *inst)
  798. {
  799. crypto_drop_cipher(skcipher_instance_ctx(inst));
  800. kfree(inst);
  801. }
  802. /**
  803. * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
  804. *
  805. * Allocate an skcipher_instance for a simple block cipher mode of operation,
  806. * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
  807. * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
  808. * alignmask, and priority are set from the underlying cipher but can be
  809. * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and
  810. * default ->setkey(), ->init(), and ->exit() methods are installed.
  811. *
  812. * @tmpl: the template being instantiated
  813. * @tb: the template parameters
  814. *
  815. * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
  816. * needs to register the instance.
  817. */
  818. struct skcipher_instance *skcipher_alloc_instance_simple(
  819. struct crypto_template *tmpl, struct rtattr **tb)
  820. {
  821. u32 mask;
  822. struct skcipher_instance *inst;
  823. struct crypto_cipher_spawn *spawn;
  824. struct crypto_alg *cipher_alg;
  825. int err;
  826. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
  827. if (err)
  828. return ERR_PTR(err);
  829. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  830. if (!inst)
  831. return ERR_PTR(-ENOMEM);
  832. spawn = skcipher_instance_ctx(inst);
  833. err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
  834. crypto_attr_alg_name(tb[1]), 0, mask);
  835. if (err)
  836. goto err_free_inst;
  837. cipher_alg = crypto_spawn_cipher_alg(spawn);
  838. err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
  839. cipher_alg);
  840. if (err)
  841. goto err_free_inst;
  842. inst->free = skcipher_free_instance_simple;
  843. /* Default algorithm properties, can be overridden */
  844. inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
  845. inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
  846. inst->alg.base.cra_priority = cipher_alg->cra_priority;
  847. inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
  848. inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
  849. inst->alg.ivsize = cipher_alg->cra_blocksize;
  850. /* Use skcipher_ctx_simple by default, can be overridden */
  851. inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
  852. inst->alg.setkey = skcipher_setkey_simple;
  853. inst->alg.init = skcipher_init_tfm_simple;
  854. inst->alg.exit = skcipher_exit_tfm_simple;
  855. return inst;
  856. err_free_inst:
  857. skcipher_free_instance_simple(inst);
  858. return ERR_PTR(err);
  859. }
  860. EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
  861. MODULE_LICENSE("GPL");
  862. MODULE_DESCRIPTION("Symmetric key cipher type");
  863. MODULE_IMPORT_NS(CRYPTO_INTERNAL);