chainiv.c 9.3 KB

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