seqiv.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/internal/skcipher.h>
  17. #include <crypto/rng.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/string.h>
  26. struct seqiv_ctx {
  27. spinlock_t lock;
  28. u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
  29. };
  30. static void seqiv_free(struct crypto_instance *inst);
  31. static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
  32. {
  33. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  34. struct crypto_ablkcipher *geniv;
  35. if (err == -EINPROGRESS)
  36. return;
  37. if (err)
  38. goto out;
  39. geniv = skcipher_givcrypt_reqtfm(req);
  40. memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
  41. out:
  42. kfree(subreq->info);
  43. }
  44. static void seqiv_complete(struct crypto_async_request *base, int err)
  45. {
  46. struct skcipher_givcrypt_request *req = base->data;
  47. seqiv_complete2(req, err);
  48. skcipher_givcrypt_complete(req, err);
  49. }
  50. static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
  51. {
  52. struct aead_request *subreq = aead_request_ctx(req);
  53. struct crypto_aead *geniv;
  54. if (err == -EINPROGRESS)
  55. return;
  56. if (err)
  57. goto out;
  58. geniv = crypto_aead_reqtfm(req);
  59. memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
  60. out:
  61. kzfree(subreq->iv);
  62. }
  63. static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
  64. int err)
  65. {
  66. struct aead_request *req = base->data;
  67. seqiv_aead_encrypt_complete2(req, err);
  68. aead_request_complete(req, err);
  69. }
  70. static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
  71. unsigned int ivsize)
  72. {
  73. unsigned int len = ivsize;
  74. if (ivsize > sizeof(u64)) {
  75. memset(info, 0, ivsize - sizeof(u64));
  76. len = sizeof(u64);
  77. }
  78. seq = cpu_to_be64(seq);
  79. memcpy(info + ivsize - len, &seq, len);
  80. crypto_xor(info, ctx->salt, ivsize);
  81. }
  82. static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
  83. {
  84. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  85. struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  86. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  87. crypto_completion_t compl;
  88. void *data;
  89. u8 *info;
  90. unsigned int ivsize;
  91. int err;
  92. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  93. compl = req->creq.base.complete;
  94. data = req->creq.base.data;
  95. info = req->creq.info;
  96. ivsize = crypto_ablkcipher_ivsize(geniv);
  97. if (unlikely(!IS_ALIGNED((unsigned long)info,
  98. crypto_ablkcipher_alignmask(geniv) + 1))) {
  99. info = kmalloc(ivsize, req->creq.base.flags &
  100. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  101. GFP_ATOMIC);
  102. if (!info)
  103. return -ENOMEM;
  104. compl = seqiv_complete;
  105. data = req;
  106. }
  107. ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
  108. data);
  109. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  110. req->creq.nbytes, info);
  111. seqiv_geniv(ctx, info, req->seq, ivsize);
  112. memcpy(req->giv, info, ivsize);
  113. err = crypto_ablkcipher_encrypt(subreq);
  114. if (unlikely(info != req->creq.info))
  115. seqiv_complete2(req, err);
  116. return err;
  117. }
  118. static int seqiv_aead_encrypt(struct aead_request *req)
  119. {
  120. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  121. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  122. struct aead_request *subreq = aead_request_ctx(req);
  123. crypto_completion_t compl;
  124. void *data;
  125. u8 *info;
  126. unsigned int ivsize = 8;
  127. int err;
  128. if (req->cryptlen < ivsize)
  129. return -EINVAL;
  130. aead_request_set_tfm(subreq, ctx->child);
  131. compl = req->base.complete;
  132. data = req->base.data;
  133. info = req->iv;
  134. if (req->src != req->dst) {
  135. struct blkcipher_desc desc = {
  136. .tfm = ctx->null,
  137. };
  138. err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
  139. req->assoclen + req->cryptlen);
  140. if (err)
  141. return err;
  142. }
  143. if (unlikely(!IS_ALIGNED((unsigned long)info,
  144. crypto_aead_alignmask(geniv) + 1))) {
  145. info = kmalloc(ivsize, req->base.flags &
  146. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  147. GFP_ATOMIC);
  148. if (!info)
  149. return -ENOMEM;
  150. memcpy(info, req->iv, ivsize);
  151. compl = seqiv_aead_encrypt_complete;
  152. data = req;
  153. }
  154. aead_request_set_callback(subreq, req->base.flags, compl, data);
  155. aead_request_set_crypt(subreq, req->dst, req->dst,
  156. req->cryptlen - ivsize, info);
  157. aead_request_set_ad(subreq, req->assoclen + ivsize);
  158. crypto_xor(info, ctx->salt, ivsize);
  159. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  160. err = crypto_aead_encrypt(subreq);
  161. if (unlikely(info != req->iv))
  162. seqiv_aead_encrypt_complete2(req, err);
  163. return err;
  164. }
  165. static int seqiv_aead_decrypt(struct aead_request *req)
  166. {
  167. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  168. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  169. struct aead_request *subreq = aead_request_ctx(req);
  170. crypto_completion_t compl;
  171. void *data;
  172. unsigned int ivsize = 8;
  173. if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
  174. return -EINVAL;
  175. aead_request_set_tfm(subreq, ctx->child);
  176. compl = req->base.complete;
  177. data = req->base.data;
  178. aead_request_set_callback(subreq, req->base.flags, compl, data);
  179. aead_request_set_crypt(subreq, req->src, req->dst,
  180. req->cryptlen - ivsize, req->iv);
  181. aead_request_set_ad(subreq, req->assoclen + ivsize);
  182. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  183. return crypto_aead_decrypt(subreq);
  184. }
  185. static int seqiv_init(struct crypto_tfm *tfm)
  186. {
  187. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  188. struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  189. int err;
  190. spin_lock_init(&ctx->lock);
  191. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  192. err = 0;
  193. if (!crypto_get_default_rng()) {
  194. crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
  195. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  196. crypto_ablkcipher_ivsize(geniv));
  197. crypto_put_default_rng();
  198. }
  199. return err ?: skcipher_geniv_init(tfm);
  200. }
  201. static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
  202. struct rtattr **tb)
  203. {
  204. struct crypto_instance *inst;
  205. int err;
  206. inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
  207. if (IS_ERR(inst))
  208. return PTR_ERR(inst);
  209. err = -EINVAL;
  210. if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
  211. goto free_inst;
  212. inst->alg.cra_init = seqiv_init;
  213. inst->alg.cra_exit = skcipher_geniv_exit;
  214. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  215. inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
  216. inst->alg.cra_alignmask |= __alignof__(u32) - 1;
  217. err = crypto_register_instance(tmpl, inst);
  218. if (err)
  219. goto free_inst;
  220. out:
  221. return err;
  222. free_inst:
  223. skcipher_geniv_free(inst);
  224. goto out;
  225. }
  226. static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
  227. {
  228. struct aead_instance *inst;
  229. struct crypto_aead_spawn *spawn;
  230. struct aead_alg *alg;
  231. int err;
  232. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  233. if (IS_ERR(inst))
  234. return PTR_ERR(inst);
  235. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  236. spawn = aead_instance_ctx(inst);
  237. alg = crypto_spawn_aead_alg(spawn);
  238. err = -EINVAL;
  239. if (inst->alg.ivsize != sizeof(u64))
  240. goto free_inst;
  241. inst->alg.encrypt = seqiv_aead_encrypt;
  242. inst->alg.decrypt = seqiv_aead_decrypt;
  243. inst->alg.init = aead_init_geniv;
  244. inst->alg.exit = aead_exit_geniv;
  245. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  246. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  247. err = aead_register_instance(tmpl, inst);
  248. if (err)
  249. goto free_inst;
  250. out:
  251. return err;
  252. free_inst:
  253. aead_geniv_free(inst);
  254. goto out;
  255. }
  256. static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
  257. {
  258. struct crypto_attr_type *algt;
  259. int err;
  260. algt = crypto_get_attr_type(tb);
  261. if (IS_ERR(algt))
  262. return PTR_ERR(algt);
  263. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
  264. err = seqiv_ablkcipher_create(tmpl, tb);
  265. else
  266. err = seqiv_aead_create(tmpl, tb);
  267. return err;
  268. }
  269. static void seqiv_free(struct crypto_instance *inst)
  270. {
  271. if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
  272. skcipher_geniv_free(inst);
  273. else
  274. aead_geniv_free(aead_instance(inst));
  275. }
  276. static struct crypto_template seqiv_tmpl = {
  277. .name = "seqiv",
  278. .create = seqiv_create,
  279. .free = seqiv_free,
  280. .module = THIS_MODULE,
  281. };
  282. static int __init seqiv_module_init(void)
  283. {
  284. return crypto_register_template(&seqiv_tmpl);
  285. }
  286. static void __exit seqiv_module_exit(void)
  287. {
  288. crypto_unregister_template(&seqiv_tmpl);
  289. }
  290. module_init(seqiv_module_init);
  291. module_exit(seqiv_module_exit);
  292. MODULE_LICENSE("GPL");
  293. MODULE_DESCRIPTION("Sequence Number IV Generator");
  294. MODULE_ALIAS_CRYPTO("seqiv");