echainiv.c 4.6 KB

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