skcipher.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*
  2. * Symmetric key cipher operations.
  3. *
  4. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  5. * multiple page boundaries by using temporary blocks. In user context,
  6. * the kernel is given a chance to schedule us once per page.
  7. *
  8. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/bug.h>
  20. #include <linux/cryptouser.h>
  21. #include <linux/compiler.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/seq_file.h>
  26. #include <net/netlink.h>
  27. #include "internal.h"
  28. enum {
  29. SKCIPHER_WALK_PHYS = 1 << 0,
  30. SKCIPHER_WALK_SLOW = 1 << 1,
  31. SKCIPHER_WALK_COPY = 1 << 2,
  32. SKCIPHER_WALK_DIFF = 1 << 3,
  33. SKCIPHER_WALK_SLEEP = 1 << 4,
  34. };
  35. struct skcipher_walk_buffer {
  36. struct list_head entry;
  37. struct scatter_walk dst;
  38. unsigned int len;
  39. u8 *data;
  40. u8 buffer[];
  41. };
  42. static int skcipher_walk_next(struct skcipher_walk *walk);
  43. static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
  44. {
  45. if (PageHighMem(scatterwalk_page(walk)))
  46. kunmap_atomic(vaddr);
  47. }
  48. static inline void *skcipher_map(struct scatter_walk *walk)
  49. {
  50. struct page *page = scatterwalk_page(walk);
  51. return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
  52. offset_in_page(walk->offset);
  53. }
  54. static inline void skcipher_map_src(struct skcipher_walk *walk)
  55. {
  56. walk->src.virt.addr = skcipher_map(&walk->in);
  57. }
  58. static inline void skcipher_map_dst(struct skcipher_walk *walk)
  59. {
  60. walk->dst.virt.addr = skcipher_map(&walk->out);
  61. }
  62. static inline void skcipher_unmap_src(struct skcipher_walk *walk)
  63. {
  64. skcipher_unmap(&walk->in, walk->src.virt.addr);
  65. }
  66. static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
  67. {
  68. skcipher_unmap(&walk->out, walk->dst.virt.addr);
  69. }
  70. static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
  71. {
  72. return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  73. }
  74. /* Get a spot of the specified length that does not straddle a page.
  75. * The caller needs to ensure that there is enough space for this operation.
  76. */
  77. static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
  78. {
  79. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  80. return max(start, end_page);
  81. }
  82. static void skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
  83. {
  84. u8 *addr;
  85. addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  86. addr = skcipher_get_spot(addr, bsize);
  87. scatterwalk_copychunks(addr, &walk->out, bsize,
  88. (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
  89. }
  90. int skcipher_walk_done(struct skcipher_walk *walk, int err)
  91. {
  92. unsigned int n; /* bytes processed */
  93. bool more;
  94. if (unlikely(err < 0))
  95. goto finish;
  96. n = walk->nbytes - err;
  97. walk->total -= n;
  98. more = (walk->total != 0);
  99. if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
  100. SKCIPHER_WALK_SLOW |
  101. SKCIPHER_WALK_COPY |
  102. SKCIPHER_WALK_DIFF)))) {
  103. unmap_src:
  104. skcipher_unmap_src(walk);
  105. } else if (walk->flags & SKCIPHER_WALK_DIFF) {
  106. skcipher_unmap_dst(walk);
  107. goto unmap_src;
  108. } else if (walk->flags & SKCIPHER_WALK_COPY) {
  109. skcipher_map_dst(walk);
  110. memcpy(walk->dst.virt.addr, walk->page, n);
  111. skcipher_unmap_dst(walk);
  112. } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
  113. if (WARN_ON(err)) {
  114. /* unexpected case; didn't process all bytes */
  115. err = -EINVAL;
  116. goto finish;
  117. }
  118. skcipher_done_slow(walk, n);
  119. goto already_advanced;
  120. }
  121. scatterwalk_advance(&walk->in, n);
  122. scatterwalk_advance(&walk->out, n);
  123. already_advanced:
  124. scatterwalk_done(&walk->in, 0, more);
  125. scatterwalk_done(&walk->out, 1, more);
  126. if (more) {
  127. crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
  128. CRYPTO_TFM_REQ_MAY_SLEEP : 0);
  129. return skcipher_walk_next(walk);
  130. }
  131. err = 0;
  132. finish:
  133. walk->nbytes = 0;
  134. /* Short-circuit for the common/fast path. */
  135. if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
  136. goto out;
  137. if (walk->flags & SKCIPHER_WALK_PHYS)
  138. goto out;
  139. if (walk->iv != walk->oiv)
  140. memcpy(walk->oiv, walk->iv, walk->ivsize);
  141. if (walk->buffer != walk->page)
  142. kfree(walk->buffer);
  143. if (walk->page)
  144. free_page((unsigned long)walk->page);
  145. out:
  146. return err;
  147. }
  148. EXPORT_SYMBOL_GPL(skcipher_walk_done);
  149. void skcipher_walk_complete(struct skcipher_walk *walk, int err)
  150. {
  151. struct skcipher_walk_buffer *p, *tmp;
  152. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  153. u8 *data;
  154. if (err)
  155. goto done;
  156. data = p->data;
  157. if (!data) {
  158. data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
  159. data = skcipher_get_spot(data, walk->stride);
  160. }
  161. scatterwalk_copychunks(data, &p->dst, p->len, 1);
  162. if (offset_in_page(p->data) + p->len + walk->stride >
  163. PAGE_SIZE)
  164. free_page((unsigned long)p->data);
  165. done:
  166. list_del(&p->entry);
  167. kfree(p);
  168. }
  169. if (!err && walk->iv != walk->oiv)
  170. memcpy(walk->oiv, walk->iv, walk->ivsize);
  171. if (walk->buffer != walk->page)
  172. kfree(walk->buffer);
  173. if (walk->page)
  174. free_page((unsigned long)walk->page);
  175. }
  176. EXPORT_SYMBOL_GPL(skcipher_walk_complete);
  177. static void skcipher_queue_write(struct skcipher_walk *walk,
  178. struct skcipher_walk_buffer *p)
  179. {
  180. p->dst = walk->out;
  181. list_add_tail(&p->entry, &walk->buffers);
  182. }
  183. static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
  184. {
  185. bool phys = walk->flags & SKCIPHER_WALK_PHYS;
  186. unsigned alignmask = walk->alignmask;
  187. struct skcipher_walk_buffer *p;
  188. unsigned a;
  189. unsigned n;
  190. u8 *buffer;
  191. void *v;
  192. if (!phys) {
  193. if (!walk->buffer)
  194. walk->buffer = walk->page;
  195. buffer = walk->buffer;
  196. if (buffer)
  197. goto ok;
  198. }
  199. /* Start with the minimum alignment of kmalloc. */
  200. a = crypto_tfm_ctx_alignment() - 1;
  201. n = bsize;
  202. if (phys) {
  203. /* Calculate the minimum alignment of p->buffer. */
  204. a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
  205. n += sizeof(*p);
  206. }
  207. /* Minimum size to align p->buffer by alignmask. */
  208. n += alignmask & ~a;
  209. /* Minimum size to ensure p->buffer does not straddle a page. */
  210. n += (bsize - 1) & ~(alignmask | a);
  211. v = kzalloc(n, skcipher_walk_gfp(walk));
  212. if (!v)
  213. return skcipher_walk_done(walk, -ENOMEM);
  214. if (phys) {
  215. p = v;
  216. p->len = bsize;
  217. skcipher_queue_write(walk, p);
  218. buffer = p->buffer;
  219. } else {
  220. walk->buffer = v;
  221. buffer = v;
  222. }
  223. ok:
  224. walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
  225. walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
  226. walk->src.virt.addr = walk->dst.virt.addr;
  227. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  228. walk->nbytes = bsize;
  229. walk->flags |= SKCIPHER_WALK_SLOW;
  230. return 0;
  231. }
  232. static int skcipher_next_copy(struct skcipher_walk *walk)
  233. {
  234. struct skcipher_walk_buffer *p;
  235. u8 *tmp = walk->page;
  236. skcipher_map_src(walk);
  237. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  238. skcipher_unmap_src(walk);
  239. walk->src.virt.addr = tmp;
  240. walk->dst.virt.addr = tmp;
  241. if (!(walk->flags & SKCIPHER_WALK_PHYS))
  242. return 0;
  243. p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
  244. if (!p)
  245. return -ENOMEM;
  246. p->data = walk->page;
  247. p->len = walk->nbytes;
  248. skcipher_queue_write(walk, p);
  249. if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
  250. PAGE_SIZE)
  251. walk->page = NULL;
  252. else
  253. walk->page += walk->nbytes;
  254. return 0;
  255. }
  256. static int skcipher_next_fast(struct skcipher_walk *walk)
  257. {
  258. unsigned long diff;
  259. walk->src.phys.page = scatterwalk_page(&walk->in);
  260. walk->src.phys.offset = offset_in_page(walk->in.offset);
  261. walk->dst.phys.page = scatterwalk_page(&walk->out);
  262. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  263. if (walk->flags & SKCIPHER_WALK_PHYS)
  264. return 0;
  265. diff = walk->src.phys.offset - walk->dst.phys.offset;
  266. diff |= walk->src.virt.page - walk->dst.virt.page;
  267. skcipher_map_src(walk);
  268. walk->dst.virt.addr = walk->src.virt.addr;
  269. if (diff) {
  270. walk->flags |= SKCIPHER_WALK_DIFF;
  271. skcipher_map_dst(walk);
  272. }
  273. return 0;
  274. }
  275. static int skcipher_walk_next(struct skcipher_walk *walk)
  276. {
  277. unsigned int bsize;
  278. unsigned int n;
  279. int err;
  280. walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
  281. SKCIPHER_WALK_DIFF);
  282. n = walk->total;
  283. bsize = min(walk->stride, max(n, walk->blocksize));
  284. n = scatterwalk_clamp(&walk->in, n);
  285. n = scatterwalk_clamp(&walk->out, n);
  286. if (unlikely(n < bsize)) {
  287. if (unlikely(walk->total < walk->blocksize))
  288. return skcipher_walk_done(walk, -EINVAL);
  289. slow_path:
  290. err = skcipher_next_slow(walk, bsize);
  291. goto set_phys_lowmem;
  292. }
  293. if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
  294. if (!walk->page) {
  295. gfp_t gfp = skcipher_walk_gfp(walk);
  296. walk->page = (void *)__get_free_page(gfp);
  297. if (!walk->page)
  298. goto slow_path;
  299. }
  300. walk->nbytes = min_t(unsigned, n,
  301. PAGE_SIZE - offset_in_page(walk->page));
  302. walk->flags |= SKCIPHER_WALK_COPY;
  303. err = skcipher_next_copy(walk);
  304. goto set_phys_lowmem;
  305. }
  306. walk->nbytes = n;
  307. return skcipher_next_fast(walk);
  308. set_phys_lowmem:
  309. if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
  310. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  311. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  312. walk->src.phys.offset &= PAGE_SIZE - 1;
  313. walk->dst.phys.offset &= PAGE_SIZE - 1;
  314. }
  315. return err;
  316. }
  317. static int skcipher_copy_iv(struct skcipher_walk *walk)
  318. {
  319. unsigned a = crypto_tfm_ctx_alignment() - 1;
  320. unsigned alignmask = walk->alignmask;
  321. unsigned ivsize = walk->ivsize;
  322. unsigned bs = walk->stride;
  323. unsigned aligned_bs;
  324. unsigned size;
  325. u8 *iv;
  326. aligned_bs = ALIGN(bs, alignmask + 1);
  327. /* Minimum size to align buffer by alignmask. */
  328. size = alignmask & ~a;
  329. if (walk->flags & SKCIPHER_WALK_PHYS)
  330. size += ivsize;
  331. else {
  332. size += aligned_bs + ivsize;
  333. /* Minimum size to ensure buffer does not straddle a page. */
  334. size += (bs - 1) & ~(alignmask | a);
  335. }
  336. walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
  337. if (!walk->buffer)
  338. return -ENOMEM;
  339. iv = PTR_ALIGN(walk->buffer, alignmask + 1);
  340. iv = skcipher_get_spot(iv, bs) + aligned_bs;
  341. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  342. return 0;
  343. }
  344. static int skcipher_walk_first(struct skcipher_walk *walk)
  345. {
  346. if (WARN_ON_ONCE(in_irq()))
  347. return -EDEADLK;
  348. walk->buffer = NULL;
  349. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  350. int err = skcipher_copy_iv(walk);
  351. if (err)
  352. return err;
  353. }
  354. walk->page = NULL;
  355. return skcipher_walk_next(walk);
  356. }
  357. static int skcipher_walk_skcipher(struct skcipher_walk *walk,
  358. struct skcipher_request *req)
  359. {
  360. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  361. walk->total = req->cryptlen;
  362. walk->nbytes = 0;
  363. walk->iv = req->iv;
  364. walk->oiv = req->iv;
  365. if (unlikely(!walk->total))
  366. return 0;
  367. scatterwalk_start(&walk->in, req->src);
  368. scatterwalk_start(&walk->out, req->dst);
  369. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  370. walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  371. SKCIPHER_WALK_SLEEP : 0;
  372. walk->blocksize = crypto_skcipher_blocksize(tfm);
  373. walk->stride = crypto_skcipher_walksize(tfm);
  374. walk->ivsize = crypto_skcipher_ivsize(tfm);
  375. walk->alignmask = crypto_skcipher_alignmask(tfm);
  376. return skcipher_walk_first(walk);
  377. }
  378. int skcipher_walk_virt(struct skcipher_walk *walk,
  379. struct skcipher_request *req, bool atomic)
  380. {
  381. int err;
  382. walk->flags &= ~SKCIPHER_WALK_PHYS;
  383. err = skcipher_walk_skcipher(walk, req);
  384. walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
  385. return err;
  386. }
  387. EXPORT_SYMBOL_GPL(skcipher_walk_virt);
  388. void skcipher_walk_atomise(struct skcipher_walk *walk)
  389. {
  390. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  391. }
  392. EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
  393. int skcipher_walk_async(struct skcipher_walk *walk,
  394. struct skcipher_request *req)
  395. {
  396. walk->flags |= SKCIPHER_WALK_PHYS;
  397. INIT_LIST_HEAD(&walk->buffers);
  398. return skcipher_walk_skcipher(walk, req);
  399. }
  400. EXPORT_SYMBOL_GPL(skcipher_walk_async);
  401. static int skcipher_walk_aead_common(struct skcipher_walk *walk,
  402. struct aead_request *req, bool atomic)
  403. {
  404. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  405. int err;
  406. walk->nbytes = 0;
  407. walk->iv = req->iv;
  408. walk->oiv = req->iv;
  409. if (unlikely(!walk->total))
  410. return 0;
  411. walk->flags &= ~SKCIPHER_WALK_PHYS;
  412. scatterwalk_start(&walk->in, req->src);
  413. scatterwalk_start(&walk->out, req->dst);
  414. scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
  415. scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
  416. scatterwalk_done(&walk->in, 0, walk->total);
  417. scatterwalk_done(&walk->out, 0, walk->total);
  418. if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  419. walk->flags |= SKCIPHER_WALK_SLEEP;
  420. else
  421. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  422. walk->blocksize = crypto_aead_blocksize(tfm);
  423. walk->stride = crypto_aead_chunksize(tfm);
  424. walk->ivsize = crypto_aead_ivsize(tfm);
  425. walk->alignmask = crypto_aead_alignmask(tfm);
  426. err = skcipher_walk_first(walk);
  427. if (atomic)
  428. walk->flags &= ~SKCIPHER_WALK_SLEEP;
  429. return err;
  430. }
  431. int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
  432. bool atomic)
  433. {
  434. walk->total = req->cryptlen;
  435. return skcipher_walk_aead_common(walk, req, atomic);
  436. }
  437. EXPORT_SYMBOL_GPL(skcipher_walk_aead);
  438. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  439. struct aead_request *req, bool atomic)
  440. {
  441. walk->total = req->cryptlen;
  442. return skcipher_walk_aead_common(walk, req, atomic);
  443. }
  444. EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
  445. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  446. struct aead_request *req, bool atomic)
  447. {
  448. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  449. walk->total = req->cryptlen - crypto_aead_authsize(tfm);
  450. return skcipher_walk_aead_common(walk, req, atomic);
  451. }
  452. EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
  453. static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  454. {
  455. if (alg->cra_type == &crypto_blkcipher_type)
  456. return sizeof(struct crypto_blkcipher *);
  457. if (alg->cra_type == &crypto_ablkcipher_type ||
  458. alg->cra_type == &crypto_givcipher_type)
  459. return sizeof(struct crypto_ablkcipher *);
  460. return crypto_alg_extsize(alg);
  461. }
  462. static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
  463. const u8 *key, unsigned int keylen)
  464. {
  465. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  466. struct crypto_blkcipher *blkcipher = *ctx;
  467. int err;
  468. crypto_blkcipher_clear_flags(blkcipher, ~0);
  469. crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
  470. CRYPTO_TFM_REQ_MASK);
  471. err = crypto_blkcipher_setkey(blkcipher, key, keylen);
  472. crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
  473. CRYPTO_TFM_RES_MASK);
  474. if (err)
  475. return err;
  476. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  477. return 0;
  478. }
  479. static int skcipher_crypt_blkcipher(struct skcipher_request *req,
  480. int (*crypt)(struct blkcipher_desc *,
  481. struct scatterlist *,
  482. struct scatterlist *,
  483. unsigned int))
  484. {
  485. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  486. struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  487. struct blkcipher_desc desc = {
  488. .tfm = *ctx,
  489. .info = req->iv,
  490. .flags = req->base.flags,
  491. };
  492. return crypt(&desc, req->dst, req->src, req->cryptlen);
  493. }
  494. static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
  495. {
  496. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  497. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  498. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  499. return skcipher_crypt_blkcipher(req, alg->encrypt);
  500. }
  501. static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
  502. {
  503. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  504. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  505. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  506. return skcipher_crypt_blkcipher(req, alg->decrypt);
  507. }
  508. static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  509. {
  510. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  511. crypto_free_blkcipher(*ctx);
  512. }
  513. static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  514. {
  515. struct crypto_alg *calg = tfm->__crt_alg;
  516. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  517. struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  518. struct crypto_blkcipher *blkcipher;
  519. struct crypto_tfm *btfm;
  520. if (!crypto_mod_get(calg))
  521. return -EAGAIN;
  522. btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
  523. CRYPTO_ALG_TYPE_MASK);
  524. if (IS_ERR(btfm)) {
  525. crypto_mod_put(calg);
  526. return PTR_ERR(btfm);
  527. }
  528. blkcipher = __crypto_blkcipher_cast(btfm);
  529. *ctx = blkcipher;
  530. tfm->exit = crypto_exit_skcipher_ops_blkcipher;
  531. skcipher->setkey = skcipher_setkey_blkcipher;
  532. skcipher->encrypt = skcipher_encrypt_blkcipher;
  533. skcipher->decrypt = skcipher_decrypt_blkcipher;
  534. skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
  535. skcipher->keysize = calg->cra_blkcipher.max_keysize;
  536. if (skcipher->keysize)
  537. crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
  538. return 0;
  539. }
  540. static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
  541. const u8 *key, unsigned int keylen)
  542. {
  543. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  544. struct crypto_ablkcipher *ablkcipher = *ctx;
  545. int err;
  546. crypto_ablkcipher_clear_flags(ablkcipher, ~0);
  547. crypto_ablkcipher_set_flags(ablkcipher,
  548. crypto_skcipher_get_flags(tfm) &
  549. CRYPTO_TFM_REQ_MASK);
  550. err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
  551. crypto_skcipher_set_flags(tfm,
  552. crypto_ablkcipher_get_flags(ablkcipher) &
  553. CRYPTO_TFM_RES_MASK);
  554. if (err)
  555. return err;
  556. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  557. return 0;
  558. }
  559. static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
  560. int (*crypt)(struct ablkcipher_request *))
  561. {
  562. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  563. struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  564. struct ablkcipher_request *subreq = skcipher_request_ctx(req);
  565. ablkcipher_request_set_tfm(subreq, *ctx);
  566. ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
  567. req->base.complete, req->base.data);
  568. ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  569. req->iv);
  570. return crypt(subreq);
  571. }
  572. static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
  573. {
  574. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  575. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  576. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  577. return skcipher_crypt_ablkcipher(req, alg->encrypt);
  578. }
  579. static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
  580. {
  581. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  582. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  583. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  584. return skcipher_crypt_ablkcipher(req, alg->decrypt);
  585. }
  586. static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  587. {
  588. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  589. crypto_free_ablkcipher(*ctx);
  590. }
  591. static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  592. {
  593. struct crypto_alg *calg = tfm->__crt_alg;
  594. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  595. struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  596. struct crypto_ablkcipher *ablkcipher;
  597. struct crypto_tfm *abtfm;
  598. if (!crypto_mod_get(calg))
  599. return -EAGAIN;
  600. abtfm = __crypto_alloc_tfm(calg, 0, 0);
  601. if (IS_ERR(abtfm)) {
  602. crypto_mod_put(calg);
  603. return PTR_ERR(abtfm);
  604. }
  605. ablkcipher = __crypto_ablkcipher_cast(abtfm);
  606. *ctx = ablkcipher;
  607. tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
  608. skcipher->setkey = skcipher_setkey_ablkcipher;
  609. skcipher->encrypt = skcipher_encrypt_ablkcipher;
  610. skcipher->decrypt = skcipher_decrypt_ablkcipher;
  611. skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  612. skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
  613. sizeof(struct ablkcipher_request);
  614. skcipher->keysize = calg->cra_ablkcipher.max_keysize;
  615. if (skcipher->keysize)
  616. crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
  617. return 0;
  618. }
  619. static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
  620. const u8 *key, unsigned int keylen)
  621. {
  622. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  623. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  624. u8 *buffer, *alignbuffer;
  625. unsigned long absize;
  626. int ret;
  627. absize = keylen + alignmask;
  628. buffer = kmalloc(absize, GFP_ATOMIC);
  629. if (!buffer)
  630. return -ENOMEM;
  631. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  632. memcpy(alignbuffer, key, keylen);
  633. ret = cipher->setkey(tfm, alignbuffer, keylen);
  634. kzfree(buffer);
  635. return ret;
  636. }
  637. static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  638. unsigned int keylen)
  639. {
  640. struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
  641. unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  642. int err;
  643. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  644. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  645. return -EINVAL;
  646. }
  647. if ((unsigned long)key & alignmask)
  648. err = skcipher_setkey_unaligned(tfm, key, keylen);
  649. else
  650. err = cipher->setkey(tfm, key, keylen);
  651. if (err)
  652. return err;
  653. crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  654. return 0;
  655. }
  656. static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  657. {
  658. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  659. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  660. alg->exit(skcipher);
  661. }
  662. static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  663. {
  664. struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  665. struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  666. if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  667. return crypto_init_skcipher_ops_blkcipher(tfm);
  668. if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  669. tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  670. return crypto_init_skcipher_ops_ablkcipher(tfm);
  671. skcipher->setkey = skcipher_setkey;
  672. skcipher->encrypt = alg->encrypt;
  673. skcipher->decrypt = alg->decrypt;
  674. skcipher->ivsize = alg->ivsize;
  675. skcipher->keysize = alg->max_keysize;
  676. if (skcipher->keysize)
  677. crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY);
  678. if (alg->exit)
  679. skcipher->base.exit = crypto_skcipher_exit_tfm;
  680. if (alg->init)
  681. return alg->init(skcipher);
  682. return 0;
  683. }
  684. static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  685. {
  686. struct skcipher_instance *skcipher =
  687. container_of(inst, struct skcipher_instance, s.base);
  688. skcipher->free(skcipher);
  689. }
  690. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  691. __maybe_unused;
  692. static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  693. {
  694. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  695. base);
  696. seq_printf(m, "type : skcipher\n");
  697. seq_printf(m, "async : %s\n",
  698. alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
  699. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  700. seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
  701. seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
  702. seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
  703. seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
  704. seq_printf(m, "walksize : %u\n", skcipher->walksize);
  705. }
  706. #ifdef CONFIG_NET
  707. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  708. {
  709. struct crypto_report_blkcipher rblkcipher;
  710. struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  711. base);
  712. strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  713. strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  714. rblkcipher.blocksize = alg->cra_blocksize;
  715. rblkcipher.min_keysize = skcipher->min_keysize;
  716. rblkcipher.max_keysize = skcipher->max_keysize;
  717. rblkcipher.ivsize = skcipher->ivsize;
  718. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  719. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  720. goto nla_put_failure;
  721. return 0;
  722. nla_put_failure:
  723. return -EMSGSIZE;
  724. }
  725. #else
  726. static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  727. {
  728. return -ENOSYS;
  729. }
  730. #endif
  731. static const struct crypto_type crypto_skcipher_type2 = {
  732. .extsize = crypto_skcipher_extsize,
  733. .init_tfm = crypto_skcipher_init_tfm,
  734. .free = crypto_skcipher_free_instance,
  735. #ifdef CONFIG_PROC_FS
  736. .show = crypto_skcipher_show,
  737. #endif
  738. .report = crypto_skcipher_report,
  739. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  740. .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  741. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  742. .tfmsize = offsetof(struct crypto_skcipher, base),
  743. };
  744. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  745. const char *name, u32 type, u32 mask)
  746. {
  747. spawn->base.frontend = &crypto_skcipher_type2;
  748. return crypto_grab_spawn(&spawn->base, name, type, mask);
  749. }
  750. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  751. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  752. u32 type, u32 mask)
  753. {
  754. return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  755. }
  756. EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
  757. int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  758. {
  759. return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  760. type, mask);
  761. }
  762. EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  763. static int skcipher_prepare_alg(struct skcipher_alg *alg)
  764. {
  765. struct crypto_alg *base = &alg->base;
  766. if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
  767. alg->walksize > PAGE_SIZE / 8)
  768. return -EINVAL;
  769. if (!alg->chunksize)
  770. alg->chunksize = base->cra_blocksize;
  771. if (!alg->walksize)
  772. alg->walksize = alg->chunksize;
  773. base->cra_type = &crypto_skcipher_type2;
  774. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  775. base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  776. return 0;
  777. }
  778. int crypto_register_skcipher(struct skcipher_alg *alg)
  779. {
  780. struct crypto_alg *base = &alg->base;
  781. int err;
  782. err = skcipher_prepare_alg(alg);
  783. if (err)
  784. return err;
  785. return crypto_register_alg(base);
  786. }
  787. EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  788. void crypto_unregister_skcipher(struct skcipher_alg *alg)
  789. {
  790. crypto_unregister_alg(&alg->base);
  791. }
  792. EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  793. int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  794. {
  795. int i, ret;
  796. for (i = 0; i < count; i++) {
  797. ret = crypto_register_skcipher(&algs[i]);
  798. if (ret)
  799. goto err;
  800. }
  801. return 0;
  802. err:
  803. for (--i; i >= 0; --i)
  804. crypto_unregister_skcipher(&algs[i]);
  805. return ret;
  806. }
  807. EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  808. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  809. {
  810. int i;
  811. for (i = count - 1; i >= 0; --i)
  812. crypto_unregister_skcipher(&algs[i]);
  813. }
  814. EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  815. int skcipher_register_instance(struct crypto_template *tmpl,
  816. struct skcipher_instance *inst)
  817. {
  818. int err;
  819. err = skcipher_prepare_alg(&inst->alg);
  820. if (err)
  821. return err;
  822. return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  823. }
  824. EXPORT_SYMBOL_GPL(skcipher_register_instance);
  825. MODULE_LICENSE("GPL");
  826. MODULE_DESCRIPTION("Symmetric key cipher type");