seqiv.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * seqiv: Sequence Number IV Generator
  3. *
  4. * This generator generates an IV based on a sequence number by xoring it
  5. * with a salt. This algorithm is mainly useful for CTR and similar modes.
  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/geniv.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <crypto/skcipher.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. static void seqiv_free(struct crypto_instance *inst);
  25. static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
  26. {
  27. struct aead_request *subreq = aead_request_ctx(req);
  28. struct crypto_aead *geniv;
  29. if (err == -EINPROGRESS)
  30. return;
  31. if (err)
  32. goto out;
  33. geniv = crypto_aead_reqtfm(req);
  34. memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
  35. out:
  36. kzfree(subreq->iv);
  37. }
  38. static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
  39. int err)
  40. {
  41. struct aead_request *req = base->data;
  42. seqiv_aead_encrypt_complete2(req, err);
  43. aead_request_complete(req, err);
  44. }
  45. static int seqiv_aead_encrypt(struct aead_request *req)
  46. {
  47. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  48. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  49. struct aead_request *subreq = aead_request_ctx(req);
  50. crypto_completion_t compl;
  51. void *data;
  52. u8 *info;
  53. unsigned int ivsize = 8;
  54. int err;
  55. if (req->cryptlen < ivsize)
  56. return -EINVAL;
  57. aead_request_set_tfm(subreq, ctx->child);
  58. compl = req->base.complete;
  59. data = req->base.data;
  60. info = req->iv;
  61. if (req->src != req->dst) {
  62. SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
  63. skcipher_request_set_tfm(nreq, ctx->sknull);
  64. skcipher_request_set_callback(nreq, req->base.flags,
  65. NULL, NULL);
  66. skcipher_request_set_crypt(nreq, req->src, req->dst,
  67. req->assoclen + req->cryptlen,
  68. NULL);
  69. err = crypto_skcipher_encrypt(nreq);
  70. if (err)
  71. return err;
  72. }
  73. if (unlikely(!IS_ALIGNED((unsigned long)info,
  74. crypto_aead_alignmask(geniv) + 1))) {
  75. info = kmalloc(ivsize, req->base.flags &
  76. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  77. GFP_ATOMIC);
  78. if (!info)
  79. return -ENOMEM;
  80. memcpy(info, req->iv, ivsize);
  81. compl = seqiv_aead_encrypt_complete;
  82. data = req;
  83. }
  84. aead_request_set_callback(subreq, req->base.flags, compl, data);
  85. aead_request_set_crypt(subreq, req->dst, req->dst,
  86. req->cryptlen - ivsize, info);
  87. aead_request_set_ad(subreq, req->assoclen + ivsize);
  88. crypto_xor(info, ctx->salt, ivsize);
  89. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  90. err = crypto_aead_encrypt(subreq);
  91. if (unlikely(info != req->iv))
  92. seqiv_aead_encrypt_complete2(req, err);
  93. return err;
  94. }
  95. static int seqiv_aead_decrypt(struct aead_request *req)
  96. {
  97. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  98. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  99. struct aead_request *subreq = aead_request_ctx(req);
  100. crypto_completion_t compl;
  101. void *data;
  102. unsigned int ivsize = 8;
  103. if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
  104. return -EINVAL;
  105. aead_request_set_tfm(subreq, ctx->child);
  106. compl = req->base.complete;
  107. data = req->base.data;
  108. aead_request_set_callback(subreq, req->base.flags, compl, data);
  109. aead_request_set_crypt(subreq, req->src, req->dst,
  110. req->cryptlen - ivsize, req->iv);
  111. aead_request_set_ad(subreq, req->assoclen + ivsize);
  112. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  113. return crypto_aead_decrypt(subreq);
  114. }
  115. static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
  116. {
  117. struct aead_instance *inst;
  118. int err;
  119. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  120. if (IS_ERR(inst))
  121. return PTR_ERR(inst);
  122. err = -EINVAL;
  123. if (inst->alg.ivsize != sizeof(u64))
  124. goto free_inst;
  125. inst->alg.encrypt = seqiv_aead_encrypt;
  126. inst->alg.decrypt = seqiv_aead_decrypt;
  127. inst->alg.init = aead_init_geniv;
  128. inst->alg.exit = aead_exit_geniv;
  129. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  130. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  131. err = aead_register_instance(tmpl, inst);
  132. if (err)
  133. goto free_inst;
  134. out:
  135. return err;
  136. free_inst:
  137. aead_geniv_free(inst);
  138. goto out;
  139. }
  140. static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
  141. {
  142. struct crypto_attr_type *algt;
  143. algt = crypto_get_attr_type(tb);
  144. if (IS_ERR(algt))
  145. return PTR_ERR(algt);
  146. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
  147. return -EINVAL;
  148. return seqiv_aead_create(tmpl, tb);
  149. }
  150. static void seqiv_free(struct crypto_instance *inst)
  151. {
  152. aead_geniv_free(aead_instance(inst));
  153. }
  154. static struct crypto_template seqiv_tmpl = {
  155. .name = "seqiv",
  156. .create = seqiv_create,
  157. .free = seqiv_free,
  158. .module = THIS_MODULE,
  159. };
  160. static int __init seqiv_module_init(void)
  161. {
  162. return crypto_register_template(&seqiv_tmpl);
  163. }
  164. static void __exit seqiv_module_exit(void)
  165. {
  166. crypto_unregister_template(&seqiv_tmpl);
  167. }
  168. module_init(seqiv_module_init);
  169. module_exit(seqiv_module_exit);
  170. MODULE_LICENSE("GPL");
  171. MODULE_DESCRIPTION("Sequence Number IV Generator");
  172. MODULE_ALIAS_CRYPTO("seqiv");