eseqiv.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * eseqiv: Encrypted Sequence Number 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. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/module.h>
  24. #include <linux/random.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/string.h>
  28. struct eseqiv_request_ctx {
  29. struct scatterlist src[2];
  30. struct scatterlist dst[2];
  31. char tail[];
  32. };
  33. struct eseqiv_ctx {
  34. spinlock_t lock;
  35. unsigned int reqoff;
  36. char salt[];
  37. };
  38. static void eseqiv_complete2(struct skcipher_givcrypt_request *req)
  39. {
  40. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  41. struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
  42. memcpy(req->giv, PTR_ALIGN((u8 *)reqctx->tail,
  43. crypto_ablkcipher_alignmask(geniv) + 1),
  44. crypto_ablkcipher_ivsize(geniv));
  45. }
  46. static void eseqiv_complete(struct crypto_async_request *base, int err)
  47. {
  48. struct skcipher_givcrypt_request *req = base->data;
  49. if (err)
  50. goto out;
  51. eseqiv_complete2(req);
  52. out:
  53. skcipher_givcrypt_complete(req, err);
  54. }
  55. static void eseqiv_chain(struct scatterlist *head, struct scatterlist *sg,
  56. int chain)
  57. {
  58. if (chain) {
  59. head->length += sg->length;
  60. sg = scatterwalk_sg_next(sg);
  61. }
  62. if (sg)
  63. scatterwalk_sg_chain(head, 2, sg);
  64. else
  65. sg_mark_end(head);
  66. }
  67. static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
  68. {
  69. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  70. struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  71. struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
  72. struct ablkcipher_request *subreq;
  73. crypto_completion_t complete;
  74. void *data;
  75. struct scatterlist *osrc, *odst;
  76. struct scatterlist *dst;
  77. struct page *srcp;
  78. struct page *dstp;
  79. u8 *giv;
  80. u8 *vsrc;
  81. u8 *vdst;
  82. __be64 seq;
  83. unsigned int ivsize;
  84. unsigned int len;
  85. int err;
  86. subreq = (void *)(reqctx->tail + ctx->reqoff);
  87. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  88. giv = req->giv;
  89. complete = req->creq.base.complete;
  90. data = req->creq.base.data;
  91. osrc = req->creq.src;
  92. odst = req->creq.dst;
  93. srcp = sg_page(osrc);
  94. dstp = sg_page(odst);
  95. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + osrc->offset;
  96. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + odst->offset;
  97. ivsize = crypto_ablkcipher_ivsize(geniv);
  98. if (vsrc != giv + ivsize && vdst != giv + ivsize) {
  99. giv = PTR_ALIGN((u8 *)reqctx->tail,
  100. crypto_ablkcipher_alignmask(geniv) + 1);
  101. complete = eseqiv_complete;
  102. data = req;
  103. }
  104. ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
  105. data);
  106. sg_init_table(reqctx->src, 2);
  107. sg_set_buf(reqctx->src, giv, ivsize);
  108. eseqiv_chain(reqctx->src, osrc, vsrc == giv + ivsize);
  109. dst = reqctx->src;
  110. if (osrc != odst) {
  111. sg_init_table(reqctx->dst, 2);
  112. sg_set_buf(reqctx->dst, giv, ivsize);
  113. eseqiv_chain(reqctx->dst, odst, vdst == giv + ivsize);
  114. dst = reqctx->dst;
  115. }
  116. ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
  117. req->creq.nbytes, req->creq.info);
  118. memcpy(req->creq.info, ctx->salt, ivsize);
  119. len = ivsize;
  120. if (ivsize > sizeof(u64)) {
  121. memset(req->giv, 0, ivsize - sizeof(u64));
  122. len = sizeof(u64);
  123. }
  124. seq = cpu_to_be64(req->seq);
  125. memcpy(req->giv + ivsize - len, &seq, len);
  126. err = crypto_ablkcipher_encrypt(subreq);
  127. if (err)
  128. goto out;
  129. eseqiv_complete2(req);
  130. out:
  131. return err;
  132. }
  133. static int eseqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  134. {
  135. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  136. struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  137. spin_lock_bh(&ctx->lock);
  138. if (crypto_ablkcipher_crt(geniv)->givencrypt != eseqiv_givencrypt_first)
  139. goto unlock;
  140. crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
  141. get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
  142. unlock:
  143. spin_unlock_bh(&ctx->lock);
  144. return eseqiv_givencrypt(req);
  145. }
  146. static int eseqiv_init(struct crypto_tfm *tfm)
  147. {
  148. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  149. struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  150. unsigned long alignmask;
  151. unsigned int reqsize;
  152. spin_lock_init(&ctx->lock);
  153. alignmask = crypto_tfm_ctx_alignment() - 1;
  154. reqsize = sizeof(struct eseqiv_request_ctx);
  155. if (alignmask & reqsize) {
  156. alignmask &= reqsize;
  157. alignmask--;
  158. }
  159. alignmask = ~alignmask;
  160. alignmask &= crypto_ablkcipher_alignmask(geniv);
  161. reqsize += alignmask;
  162. reqsize += crypto_ablkcipher_ivsize(geniv);
  163. reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
  164. ctx->reqoff = reqsize - sizeof(struct eseqiv_request_ctx);
  165. tfm->crt_ablkcipher.reqsize = reqsize +
  166. sizeof(struct ablkcipher_request);
  167. return skcipher_geniv_init(tfm);
  168. }
  169. static struct crypto_template eseqiv_tmpl;
  170. static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
  171. {
  172. struct crypto_instance *inst;
  173. int err;
  174. inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
  175. if (IS_ERR(inst))
  176. goto out;
  177. err = -EINVAL;
  178. if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
  179. goto free_inst;
  180. inst->alg.cra_ablkcipher.givencrypt = eseqiv_givencrypt_first;
  181. inst->alg.cra_init = eseqiv_init;
  182. inst->alg.cra_exit = skcipher_geniv_exit;
  183. inst->alg.cra_ctxsize = sizeof(struct eseqiv_ctx);
  184. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  185. out:
  186. return inst;
  187. free_inst:
  188. skcipher_geniv_free(inst);
  189. inst = ERR_PTR(err);
  190. goto out;
  191. }
  192. static struct crypto_template eseqiv_tmpl = {
  193. .name = "eseqiv",
  194. .alloc = eseqiv_alloc,
  195. .free = skcipher_geniv_free,
  196. .module = THIS_MODULE,
  197. };
  198. static int __init eseqiv_module_init(void)
  199. {
  200. return crypto_register_template(&eseqiv_tmpl);
  201. }
  202. static void __exit eseqiv_module_exit(void)
  203. {
  204. crypto_unregister_template(&eseqiv_tmpl);
  205. }
  206. module_init(eseqiv_module_init);
  207. module_exit(eseqiv_module_exit);
  208. MODULE_LICENSE("GPL");
  209. MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");