echainiv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * echainiv: Encrypted Chain 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. * This generator can only be used by algorithms where authentication
  10. * is performed after encryption (i.e., authenc).
  11. *
  12. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <crypto/internal/aead.h>
  21. #include <crypto/null.h>
  22. #include <crypto/rng.h>
  23. #include <crypto/scatterwalk.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/percpu.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/string.h>
  32. #define MAX_IV_SIZE 16
  33. struct echainiv_request_ctx {
  34. struct scatterlist src[2];
  35. struct scatterlist dst[2];
  36. struct scatterlist ivbuf[2];
  37. struct scatterlist *ivsg;
  38. struct aead_givcrypt_request subreq;
  39. };
  40. struct echainiv_ctx {
  41. struct crypto_aead *child;
  42. spinlock_t lock;
  43. struct crypto_blkcipher *null;
  44. u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
  45. };
  46. static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv);
  47. static int echainiv_setkey(struct crypto_aead *tfm,
  48. const u8 *key, unsigned int keylen)
  49. {
  50. struct echainiv_ctx *ctx = crypto_aead_ctx(tfm);
  51. return crypto_aead_setkey(ctx->child, key, keylen);
  52. }
  53. static int echainiv_setauthsize(struct crypto_aead *tfm,
  54. unsigned int authsize)
  55. {
  56. struct echainiv_ctx *ctx = crypto_aead_ctx(tfm);
  57. return crypto_aead_setauthsize(ctx->child, authsize);
  58. }
  59. /* We don't care if we get preempted and read/write IVs from the next CPU. */
  60. static void echainiv_read_iv(u8 *dst, unsigned size)
  61. {
  62. u32 *a = (u32 *)dst;
  63. u32 __percpu *b = echainiv_iv;
  64. for (; size >= 4; size -= 4) {
  65. *a++ = this_cpu_read(*b);
  66. b++;
  67. }
  68. }
  69. static void echainiv_write_iv(const u8 *src, unsigned size)
  70. {
  71. const u32 *a = (const u32 *)src;
  72. u32 __percpu *b = echainiv_iv;
  73. for (; size >= 4; size -= 4) {
  74. this_cpu_write(*b, *a);
  75. a++;
  76. b++;
  77. }
  78. }
  79. static void echainiv_encrypt_compat_complete2(struct aead_request *req,
  80. int err)
  81. {
  82. struct echainiv_request_ctx *rctx = aead_request_ctx(req);
  83. struct aead_givcrypt_request *subreq = &rctx->subreq;
  84. struct crypto_aead *geniv;
  85. if (err == -EINPROGRESS)
  86. return;
  87. if (err)
  88. goto out;
  89. geniv = crypto_aead_reqtfm(req);
  90. scatterwalk_map_and_copy(subreq->giv, rctx->ivsg, 0,
  91. crypto_aead_ivsize(geniv), 1);
  92. out:
  93. kzfree(subreq->giv);
  94. }
  95. static void echainiv_encrypt_compat_complete(
  96. struct crypto_async_request *base, int err)
  97. {
  98. struct aead_request *req = base->data;
  99. echainiv_encrypt_compat_complete2(req, err);
  100. aead_request_complete(req, err);
  101. }
  102. static void echainiv_encrypt_complete2(struct aead_request *req, int err)
  103. {
  104. struct aead_request *subreq = aead_request_ctx(req);
  105. struct crypto_aead *geniv;
  106. unsigned int ivsize;
  107. if (err == -EINPROGRESS)
  108. return;
  109. if (err)
  110. goto out;
  111. geniv = crypto_aead_reqtfm(req);
  112. ivsize = crypto_aead_ivsize(geniv);
  113. echainiv_write_iv(subreq->iv, ivsize);
  114. if (req->iv != subreq->iv)
  115. memcpy(req->iv, subreq->iv, ivsize);
  116. out:
  117. if (req->iv != subreq->iv)
  118. kzfree(subreq->iv);
  119. }
  120. static void echainiv_encrypt_complete(struct crypto_async_request *base,
  121. int err)
  122. {
  123. struct aead_request *req = base->data;
  124. echainiv_encrypt_complete2(req, err);
  125. aead_request_complete(req, err);
  126. }
  127. static int echainiv_encrypt_compat(struct aead_request *req)
  128. {
  129. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  130. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  131. struct echainiv_request_ctx *rctx = aead_request_ctx(req);
  132. struct aead_givcrypt_request *subreq = &rctx->subreq;
  133. unsigned int ivsize = crypto_aead_ivsize(geniv);
  134. crypto_completion_t compl;
  135. void *data;
  136. u8 *info;
  137. __be64 seq;
  138. int err;
  139. if (req->cryptlen < ivsize)
  140. return -EINVAL;
  141. compl = req->base.complete;
  142. data = req->base.data;
  143. rctx->ivsg = scatterwalk_ffwd(rctx->ivbuf, req->dst, req->assoclen);
  144. info = PageHighMem(sg_page(rctx->ivsg)) ? NULL : sg_virt(rctx->ivsg);
  145. if (!info) {
  146. info = kmalloc(ivsize, req->base.flags &
  147. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  148. GFP_ATOMIC);
  149. if (!info)
  150. return -ENOMEM;
  151. compl = echainiv_encrypt_compat_complete;
  152. data = req;
  153. }
  154. memcpy(&seq, req->iv + ivsize - sizeof(seq), sizeof(seq));
  155. aead_givcrypt_set_tfm(subreq, ctx->child);
  156. aead_givcrypt_set_callback(subreq, req->base.flags,
  157. req->base.complete, req->base.data);
  158. aead_givcrypt_set_crypt(subreq,
  159. scatterwalk_ffwd(rctx->src, req->src,
  160. req->assoclen + ivsize),
  161. scatterwalk_ffwd(rctx->dst, rctx->ivsg,
  162. ivsize),
  163. req->cryptlen - ivsize, req->iv);
  164. aead_givcrypt_set_assoc(subreq, req->src, req->assoclen);
  165. aead_givcrypt_set_giv(subreq, info, be64_to_cpu(seq));
  166. err = crypto_aead_givencrypt(subreq);
  167. if (unlikely(PageHighMem(sg_page(rctx->ivsg))))
  168. echainiv_encrypt_compat_complete2(req, err);
  169. return err;
  170. }
  171. static int echainiv_encrypt(struct aead_request *req)
  172. {
  173. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  174. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  175. struct aead_request *subreq = aead_request_ctx(req);
  176. crypto_completion_t compl;
  177. void *data;
  178. u8 *info;
  179. unsigned int ivsize = crypto_aead_ivsize(geniv);
  180. int err;
  181. if (req->cryptlen < ivsize)
  182. return -EINVAL;
  183. aead_request_set_tfm(subreq, ctx->child);
  184. compl = echainiv_encrypt_complete;
  185. data = req;
  186. info = req->iv;
  187. if (req->src != req->dst) {
  188. struct blkcipher_desc desc = {
  189. .tfm = ctx->null,
  190. };
  191. err = crypto_blkcipher_encrypt(
  192. &desc, req->dst, req->src,
  193. req->assoclen + req->cryptlen);
  194. if (err)
  195. return err;
  196. }
  197. if (unlikely(!IS_ALIGNED((unsigned long)info,
  198. crypto_aead_alignmask(geniv) + 1))) {
  199. info = kmalloc(ivsize, req->base.flags &
  200. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  201. GFP_ATOMIC);
  202. if (!info)
  203. return -ENOMEM;
  204. memcpy(info, req->iv, ivsize);
  205. }
  206. aead_request_set_callback(subreq, req->base.flags, compl, data);
  207. aead_request_set_crypt(subreq, req->dst, req->dst,
  208. req->cryptlen - ivsize, info);
  209. aead_request_set_ad(subreq, req->assoclen + ivsize);
  210. crypto_xor(info, ctx->salt, ivsize);
  211. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  212. echainiv_read_iv(info, ivsize);
  213. err = crypto_aead_encrypt(subreq);
  214. echainiv_encrypt_complete2(req, err);
  215. return err;
  216. }
  217. static int echainiv_decrypt_compat(struct aead_request *req)
  218. {
  219. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  220. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  221. struct echainiv_request_ctx *rctx = aead_request_ctx(req);
  222. struct aead_request *subreq = &rctx->subreq.areq;
  223. crypto_completion_t compl;
  224. void *data;
  225. unsigned int ivsize = crypto_aead_ivsize(geniv);
  226. if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
  227. return -EINVAL;
  228. aead_request_set_tfm(subreq, ctx->child);
  229. compl = req->base.complete;
  230. data = req->base.data;
  231. aead_request_set_callback(subreq, req->base.flags, compl, data);
  232. aead_request_set_crypt(subreq,
  233. scatterwalk_ffwd(rctx->src, req->src,
  234. req->assoclen + ivsize),
  235. scatterwalk_ffwd(rctx->dst, req->dst,
  236. req->assoclen + ivsize),
  237. req->cryptlen - ivsize, req->iv);
  238. aead_request_set_assoc(subreq, req->src, req->assoclen);
  239. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  240. return crypto_aead_decrypt(subreq);
  241. }
  242. static int echainiv_decrypt(struct aead_request *req)
  243. {
  244. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  245. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  246. struct aead_request *subreq = aead_request_ctx(req);
  247. crypto_completion_t compl;
  248. void *data;
  249. unsigned int ivsize = crypto_aead_ivsize(geniv);
  250. if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
  251. return -EINVAL;
  252. aead_request_set_tfm(subreq, ctx->child);
  253. compl = req->base.complete;
  254. data = req->base.data;
  255. aead_request_set_callback(subreq, req->base.flags, compl, data);
  256. aead_request_set_crypt(subreq, req->src, req->dst,
  257. req->cryptlen - ivsize, req->iv);
  258. aead_request_set_ad(subreq, req->assoclen + ivsize);
  259. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  260. if (req->src != req->dst)
  261. scatterwalk_map_and_copy(req->iv, req->dst,
  262. req->assoclen, ivsize, 1);
  263. return crypto_aead_decrypt(subreq);
  264. }
  265. static int echainiv_encrypt_compat_first(struct aead_request *req)
  266. {
  267. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  268. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  269. int err = 0;
  270. spin_lock_bh(&ctx->lock);
  271. if (geniv->encrypt != echainiv_encrypt_compat_first)
  272. goto unlock;
  273. geniv->encrypt = echainiv_encrypt_compat;
  274. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  275. crypto_aead_ivsize(geniv));
  276. unlock:
  277. spin_unlock_bh(&ctx->lock);
  278. if (err)
  279. return err;
  280. return echainiv_encrypt_compat(req);
  281. }
  282. static int echainiv_encrypt_first(struct aead_request *req)
  283. {
  284. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  285. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  286. int err = 0;
  287. spin_lock_bh(&ctx->lock);
  288. if (geniv->encrypt != echainiv_encrypt_first)
  289. goto unlock;
  290. geniv->encrypt = echainiv_encrypt;
  291. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  292. crypto_aead_ivsize(geniv));
  293. unlock:
  294. spin_unlock_bh(&ctx->lock);
  295. if (err)
  296. return err;
  297. return echainiv_encrypt(req);
  298. }
  299. static int echainiv_compat_init(struct crypto_tfm *tfm)
  300. {
  301. struct crypto_aead *geniv = __crypto_aead_cast(tfm);
  302. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  303. int err;
  304. spin_lock_init(&ctx->lock);
  305. crypto_aead_set_reqsize(geniv, sizeof(struct echainiv_request_ctx));
  306. err = aead_geniv_init(tfm);
  307. ctx->child = geniv->child;
  308. geniv->child = geniv;
  309. return err;
  310. }
  311. static int echainiv_init(struct crypto_tfm *tfm)
  312. {
  313. struct crypto_aead *geniv = __crypto_aead_cast(tfm);
  314. struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
  315. int err;
  316. spin_lock_init(&ctx->lock);
  317. crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
  318. ctx->null = crypto_get_default_null_skcipher();
  319. err = PTR_ERR(ctx->null);
  320. if (IS_ERR(ctx->null))
  321. goto out;
  322. err = aead_geniv_init(tfm);
  323. if (err)
  324. goto drop_null;
  325. ctx->child = geniv->child;
  326. geniv->child = geniv;
  327. out:
  328. return err;
  329. drop_null:
  330. crypto_put_default_null_skcipher();
  331. goto out;
  332. }
  333. static void echainiv_compat_exit(struct crypto_tfm *tfm)
  334. {
  335. struct echainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  336. crypto_free_aead(ctx->child);
  337. }
  338. static void echainiv_exit(struct crypto_tfm *tfm)
  339. {
  340. struct echainiv_ctx *ctx = crypto_tfm_ctx(tfm);
  341. crypto_free_aead(ctx->child);
  342. crypto_put_default_null_skcipher();
  343. }
  344. static int echainiv_aead_create(struct crypto_template *tmpl,
  345. struct rtattr **tb)
  346. {
  347. struct aead_instance *inst;
  348. struct crypto_aead_spawn *spawn;
  349. struct aead_alg *alg;
  350. int err;
  351. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  352. if (IS_ERR(inst))
  353. return PTR_ERR(inst);
  354. err = -EINVAL;
  355. if (inst->alg.ivsize < sizeof(u64) ||
  356. inst->alg.ivsize & (sizeof(u32) - 1) ||
  357. inst->alg.ivsize > MAX_IV_SIZE)
  358. goto free_inst;
  359. spawn = aead_instance_ctx(inst);
  360. alg = crypto_spawn_aead_alg(spawn);
  361. inst->alg.setkey = echainiv_setkey;
  362. inst->alg.setauthsize = echainiv_setauthsize;
  363. inst->alg.encrypt = echainiv_encrypt_first;
  364. inst->alg.decrypt = echainiv_decrypt;
  365. inst->alg.base.cra_init = echainiv_init;
  366. inst->alg.base.cra_exit = echainiv_exit;
  367. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  368. inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx);
  369. inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
  370. if (alg->base.cra_aead.encrypt) {
  371. inst->alg.encrypt = echainiv_encrypt_compat_first;
  372. inst->alg.decrypt = echainiv_decrypt_compat;
  373. inst->alg.base.cra_init = echainiv_compat_init;
  374. inst->alg.base.cra_exit = echainiv_compat_exit;
  375. }
  376. err = aead_register_instance(tmpl, inst);
  377. if (err)
  378. goto free_inst;
  379. out:
  380. return err;
  381. free_inst:
  382. aead_geniv_free(inst);
  383. goto out;
  384. }
  385. static int echainiv_create(struct crypto_template *tmpl, struct rtattr **tb)
  386. {
  387. int err;
  388. err = crypto_get_default_rng();
  389. if (err)
  390. goto out;
  391. err = echainiv_aead_create(tmpl, tb);
  392. if (err)
  393. goto put_rng;
  394. out:
  395. return err;
  396. put_rng:
  397. crypto_put_default_rng();
  398. goto out;
  399. }
  400. static void echainiv_free(struct crypto_instance *inst)
  401. {
  402. aead_geniv_free(aead_instance(inst));
  403. crypto_put_default_rng();
  404. }
  405. static struct crypto_template echainiv_tmpl = {
  406. .name = "echainiv",
  407. .create = echainiv_create,
  408. .free = echainiv_free,
  409. .module = THIS_MODULE,
  410. };
  411. static int __init echainiv_module_init(void)
  412. {
  413. return crypto_register_template(&echainiv_tmpl);
  414. }
  415. static void __exit echainiv_module_exit(void)
  416. {
  417. crypto_unregister_template(&echainiv_tmpl);
  418. }
  419. module_init(echainiv_module_init);
  420. module_exit(echainiv_module_exit);
  421. MODULE_LICENSE("GPL");
  422. MODULE_DESCRIPTION("Encrypted Chain IV Generator");
  423. MODULE_ALIAS_CRYPTO("echainiv");