echainiv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * echainiv: Encrypted Chain IV Generator
  3. *
  4. * This generator generates an IV based on a sequence number by xoring it
  5. * with a salt and then encrypting it with the same key as used to encrypt
  6. * the plain text. This algorithm requires that the block size be equal
  7. * to the IV size. It is mainly useful for CBC.
  8. *
  9. * This generator can only be used by algorithms where authentication
  10. * is performed after encryption (i.e., authenc).
  11. *
  12. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <crypto/internal/geniv.h>
  21. #include <crypto/null.h>
  22. #include <crypto/rng.h>
  23. #include <crypto/scatterwalk.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/percpu.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/string.h>
  32. #define MAX_IV_SIZE 16
  33. struct echainiv_ctx {
  34. /* aead_geniv_ctx must be first the element */
  35. struct aead_geniv_ctx geniv;
  36. struct crypto_blkcipher *null;
  37. u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
  38. };
  39. static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv);
  40. /* We don't care if we get preempted and read/write IVs from the next CPU. */
  41. static void echainiv_read_iv(u8 *dst, unsigned size)
  42. {
  43. u32 *a = (u32 *)dst;
  44. u32 __percpu *b = echainiv_iv;
  45. for (; size >= 4; size -= 4) {
  46. *a++ = this_cpu_read(*b);
  47. b++;
  48. }
  49. }
  50. static void echainiv_write_iv(const u8 *src, unsigned size)
  51. {
  52. const u32 *a = (const u32 *)src;
  53. u32 __percpu *b = echainiv_iv;
  54. for (; size >= 4; size -= 4) {
  55. this_cpu_write(*b, *a);
  56. a++;
  57. b++;
  58. }
  59. }
  60. static void echainiv_encrypt_complete2(struct aead_request *req, int err)
  61. {
  62. struct aead_request *subreq = aead_request_ctx(req);
  63. struct crypto_aead *geniv;
  64. unsigned int ivsize;
  65. if (err == -EINPROGRESS)
  66. return;
  67. if (err)
  68. goto out;
  69. geniv = crypto_aead_reqtfm(req);
  70. ivsize = crypto_aead_ivsize(geniv);
  71. echainiv_write_iv(subreq->iv, ivsize);
  72. if (req->iv != subreq->iv)
  73. memcpy(req->iv, subreq->iv, ivsize);
  74. out:
  75. if (req->iv != subreq->iv)
  76. kzfree(subreq->iv);
  77. }
  78. static void echainiv_encrypt_complete(struct crypto_async_request *base,
  79. int err)
  80. {
  81. struct aead_request *req = base->data;
  82. echainiv_encrypt_complete2(req, err);
  83. aead_request_complete(req, err);
  84. }
  85. static int echainiv_encrypt(struct aead_request *req)
  86. {
  87. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  88. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  89. struct aead_request *subreq = aead_request_ctx(req);
  90. crypto_completion_t compl;
  91. void *data;
  92. u8 *info;
  93. unsigned int ivsize = crypto_aead_ivsize(geniv);
  94. int err;
  95. if (req->cryptlen < ivsize)
  96. return -EINVAL;
  97. aead_request_set_tfm(subreq, ctx->geniv.child);
  98. compl = echainiv_encrypt_complete;
  99. data = req;
  100. info = req->iv;
  101. if (req->src != req->dst) {
  102. struct blkcipher_desc desc = {
  103. .tfm = ctx->null,
  104. };
  105. err = crypto_blkcipher_encrypt(
  106. &desc, req->dst, req->src,
  107. req->assoclen + req->cryptlen);
  108. if (err)
  109. return err;
  110. }
  111. if (unlikely(!IS_ALIGNED((unsigned long)info,
  112. crypto_aead_alignmask(geniv) + 1))) {
  113. info = kmalloc(ivsize, req->base.flags &
  114. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  115. GFP_ATOMIC);
  116. if (!info)
  117. return -ENOMEM;
  118. memcpy(info, req->iv, ivsize);
  119. }
  120. aead_request_set_callback(subreq, req->base.flags, compl, data);
  121. aead_request_set_crypt(subreq, req->dst, req->dst,
  122. req->cryptlen - ivsize, info);
  123. aead_request_set_ad(subreq, req->assoclen + ivsize);
  124. crypto_xor(info, ctx->salt, ivsize);
  125. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  126. echainiv_read_iv(info, ivsize);
  127. err = crypto_aead_encrypt(subreq);
  128. echainiv_encrypt_complete2(req, err);
  129. return err;
  130. }
  131. static int echainiv_decrypt(struct aead_request *req)
  132. {
  133. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  134. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  135. struct aead_request *subreq = aead_request_ctx(req);
  136. crypto_completion_t compl;
  137. void *data;
  138. unsigned int ivsize = crypto_aead_ivsize(geniv);
  139. if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
  140. return -EINVAL;
  141. aead_request_set_tfm(subreq, ctx->geniv.child);
  142. compl = req->base.complete;
  143. data = req->base.data;
  144. aead_request_set_callback(subreq, req->base.flags, compl, data);
  145. aead_request_set_crypt(subreq, req->src, req->dst,
  146. req->cryptlen - ivsize, req->iv);
  147. aead_request_set_ad(subreq, req->assoclen + ivsize);
  148. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  149. if (req->src != req->dst)
  150. scatterwalk_map_and_copy(req->iv, req->dst,
  151. req->assoclen, ivsize, 1);
  152. return crypto_aead_decrypt(subreq);
  153. }
  154. static int echainiv_init(struct crypto_tfm *tfm)
  155. {
  156. struct crypto_aead *geniv = __crypto_aead_cast(tfm);
  157. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  158. int err;
  159. spin_lock_init(&ctx->geniv.lock);
  160. crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
  161. err = crypto_get_default_rng();
  162. if (err)
  163. goto out;
  164. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  165. crypto_aead_ivsize(geniv));
  166. crypto_put_default_rng();
  167. if (err)
  168. goto out;
  169. ctx->null = crypto_get_default_null_skcipher();
  170. err = PTR_ERR(ctx->null);
  171. if (IS_ERR(ctx->null))
  172. goto out;
  173. err = aead_geniv_init(tfm);
  174. if (err)
  175. goto drop_null;
  176. ctx->geniv.child = geniv->child;
  177. geniv->child = geniv;
  178. out:
  179. return err;
  180. drop_null:
  181. crypto_put_default_null_skcipher();
  182. goto out;
  183. }
  184. static void echainiv_exit(struct crypto_tfm *tfm)
  185. {
  186. struct echainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  187. crypto_free_aead(ctx->geniv.child);
  188. crypto_put_default_null_skcipher();
  189. }
  190. static int echainiv_aead_create(struct crypto_template *tmpl,
  191. struct rtattr **tb)
  192. {
  193. struct aead_instance *inst;
  194. struct crypto_aead_spawn *spawn;
  195. struct aead_alg *alg;
  196. int err;
  197. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  198. if (IS_ERR(inst))
  199. return PTR_ERR(inst);
  200. spawn = aead_instance_ctx(inst);
  201. alg = crypto_spawn_aead_alg(spawn);
  202. if (alg->base.cra_aead.encrypt)
  203. goto done;
  204. err = -EINVAL;
  205. if (inst->alg.ivsize & (sizeof(u32) - 1) ||
  206. inst->alg.ivsize > MAX_IV_SIZE)
  207. goto free_inst;
  208. inst->alg.encrypt = echainiv_encrypt;
  209. inst->alg.decrypt = echainiv_decrypt;
  210. inst->alg.base.cra_init = echainiv_init;
  211. inst->alg.base.cra_exit = echainiv_exit;
  212. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  213. inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx);
  214. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  215. done:
  216. err = aead_register_instance(tmpl, inst);
  217. if (err)
  218. goto free_inst;
  219. out:
  220. return err;
  221. free_inst:
  222. aead_geniv_free(inst);
  223. goto out;
  224. }
  225. static void echainiv_free(struct crypto_instance *inst)
  226. {
  227. aead_geniv_free(aead_instance(inst));
  228. }
  229. static struct crypto_template echainiv_tmpl = {
  230. .name = "echainiv",
  231. .create = echainiv_aead_create,
  232. .free = echainiv_free,
  233. .module = THIS_MODULE,
  234. };
  235. static int __init echainiv_module_init(void)
  236. {
  237. return crypto_register_template(&echainiv_tmpl);
  238. }
  239. static void __exit echainiv_module_exit(void)
  240. {
  241. crypto_unregister_template(&echainiv_tmpl);
  242. }
  243. module_init(echainiv_module_init);
  244. module_exit(echainiv_module_exit);
  245. MODULE_LICENSE("GPL");
  246. MODULE_DESCRIPTION("Encrypted Chain IV Generator");
  247. MODULE_ALIAS_CRYPTO("echainiv");