seqiv.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/skcipher.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/random.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. struct seqiv_ctx {
  24. spinlock_t lock;
  25. u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
  26. };
  27. static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
  28. {
  29. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  30. struct crypto_ablkcipher *geniv;
  31. if (err == -EINPROGRESS)
  32. return;
  33. if (err)
  34. goto out;
  35. geniv = skcipher_givcrypt_reqtfm(req);
  36. memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
  37. out:
  38. kfree(subreq->info);
  39. }
  40. static void seqiv_complete(struct crypto_async_request *base, int err)
  41. {
  42. struct skcipher_givcrypt_request *req = base->data;
  43. seqiv_complete2(req, err);
  44. skcipher_givcrypt_complete(req, err);
  45. }
  46. static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
  47. {
  48. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  49. struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  50. struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
  51. crypto_completion_t complete;
  52. void *data;
  53. u8 *info;
  54. __be64 seq;
  55. unsigned int ivsize;
  56. unsigned int len;
  57. int err;
  58. ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
  59. complete = req->creq.base.complete;
  60. data = req->creq.base.data;
  61. info = req->creq.info;
  62. ivsize = crypto_ablkcipher_ivsize(geniv);
  63. if (unlikely(!IS_ALIGNED((unsigned long)info,
  64. crypto_ablkcipher_alignmask(geniv) + 1))) {
  65. info = kmalloc(ivsize, req->creq.base.flags &
  66. CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
  67. GFP_ATOMIC);
  68. if (!info)
  69. return -ENOMEM;
  70. complete = seqiv_complete;
  71. data = req;
  72. }
  73. ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
  74. data);
  75. ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
  76. req->creq.nbytes, info);
  77. len = ivsize;
  78. if (ivsize > sizeof(u64)) {
  79. memset(info, 0, ivsize - sizeof(u64));
  80. len = sizeof(u64);
  81. }
  82. seq = cpu_to_be64(req->seq);
  83. memcpy(info + ivsize - len, &seq, len);
  84. crypto_xor(info, ctx->salt, ivsize);
  85. memcpy(req->giv, info, ivsize);
  86. err = crypto_ablkcipher_encrypt(subreq);
  87. if (unlikely(info != req->creq.info))
  88. seqiv_complete2(req, err);
  89. return err;
  90. }
  91. static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
  92. {
  93. struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
  94. struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  95. spin_lock_bh(&ctx->lock);
  96. if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
  97. goto unlock;
  98. crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
  99. get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
  100. unlock:
  101. spin_unlock_bh(&ctx->lock);
  102. return seqiv_givencrypt(req);
  103. }
  104. static int seqiv_init(struct crypto_tfm *tfm)
  105. {
  106. struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
  107. struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
  108. spin_lock_init(&ctx->lock);
  109. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
  110. return skcipher_geniv_init(tfm);
  111. }
  112. static struct crypto_template seqiv_tmpl;
  113. static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
  114. {
  115. struct crypto_instance *inst;
  116. inst = skcipher_geniv_alloc(&seqiv_tmpl, tb, 0, 0);
  117. if (IS_ERR(inst))
  118. goto out;
  119. inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt_first;
  120. inst->alg.cra_init = seqiv_init;
  121. inst->alg.cra_exit = skcipher_geniv_exit;
  122. inst->alg.cra_alignmask |= __alignof__(u32) - 1;
  123. inst->alg.cra_ctxsize = sizeof(struct seqiv_ctx);
  124. inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
  125. out:
  126. return inst;
  127. }
  128. static struct crypto_template seqiv_tmpl = {
  129. .name = "seqiv",
  130. .alloc = seqiv_alloc,
  131. .free = skcipher_geniv_free,
  132. .module = THIS_MODULE,
  133. };
  134. static int __init seqiv_module_init(void)
  135. {
  136. return crypto_register_template(&seqiv_tmpl);
  137. }
  138. static void __exit seqiv_module_exit(void)
  139. {
  140. crypto_unregister_template(&seqiv_tmpl);
  141. }
  142. module_init(seqiv_module_init);
  143. module_exit(seqiv_module_exit);
  144. MODULE_LICENSE("GPL");
  145. MODULE_DESCRIPTION("Sequence Number IV Generator");