chainiv.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_givencrypt_first(struct skcipher_givcrypt_request *req)
  67. {
  68. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  69. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  70. int err = 0;
  71. spin_lock_bh(&ctx->lock);
  72. if (crypto_ablkcipher_crt(geniv)->givencrypt !=
  73. chainiv_givencrypt_first)
  74. goto unlock;
  75. crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
  76. err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
  77. crypto_ablkcipher_ivsize(geniv));
  78. unlock:
  79. spin_unlock_bh(&ctx->lock);
  80. if (err)
  81. return err;
  82. return chainiv_givencrypt(req);
  83. }
  84. static int chainiv_init_common(struct crypto_tfm *tfm)
  85. {
  86. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  87. return skcipher_geniv_init(tfm);
  88. }
  89. static int chainiv_init(struct crypto_tfm *tfm)
  90. {
  91. struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  92. spin_lock_init(&ctx->lock);
  93. return chainiv_init_common(tfm);
  94. }
  95. static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
  96. {
  97. int queued;
  98. int err = ctx->err;
  99. if (!ctx->queue.qlen) {
  100. smp_mb__before_clear_bit();
  101. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  102. if (!ctx->queue.qlen ||
  103. test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  104. goto out;
  105. }
  106. queued = queue_work(kcrypto_wq, &ctx->postponed);
  107. BUG_ON(!queued);
  108. out:
  109. return err;
  110. }
  111. static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
  112. {
  113. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  114. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  115. int err;
  116. spin_lock_bh(&ctx->lock);
  117. err = skcipher_enqueue_givcrypt(&ctx->queue, req);
  118. spin_unlock_bh(&ctx->lock);
  119. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  120. return err;
  121. ctx->err = err;
  122. return async_chainiv_schedule_work(ctx);
  123. }
  124. static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
  125. {
  126. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  127. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  128. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  129. unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
  130. memcpy(req->giv, ctx->iv, ivsize);
  131. memcpy(subreq->info, ctx->iv, ivsize);
  132. ctx->err = crypto_ablkcipher_encrypt(subreq);
  133. if (ctx->err)
  134. goto out;
  135. memcpy(ctx->iv, subreq->info, ivsize);
  136. out:
  137. return async_chainiv_schedule_work(ctx);
  138. }
  139. static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  140. {
  141. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  142. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  143. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  144. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  145. ablkcipher_request_set_callback(subreq, req->creq.base.flags,
  146. req->creq.base.complete,
  147. req->creq.base.data);
  148. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  149. req->creq.nbytes, req->creq.info);
  150. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  151. goto postpone;
  152. if (ctx->queue.qlen) {
  153. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  154. goto postpone;
  155. }
  156. return async_chainiv_givencrypt_tail(req);
  157. postpone:
  158. return async_chainiv_postpone_request(req);
  159. }
  160. static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  161. {
  162. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  163. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  164. int err = 0;
  165. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  166. goto out;
  167. if (crypto_ablkcipher_crt(geniv)->givencrypt !=
  168. async_chainiv_givencrypt_first)
  169. goto unlock;
  170. crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt;
  171. err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
  172. crypto_ablkcipher_ivsize(geniv));
  173. unlock:
  174. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  175. if (err)
  176. return err;
  177. out:
  178. return async_chainiv_givencrypt(req);
  179. }
  180. static void async_chainiv_do_postponed(struct work_struct *work)
  181. {
  182. struct async_chainiv_ctx *ctx = container_of(work,
  183. struct async_chainiv_ctx,
  184. postponed);
  185. struct skcipher_givcrypt_request *req;
  186. struct ablkcipher_request *subreq;
  187. int err;
  188. /* Only handle one request at a time to avoid hogging keventd. */
  189. spin_lock_bh(&ctx->lock);
  190. req = skcipher_dequeue_givcrypt(&ctx->queue);
  191. spin_unlock_bh(&ctx->lock);
  192. if (!req) {
  193. async_chainiv_schedule_work(ctx);
  194. return;
  195. }
  196. subreq = skcipher_givcrypt_reqctx(req);
  197. subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
  198. err = async_chainiv_givencrypt_tail(req);
  199. local_bh_disable();
  200. skcipher_givcrypt_complete(req, err);
  201. local_bh_enable();
  202. }
  203. static int async_chainiv_init(struct crypto_tfm *tfm)
  204. {
  205. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  206. spin_lock_init(&ctx->lock);
  207. crypto_init_queue(&ctx->queue, 100);
  208. INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
  209. return chainiv_init_common(tfm);
  210. }
  211. static void async_chainiv_exit(struct crypto_tfm *tfm)
  212. {
  213. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  214. BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
  215. skcipher_geniv_exit(tfm);
  216. }
  217. static struct crypto_template chainiv_tmpl;
  218. static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
  219. {
  220. struct crypto_attr_type *algt;
  221. struct crypto_instance *inst;
  222. int err;
  223. algt = crypto_get_attr_type(tb);
  224. err = PTR_ERR(algt);
  225. if (IS_ERR(algt))
  226. return ERR_PTR(err);
  227. err = crypto_get_default_rng();
  228. if (err)
  229. return ERR_PTR(err);
  230. inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
  231. if (IS_ERR(inst))
  232. goto put_rng;
  233. inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
  234. inst->alg.cra_init = chainiv_init;
  235. inst->alg.cra_exit = skcipher_geniv_exit;
  236. inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
  237. if (!crypto_requires_sync(algt->type, algt->mask)) {
  238. inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
  239. inst->alg.cra_ablkcipher.givencrypt =
  240. async_chainiv_givencrypt_first;
  241. inst->alg.cra_init = async_chainiv_init;
  242. inst->alg.cra_exit = async_chainiv_exit;
  243. inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
  244. }
  245. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  246. out:
  247. return inst;
  248. put_rng:
  249. crypto_put_default_rng();
  250. goto out;
  251. }
  252. static void chainiv_free(struct crypto_instance *inst)
  253. {
  254. skcipher_geniv_free(inst);
  255. crypto_put_default_rng();
  256. }
  257. static struct crypto_template chainiv_tmpl = {
  258. .name = "chainiv",
  259. .alloc = chainiv_alloc,
  260. .free = chainiv_free,
  261. .module = THIS_MODULE,
  262. };
  263. static int __init chainiv_module_init(void)
  264. {
  265. return crypto_register_template(&chainiv_tmpl);
  266. }
  267. static void chainiv_module_exit(void)
  268. {
  269. crypto_unregister_template(&chainiv_tmpl);
  270. }
  271. module_init(chainiv_module_init);
  272. module_exit(chainiv_module_exit);
  273. MODULE_LICENSE("GPL");
  274. MODULE_DESCRIPTION("Chain IV Generator");