seqiv.c 9.2 KB

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