echainiv.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * echainiv: Encrypted Chain IV Generator
  3. *
  4. * This generator generates an IV based on a sequence number by multiplying
  5. * it 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/geniv.h>
  21. #include <crypto/scatterwalk.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. static int echainiv_encrypt(struct aead_request *req)
  29. {
  30. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  31. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  32. struct aead_request *subreq = aead_request_ctx(req);
  33. __be64 nseqno;
  34. u64 seqno;
  35. u8 *info;
  36. unsigned int ivsize = crypto_aead_ivsize(geniv);
  37. int err;
  38. if (req->cryptlen < ivsize)
  39. return -EINVAL;
  40. aead_request_set_tfm(subreq, ctx->child);
  41. info = req->iv;
  42. if (req->src != req->dst) {
  43. struct blkcipher_desc desc = {
  44. .tfm = ctx->null,
  45. };
  46. err = crypto_blkcipher_encrypt(
  47. &desc, req->dst, req->src,
  48. req->assoclen + req->cryptlen);
  49. if (err)
  50. return err;
  51. }
  52. aead_request_set_callback(subreq, req->base.flags,
  53. req->base.complete, req->base.data);
  54. aead_request_set_crypt(subreq, req->dst, req->dst,
  55. req->cryptlen, info);
  56. aead_request_set_ad(subreq, req->assoclen);
  57. memcpy(&nseqno, info + ivsize - 8, 8);
  58. seqno = be64_to_cpu(nseqno);
  59. memset(info, 0, ivsize);
  60. scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
  61. do {
  62. u64 a;
  63. memcpy(&a, ctx->salt + ivsize - 8, 8);
  64. a |= 1;
  65. a *= seqno;
  66. memcpy(info + ivsize - 8, &a, 8);
  67. } while ((ivsize -= 8));
  68. return crypto_aead_encrypt(subreq);
  69. }
  70. static int echainiv_decrypt(struct aead_request *req)
  71. {
  72. struct crypto_aead *geniv = crypto_aead_reqtfm(req);
  73. struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
  74. struct aead_request *subreq = aead_request_ctx(req);
  75. crypto_completion_t compl;
  76. void *data;
  77. unsigned int ivsize = crypto_aead_ivsize(geniv);
  78. if (req->cryptlen < ivsize)
  79. return -EINVAL;
  80. aead_request_set_tfm(subreq, ctx->child);
  81. compl = req->base.complete;
  82. data = req->base.data;
  83. aead_request_set_callback(subreq, req->base.flags, compl, data);
  84. aead_request_set_crypt(subreq, req->src, req->dst,
  85. req->cryptlen - ivsize, req->iv);
  86. aead_request_set_ad(subreq, req->assoclen + ivsize);
  87. scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
  88. return crypto_aead_decrypt(subreq);
  89. }
  90. static int echainiv_aead_create(struct crypto_template *tmpl,
  91. struct rtattr **tb)
  92. {
  93. struct aead_instance *inst;
  94. struct crypto_aead_spawn *spawn;
  95. struct aead_alg *alg;
  96. int err;
  97. inst = aead_geniv_alloc(tmpl, tb, 0, 0);
  98. if (IS_ERR(inst))
  99. return PTR_ERR(inst);
  100. spawn = aead_instance_ctx(inst);
  101. alg = crypto_spawn_aead_alg(spawn);
  102. err = -EINVAL;
  103. if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
  104. goto free_inst;
  105. inst->alg.encrypt = echainiv_encrypt;
  106. inst->alg.decrypt = echainiv_decrypt;
  107. inst->alg.init = aead_init_geniv;
  108. inst->alg.exit = aead_exit_geniv;
  109. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  110. inst->alg.base.cra_ctxsize += inst->alg.ivsize;
  111. inst->free = aead_geniv_free;
  112. err = aead_register_instance(tmpl, inst);
  113. if (err)
  114. goto free_inst;
  115. out:
  116. return err;
  117. free_inst:
  118. aead_geniv_free(inst);
  119. goto out;
  120. }
  121. static void echainiv_free(struct crypto_instance *inst)
  122. {
  123. aead_geniv_free(aead_instance(inst));
  124. }
  125. static struct crypto_template echainiv_tmpl = {
  126. .name = "echainiv",
  127. .create = echainiv_aead_create,
  128. .free = echainiv_free,
  129. .module = THIS_MODULE,
  130. };
  131. static int __init echainiv_module_init(void)
  132. {
  133. return crypto_register_template(&echainiv_tmpl);
  134. }
  135. static void __exit echainiv_module_exit(void)
  136. {
  137. crypto_unregister_template(&echainiv_tmpl);
  138. }
  139. module_init(echainiv_module_init);
  140. module_exit(echainiv_module_exit);
  141. MODULE_LICENSE("GPL");
  142. MODULE_DESCRIPTION("Encrypted Chain IV Generator");
  143. MODULE_ALIAS_CRYPTO("echainiv");