seqiv.c 8.5 KB

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