chainiv.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * chainiv: Chain IV Generator
  3. *
  4. * Generate IVs simply be using the last block of the previous encryption.
  5. * This is mainly useful for CBC with a synchronous algorithm.
  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/skcipher.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/random.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/string.h>
  22. struct chainiv_ctx {
  23. spinlock_t lock;
  24. char iv[];
  25. };
  26. static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
  27. {
  28. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  29. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  30. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  31. unsigned int ivsize;
  32. int err;
  33. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  34. ablkcipher_request_set_callback(subreq, req->creq.base.flags &
  35. ~CRYPTO_TFM_REQ_MAY_SLEEP,
  36. req->creq.base.complete,
  37. req->creq.base.data);
  38. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  39. req->creq.nbytes, req->creq.info);
  40. spin_lock_bh(&ctx->lock);
  41. ivsize = crypto_ablkcipher_ivsize(geniv);
  42. memcpy(req->giv, ctx->iv, ivsize);
  43. memcpy(subreq->info, ctx->iv, ivsize);
  44. err = crypto_ablkcipher_encrypt(subreq);
  45. if (err)
  46. goto unlock;
  47. memcpy(ctx->iv, subreq->info, ivsize);
  48. unlock:
  49. spin_unlock_bh(&ctx->lock);
  50. return err;
  51. }
  52. static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  53. {
  54. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  55. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  56. spin_lock_bh(&ctx->lock);
  57. if (crypto_ablkcipher_crt(geniv)->givencrypt !=
  58. chainiv_givencrypt_first)
  59. goto unlock;
  60. crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
  61. get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
  62. unlock:
  63. spin_unlock_bh(&ctx->lock);
  64. return chainiv_givencrypt(req);
  65. }
  66. static int chainiv_init(struct crypto_tfm *tfm)
  67. {
  68. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  69. struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  70. spin_lock_init(&ctx->lock);
  71. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  72. return skcipher_geniv_init(tfm);
  73. }
  74. static struct crypto_template chainiv_tmpl;
  75. static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
  76. {
  77. struct crypto_instance *inst;
  78. inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0,
  79. CRYPTO_ALG_ASYNC);
  80. if (IS_ERR(inst))
  81. goto out;
  82. inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
  83. inst->alg.cra_init = chainiv_init;
  84. inst->alg.cra_exit = skcipher_geniv_exit;
  85. inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx) +
  86. inst->alg.cra_ablkcipher.ivsize;
  87. out:
  88. return inst;
  89. }
  90. static struct crypto_template chainiv_tmpl = {
  91. .name = "chainiv",
  92. .alloc = chainiv_alloc,
  93. .free = skcipher_geniv_free,
  94. .module = THIS_MODULE,
  95. };
  96. static int __init chainiv_module_init(void)
  97. {
  98. return crypto_register_template(&chainiv_tmpl);
  99. }
  100. static void __exit chainiv_module_exit(void)
  101. {
  102. crypto_unregister_template(&chainiv_tmpl);
  103. }
  104. module_init(chainiv_module_init);
  105. module_exit(chainiv_module_exit);
  106. MODULE_LICENSE("GPL");
  107. MODULE_DESCRIPTION("Chain IV Generator");