echainiv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/scatterwalk.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/percpu.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/string.h>
  30. #define MAX_IV_SIZE 16
  31. static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv);
  32. /* We don't care if we get preempted and read/write IVs from the next CPU. */
  33. static void echainiv_read_iv(u8 *dst, unsigned size)
  34. {
  35. u32 *a = (u32 *)dst;
  36. u32 __percpu *b = echainiv_iv;
  37. for (; size >= 4; size -= 4) {
  38. *a++ = this_cpu_read(*b);
  39. b++;
  40. }
  41. }
  42. static void echainiv_write_iv(const u8 *src, unsigned size)
  43. {
  44. const u32 *a = (const u32 *)src;
  45. u32 __percpu *b = echainiv_iv;
  46. for (; size >= 4; size -= 4) {
  47. this_cpu_write(*b, *a);
  48. a++;
  49. b++;
  50. }
  51. }
  52. static void echainiv_encrypt_complete2(struct aead_request *req, int err)
  53. {
  54. struct aead_request *subreq = aead_request_ctx(req);
  55. struct crypto_aead *geniv;
  56. unsigned int ivsize;
  57. if (err == -EINPROGRESS)
  58. return;
  59. if (err)
  60. goto out;
  61. geniv = crypto_aead_reqtfm(req);
  62. ivsize = crypto_aead_ivsize(geniv);
  63. echainiv_write_iv(subreq->iv, ivsize);
  64. if (req->iv != subreq->iv)
  65. memcpy(req->iv, subreq->iv, ivsize);
  66. out:
  67. if (req->iv != subreq->iv)
  68. kzfree(subreq->iv);
  69. }
  70. static void echainiv_encrypt_complete(struct crypto_async_request *base,
  71. int err)
  72. {
  73. struct aead_request *req = base->data;
  74. echainiv_encrypt_complete2(req, err);
  75. aead_request_complete(req, err);
  76. }
  77. static int echainiv_encrypt(struct aead_request *req)
  78. {
  79. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  80. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  81. struct aead_request *subreq = aead_request_ctx(req);
  82. crypto_completion_t compl;
  83. void *data;
  84. u8 *info;
  85. unsigned int ivsize = crypto_aead_ivsize(geniv);
  86. int err;
  87. if (req->cryptlen < ivsize)
  88. return -EINVAL;
  89. aead_request_set_tfm(subreq, ctx->child);
  90. compl = echainiv_encrypt_complete;
  91. data = req;
  92. info = req->iv;
  93. if (req->src != req->dst) {
  94. struct blkcipher_desc desc = {
  95. .tfm = ctx->null,
  96. };
  97. err = crypto_blkcipher_encrypt(
  98. &desc, req->dst, req->src,
  99. req->assoclen + req->cryptlen);
  100. if (err)
  101. return err;
  102. }
  103. if (unlikely(!IS_ALIGNED((unsigned long)info,
  104. crypto_aead_alignmask(geniv) + 1))) {
  105. info = kmalloc(ivsize, req->base.flags &
  106. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  107. GFP_ATOMIC);
  108. if (!info)
  109. return -ENOMEM;
  110. memcpy(info, req->iv, ivsize);
  111. }
  112. aead_request_set_callback(subreq, req->base.flags, compl, data);
  113. aead_request_set_crypt(subreq, req->dst, req->dst,
  114. req->cryptlen, info);
  115. aead_request_set_ad(subreq, req->assoclen);
  116. crypto_xor(info, ctx->salt, ivsize);
  117. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  118. echainiv_read_iv(info, ivsize);
  119. err = crypto_aead_encrypt(subreq);
  120. echainiv_encrypt_complete2(req, err);
  121. return err;
  122. }
  123. static int echainiv_decrypt(struct aead_request *req)
  124. {
  125. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  126. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  127. struct aead_request *subreq = aead_request_ctx(req);
  128. crypto_completion_t compl;
  129. void *data;
  130. unsigned int ivsize = crypto_aead_ivsize(geniv);
  131. if (req->cryptlen < ivsize)
  132. return -EINVAL;
  133. aead_request_set_tfm(subreq, ctx->child);
  134. compl = req->base.complete;
  135. data = req->base.data;
  136. aead_request_set_callback(subreq, req->base.flags, compl, data);
  137. aead_request_set_crypt(subreq, req->src, req->dst,
  138. req->cryptlen - ivsize, req->iv);
  139. aead_request_set_ad(subreq, req->assoclen + ivsize);
  140. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  141. return crypto_aead_decrypt(subreq);
  142. }
  143. static int echainiv_aead_create(struct crypto_template *tmpl,
  144. struct rtattr **tb)
  145. {
  146. struct aead_instance *inst;
  147. struct crypto_aead_spawn *spawn;
  148. struct aead_alg *alg;
  149. int err;
  150. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  151. if (IS_ERR(inst))
  152. return PTR_ERR(inst);
  153. spawn = aead_instance_ctx(inst);
  154. alg = crypto_spawn_aead_alg(spawn);
  155. err = -EINVAL;
  156. if (inst->alg.ivsize & (sizeof(u32) - 1) ||
  157. inst->alg.ivsize > MAX_IV_SIZE)
  158. goto free_inst;
  159. inst->alg.encrypt = echainiv_encrypt;
  160. inst->alg.decrypt = echainiv_decrypt;
  161. inst->alg.init = aead_init_geniv;
  162. inst->alg.exit = aead_exit_geniv;
  163. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  164. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  165. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  166. inst->free = aead_geniv_free;
  167. err = aead_register_instance(tmpl, inst);
  168. if (err)
  169. goto free_inst;
  170. out:
  171. return err;
  172. free_inst:
  173. aead_geniv_free(inst);
  174. goto out;
  175. }
  176. static void echainiv_free(struct crypto_instance *inst)
  177. {
  178. aead_geniv_free(aead_instance(inst));
  179. }
  180. static struct crypto_template echainiv_tmpl = {
  181. .name = "echainiv",
  182. .create = echainiv_aead_create,
  183. .free = echainiv_free,
  184. .module = THIS_MODULE,
  185. };
  186. static int __init echainiv_module_init(void)
  187. {
  188. return crypto_register_template(&echainiv_tmpl);
  189. }
  190. static void __exit echainiv_module_exit(void)
  191. {
  192. crypto_unregister_template(&echainiv_tmpl);
  193. }
  194. module_init(echainiv_module_init);
  195. module_exit(echainiv_module_exit);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("Encrypted Chain IV Generator");
  198. MODULE_ALIAS_CRYPTO("echainiv");