echainiv.c 6.1 KB

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