chainiv.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * chainiv: Chain IV Generator
  3. *
  4. * Generate IVs simply be using the last block of the previous encryption.
  5. * This is mainly useful for CBC with a synchronous algorithm.
  6. *
  7. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/skcipher.h>
  16. #include <crypto/rng.h>
  17. #include <crypto/crypto_wq.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <linux/workqueue.h>
  25. enum {
  26. CHAINIV_STATE_INUSE = 0,
  27. };
  28. struct chainiv_ctx {
  29. spinlock_t lock;
  30. char iv[];
  31. };
  32. struct async_chainiv_ctx {
  33. unsigned long state;
  34. spinlock_t lock;
  35. int err;
  36. struct crypto_queue queue;
  37. struct work_struct postponed;
  38. char iv[];
  39. };
  40. static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  41. {
  42. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  43. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  44. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  45. unsigned int ivsize;
  46. int err;
  47. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  48. ablkcipher_request_set_callback(subreq, req->creq.base.flags &
  49. ~CRYPTO_TFM_REQ_MAY_SLEEP,
  50. req->creq.base.complete,
  51. req->creq.base.data);
  52. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  53. req->creq.nbytes, req->creq.info);
  54. spin_lock_bh(&ctx->lock);
  55. ivsize = crypto_ablkcipher_ivsize(geniv);
  56. memcpy(req->giv, ctx->iv, ivsize);
  57. memcpy(subreq->info, ctx->iv, ivsize);
  58. err = crypto_ablkcipher_encrypt(subreq);
  59. if (err)
  60. goto unlock;
  61. memcpy(ctx->iv, subreq->info, ivsize);
  62. unlock:
  63. spin_unlock_bh(&ctx->lock);
  64. return err;
  65. }
  66. static int chainiv_init_common(struct crypto_tfm *tfm, char iv[])
  67. {
  68. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  69. int err = 0;
  70. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  71. if (iv) {
  72. err = crypto_rng_get_bytes(crypto_default_rng, iv,
  73. crypto_ablkcipher_ivsize(geniv));
  74. crypto_put_default_rng();
  75. }
  76. return err ?: skcipher_geniv_init(tfm);
  77. }
  78. static int chainiv_init(struct crypto_tfm *tfm)
  79. {
  80. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  81. struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  82. char *iv;
  83. spin_lock_init(&ctx->lock);
  84. iv = NULL;
  85. if (!crypto_get_default_rng()) {
  86. crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
  87. iv = ctx->iv;
  88. }
  89. return chainiv_init_common(tfm, iv);
  90. }
  91. static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
  92. {
  93. int queued;
  94. int err = ctx->err;
  95. if (!ctx->queue.qlen) {
  96. smp_mb__before_atomic();
  97. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  98. if (!ctx->queue.qlen ||
  99. test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  100. goto out;
  101. }
  102. queued = queue_work(kcrypto_wq, &ctx->postponed);
  103. BUG_ON(!queued);
  104. out:
  105. return err;
  106. }
  107. static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
  108. {
  109. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  110. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  111. int err;
  112. spin_lock_bh(&ctx->lock);
  113. err = skcipher_enqueue_givcrypt(&ctx->queue, req);
  114. spin_unlock_bh(&ctx->lock);
  115. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  116. return err;
  117. ctx->err = err;
  118. return async_chainiv_schedule_work(ctx);
  119. }
  120. static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
  121. {
  122. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  123. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  124. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  125. unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
  126. memcpy(req->giv, ctx->iv, ivsize);
  127. memcpy(subreq->info, ctx->iv, ivsize);
  128. ctx->err = crypto_ablkcipher_encrypt(subreq);
  129. if (ctx->err)
  130. goto out;
  131. memcpy(ctx->iv, subreq->info, ivsize);
  132. out:
  133. return async_chainiv_schedule_work(ctx);
  134. }
  135. static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  136. {
  137. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  138. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  139. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  140. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  141. ablkcipher_request_set_callback(subreq, req->creq.base.flags,
  142. req->creq.base.complete,
  143. req->creq.base.data);
  144. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  145. req->creq.nbytes, req->creq.info);
  146. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  147. goto postpone;
  148. if (ctx->queue.qlen) {
  149. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  150. goto postpone;
  151. }
  152. return async_chainiv_givencrypt_tail(req);
  153. postpone:
  154. return async_chainiv_postpone_request(req);
  155. }
  156. static void async_chainiv_do_postponed(struct work_struct *work)
  157. {
  158. struct async_chainiv_ctx *ctx = container_of(work,
  159. struct async_chainiv_ctx,
  160. postponed);
  161. struct skcipher_givcrypt_request *req;
  162. struct ablkcipher_request *subreq;
  163. int err;
  164. /* Only handle one request at a time to avoid hogging keventd. */
  165. spin_lock_bh(&ctx->lock);
  166. req = skcipher_dequeue_givcrypt(&ctx->queue);
  167. spin_unlock_bh(&ctx->lock);
  168. if (!req) {
  169. async_chainiv_schedule_work(ctx);
  170. return;
  171. }
  172. subreq = skcipher_givcrypt_reqctx(req);
  173. subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
  174. err = async_chainiv_givencrypt_tail(req);
  175. local_bh_disable();
  176. skcipher_givcrypt_complete(req, err);
  177. local_bh_enable();
  178. }
  179. static int async_chainiv_init(struct crypto_tfm *tfm)
  180. {
  181. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  182. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  183. char *iv;
  184. spin_lock_init(&ctx->lock);
  185. crypto_init_queue(&ctx->queue, 100);
  186. INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
  187. iv = NULL;
  188. if (!crypto_get_default_rng()) {
  189. crypto_ablkcipher_crt(geniv)->givencrypt =
  190. async_chainiv_givencrypt;
  191. iv = ctx->iv;
  192. }
  193. return chainiv_init_common(tfm, iv);
  194. }
  195. static void async_chainiv_exit(struct crypto_tfm *tfm)
  196. {
  197. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  198. BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
  199. skcipher_geniv_exit(tfm);
  200. }
  201. static struct crypto_template chainiv_tmpl;
  202. static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
  203. {
  204. struct crypto_attr_type *algt;
  205. struct crypto_instance *inst;
  206. algt = crypto_get_attr_type(tb);
  207. if (IS_ERR(algt))
  208. return ERR_CAST(algt);
  209. inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
  210. if (IS_ERR(inst))
  211. goto out;
  212. inst->alg.cra_init = chainiv_init;
  213. inst->alg.cra_exit = skcipher_geniv_exit;
  214. inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
  215. if (!crypto_requires_sync(algt->type, algt->mask)) {
  216. inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
  217. inst->alg.cra_init = async_chainiv_init;
  218. inst->alg.cra_exit = async_chainiv_exit;
  219. inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
  220. }
  221. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  222. out:
  223. return inst;
  224. }
  225. static struct crypto_template chainiv_tmpl = {
  226. .name = "chainiv",
  227. .alloc = chainiv_alloc,
  228. .free = skcipher_geniv_free,
  229. .module = THIS_MODULE,
  230. };
  231. static int __init chainiv_module_init(void)
  232. {
  233. return crypto_register_template(&chainiv_tmpl);
  234. }
  235. static void chainiv_module_exit(void)
  236. {
  237. crypto_unregister_template(&chainiv_tmpl);
  238. }
  239. module_init(chainiv_module_init);
  240. module_exit(chainiv_module_exit);
  241. MODULE_LICENSE("GPL");
  242. MODULE_DESCRIPTION("Chain IV Generator");
  243. MODULE_ALIAS_CRYPTO("chainiv");