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