cbc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * CBC: Cipher Block Chaining mode
  3. *
  4. * Copyright (c) 2016 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_CBC_H
  13. #define _CRYPTO_CBC_H
  14. #include <crypto/internal/skcipher.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. static inline int crypto_cbc_encrypt_segment(
  18. struct skcipher_walk *walk, struct crypto_skcipher *tfm,
  19. void (*fn)(struct crypto_skcipher *, const u8 *, u8 *))
  20. {
  21. unsigned int bsize = crypto_skcipher_blocksize(tfm);
  22. unsigned int nbytes = walk->nbytes;
  23. u8 *src = walk->src.virt.addr;
  24. u8 *dst = walk->dst.virt.addr;
  25. u8 *iv = walk->iv;
  26. do {
  27. crypto_xor(iv, src, bsize);
  28. fn(tfm, iv, dst);
  29. memcpy(iv, dst, bsize);
  30. src += bsize;
  31. dst += bsize;
  32. } while ((nbytes -= bsize) >= bsize);
  33. return nbytes;
  34. }
  35. static inline int crypto_cbc_encrypt_inplace(
  36. struct skcipher_walk *walk, struct crypto_skcipher *tfm,
  37. void (*fn)(struct crypto_skcipher *, const u8 *, u8 *))
  38. {
  39. unsigned int bsize = crypto_skcipher_blocksize(tfm);
  40. unsigned int nbytes = walk->nbytes;
  41. u8 *src = walk->src.virt.addr;
  42. u8 *iv = walk->iv;
  43. do {
  44. crypto_xor(src, iv, bsize);
  45. fn(tfm, src, src);
  46. iv = src;
  47. src += bsize;
  48. } while ((nbytes -= bsize) >= bsize);
  49. memcpy(walk->iv, iv, bsize);
  50. return nbytes;
  51. }
  52. static inline int crypto_cbc_encrypt_walk(struct skcipher_request *req,
  53. void (*fn)(struct crypto_skcipher *,
  54. const u8 *, u8 *))
  55. {
  56. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  57. struct skcipher_walk walk;
  58. int err;
  59. err = skcipher_walk_virt(&walk, req, false);
  60. while (walk.nbytes) {
  61. if (walk.src.virt.addr == walk.dst.virt.addr)
  62. err = crypto_cbc_encrypt_inplace(&walk, tfm, fn);
  63. else
  64. err = crypto_cbc_encrypt_segment(&walk, tfm, fn);
  65. err = skcipher_walk_done(&walk, err);
  66. }
  67. return err;
  68. }
  69. static inline int crypto_cbc_decrypt_segment(
  70. struct skcipher_walk *walk, struct crypto_skcipher *tfm,
  71. void (*fn)(struct crypto_skcipher *, const u8 *, u8 *))
  72. {
  73. unsigned int bsize = crypto_skcipher_blocksize(tfm);
  74. unsigned int nbytes = walk->nbytes;
  75. u8 *src = walk->src.virt.addr;
  76. u8 *dst = walk->dst.virt.addr;
  77. u8 *iv = walk->iv;
  78. do {
  79. fn(tfm, src, dst);
  80. crypto_xor(dst, iv, bsize);
  81. iv = src;
  82. src += bsize;
  83. dst += bsize;
  84. } while ((nbytes -= bsize) >= bsize);
  85. memcpy(walk->iv, iv, bsize);
  86. return nbytes;
  87. }
  88. static inline int crypto_cbc_decrypt_inplace(
  89. struct skcipher_walk *walk, struct crypto_skcipher *tfm,
  90. void (*fn)(struct crypto_skcipher *, const u8 *, u8 *))
  91. {
  92. unsigned int bsize = crypto_skcipher_blocksize(tfm);
  93. unsigned int nbytes = walk->nbytes;
  94. u8 *src = walk->src.virt.addr;
  95. u8 last_iv[bsize];
  96. /* Start of the last block. */
  97. src += nbytes - (nbytes & (bsize - 1)) - bsize;
  98. memcpy(last_iv, src, bsize);
  99. for (;;) {
  100. fn(tfm, src, src);
  101. if ((nbytes -= bsize) < bsize)
  102. break;
  103. crypto_xor(src, src - bsize, bsize);
  104. src -= bsize;
  105. }
  106. crypto_xor(src, walk->iv, bsize);
  107. memcpy(walk->iv, last_iv, bsize);
  108. return nbytes;
  109. }
  110. static inline int crypto_cbc_decrypt_blocks(
  111. struct skcipher_walk *walk, struct crypto_skcipher *tfm,
  112. void (*fn)(struct crypto_skcipher *, const u8 *, u8 *))
  113. {
  114. if (walk->src.virt.addr == walk->dst.virt.addr)
  115. return crypto_cbc_decrypt_inplace(walk, tfm, fn);
  116. else
  117. return crypto_cbc_decrypt_segment(walk, tfm, fn);
  118. }
  119. #endif /* _CRYPTO_CBC_H */