blkcipher.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Block chaining 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) 2006 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/aead.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/errno.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/cryptouser.h>
  28. #include <net/netlink.h>
  29. #include "internal.h"
  30. enum {
  31. BLKCIPHER_WALK_PHYS = 1 << 0,
  32. BLKCIPHER_WALK_SLOW = 1 << 1,
  33. BLKCIPHER_WALK_COPY = 1 << 2,
  34. BLKCIPHER_WALK_DIFF = 1 << 3,
  35. };
  36. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  37. struct blkcipher_walk *walk);
  38. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  39. struct blkcipher_walk *walk);
  40. static inline void blkcipher_map_src(struct blkcipher_walk *walk)
  41. {
  42. walk->src.virt.addr = scatterwalk_map(&walk->in);
  43. }
  44. static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
  45. {
  46. walk->dst.virt.addr = scatterwalk_map(&walk->out);
  47. }
  48. static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
  49. {
  50. scatterwalk_unmap(walk->src.virt.addr);
  51. }
  52. static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
  53. {
  54. scatterwalk_unmap(walk->dst.virt.addr);
  55. }
  56. /* Get a spot of the specified length that does not straddle a page.
  57. * The caller needs to ensure that there is enough space for this operation.
  58. */
  59. static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
  60. {
  61. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  62. return max(start, end_page);
  63. }
  64. static inline void blkcipher_done_slow(struct blkcipher_walk *walk,
  65. unsigned int bsize)
  66. {
  67. u8 *addr;
  68. addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  69. addr = blkcipher_get_spot(addr, bsize);
  70. scatterwalk_copychunks(addr, &walk->out, bsize, 1);
  71. }
  72. static inline void blkcipher_done_fast(struct blkcipher_walk *walk,
  73. unsigned int n)
  74. {
  75. if (walk->flags & BLKCIPHER_WALK_COPY) {
  76. blkcipher_map_dst(walk);
  77. memcpy(walk->dst.virt.addr, walk->page, n);
  78. blkcipher_unmap_dst(walk);
  79. } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
  80. if (walk->flags & BLKCIPHER_WALK_DIFF)
  81. blkcipher_unmap_dst(walk);
  82. blkcipher_unmap_src(walk);
  83. }
  84. scatterwalk_advance(&walk->in, n);
  85. scatterwalk_advance(&walk->out, n);
  86. }
  87. int blkcipher_walk_done(struct blkcipher_desc *desc,
  88. struct blkcipher_walk *walk, int err)
  89. {
  90. unsigned int n; /* bytes processed */
  91. bool more;
  92. if (unlikely(err < 0))
  93. goto finish;
  94. n = walk->nbytes - err;
  95. walk->total -= n;
  96. more = (walk->total != 0);
  97. if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW))) {
  98. blkcipher_done_fast(walk, n);
  99. } else {
  100. if (WARN_ON(err)) {
  101. /* unexpected case; didn't process all bytes */
  102. err = -EINVAL;
  103. goto finish;
  104. }
  105. blkcipher_done_slow(walk, n);
  106. }
  107. scatterwalk_done(&walk->in, 0, more);
  108. scatterwalk_done(&walk->out, 1, more);
  109. if (more) {
  110. crypto_yield(desc->flags);
  111. return blkcipher_walk_next(desc, walk);
  112. }
  113. err = 0;
  114. finish:
  115. walk->nbytes = 0;
  116. if (walk->iv != desc->info)
  117. memcpy(desc->info, walk->iv, walk->ivsize);
  118. if (walk->buffer != walk->page)
  119. kfree(walk->buffer);
  120. if (walk->page)
  121. free_page((unsigned long)walk->page);
  122. return err;
  123. }
  124. EXPORT_SYMBOL_GPL(blkcipher_walk_done);
  125. static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
  126. struct blkcipher_walk *walk,
  127. unsigned int bsize,
  128. unsigned int alignmask)
  129. {
  130. unsigned int n;
  131. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  132. if (walk->buffer)
  133. goto ok;
  134. walk->buffer = walk->page;
  135. if (walk->buffer)
  136. goto ok;
  137. n = aligned_bsize * 3 - (alignmask + 1) +
  138. (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  139. walk->buffer = kmalloc(n, GFP_ATOMIC);
  140. if (!walk->buffer)
  141. return blkcipher_walk_done(desc, walk, -ENOMEM);
  142. ok:
  143. walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
  144. alignmask + 1);
  145. walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
  146. walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr +
  147. aligned_bsize, bsize);
  148. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  149. walk->nbytes = bsize;
  150. walk->flags |= BLKCIPHER_WALK_SLOW;
  151. return 0;
  152. }
  153. static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
  154. {
  155. u8 *tmp = walk->page;
  156. blkcipher_map_src(walk);
  157. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  158. blkcipher_unmap_src(walk);
  159. walk->src.virt.addr = tmp;
  160. walk->dst.virt.addr = tmp;
  161. return 0;
  162. }
  163. static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
  164. struct blkcipher_walk *walk)
  165. {
  166. unsigned long diff;
  167. walk->src.phys.page = scatterwalk_page(&walk->in);
  168. walk->src.phys.offset = offset_in_page(walk->in.offset);
  169. walk->dst.phys.page = scatterwalk_page(&walk->out);
  170. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  171. if (walk->flags & BLKCIPHER_WALK_PHYS)
  172. return 0;
  173. diff = walk->src.phys.offset - walk->dst.phys.offset;
  174. diff |= walk->src.virt.page - walk->dst.virt.page;
  175. blkcipher_map_src(walk);
  176. walk->dst.virt.addr = walk->src.virt.addr;
  177. if (diff) {
  178. walk->flags |= BLKCIPHER_WALK_DIFF;
  179. blkcipher_map_dst(walk);
  180. }
  181. return 0;
  182. }
  183. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  184. struct blkcipher_walk *walk)
  185. {
  186. unsigned int bsize;
  187. unsigned int n;
  188. int err;
  189. n = walk->total;
  190. if (unlikely(n < walk->cipher_blocksize)) {
  191. desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  192. return blkcipher_walk_done(desc, walk, -EINVAL);
  193. }
  194. bsize = min(walk->walk_blocksize, n);
  195. walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
  196. BLKCIPHER_WALK_DIFF);
  197. if (!scatterwalk_aligned(&walk->in, walk->alignmask) ||
  198. !scatterwalk_aligned(&walk->out, walk->alignmask)) {
  199. walk->flags |= BLKCIPHER_WALK_COPY;
  200. if (!walk->page) {
  201. walk->page = (void *)__get_free_page(GFP_ATOMIC);
  202. if (!walk->page)
  203. n = 0;
  204. }
  205. }
  206. n = scatterwalk_clamp(&walk->in, n);
  207. n = scatterwalk_clamp(&walk->out, n);
  208. if (unlikely(n < bsize)) {
  209. err = blkcipher_next_slow(desc, walk, bsize, walk->alignmask);
  210. goto set_phys_lowmem;
  211. }
  212. walk->nbytes = n;
  213. if (walk->flags & BLKCIPHER_WALK_COPY) {
  214. err = blkcipher_next_copy(walk);
  215. goto set_phys_lowmem;
  216. }
  217. return blkcipher_next_fast(desc, walk);
  218. set_phys_lowmem:
  219. if (walk->flags & BLKCIPHER_WALK_PHYS) {
  220. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  221. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  222. walk->src.phys.offset &= PAGE_SIZE - 1;
  223. walk->dst.phys.offset &= PAGE_SIZE - 1;
  224. }
  225. return err;
  226. }
  227. static inline int blkcipher_copy_iv(struct blkcipher_walk *walk)
  228. {
  229. unsigned bs = walk->walk_blocksize;
  230. unsigned aligned_bs = ALIGN(bs, walk->alignmask + 1);
  231. unsigned int size = aligned_bs * 2 +
  232. walk->ivsize + max(aligned_bs, walk->ivsize) -
  233. (walk->alignmask + 1);
  234. u8 *iv;
  235. size += walk->alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  236. walk->buffer = kmalloc(size, GFP_ATOMIC);
  237. if (!walk->buffer)
  238. return -ENOMEM;
  239. iv = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  240. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  241. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  242. iv = blkcipher_get_spot(iv, walk->ivsize);
  243. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  244. return 0;
  245. }
  246. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  247. struct blkcipher_walk *walk)
  248. {
  249. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  250. walk->walk_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  251. walk->cipher_blocksize = walk->walk_blocksize;
  252. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  253. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  254. return blkcipher_walk_first(desc, walk);
  255. }
  256. EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
  257. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  258. struct blkcipher_walk *walk)
  259. {
  260. walk->flags |= BLKCIPHER_WALK_PHYS;
  261. walk->walk_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  262. walk->cipher_blocksize = walk->walk_blocksize;
  263. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  264. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  265. return blkcipher_walk_first(desc, walk);
  266. }
  267. EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
  268. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  269. struct blkcipher_walk *walk)
  270. {
  271. if (WARN_ON_ONCE(in_irq()))
  272. return -EDEADLK;
  273. walk->iv = desc->info;
  274. walk->nbytes = walk->total;
  275. if (unlikely(!walk->total))
  276. return 0;
  277. walk->buffer = NULL;
  278. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  279. int err = blkcipher_copy_iv(walk);
  280. if (err)
  281. return err;
  282. }
  283. scatterwalk_start(&walk->in, walk->in.sg);
  284. scatterwalk_start(&walk->out, walk->out.sg);
  285. walk->page = NULL;
  286. return blkcipher_walk_next(desc, walk);
  287. }
  288. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  289. struct blkcipher_walk *walk,
  290. unsigned int blocksize)
  291. {
  292. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  293. walk->walk_blocksize = blocksize;
  294. walk->cipher_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  295. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  296. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  297. return blkcipher_walk_first(desc, walk);
  298. }
  299. EXPORT_SYMBOL_GPL(blkcipher_walk_virt_block);
  300. int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
  301. struct blkcipher_walk *walk,
  302. struct crypto_aead *tfm,
  303. unsigned int blocksize)
  304. {
  305. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  306. walk->walk_blocksize = blocksize;
  307. walk->cipher_blocksize = crypto_aead_blocksize(tfm);
  308. walk->ivsize = crypto_aead_ivsize(tfm);
  309. walk->alignmask = crypto_aead_alignmask(tfm);
  310. return blkcipher_walk_first(desc, walk);
  311. }
  312. EXPORT_SYMBOL_GPL(blkcipher_aead_walk_virt_block);
  313. static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
  314. unsigned int keylen)
  315. {
  316. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  317. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  318. int ret;
  319. u8 *buffer, *alignbuffer;
  320. unsigned long absize;
  321. absize = keylen + alignmask;
  322. buffer = kmalloc(absize, GFP_ATOMIC);
  323. if (!buffer)
  324. return -ENOMEM;
  325. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  326. memcpy(alignbuffer, key, keylen);
  327. ret = cipher->setkey(tfm, alignbuffer, keylen);
  328. memset(alignbuffer, 0, keylen);
  329. kfree(buffer);
  330. return ret;
  331. }
  332. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  333. {
  334. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  335. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  336. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  337. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  338. return -EINVAL;
  339. }
  340. if ((unsigned long)key & alignmask)
  341. return setkey_unaligned(tfm, key, keylen);
  342. return cipher->setkey(tfm, key, keylen);
  343. }
  344. static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  345. unsigned int keylen)
  346. {
  347. return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
  348. }
  349. static int async_encrypt(struct ablkcipher_request *req)
  350. {
  351. struct crypto_tfm *tfm = req->base.tfm;
  352. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  353. struct blkcipher_desc desc = {
  354. .tfm = __crypto_blkcipher_cast(tfm),
  355. .info = req->info,
  356. .flags = req->base.flags,
  357. };
  358. return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
  359. }
  360. static int async_decrypt(struct ablkcipher_request *req)
  361. {
  362. struct crypto_tfm *tfm = req->base.tfm;
  363. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  364. struct blkcipher_desc desc = {
  365. .tfm = __crypto_blkcipher_cast(tfm),
  366. .info = req->info,
  367. .flags = req->base.flags,
  368. };
  369. return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
  370. }
  371. static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  372. u32 mask)
  373. {
  374. struct blkcipher_alg *cipher = &alg->cra_blkcipher;
  375. unsigned int len = alg->cra_ctxsize;
  376. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK &&
  377. cipher->ivsize) {
  378. len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
  379. len += cipher->ivsize;
  380. }
  381. return len;
  382. }
  383. static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
  384. {
  385. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  386. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  387. crt->setkey = async_setkey;
  388. crt->encrypt = async_encrypt;
  389. crt->decrypt = async_decrypt;
  390. if (!alg->ivsize) {
  391. crt->givencrypt = skcipher_null_givencrypt;
  392. crt->givdecrypt = skcipher_null_givdecrypt;
  393. }
  394. crt->base = __crypto_ablkcipher_cast(tfm);
  395. crt->ivsize = alg->ivsize;
  396. return 0;
  397. }
  398. static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
  399. {
  400. struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
  401. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  402. unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
  403. unsigned long addr;
  404. crt->setkey = setkey;
  405. crt->encrypt = alg->encrypt;
  406. crt->decrypt = alg->decrypt;
  407. addr = (unsigned long)crypto_tfm_ctx(tfm);
  408. addr = ALIGN(addr, align);
  409. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  410. crt->iv = (void *)addr;
  411. return 0;
  412. }
  413. static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  414. {
  415. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  416. if (alg->ivsize > PAGE_SIZE / 8)
  417. return -EINVAL;
  418. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK)
  419. return crypto_init_blkcipher_ops_sync(tfm);
  420. else
  421. return crypto_init_blkcipher_ops_async(tfm);
  422. }
  423. #ifdef CONFIG_NET
  424. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  425. {
  426. struct crypto_report_blkcipher rblkcipher;
  427. strncpy(rblkcipher.type, "blkcipher", sizeof(rblkcipher.type));
  428. strncpy(rblkcipher.geniv, alg->cra_blkcipher.geniv ?: "<default>",
  429. sizeof(rblkcipher.geniv));
  430. rblkcipher.geniv[sizeof(rblkcipher.geniv) - 1] = '\0';
  431. rblkcipher.blocksize = alg->cra_blocksize;
  432. rblkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  433. rblkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  434. rblkcipher.ivsize = alg->cra_blkcipher.ivsize;
  435. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  436. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  437. goto nla_put_failure;
  438. return 0;
  439. nla_put_failure:
  440. return -EMSGSIZE;
  441. }
  442. #else
  443. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  444. {
  445. return -ENOSYS;
  446. }
  447. #endif
  448. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  449. __attribute__ ((unused));
  450. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  451. {
  452. seq_printf(m, "type : blkcipher\n");
  453. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  454. seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
  455. seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
  456. seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
  457. seq_printf(m, "geniv : %s\n", alg->cra_blkcipher.geniv ?:
  458. "<default>");
  459. }
  460. const struct crypto_type crypto_blkcipher_type = {
  461. .ctxsize = crypto_blkcipher_ctxsize,
  462. .init = crypto_init_blkcipher_ops,
  463. #ifdef CONFIG_PROC_FS
  464. .show = crypto_blkcipher_show,
  465. #endif
  466. .report = crypto_blkcipher_report,
  467. };
  468. EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
  469. static int crypto_grab_nivcipher(struct crypto_skcipher_spawn *spawn,
  470. const char *name, u32 type, u32 mask)
  471. {
  472. struct crypto_alg *alg;
  473. int err;
  474. type = crypto_skcipher_type(type);
  475. mask = crypto_skcipher_mask(mask)| CRYPTO_ALG_GENIV;
  476. alg = crypto_alg_mod_lookup(name, type, mask);
  477. if (IS_ERR(alg))
  478. return PTR_ERR(alg);
  479. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  480. crypto_mod_put(alg);
  481. return err;
  482. }
  483. struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,
  484. struct rtattr **tb, u32 type,
  485. u32 mask)
  486. {
  487. struct {
  488. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  489. unsigned int keylen);
  490. int (*encrypt)(struct ablkcipher_request *req);
  491. int (*decrypt)(struct ablkcipher_request *req);
  492. unsigned int min_keysize;
  493. unsigned int max_keysize;
  494. unsigned int ivsize;
  495. const char *geniv;
  496. } balg;
  497. const char *name;
  498. struct crypto_skcipher_spawn *spawn;
  499. struct crypto_attr_type *algt;
  500. struct crypto_instance *inst;
  501. struct crypto_alg *alg;
  502. int err;
  503. algt = crypto_get_attr_type(tb);
  504. if (IS_ERR(algt))
  505. return ERR_CAST(algt);
  506. if ((algt->type ^ (CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV)) &
  507. algt->mask)
  508. return ERR_PTR(-EINVAL);
  509. name = crypto_attr_alg_name(tb[1]);
  510. if (IS_ERR(name))
  511. return ERR_CAST(name);
  512. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  513. if (!inst)
  514. return ERR_PTR(-ENOMEM);
  515. spawn = crypto_instance_ctx(inst);
  516. /* Ignore async algorithms if necessary. */
  517. mask |= crypto_requires_sync(algt->type, algt->mask);
  518. crypto_set_skcipher_spawn(spawn, inst);
  519. err = crypto_grab_nivcipher(spawn, name, type, mask);
  520. if (err)
  521. goto err_free_inst;
  522. alg = crypto_skcipher_spawn_alg(spawn);
  523. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  524. CRYPTO_ALG_TYPE_BLKCIPHER) {
  525. balg.ivsize = alg->cra_blkcipher.ivsize;
  526. balg.min_keysize = alg->cra_blkcipher.min_keysize;
  527. balg.max_keysize = alg->cra_blkcipher.max_keysize;
  528. balg.setkey = async_setkey;
  529. balg.encrypt = async_encrypt;
  530. balg.decrypt = async_decrypt;
  531. balg.geniv = alg->cra_blkcipher.geniv;
  532. } else {
  533. balg.ivsize = alg->cra_ablkcipher.ivsize;
  534. balg.min_keysize = alg->cra_ablkcipher.min_keysize;
  535. balg.max_keysize = alg->cra_ablkcipher.max_keysize;
  536. balg.setkey = alg->cra_ablkcipher.setkey;
  537. balg.encrypt = alg->cra_ablkcipher.encrypt;
  538. balg.decrypt = alg->cra_ablkcipher.decrypt;
  539. balg.geniv = alg->cra_ablkcipher.geniv;
  540. }
  541. err = -EINVAL;
  542. if (!balg.ivsize)
  543. goto err_drop_alg;
  544. /*
  545. * This is only true if we're constructing an algorithm with its
  546. * default IV generator. For the default generator we elide the
  547. * template name and double-check the IV generator.
  548. */
  549. if (algt->mask & CRYPTO_ALG_GENIV) {
  550. if (!balg.geniv)
  551. balg.geniv = crypto_default_geniv(alg);
  552. err = -EAGAIN;
  553. if (strcmp(tmpl->name, balg.geniv))
  554. goto err_drop_alg;
  555. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  556. memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
  557. CRYPTO_MAX_ALG_NAME);
  558. } else {
  559. err = -ENAMETOOLONG;
  560. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  561. "%s(%s)", tmpl->name, alg->cra_name) >=
  562. CRYPTO_MAX_ALG_NAME)
  563. goto err_drop_alg;
  564. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  565. "%s(%s)", tmpl->name, alg->cra_driver_name) >=
  566. CRYPTO_MAX_ALG_NAME)
  567. goto err_drop_alg;
  568. }
  569. inst->alg.cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV;
  570. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  571. inst->alg.cra_priority = alg->cra_priority;
  572. inst->alg.cra_blocksize = alg->cra_blocksize;
  573. inst->alg.cra_alignmask = alg->cra_alignmask;
  574. inst->alg.cra_type = &crypto_givcipher_type;
  575. inst->alg.cra_ablkcipher.ivsize = balg.ivsize;
  576. inst->alg.cra_ablkcipher.min_keysize = balg.min_keysize;
  577. inst->alg.cra_ablkcipher.max_keysize = balg.max_keysize;
  578. inst->alg.cra_ablkcipher.geniv = balg.geniv;
  579. inst->alg.cra_ablkcipher.setkey = balg.setkey;
  580. inst->alg.cra_ablkcipher.encrypt = balg.encrypt;
  581. inst->alg.cra_ablkcipher.decrypt = balg.decrypt;
  582. out:
  583. return inst;
  584. err_drop_alg:
  585. crypto_drop_skcipher(spawn);
  586. err_free_inst:
  587. kfree(inst);
  588. inst = ERR_PTR(err);
  589. goto out;
  590. }
  591. EXPORT_SYMBOL_GPL(skcipher_geniv_alloc);
  592. void skcipher_geniv_free(struct crypto_instance *inst)
  593. {
  594. crypto_drop_skcipher(crypto_instance_ctx(inst));
  595. kfree(inst);
  596. }
  597. EXPORT_SYMBOL_GPL(skcipher_geniv_free);
  598. int skcipher_geniv_init(struct crypto_tfm *tfm)
  599. {
  600. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  601. struct crypto_ablkcipher *cipher;
  602. cipher = crypto_spawn_skcipher(crypto_instance_ctx(inst));
  603. if (IS_ERR(cipher))
  604. return PTR_ERR(cipher);
  605. tfm->crt_ablkcipher.base = cipher;
  606. tfm->crt_ablkcipher.reqsize += crypto_ablkcipher_reqsize(cipher);
  607. return 0;
  608. }
  609. EXPORT_SYMBOL_GPL(skcipher_geniv_init);
  610. void skcipher_geniv_exit(struct crypto_tfm *tfm)
  611. {
  612. crypto_free_ablkcipher(tfm->crt_ablkcipher.base);
  613. }
  614. EXPORT_SYMBOL_GPL(skcipher_geniv_exit);
  615. MODULE_LICENSE("GPL");
  616. MODULE_DESCRIPTION("Generic block chaining cipher type");