chainiv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/random.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. #include <linux/workqueue.h>
  24. enum {
  25. CHAINIV_STATE_INUSE = 0,
  26. };
  27. struct chainiv_ctx {
  28. spinlock_t lock;
  29. char iv[];
  30. };
  31. struct async_chainiv_ctx {
  32. unsigned long state;
  33. spinlock_t lock;
  34. int err;
  35. struct crypto_queue queue;
  36. struct work_struct postponed;
  37. char iv[];
  38. };
  39. static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  40. {
  41. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  42. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  43. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  44. unsigned int ivsize;
  45. int err;
  46. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  47. ablkcipher_request_set_callback(subreq, req->creq.base.flags &
  48. ~CRYPTO_TFM_REQ_MAY_SLEEP,
  49. req->creq.base.complete,
  50. req->creq.base.data);
  51. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  52. req->creq.nbytes, req->creq.info);
  53. spin_lock_bh(&ctx->lock);
  54. ivsize = crypto_ablkcipher_ivsize(geniv);
  55. memcpy(req->giv, ctx->iv, ivsize);
  56. memcpy(subreq->info, ctx->iv, ivsize);
  57. err = crypto_ablkcipher_encrypt(subreq);
  58. if (err)
  59. goto unlock;
  60. memcpy(ctx->iv, subreq->info, ivsize);
  61. unlock:
  62. spin_unlock_bh(&ctx->lock);
  63. return err;
  64. }
  65. static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  66. {
  67. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  68. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  69. spin_lock_bh(&ctx->lock);
  70. if (crypto_ablkcipher_crt(geniv)->givencrypt !=
  71. chainiv_givencrypt_first)
  72. goto unlock;
  73. crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
  74. get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
  75. unlock:
  76. spin_unlock_bh(&ctx->lock);
  77. return chainiv_givencrypt(req);
  78. }
  79. static int chainiv_init_common(struct crypto_tfm *tfm)
  80. {
  81. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  82. return skcipher_geniv_init(tfm);
  83. }
  84. static int chainiv_init(struct crypto_tfm *tfm)
  85. {
  86. struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  87. spin_lock_init(&ctx->lock);
  88. return chainiv_init_common(tfm);
  89. }
  90. static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
  91. {
  92. int queued;
  93. if (!ctx->queue.qlen) {
  94. smp_mb__before_clear_bit();
  95. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  96. if (!ctx->queue.qlen ||
  97. test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  98. goto out;
  99. }
  100. queued = schedule_work(&ctx->postponed);
  101. BUG_ON(!queued);
  102. out:
  103. return ctx->err;
  104. }
  105. static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
  106. {
  107. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  108. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  109. int err;
  110. spin_lock_bh(&ctx->lock);
  111. err = skcipher_enqueue_givcrypt(&ctx->queue, req);
  112. spin_unlock_bh(&ctx->lock);
  113. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  114. return err;
  115. ctx->err = err;
  116. return async_chainiv_schedule_work(ctx);
  117. }
  118. static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
  119. {
  120. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  121. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  122. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  123. unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
  124. memcpy(req->giv, ctx->iv, ivsize);
  125. memcpy(subreq->info, ctx->iv, ivsize);
  126. ctx->err = crypto_ablkcipher_encrypt(subreq);
  127. if (ctx->err)
  128. goto out;
  129. memcpy(ctx->iv, subreq->info, ivsize);
  130. out:
  131. return async_chainiv_schedule_work(ctx);
  132. }
  133. static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  134. {
  135. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  136. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  137. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  138. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  139. ablkcipher_request_set_callback(subreq, req->creq.base.flags,
  140. req->creq.base.complete,
  141. req->creq.base.data);
  142. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  143. req->creq.nbytes, req->creq.info);
  144. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  145. goto postpone;
  146. if (ctx->queue.qlen) {
  147. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  148. goto postpone;
  149. }
  150. return async_chainiv_givencrypt_tail(req);
  151. postpone:
  152. return async_chainiv_postpone_request(req);
  153. }
  154. static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  155. {
  156. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  157. struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  158. if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
  159. goto out;
  160. if (crypto_ablkcipher_crt(geniv)->givencrypt !=
  161. async_chainiv_givencrypt_first)
  162. goto unlock;
  163. crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt;
  164. get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
  165. unlock:
  166. clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
  167. out:
  168. return async_chainiv_givencrypt(req);
  169. }
  170. static void async_chainiv_do_postponed(struct work_struct *work)
  171. {
  172. struct async_chainiv_ctx *ctx = container_of(work,
  173. struct async_chainiv_ctx,
  174. postponed);
  175. struct skcipher_givcrypt_request *req;
  176. struct ablkcipher_request *subreq;
  177. /* Only handle one request at a time to avoid hogging keventd. */
  178. spin_lock_bh(&ctx->lock);
  179. req = skcipher_dequeue_givcrypt(&ctx->queue);
  180. spin_unlock_bh(&ctx->lock);
  181. if (!req) {
  182. async_chainiv_schedule_work(ctx);
  183. return;
  184. }
  185. subreq = skcipher_givcrypt_reqctx(req);
  186. subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
  187. async_chainiv_givencrypt_tail(req);
  188. }
  189. static int async_chainiv_init(struct crypto_tfm *tfm)
  190. {
  191. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  192. spin_lock_init(&ctx->lock);
  193. crypto_init_queue(&ctx->queue, 100);
  194. INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
  195. return chainiv_init_common(tfm);
  196. }
  197. static void async_chainiv_exit(struct crypto_tfm *tfm)
  198. {
  199. struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  200. BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
  201. skcipher_geniv_exit(tfm);
  202. }
  203. static struct crypto_template chainiv_tmpl;
  204. static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
  205. {
  206. struct crypto_attr_type *algt;
  207. struct crypto_instance *inst;
  208. int err;
  209. algt = crypto_get_attr_type(tb);
  210. err = PTR_ERR(algt);
  211. if (IS_ERR(algt))
  212. return ERR_PTR(err);
  213. inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
  214. if (IS_ERR(inst))
  215. goto out;
  216. inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
  217. inst->alg.cra_init = chainiv_init;
  218. inst->alg.cra_exit = skcipher_geniv_exit;
  219. inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
  220. if (!crypto_requires_sync(algt->type, algt->mask)) {
  221. inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
  222. inst->alg.cra_ablkcipher.givencrypt =
  223. async_chainiv_givencrypt_first;
  224. inst->alg.cra_init = async_chainiv_init;
  225. inst->alg.cra_exit = async_chainiv_exit;
  226. inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
  227. }
  228. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  229. out:
  230. return inst;
  231. }
  232. static struct crypto_template chainiv_tmpl = {
  233. .name = "chainiv",
  234. .alloc = chainiv_alloc,
  235. .free = skcipher_geniv_free,
  236. .module = THIS_MODULE,
  237. };
  238. static int __init chainiv_module_init(void)
  239. {
  240. return crypto_register_template(&chainiv_tmpl);
  241. }
  242. static void __exit chainiv_module_exit(void)
  243. {
  244. crypto_unregister_template(&chainiv_tmpl);
  245. }
  246. module_init(chainiv_module_init);
  247. module_exit(chainiv_module_exit);
  248. MODULE_LICENSE("GPL");
  249. MODULE_DESCRIPTION("Chain IV Generator");