seqiv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. struct crypto_aead_spawn *spawn;
  119. struct aead_alg *alg;
  120. int err;
  121. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  122. if (IS_ERR(inst))
  123. return PTR_ERR(inst);
  124. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  125. spawn = aead_instance_ctx(inst);
  126. alg = crypto_spawn_aead_alg(spawn);
  127. err = -EINVAL;
  128. if (inst->alg.ivsize != sizeof(u64))
  129. goto free_inst;
  130. inst->alg.encrypt = seqiv_aead_encrypt;
  131. inst->alg.decrypt = seqiv_aead_decrypt;
  132. inst->alg.init = aead_init_geniv;
  133. inst->alg.exit = aead_exit_geniv;
  134. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  135. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  136. err = aead_register_instance(tmpl, inst);
  137. if (err)
  138. goto free_inst;
  139. out:
  140. return err;
  141. free_inst:
  142. aead_geniv_free(inst);
  143. goto out;
  144. }
  145. static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
  146. {
  147. struct crypto_attr_type *algt;
  148. algt = crypto_get_attr_type(tb);
  149. if (IS_ERR(algt))
  150. return PTR_ERR(algt);
  151. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
  152. return -EINVAL;
  153. return seqiv_aead_create(tmpl, tb);
  154. }
  155. static void seqiv_free(struct crypto_instance *inst)
  156. {
  157. aead_geniv_free(aead_instance(inst));
  158. }
  159. static struct crypto_template seqiv_tmpl = {
  160. .name = "seqiv",
  161. .create = seqiv_create,
  162. .free = seqiv_free,
  163. .module = THIS_MODULE,
  164. };
  165. static int __init seqiv_module_init(void)
  166. {
  167. return crypto_register_template(&seqiv_tmpl);
  168. }
  169. static void __exit seqiv_module_exit(void)
  170. {
  171. crypto_unregister_template(&seqiv_tmpl);
  172. }
  173. module_init(seqiv_module_init);
  174. module_exit(seqiv_module_exit);
  175. MODULE_LICENSE("GPL");
  176. MODULE_DESCRIPTION("Sequence Number IV Generator");
  177. MODULE_ALIAS_CRYPTO("seqiv");