eseqiv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/rng.h>
  19. #include <crypto/scatterwalk.h>
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/string.h>
  27. #ifndef CONFIG_CRYPTO_DRBG
  28. #include <linux/spinlock.h>
  29. #endif
  30. struct eseqiv_request_ctx {
  31. struct scatterlist src[2];
  32. struct scatterlist dst[2];
  33. char tail[];
  34. };
  35. struct eseqiv_ctx {
  36. #ifndef CONFIG_CRYPTO_DRBG
  37. spinlock_t lock;
  38. #endif
  39. unsigned int reqoff;
  40. char salt[];
  41. };
  42. static void eseqiv_complete2(struct skcipher_givcrypt_request *req)
  43. {
  44. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  45. struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
  46. memcpy(req->giv, PTR_ALIGN((u8 *)reqctx->tail,
  47. crypto_ablkcipher_alignmask(geniv) + 1),
  48. crypto_ablkcipher_ivsize(geniv));
  49. }
  50. static void eseqiv_complete(struct crypto_async_request *base, int err)
  51. {
  52. struct skcipher_givcrypt_request *req = base->data;
  53. if (err)
  54. goto out;
  55. eseqiv_complete2(req);
  56. out:
  57. skcipher_givcrypt_complete(req, err);
  58. }
  59. static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
  60. {
  61. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  62. struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  63. struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
  64. struct ablkcipher_request *subreq;
  65. crypto_completion_t complete;
  66. void *data;
  67. struct scatterlist *osrc, *odst;
  68. struct scatterlist *dst;
  69. struct page *srcp;
  70. struct page *dstp;
  71. u8 *giv;
  72. u8 *vsrc;
  73. u8 *vdst;
  74. __be64 seq;
  75. unsigned int ivsize;
  76. unsigned int len;
  77. int err;
  78. subreq = (void *)(reqctx->tail + ctx->reqoff);
  79. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  80. giv = req->giv;
  81. complete = req->creq.base.complete;
  82. data = req->creq.base.data;
  83. osrc = req->creq.src;
  84. odst = req->creq.dst;
  85. srcp = sg_page(osrc);
  86. dstp = sg_page(odst);
  87. vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + osrc->offset;
  88. vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + odst->offset;
  89. ivsize = crypto_ablkcipher_ivsize(geniv);
  90. if (vsrc != giv + ivsize && vdst != giv + ivsize) {
  91. giv = PTR_ALIGN((u8 *)reqctx->tail,
  92. crypto_ablkcipher_alignmask(geniv) + 1);
  93. complete = eseqiv_complete;
  94. data = req;
  95. }
  96. ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
  97. data);
  98. sg_init_table(reqctx->src, 2);
  99. sg_set_buf(reqctx->src, giv, ivsize);
  100. scatterwalk_crypto_chain(reqctx->src, osrc, vsrc == giv + ivsize, 2);
  101. dst = reqctx->src;
  102. if (osrc != odst) {
  103. sg_init_table(reqctx->dst, 2);
  104. sg_set_buf(reqctx->dst, giv, ivsize);
  105. scatterwalk_crypto_chain(reqctx->dst, odst, vdst == giv + ivsize, 2);
  106. dst = reqctx->dst;
  107. }
  108. ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
  109. req->creq.nbytes + ivsize,
  110. req->creq.info);
  111. memcpy(req->creq.info, ctx->salt, ivsize);
  112. len = ivsize;
  113. if (ivsize > sizeof(u64)) {
  114. memset(req->giv, 0, ivsize - sizeof(u64));
  115. len = sizeof(u64);
  116. }
  117. seq = cpu_to_be64(req->seq);
  118. memcpy(req->giv + ivsize - len, &seq, len);
  119. err = crypto_ablkcipher_encrypt(subreq);
  120. if (err)
  121. goto out;
  122. if (giv != req->giv)
  123. eseqiv_complete2(req);
  124. out:
  125. return err;
  126. }
  127. #ifndef CONFIG_CRYPTO_DRBG
  128. static int eseqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  129. {
  130. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  131. struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  132. int err = 0;
  133. spin_lock_bh(&ctx->lock);
  134. if (crypto_ablkcipher_crt(geniv)->givencrypt != eseqiv_givencrypt_first)
  135. goto unlock;
  136. crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
  137. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  138. crypto_ablkcipher_ivsize(geniv));
  139. unlock:
  140. spin_unlock_bh(&ctx->lock);
  141. if (err)
  142. return err;
  143. return eseqiv_givencrypt(req);
  144. }
  145. #endif
  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. #ifndef CONFIG_CRYPTO_DRBG
  153. spin_lock_init(&ctx->lock);
  154. #endif
  155. alignmask = crypto_tfm_ctx_alignment() - 1;
  156. reqsize = sizeof(struct eseqiv_request_ctx);
  157. if (alignmask & reqsize) {
  158. alignmask &= reqsize;
  159. alignmask--;
  160. }
  161. alignmask = ~alignmask;
  162. alignmask &= crypto_ablkcipher_alignmask(geniv);
  163. reqsize += alignmask;
  164. reqsize += crypto_ablkcipher_ivsize(geniv);
  165. reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
  166. ctx->reqoff = reqsize - sizeof(struct eseqiv_request_ctx);
  167. tfm->crt_ablkcipher.reqsize = reqsize +
  168. sizeof(struct ablkcipher_request);
  169. #ifdef CONFIG_CRYPTO_DRBG
  170. crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  171. crypto_ablkcipher_ivsize(geniv));
  172. #endif
  173. return skcipher_geniv_init(tfm);
  174. }
  175. static struct crypto_template eseqiv_tmpl;
  176. static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
  177. {
  178. struct crypto_instance *inst;
  179. int err;
  180. err = crypto_get_default_rng();
  181. if (err)
  182. return ERR_PTR(err);
  183. inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
  184. if (IS_ERR(inst))
  185. goto put_rng;
  186. err = -EINVAL;
  187. if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
  188. goto free_inst;
  189. #ifdef CONFIG_CRYPTO_DRBG
  190. inst->alg.cra_ablkcipher.givencrypt = eseqiv_givencrypt;
  191. #else
  192. inst->alg.cra_ablkcipher.givencrypt = eseqiv_givencrypt_first;
  193. #endif
  194. inst->alg.cra_init = eseqiv_init;
  195. inst->alg.cra_exit = skcipher_geniv_exit;
  196. inst->alg.cra_ctxsize = sizeof(struct eseqiv_ctx);
  197. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  198. out:
  199. return inst;
  200. free_inst:
  201. skcipher_geniv_free(inst);
  202. inst = ERR_PTR(err);
  203. put_rng:
  204. crypto_put_default_rng();
  205. goto out;
  206. }
  207. static void eseqiv_free(struct crypto_instance *inst)
  208. {
  209. skcipher_geniv_free(inst);
  210. crypto_put_default_rng();
  211. }
  212. static struct crypto_template eseqiv_tmpl = {
  213. .name = "eseqiv",
  214. .alloc = eseqiv_alloc,
  215. .free = eseqiv_free,
  216. .module = THIS_MODULE,
  217. };
  218. static int __init eseqiv_module_init(void)
  219. {
  220. return crypto_register_template(&eseqiv_tmpl);
  221. }
  222. static void __exit eseqiv_module_exit(void)
  223. {
  224. crypto_unregister_template(&eseqiv_tmpl);
  225. }
  226. module_init(eseqiv_module_init);
  227. module_exit(eseqiv_module_exit);
  228. MODULE_LICENSE("GPL");
  229. MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");