chainiv.c 8.5 KB

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