cryptoloop.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Linux loop encryption enabling module
  3. Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
  4. Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
  5. This module is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This module is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this module; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <linux/module.h>
  18. #include <crypto/skcipher.h>
  19. #include <linux/init.h>
  20. #include <linux/string.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/scatterlist.h>
  23. #include <asm/uaccess.h>
  24. #include "loop.h"
  25. MODULE_LICENSE("GPL");
  26. MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
  27. MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
  28. #define LOOP_IV_SECTOR_BITS 9
  29. #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
  30. static int
  31. cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
  32. {
  33. int err = -EINVAL;
  34. int cipher_len;
  35. int mode_len;
  36. char cms[LO_NAME_SIZE]; /* cipher-mode string */
  37. char *cipher;
  38. char *mode;
  39. char *cmsp = cms; /* c-m string pointer */
  40. struct crypto_skcipher *tfm;
  41. /* encryption breaks for non sector aligned offsets */
  42. if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
  43. goto out;
  44. strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
  45. cms[LO_NAME_SIZE - 1] = 0;
  46. cipher = cmsp;
  47. cipher_len = strcspn(cmsp, "-");
  48. mode = cmsp + cipher_len;
  49. mode_len = 0;
  50. if (*mode) {
  51. mode++;
  52. mode_len = strcspn(mode, "-");
  53. }
  54. if (!mode_len) {
  55. mode = "cbc";
  56. mode_len = 3;
  57. }
  58. if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
  59. return -EINVAL;
  60. memmove(cms, mode, mode_len);
  61. cmsp = cms + mode_len;
  62. *cmsp++ = '(';
  63. memcpy(cmsp, info->lo_crypt_name, cipher_len);
  64. cmsp += cipher_len;
  65. *cmsp++ = ')';
  66. *cmsp = 0;
  67. tfm = crypto_alloc_skcipher(cms, 0, CRYPTO_ALG_ASYNC);
  68. if (IS_ERR(tfm))
  69. return PTR_ERR(tfm);
  70. err = crypto_skcipher_setkey(tfm, info->lo_encrypt_key,
  71. info->lo_encrypt_key_size);
  72. if (err != 0)
  73. goto out_free_tfm;
  74. lo->key_data = tfm;
  75. return 0;
  76. out_free_tfm:
  77. crypto_free_skcipher(tfm);
  78. out:
  79. return err;
  80. }
  81. typedef int (*encdec_cbc_t)(struct skcipher_request *req);
  82. static int
  83. cryptoloop_transfer(struct loop_device *lo, int cmd,
  84. struct page *raw_page, unsigned raw_off,
  85. struct page *loop_page, unsigned loop_off,
  86. int size, sector_t IV)
  87. {
  88. struct crypto_skcipher *tfm = lo->key_data;
  89. SKCIPHER_REQUEST_ON_STACK(req, tfm);
  90. struct scatterlist sg_out;
  91. struct scatterlist sg_in;
  92. encdec_cbc_t encdecfunc;
  93. struct page *in_page, *out_page;
  94. unsigned in_offs, out_offs;
  95. int err;
  96. skcipher_request_set_tfm(req, tfm);
  97. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  98. NULL, NULL);
  99. sg_init_table(&sg_out, 1);
  100. sg_init_table(&sg_in, 1);
  101. if (cmd == READ) {
  102. in_page = raw_page;
  103. in_offs = raw_off;
  104. out_page = loop_page;
  105. out_offs = loop_off;
  106. encdecfunc = crypto_skcipher_decrypt;
  107. } else {
  108. in_page = loop_page;
  109. in_offs = loop_off;
  110. out_page = raw_page;
  111. out_offs = raw_off;
  112. encdecfunc = crypto_skcipher_encrypt;
  113. }
  114. while (size > 0) {
  115. const int sz = min(size, LOOP_IV_SECTOR_SIZE);
  116. u32 iv[4] = { 0, };
  117. iv[0] = cpu_to_le32(IV & 0xffffffff);
  118. sg_set_page(&sg_in, in_page, sz, in_offs);
  119. sg_set_page(&sg_out, out_page, sz, out_offs);
  120. skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
  121. err = encdecfunc(req);
  122. if (err)
  123. goto out;
  124. IV++;
  125. size -= sz;
  126. in_offs += sz;
  127. out_offs += sz;
  128. }
  129. err = 0;
  130. out:
  131. skcipher_request_zero(req);
  132. return err;
  133. }
  134. static int
  135. cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
  136. {
  137. return -EINVAL;
  138. }
  139. static int
  140. cryptoloop_release(struct loop_device *lo)
  141. {
  142. struct crypto_skcipher *tfm = lo->key_data;
  143. if (tfm != NULL) {
  144. crypto_free_skcipher(tfm);
  145. lo->key_data = NULL;
  146. return 0;
  147. }
  148. printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
  149. return -EINVAL;
  150. }
  151. static struct loop_func_table cryptoloop_funcs = {
  152. .number = LO_CRYPT_CRYPTOAPI,
  153. .init = cryptoloop_init,
  154. .ioctl = cryptoloop_ioctl,
  155. .transfer = cryptoloop_transfer,
  156. .release = cryptoloop_release,
  157. .owner = THIS_MODULE
  158. };
  159. static int __init
  160. init_cryptoloop(void)
  161. {
  162. int rc = loop_register_transfer(&cryptoloop_funcs);
  163. if (rc)
  164. printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
  165. return rc;
  166. }
  167. static void __exit
  168. cleanup_cryptoloop(void)
  169. {
  170. if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
  171. printk(KERN_ERR
  172. "cryptoloop: loop_unregister_transfer failed\n");
  173. }
  174. module_init(init_cryptoloop);
  175. module_exit(cleanup_cryptoloop);