dm-crypt.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. /*
  2. * Copyright (C) 2003 Jana Saout <jana@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com>
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/bio.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/mempool.h>
  17. #include <linux/slab.h>
  18. #include <linux/crypto.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/atomic.h>
  22. #include <linux/scatterlist.h>
  23. #include <asm/page.h>
  24. #include <asm/unaligned.h>
  25. #include <crypto/hash.h>
  26. #include <crypto/md5.h>
  27. #include <crypto/algapi.h>
  28. #include <linux/device-mapper.h>
  29. #define DM_MSG_PREFIX "crypt"
  30. /*
  31. * context holding the current state of a multi-part conversion
  32. */
  33. struct convert_context {
  34. struct completion restart;
  35. struct bio *bio_in;
  36. struct bio *bio_out;
  37. struct bvec_iter iter_in;
  38. struct bvec_iter iter_out;
  39. sector_t cc_sector;
  40. atomic_t cc_pending;
  41. struct ablkcipher_request *req;
  42. };
  43. /*
  44. * per bio private data
  45. */
  46. struct dm_crypt_io {
  47. struct crypt_config *cc;
  48. struct bio *base_bio;
  49. struct work_struct work;
  50. struct convert_context ctx;
  51. atomic_t io_pending;
  52. int error;
  53. sector_t sector;
  54. struct dm_crypt_io *base_io;
  55. };
  56. struct dm_crypt_request {
  57. struct convert_context *ctx;
  58. struct scatterlist sg_in;
  59. struct scatterlist sg_out;
  60. sector_t iv_sector;
  61. };
  62. struct crypt_config;
  63. struct crypt_iv_operations {
  64. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  65. const char *opts);
  66. void (*dtr)(struct crypt_config *cc);
  67. int (*init)(struct crypt_config *cc);
  68. int (*wipe)(struct crypt_config *cc);
  69. int (*generator)(struct crypt_config *cc, u8 *iv,
  70. struct dm_crypt_request *dmreq);
  71. int (*post)(struct crypt_config *cc, u8 *iv,
  72. struct dm_crypt_request *dmreq);
  73. };
  74. struct iv_essiv_private {
  75. struct crypto_hash *hash_tfm;
  76. u8 *salt;
  77. };
  78. struct iv_benbi_private {
  79. int shift;
  80. };
  81. #define LMK_SEED_SIZE 64 /* hash + 0 */
  82. struct iv_lmk_private {
  83. struct crypto_shash *hash_tfm;
  84. u8 *seed;
  85. };
  86. #define TCW_WHITENING_SIZE 16
  87. struct iv_tcw_private {
  88. struct crypto_shash *crc32_tfm;
  89. u8 *iv_seed;
  90. u8 *whitening;
  91. };
  92. /*
  93. * Crypt: maps a linear range of a block device
  94. * and encrypts / decrypts at the same time.
  95. */
  96. enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
  97. /*
  98. * The fields in here must be read only after initialization.
  99. */
  100. struct crypt_config {
  101. struct dm_dev *dev;
  102. sector_t start;
  103. /*
  104. * pool for per bio private data, crypto requests and
  105. * encryption requeusts/buffer pages
  106. */
  107. mempool_t *io_pool;
  108. mempool_t *req_pool;
  109. mempool_t *page_pool;
  110. struct bio_set *bs;
  111. struct workqueue_struct *io_queue;
  112. struct workqueue_struct *crypt_queue;
  113. char *cipher;
  114. char *cipher_string;
  115. struct crypt_iv_operations *iv_gen_ops;
  116. union {
  117. struct iv_essiv_private essiv;
  118. struct iv_benbi_private benbi;
  119. struct iv_lmk_private lmk;
  120. struct iv_tcw_private tcw;
  121. } iv_gen_private;
  122. sector_t iv_offset;
  123. unsigned int iv_size;
  124. /* ESSIV: struct crypto_cipher *essiv_tfm */
  125. void *iv_private;
  126. struct crypto_ablkcipher **tfms;
  127. unsigned tfms_count;
  128. /*
  129. * Layout of each crypto request:
  130. *
  131. * struct ablkcipher_request
  132. * context
  133. * padding
  134. * struct dm_crypt_request
  135. * padding
  136. * IV
  137. *
  138. * The padding is added so that dm_crypt_request and the IV are
  139. * correctly aligned.
  140. */
  141. unsigned int dmreq_start;
  142. unsigned long flags;
  143. unsigned int key_size;
  144. unsigned int key_parts; /* independent parts in key buffer */
  145. unsigned int key_extra_size; /* additional keys length */
  146. u8 key[0];
  147. };
  148. #define MIN_IOS 16
  149. #define MIN_POOL_PAGES 32
  150. static struct kmem_cache *_crypt_io_pool;
  151. static void clone_init(struct dm_crypt_io *, struct bio *);
  152. static void kcryptd_queue_crypt(struct dm_crypt_io *io);
  153. static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
  154. /*
  155. * Use this to access cipher attributes that are the same for each CPU.
  156. */
  157. static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
  158. {
  159. return cc->tfms[0];
  160. }
  161. /*
  162. * Different IV generation algorithms:
  163. *
  164. * plain: the initial vector is the 32-bit little-endian version of the sector
  165. * number, padded with zeros if necessary.
  166. *
  167. * plain64: the initial vector is the 64-bit little-endian version of the sector
  168. * number, padded with zeros if necessary.
  169. *
  170. * essiv: "encrypted sector|salt initial vector", the sector number is
  171. * encrypted with the bulk cipher using a salt as key. The salt
  172. * should be derived from the bulk cipher's key via hashing.
  173. *
  174. * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
  175. * (needed for LRW-32-AES and possible other narrow block modes)
  176. *
  177. * null: the initial vector is always zero. Provides compatibility with
  178. * obsolete loop_fish2 devices. Do not use for new devices.
  179. *
  180. * lmk: Compatible implementation of the block chaining mode used
  181. * by the Loop-AES block device encryption system
  182. * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
  183. * It operates on full 512 byte sectors and uses CBC
  184. * with an IV derived from the sector number, the data and
  185. * optionally extra IV seed.
  186. * This means that after decryption the first block
  187. * of sector must be tweaked according to decrypted data.
  188. * Loop-AES can use three encryption schemes:
  189. * version 1: is plain aes-cbc mode
  190. * version 2: uses 64 multikey scheme with lmk IV generator
  191. * version 3: the same as version 2 with additional IV seed
  192. * (it uses 65 keys, last key is used as IV seed)
  193. *
  194. * tcw: Compatible implementation of the block chaining mode used
  195. * by the TrueCrypt device encryption system (prior to version 4.1).
  196. * For more info see: http://www.truecrypt.org
  197. * It operates on full 512 byte sectors and uses CBC
  198. * with an IV derived from initial key and the sector number.
  199. * In addition, whitening value is applied on every sector, whitening
  200. * is calculated from initial key, sector number and mixed using CRC32.
  201. * Note that this encryption scheme is vulnerable to watermarking attacks
  202. * and should be used for old compatible containers access only.
  203. *
  204. * plumb: unimplemented, see:
  205. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  206. */
  207. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
  208. struct dm_crypt_request *dmreq)
  209. {
  210. memset(iv, 0, cc->iv_size);
  211. *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
  212. return 0;
  213. }
  214. static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
  215. struct dm_crypt_request *dmreq)
  216. {
  217. memset(iv, 0, cc->iv_size);
  218. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  219. return 0;
  220. }
  221. /* Initialise ESSIV - compute salt but no local memory allocations */
  222. static int crypt_iv_essiv_init(struct crypt_config *cc)
  223. {
  224. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  225. struct hash_desc desc;
  226. struct scatterlist sg;
  227. struct crypto_cipher *essiv_tfm;
  228. int err;
  229. sg_init_one(&sg, cc->key, cc->key_size);
  230. desc.tfm = essiv->hash_tfm;
  231. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  232. err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
  233. if (err)
  234. return err;
  235. essiv_tfm = cc->iv_private;
  236. err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
  237. crypto_hash_digestsize(essiv->hash_tfm));
  238. if (err)
  239. return err;
  240. return 0;
  241. }
  242. /* Wipe salt and reset key derived from volume key */
  243. static int crypt_iv_essiv_wipe(struct crypt_config *cc)
  244. {
  245. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  246. unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
  247. struct crypto_cipher *essiv_tfm;
  248. int r, err = 0;
  249. memset(essiv->salt, 0, salt_size);
  250. essiv_tfm = cc->iv_private;
  251. r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
  252. if (r)
  253. err = r;
  254. return err;
  255. }
  256. /* Set up per cpu cipher state */
  257. static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
  258. struct dm_target *ti,
  259. u8 *salt, unsigned saltsize)
  260. {
  261. struct crypto_cipher *essiv_tfm;
  262. int err;
  263. /* Setup the essiv_tfm with the given salt */
  264. essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  265. if (IS_ERR(essiv_tfm)) {
  266. ti->error = "Error allocating crypto tfm for ESSIV";
  267. return essiv_tfm;
  268. }
  269. if (crypto_cipher_blocksize(essiv_tfm) !=
  270. crypto_ablkcipher_ivsize(any_tfm(cc))) {
  271. ti->error = "Block size of ESSIV cipher does "
  272. "not match IV size of block cipher";
  273. crypto_free_cipher(essiv_tfm);
  274. return ERR_PTR(-EINVAL);
  275. }
  276. err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
  277. if (err) {
  278. ti->error = "Failed to set key for ESSIV cipher";
  279. crypto_free_cipher(essiv_tfm);
  280. return ERR_PTR(err);
  281. }
  282. return essiv_tfm;
  283. }
  284. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  285. {
  286. struct crypto_cipher *essiv_tfm;
  287. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  288. crypto_free_hash(essiv->hash_tfm);
  289. essiv->hash_tfm = NULL;
  290. kzfree(essiv->salt);
  291. essiv->salt = NULL;
  292. essiv_tfm = cc->iv_private;
  293. if (essiv_tfm)
  294. crypto_free_cipher(essiv_tfm);
  295. cc->iv_private = NULL;
  296. }
  297. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  298. const char *opts)
  299. {
  300. struct crypto_cipher *essiv_tfm = NULL;
  301. struct crypto_hash *hash_tfm = NULL;
  302. u8 *salt = NULL;
  303. int err;
  304. if (!opts) {
  305. ti->error = "Digest algorithm missing for ESSIV mode";
  306. return -EINVAL;
  307. }
  308. /* Allocate hash algorithm */
  309. hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
  310. if (IS_ERR(hash_tfm)) {
  311. ti->error = "Error initializing ESSIV hash";
  312. err = PTR_ERR(hash_tfm);
  313. goto bad;
  314. }
  315. salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
  316. if (!salt) {
  317. ti->error = "Error kmallocing salt storage in ESSIV";
  318. err = -ENOMEM;
  319. goto bad;
  320. }
  321. cc->iv_gen_private.essiv.salt = salt;
  322. cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
  323. essiv_tfm = setup_essiv_cpu(cc, ti, salt,
  324. crypto_hash_digestsize(hash_tfm));
  325. if (IS_ERR(essiv_tfm)) {
  326. crypt_iv_essiv_dtr(cc);
  327. return PTR_ERR(essiv_tfm);
  328. }
  329. cc->iv_private = essiv_tfm;
  330. return 0;
  331. bad:
  332. if (hash_tfm && !IS_ERR(hash_tfm))
  333. crypto_free_hash(hash_tfm);
  334. kfree(salt);
  335. return err;
  336. }
  337. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
  338. struct dm_crypt_request *dmreq)
  339. {
  340. struct crypto_cipher *essiv_tfm = cc->iv_private;
  341. memset(iv, 0, cc->iv_size);
  342. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  343. crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
  344. return 0;
  345. }
  346. static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
  347. const char *opts)
  348. {
  349. unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
  350. int log = ilog2(bs);
  351. /* we need to calculate how far we must shift the sector count
  352. * to get the cipher block count, we use this shift in _gen */
  353. if (1 << log != bs) {
  354. ti->error = "cypher blocksize is not a power of 2";
  355. return -EINVAL;
  356. }
  357. if (log > 9) {
  358. ti->error = "cypher blocksize is > 512";
  359. return -EINVAL;
  360. }
  361. cc->iv_gen_private.benbi.shift = 9 - log;
  362. return 0;
  363. }
  364. static void crypt_iv_benbi_dtr(struct crypt_config *cc)
  365. {
  366. }
  367. static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
  368. struct dm_crypt_request *dmreq)
  369. {
  370. __be64 val;
  371. memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
  372. val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
  373. put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
  374. return 0;
  375. }
  376. static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
  377. struct dm_crypt_request *dmreq)
  378. {
  379. memset(iv, 0, cc->iv_size);
  380. return 0;
  381. }
  382. static void crypt_iv_lmk_dtr(struct crypt_config *cc)
  383. {
  384. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  385. if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
  386. crypto_free_shash(lmk->hash_tfm);
  387. lmk->hash_tfm = NULL;
  388. kzfree(lmk->seed);
  389. lmk->seed = NULL;
  390. }
  391. static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
  392. const char *opts)
  393. {
  394. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  395. lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
  396. if (IS_ERR(lmk->hash_tfm)) {
  397. ti->error = "Error initializing LMK hash";
  398. return PTR_ERR(lmk->hash_tfm);
  399. }
  400. /* No seed in LMK version 2 */
  401. if (cc->key_parts == cc->tfms_count) {
  402. lmk->seed = NULL;
  403. return 0;
  404. }
  405. lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
  406. if (!lmk->seed) {
  407. crypt_iv_lmk_dtr(cc);
  408. ti->error = "Error kmallocing seed storage in LMK";
  409. return -ENOMEM;
  410. }
  411. return 0;
  412. }
  413. static int crypt_iv_lmk_init(struct crypt_config *cc)
  414. {
  415. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  416. int subkey_size = cc->key_size / cc->key_parts;
  417. /* LMK seed is on the position of LMK_KEYS + 1 key */
  418. if (lmk->seed)
  419. memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
  420. crypto_shash_digestsize(lmk->hash_tfm));
  421. return 0;
  422. }
  423. static int crypt_iv_lmk_wipe(struct crypt_config *cc)
  424. {
  425. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  426. if (lmk->seed)
  427. memset(lmk->seed, 0, LMK_SEED_SIZE);
  428. return 0;
  429. }
  430. static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
  431. struct dm_crypt_request *dmreq,
  432. u8 *data)
  433. {
  434. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  435. struct {
  436. struct shash_desc desc;
  437. char ctx[crypto_shash_descsize(lmk->hash_tfm)];
  438. } sdesc;
  439. struct md5_state md5state;
  440. __le32 buf[4];
  441. int i, r;
  442. sdesc.desc.tfm = lmk->hash_tfm;
  443. sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  444. r = crypto_shash_init(&sdesc.desc);
  445. if (r)
  446. return r;
  447. if (lmk->seed) {
  448. r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
  449. if (r)
  450. return r;
  451. }
  452. /* Sector is always 512B, block size 16, add data of blocks 1-31 */
  453. r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
  454. if (r)
  455. return r;
  456. /* Sector is cropped to 56 bits here */
  457. buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
  458. buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
  459. buf[2] = cpu_to_le32(4024);
  460. buf[3] = 0;
  461. r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
  462. if (r)
  463. return r;
  464. /* No MD5 padding here */
  465. r = crypto_shash_export(&sdesc.desc, &md5state);
  466. if (r)
  467. return r;
  468. for (i = 0; i < MD5_HASH_WORDS; i++)
  469. __cpu_to_le32s(&md5state.hash[i]);
  470. memcpy(iv, &md5state.hash, cc->iv_size);
  471. return 0;
  472. }
  473. static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
  474. struct dm_crypt_request *dmreq)
  475. {
  476. u8 *src;
  477. int r = 0;
  478. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
  479. src = kmap_atomic(sg_page(&dmreq->sg_in));
  480. r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
  481. kunmap_atomic(src);
  482. } else
  483. memset(iv, 0, cc->iv_size);
  484. return r;
  485. }
  486. static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
  487. struct dm_crypt_request *dmreq)
  488. {
  489. u8 *dst;
  490. int r;
  491. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
  492. return 0;
  493. dst = kmap_atomic(sg_page(&dmreq->sg_out));
  494. r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
  495. /* Tweak the first block of plaintext sector */
  496. if (!r)
  497. crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
  498. kunmap_atomic(dst);
  499. return r;
  500. }
  501. static void crypt_iv_tcw_dtr(struct crypt_config *cc)
  502. {
  503. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  504. kzfree(tcw->iv_seed);
  505. tcw->iv_seed = NULL;
  506. kzfree(tcw->whitening);
  507. tcw->whitening = NULL;
  508. if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
  509. crypto_free_shash(tcw->crc32_tfm);
  510. tcw->crc32_tfm = NULL;
  511. }
  512. static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
  513. const char *opts)
  514. {
  515. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  516. if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
  517. ti->error = "Wrong key size for TCW";
  518. return -EINVAL;
  519. }
  520. tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
  521. if (IS_ERR(tcw->crc32_tfm)) {
  522. ti->error = "Error initializing CRC32 in TCW";
  523. return PTR_ERR(tcw->crc32_tfm);
  524. }
  525. tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
  526. tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
  527. if (!tcw->iv_seed || !tcw->whitening) {
  528. crypt_iv_tcw_dtr(cc);
  529. ti->error = "Error allocating seed storage in TCW";
  530. return -ENOMEM;
  531. }
  532. return 0;
  533. }
  534. static int crypt_iv_tcw_init(struct crypt_config *cc)
  535. {
  536. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  537. int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
  538. memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
  539. memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
  540. TCW_WHITENING_SIZE);
  541. return 0;
  542. }
  543. static int crypt_iv_tcw_wipe(struct crypt_config *cc)
  544. {
  545. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  546. memset(tcw->iv_seed, 0, cc->iv_size);
  547. memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
  548. return 0;
  549. }
  550. static int crypt_iv_tcw_whitening(struct crypt_config *cc,
  551. struct dm_crypt_request *dmreq,
  552. u8 *data)
  553. {
  554. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  555. u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
  556. u8 buf[TCW_WHITENING_SIZE];
  557. struct {
  558. struct shash_desc desc;
  559. char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
  560. } sdesc;
  561. int i, r;
  562. /* xor whitening with sector number */
  563. memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
  564. crypto_xor(buf, (u8 *)&sector, 8);
  565. crypto_xor(&buf[8], (u8 *)&sector, 8);
  566. /* calculate crc32 for every 32bit part and xor it */
  567. sdesc.desc.tfm = tcw->crc32_tfm;
  568. sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  569. for (i = 0; i < 4; i++) {
  570. r = crypto_shash_init(&sdesc.desc);
  571. if (r)
  572. goto out;
  573. r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
  574. if (r)
  575. goto out;
  576. r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
  577. if (r)
  578. goto out;
  579. }
  580. crypto_xor(&buf[0], &buf[12], 4);
  581. crypto_xor(&buf[4], &buf[8], 4);
  582. /* apply whitening (8 bytes) to whole sector */
  583. for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
  584. crypto_xor(data + i * 8, buf, 8);
  585. out:
  586. memset(buf, 0, sizeof(buf));
  587. return r;
  588. }
  589. static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
  590. struct dm_crypt_request *dmreq)
  591. {
  592. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  593. u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
  594. u8 *src;
  595. int r = 0;
  596. /* Remove whitening from ciphertext */
  597. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
  598. src = kmap_atomic(sg_page(&dmreq->sg_in));
  599. r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
  600. kunmap_atomic(src);
  601. }
  602. /* Calculate IV */
  603. memcpy(iv, tcw->iv_seed, cc->iv_size);
  604. crypto_xor(iv, (u8 *)&sector, 8);
  605. if (cc->iv_size > 8)
  606. crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
  607. return r;
  608. }
  609. static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
  610. struct dm_crypt_request *dmreq)
  611. {
  612. u8 *dst;
  613. int r;
  614. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
  615. return 0;
  616. /* Apply whitening on ciphertext */
  617. dst = kmap_atomic(sg_page(&dmreq->sg_out));
  618. r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
  619. kunmap_atomic(dst);
  620. return r;
  621. }
  622. static struct crypt_iv_operations crypt_iv_plain_ops = {
  623. .generator = crypt_iv_plain_gen
  624. };
  625. static struct crypt_iv_operations crypt_iv_plain64_ops = {
  626. .generator = crypt_iv_plain64_gen
  627. };
  628. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  629. .ctr = crypt_iv_essiv_ctr,
  630. .dtr = crypt_iv_essiv_dtr,
  631. .init = crypt_iv_essiv_init,
  632. .wipe = crypt_iv_essiv_wipe,
  633. .generator = crypt_iv_essiv_gen
  634. };
  635. static struct crypt_iv_operations crypt_iv_benbi_ops = {
  636. .ctr = crypt_iv_benbi_ctr,
  637. .dtr = crypt_iv_benbi_dtr,
  638. .generator = crypt_iv_benbi_gen
  639. };
  640. static struct crypt_iv_operations crypt_iv_null_ops = {
  641. .generator = crypt_iv_null_gen
  642. };
  643. static struct crypt_iv_operations crypt_iv_lmk_ops = {
  644. .ctr = crypt_iv_lmk_ctr,
  645. .dtr = crypt_iv_lmk_dtr,
  646. .init = crypt_iv_lmk_init,
  647. .wipe = crypt_iv_lmk_wipe,
  648. .generator = crypt_iv_lmk_gen,
  649. .post = crypt_iv_lmk_post
  650. };
  651. static struct crypt_iv_operations crypt_iv_tcw_ops = {
  652. .ctr = crypt_iv_tcw_ctr,
  653. .dtr = crypt_iv_tcw_dtr,
  654. .init = crypt_iv_tcw_init,
  655. .wipe = crypt_iv_tcw_wipe,
  656. .generator = crypt_iv_tcw_gen,
  657. .post = crypt_iv_tcw_post
  658. };
  659. static void crypt_convert_init(struct crypt_config *cc,
  660. struct convert_context *ctx,
  661. struct bio *bio_out, struct bio *bio_in,
  662. sector_t sector)
  663. {
  664. ctx->bio_in = bio_in;
  665. ctx->bio_out = bio_out;
  666. if (bio_in)
  667. ctx->iter_in = bio_in->bi_iter;
  668. if (bio_out)
  669. ctx->iter_out = bio_out->bi_iter;
  670. ctx->cc_sector = sector + cc->iv_offset;
  671. init_completion(&ctx->restart);
  672. }
  673. static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
  674. struct ablkcipher_request *req)
  675. {
  676. return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
  677. }
  678. static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
  679. struct dm_crypt_request *dmreq)
  680. {
  681. return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
  682. }
  683. static u8 *iv_of_dmreq(struct crypt_config *cc,
  684. struct dm_crypt_request *dmreq)
  685. {
  686. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  687. crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
  688. }
  689. static int crypt_convert_block(struct crypt_config *cc,
  690. struct convert_context *ctx,
  691. struct ablkcipher_request *req)
  692. {
  693. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  694. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  695. struct dm_crypt_request *dmreq;
  696. u8 *iv;
  697. int r;
  698. dmreq = dmreq_of_req(cc, req);
  699. iv = iv_of_dmreq(cc, dmreq);
  700. dmreq->iv_sector = ctx->cc_sector;
  701. dmreq->ctx = ctx;
  702. sg_init_table(&dmreq->sg_in, 1);
  703. sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
  704. bv_in.bv_offset);
  705. sg_init_table(&dmreq->sg_out, 1);
  706. sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
  707. bv_out.bv_offset);
  708. bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
  709. bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
  710. if (cc->iv_gen_ops) {
  711. r = cc->iv_gen_ops->generator(cc, iv, dmreq);
  712. if (r < 0)
  713. return r;
  714. }
  715. ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
  716. 1 << SECTOR_SHIFT, iv);
  717. if (bio_data_dir(ctx->bio_in) == WRITE)
  718. r = crypto_ablkcipher_encrypt(req);
  719. else
  720. r = crypto_ablkcipher_decrypt(req);
  721. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  722. r = cc->iv_gen_ops->post(cc, iv, dmreq);
  723. return r;
  724. }
  725. static void kcryptd_async_done(struct crypto_async_request *async_req,
  726. int error);
  727. static void crypt_alloc_req(struct crypt_config *cc,
  728. struct convert_context *ctx)
  729. {
  730. unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
  731. if (!ctx->req)
  732. ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
  733. ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
  734. ablkcipher_request_set_callback(ctx->req,
  735. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  736. kcryptd_async_done, dmreq_of_req(cc, ctx->req));
  737. }
  738. /*
  739. * Encrypt / decrypt data from one bio to another one (can be the same one)
  740. */
  741. static int crypt_convert(struct crypt_config *cc,
  742. struct convert_context *ctx)
  743. {
  744. int r;
  745. atomic_set(&ctx->cc_pending, 1);
  746. while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
  747. crypt_alloc_req(cc, ctx);
  748. atomic_inc(&ctx->cc_pending);
  749. r = crypt_convert_block(cc, ctx, ctx->req);
  750. switch (r) {
  751. /* async */
  752. case -EBUSY:
  753. wait_for_completion(&ctx->restart);
  754. reinit_completion(&ctx->restart);
  755. /* fall through*/
  756. case -EINPROGRESS:
  757. ctx->req = NULL;
  758. ctx->cc_sector++;
  759. continue;
  760. /* sync */
  761. case 0:
  762. atomic_dec(&ctx->cc_pending);
  763. ctx->cc_sector++;
  764. cond_resched();
  765. continue;
  766. /* error */
  767. default:
  768. atomic_dec(&ctx->cc_pending);
  769. return r;
  770. }
  771. }
  772. return 0;
  773. }
  774. /*
  775. * Generate a new unfragmented bio with the given size
  776. * This should never violate the device limitations
  777. * May return a smaller bio when running out of pages, indicated by
  778. * *out_of_pages set to 1.
  779. */
  780. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
  781. unsigned *out_of_pages)
  782. {
  783. struct crypt_config *cc = io->cc;
  784. struct bio *clone;
  785. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  786. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  787. unsigned i, len;
  788. struct page *page;
  789. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  790. if (!clone)
  791. return NULL;
  792. clone_init(io, clone);
  793. *out_of_pages = 0;
  794. for (i = 0; i < nr_iovecs; i++) {
  795. page = mempool_alloc(cc->page_pool, gfp_mask);
  796. if (!page) {
  797. *out_of_pages = 1;
  798. break;
  799. }
  800. /*
  801. * If additional pages cannot be allocated without waiting,
  802. * return a partially-allocated bio. The caller will then try
  803. * to allocate more bios while submitting this partial bio.
  804. */
  805. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  806. len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  807. if (!bio_add_page(clone, page, len, 0)) {
  808. mempool_free(page, cc->page_pool);
  809. break;
  810. }
  811. size -= len;
  812. }
  813. if (!clone->bi_iter.bi_size) {
  814. bio_put(clone);
  815. return NULL;
  816. }
  817. return clone;
  818. }
  819. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  820. {
  821. unsigned int i;
  822. struct bio_vec *bv;
  823. bio_for_each_segment_all(bv, clone, i) {
  824. BUG_ON(!bv->bv_page);
  825. mempool_free(bv->bv_page, cc->page_pool);
  826. bv->bv_page = NULL;
  827. }
  828. }
  829. static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc,
  830. struct bio *bio, sector_t sector)
  831. {
  832. struct dm_crypt_io *io;
  833. io = mempool_alloc(cc->io_pool, GFP_NOIO);
  834. io->cc = cc;
  835. io->base_bio = bio;
  836. io->sector = sector;
  837. io->error = 0;
  838. io->base_io = NULL;
  839. io->ctx.req = NULL;
  840. atomic_set(&io->io_pending, 0);
  841. return io;
  842. }
  843. static void crypt_inc_pending(struct dm_crypt_io *io)
  844. {
  845. atomic_inc(&io->io_pending);
  846. }
  847. /*
  848. * One of the bios was finished. Check for completion of
  849. * the whole request and correctly clean up the buffer.
  850. * If base_io is set, wait for the last fragment to complete.
  851. */
  852. static void crypt_dec_pending(struct dm_crypt_io *io)
  853. {
  854. struct crypt_config *cc = io->cc;
  855. struct bio *base_bio = io->base_bio;
  856. struct dm_crypt_io *base_io = io->base_io;
  857. int error = io->error;
  858. if (!atomic_dec_and_test(&io->io_pending))
  859. return;
  860. if (io->ctx.req)
  861. mempool_free(io->ctx.req, cc->req_pool);
  862. mempool_free(io, cc->io_pool);
  863. if (likely(!base_io))
  864. bio_endio(base_bio, error);
  865. else {
  866. if (error && !base_io->error)
  867. base_io->error = error;
  868. crypt_dec_pending(base_io);
  869. }
  870. }
  871. /*
  872. * kcryptd/kcryptd_io:
  873. *
  874. * Needed because it would be very unwise to do decryption in an
  875. * interrupt context.
  876. *
  877. * kcryptd performs the actual encryption or decryption.
  878. *
  879. * kcryptd_io performs the IO submission.
  880. *
  881. * They must be separated as otherwise the final stages could be
  882. * starved by new requests which can block in the first stages due
  883. * to memory allocation.
  884. *
  885. * The work is done per CPU global for all dm-crypt instances.
  886. * They should not depend on each other and do not block.
  887. */
  888. static void crypt_endio(struct bio *clone, int error)
  889. {
  890. struct dm_crypt_io *io = clone->bi_private;
  891. struct crypt_config *cc = io->cc;
  892. unsigned rw = bio_data_dir(clone);
  893. if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
  894. error = -EIO;
  895. /*
  896. * free the processed pages
  897. */
  898. if (rw == WRITE)
  899. crypt_free_buffer_pages(cc, clone);
  900. bio_put(clone);
  901. if (rw == READ && !error) {
  902. kcryptd_queue_crypt(io);
  903. return;
  904. }
  905. if (unlikely(error))
  906. io->error = error;
  907. crypt_dec_pending(io);
  908. }
  909. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  910. {
  911. struct crypt_config *cc = io->cc;
  912. clone->bi_private = io;
  913. clone->bi_end_io = crypt_endio;
  914. clone->bi_bdev = cc->dev->bdev;
  915. clone->bi_rw = io->base_bio->bi_rw;
  916. }
  917. static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
  918. {
  919. struct crypt_config *cc = io->cc;
  920. struct bio *base_bio = io->base_bio;
  921. struct bio *clone;
  922. /*
  923. * The block layer might modify the bvec array, so always
  924. * copy the required bvecs because we need the original
  925. * one in order to decrypt the whole bio data *afterwards*.
  926. */
  927. clone = bio_clone_bioset(base_bio, gfp, cc->bs);
  928. if (!clone)
  929. return 1;
  930. crypt_inc_pending(io);
  931. clone_init(io, clone);
  932. clone->bi_iter.bi_sector = cc->start + io->sector;
  933. generic_make_request(clone);
  934. return 0;
  935. }
  936. static void kcryptd_io_write(struct dm_crypt_io *io)
  937. {
  938. struct bio *clone = io->ctx.bio_out;
  939. generic_make_request(clone);
  940. }
  941. static void kcryptd_io(struct work_struct *work)
  942. {
  943. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  944. if (bio_data_dir(io->base_bio) == READ) {
  945. crypt_inc_pending(io);
  946. if (kcryptd_io_read(io, GFP_NOIO))
  947. io->error = -ENOMEM;
  948. crypt_dec_pending(io);
  949. } else
  950. kcryptd_io_write(io);
  951. }
  952. static void kcryptd_queue_io(struct dm_crypt_io *io)
  953. {
  954. struct crypt_config *cc = io->cc;
  955. INIT_WORK(&io->work, kcryptd_io);
  956. queue_work(cc->io_queue, &io->work);
  957. }
  958. static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
  959. {
  960. struct bio *clone = io->ctx.bio_out;
  961. struct crypt_config *cc = io->cc;
  962. if (unlikely(io->error < 0)) {
  963. crypt_free_buffer_pages(cc, clone);
  964. bio_put(clone);
  965. crypt_dec_pending(io);
  966. return;
  967. }
  968. /* crypt_convert should have filled the clone bio */
  969. BUG_ON(io->ctx.iter_out.bi_size);
  970. clone->bi_iter.bi_sector = cc->start + io->sector;
  971. if (async)
  972. kcryptd_queue_io(io);
  973. else
  974. generic_make_request(clone);
  975. }
  976. static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
  977. {
  978. struct crypt_config *cc = io->cc;
  979. struct bio *clone;
  980. struct dm_crypt_io *new_io;
  981. int crypt_finished;
  982. unsigned out_of_pages = 0;
  983. unsigned remaining = io->base_bio->bi_iter.bi_size;
  984. sector_t sector = io->sector;
  985. int r;
  986. /*
  987. * Prevent io from disappearing until this function completes.
  988. */
  989. crypt_inc_pending(io);
  990. crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
  991. /*
  992. * The allocated buffers can be smaller than the whole bio,
  993. * so repeat the whole process until all the data can be handled.
  994. */
  995. while (remaining) {
  996. clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
  997. if (unlikely(!clone)) {
  998. io->error = -ENOMEM;
  999. break;
  1000. }
  1001. io->ctx.bio_out = clone;
  1002. io->ctx.iter_out = clone->bi_iter;
  1003. remaining -= clone->bi_iter.bi_size;
  1004. sector += bio_sectors(clone);
  1005. crypt_inc_pending(io);
  1006. r = crypt_convert(cc, &io->ctx);
  1007. if (r < 0)
  1008. io->error = -EIO;
  1009. crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
  1010. /* Encryption was already finished, submit io now */
  1011. if (crypt_finished) {
  1012. kcryptd_crypt_write_io_submit(io, 0);
  1013. /*
  1014. * If there was an error, do not try next fragments.
  1015. * For async, error is processed in async handler.
  1016. */
  1017. if (unlikely(r < 0))
  1018. break;
  1019. io->sector = sector;
  1020. }
  1021. /*
  1022. * Out of memory -> run queues
  1023. * But don't wait if split was due to the io size restriction
  1024. */
  1025. if (unlikely(out_of_pages))
  1026. congestion_wait(BLK_RW_ASYNC, HZ/100);
  1027. /*
  1028. * With async crypto it is unsafe to share the crypto context
  1029. * between fragments, so switch to a new dm_crypt_io structure.
  1030. */
  1031. if (unlikely(!crypt_finished && remaining)) {
  1032. new_io = crypt_io_alloc(io->cc, io->base_bio,
  1033. sector);
  1034. crypt_inc_pending(new_io);
  1035. crypt_convert_init(cc, &new_io->ctx, NULL,
  1036. io->base_bio, sector);
  1037. new_io->ctx.iter_in = io->ctx.iter_in;
  1038. /*
  1039. * Fragments after the first use the base_io
  1040. * pending count.
  1041. */
  1042. if (!io->base_io)
  1043. new_io->base_io = io;
  1044. else {
  1045. new_io->base_io = io->base_io;
  1046. crypt_inc_pending(io->base_io);
  1047. crypt_dec_pending(io);
  1048. }
  1049. io = new_io;
  1050. }
  1051. }
  1052. crypt_dec_pending(io);
  1053. }
  1054. static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
  1055. {
  1056. crypt_dec_pending(io);
  1057. }
  1058. static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
  1059. {
  1060. struct crypt_config *cc = io->cc;
  1061. int r = 0;
  1062. crypt_inc_pending(io);
  1063. crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
  1064. io->sector);
  1065. r = crypt_convert(cc, &io->ctx);
  1066. if (r < 0)
  1067. io->error = -EIO;
  1068. if (atomic_dec_and_test(&io->ctx.cc_pending))
  1069. kcryptd_crypt_read_done(io);
  1070. crypt_dec_pending(io);
  1071. }
  1072. static void kcryptd_async_done(struct crypto_async_request *async_req,
  1073. int error)
  1074. {
  1075. struct dm_crypt_request *dmreq = async_req->data;
  1076. struct convert_context *ctx = dmreq->ctx;
  1077. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  1078. struct crypt_config *cc = io->cc;
  1079. if (error == -EINPROGRESS) {
  1080. complete(&ctx->restart);
  1081. return;
  1082. }
  1083. if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
  1084. error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
  1085. if (error < 0)
  1086. io->error = -EIO;
  1087. mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
  1088. if (!atomic_dec_and_test(&ctx->cc_pending))
  1089. return;
  1090. if (bio_data_dir(io->base_bio) == READ)
  1091. kcryptd_crypt_read_done(io);
  1092. else
  1093. kcryptd_crypt_write_io_submit(io, 1);
  1094. }
  1095. static void kcryptd_crypt(struct work_struct *work)
  1096. {
  1097. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1098. if (bio_data_dir(io->base_bio) == READ)
  1099. kcryptd_crypt_read_convert(io);
  1100. else
  1101. kcryptd_crypt_write_convert(io);
  1102. }
  1103. static void kcryptd_queue_crypt(struct dm_crypt_io *io)
  1104. {
  1105. struct crypt_config *cc = io->cc;
  1106. INIT_WORK(&io->work, kcryptd_crypt);
  1107. queue_work(cc->crypt_queue, &io->work);
  1108. }
  1109. /*
  1110. * Decode key from its hex representation
  1111. */
  1112. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  1113. {
  1114. char buffer[3];
  1115. unsigned int i;
  1116. buffer[2] = '\0';
  1117. for (i = 0; i < size; i++) {
  1118. buffer[0] = *hex++;
  1119. buffer[1] = *hex++;
  1120. if (kstrtou8(buffer, 16, &key[i]))
  1121. return -EINVAL;
  1122. }
  1123. if (*hex != '\0')
  1124. return -EINVAL;
  1125. return 0;
  1126. }
  1127. static void crypt_free_tfms(struct crypt_config *cc)
  1128. {
  1129. unsigned i;
  1130. if (!cc->tfms)
  1131. return;
  1132. for (i = 0; i < cc->tfms_count; i++)
  1133. if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
  1134. crypto_free_ablkcipher(cc->tfms[i]);
  1135. cc->tfms[i] = NULL;
  1136. }
  1137. kfree(cc->tfms);
  1138. cc->tfms = NULL;
  1139. }
  1140. static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
  1141. {
  1142. unsigned i;
  1143. int err;
  1144. cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
  1145. GFP_KERNEL);
  1146. if (!cc->tfms)
  1147. return -ENOMEM;
  1148. for (i = 0; i < cc->tfms_count; i++) {
  1149. cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
  1150. if (IS_ERR(cc->tfms[i])) {
  1151. err = PTR_ERR(cc->tfms[i]);
  1152. crypt_free_tfms(cc);
  1153. return err;
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. static int crypt_setkey_allcpus(struct crypt_config *cc)
  1159. {
  1160. unsigned subkey_size;
  1161. int err = 0, i, r;
  1162. /* Ignore extra keys (which are used for IV etc) */
  1163. subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
  1164. for (i = 0; i < cc->tfms_count; i++) {
  1165. r = crypto_ablkcipher_setkey(cc->tfms[i],
  1166. cc->key + (i * subkey_size),
  1167. subkey_size);
  1168. if (r)
  1169. err = r;
  1170. }
  1171. return err;
  1172. }
  1173. static int crypt_set_key(struct crypt_config *cc, char *key)
  1174. {
  1175. int r = -EINVAL;
  1176. int key_string_len = strlen(key);
  1177. /* The key size may not be changed. */
  1178. if (cc->key_size != (key_string_len >> 1))
  1179. goto out;
  1180. /* Hyphen (which gives a key_size of zero) means there is no key. */
  1181. if (!cc->key_size && strcmp(key, "-"))
  1182. goto out;
  1183. if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
  1184. goto out;
  1185. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1186. r = crypt_setkey_allcpus(cc);
  1187. out:
  1188. /* Hex key string not needed after here, so wipe it. */
  1189. memset(key, '0', key_string_len);
  1190. return r;
  1191. }
  1192. static int crypt_wipe_key(struct crypt_config *cc)
  1193. {
  1194. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1195. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  1196. return crypt_setkey_allcpus(cc);
  1197. }
  1198. static void crypt_dtr(struct dm_target *ti)
  1199. {
  1200. struct crypt_config *cc = ti->private;
  1201. ti->private = NULL;
  1202. if (!cc)
  1203. return;
  1204. if (cc->io_queue)
  1205. destroy_workqueue(cc->io_queue);
  1206. if (cc->crypt_queue)
  1207. destroy_workqueue(cc->crypt_queue);
  1208. crypt_free_tfms(cc);
  1209. if (cc->bs)
  1210. bioset_free(cc->bs);
  1211. if (cc->page_pool)
  1212. mempool_destroy(cc->page_pool);
  1213. if (cc->req_pool)
  1214. mempool_destroy(cc->req_pool);
  1215. if (cc->io_pool)
  1216. mempool_destroy(cc->io_pool);
  1217. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  1218. cc->iv_gen_ops->dtr(cc);
  1219. if (cc->dev)
  1220. dm_put_device(ti, cc->dev);
  1221. kzfree(cc->cipher);
  1222. kzfree(cc->cipher_string);
  1223. /* Must zero key material before freeing */
  1224. kzfree(cc);
  1225. }
  1226. static int crypt_ctr_cipher(struct dm_target *ti,
  1227. char *cipher_in, char *key)
  1228. {
  1229. struct crypt_config *cc = ti->private;
  1230. char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
  1231. char *cipher_api = NULL;
  1232. int ret = -EINVAL;
  1233. char dummy;
  1234. /* Convert to crypto api definition? */
  1235. if (strchr(cipher_in, '(')) {
  1236. ti->error = "Bad cipher specification";
  1237. return -EINVAL;
  1238. }
  1239. cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
  1240. if (!cc->cipher_string)
  1241. goto bad_mem;
  1242. /*
  1243. * Legacy dm-crypt cipher specification
  1244. * cipher[:keycount]-mode-iv:ivopts
  1245. */
  1246. tmp = cipher_in;
  1247. keycount = strsep(&tmp, "-");
  1248. cipher = strsep(&keycount, ":");
  1249. if (!keycount)
  1250. cc->tfms_count = 1;
  1251. else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
  1252. !is_power_of_2(cc->tfms_count)) {
  1253. ti->error = "Bad cipher key count specification";
  1254. return -EINVAL;
  1255. }
  1256. cc->key_parts = cc->tfms_count;
  1257. cc->key_extra_size = 0;
  1258. cc->cipher = kstrdup(cipher, GFP_KERNEL);
  1259. if (!cc->cipher)
  1260. goto bad_mem;
  1261. chainmode = strsep(&tmp, "-");
  1262. ivopts = strsep(&tmp, "-");
  1263. ivmode = strsep(&ivopts, ":");
  1264. if (tmp)
  1265. DMWARN("Ignoring unexpected additional cipher options");
  1266. /*
  1267. * For compatibility with the original dm-crypt mapping format, if
  1268. * only the cipher name is supplied, use cbc-plain.
  1269. */
  1270. if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
  1271. chainmode = "cbc";
  1272. ivmode = "plain";
  1273. }
  1274. if (strcmp(chainmode, "ecb") && !ivmode) {
  1275. ti->error = "IV mechanism required";
  1276. return -EINVAL;
  1277. }
  1278. cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
  1279. if (!cipher_api)
  1280. goto bad_mem;
  1281. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1282. "%s(%s)", chainmode, cipher);
  1283. if (ret < 0) {
  1284. kfree(cipher_api);
  1285. goto bad_mem;
  1286. }
  1287. /* Allocate cipher */
  1288. ret = crypt_alloc_tfms(cc, cipher_api);
  1289. if (ret < 0) {
  1290. ti->error = "Error allocating crypto tfm";
  1291. goto bad;
  1292. }
  1293. /* Initialize IV */
  1294. cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
  1295. if (cc->iv_size)
  1296. /* at least a 64 bit sector number should fit in our buffer */
  1297. cc->iv_size = max(cc->iv_size,
  1298. (unsigned int)(sizeof(u64) / sizeof(u8)));
  1299. else if (ivmode) {
  1300. DMWARN("Selected cipher does not support IVs");
  1301. ivmode = NULL;
  1302. }
  1303. /* Choose ivmode, see comments at iv code. */
  1304. if (ivmode == NULL)
  1305. cc->iv_gen_ops = NULL;
  1306. else if (strcmp(ivmode, "plain") == 0)
  1307. cc->iv_gen_ops = &crypt_iv_plain_ops;
  1308. else if (strcmp(ivmode, "plain64") == 0)
  1309. cc->iv_gen_ops = &crypt_iv_plain64_ops;
  1310. else if (strcmp(ivmode, "essiv") == 0)
  1311. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  1312. else if (strcmp(ivmode, "benbi") == 0)
  1313. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  1314. else if (strcmp(ivmode, "null") == 0)
  1315. cc->iv_gen_ops = &crypt_iv_null_ops;
  1316. else if (strcmp(ivmode, "lmk") == 0) {
  1317. cc->iv_gen_ops = &crypt_iv_lmk_ops;
  1318. /*
  1319. * Version 2 and 3 is recognised according
  1320. * to length of provided multi-key string.
  1321. * If present (version 3), last key is used as IV seed.
  1322. * All keys (including IV seed) are always the same size.
  1323. */
  1324. if (cc->key_size % cc->key_parts) {
  1325. cc->key_parts++;
  1326. cc->key_extra_size = cc->key_size / cc->key_parts;
  1327. }
  1328. } else if (strcmp(ivmode, "tcw") == 0) {
  1329. cc->iv_gen_ops = &crypt_iv_tcw_ops;
  1330. cc->key_parts += 2; /* IV + whitening */
  1331. cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
  1332. } else {
  1333. ret = -EINVAL;
  1334. ti->error = "Invalid IV mode";
  1335. goto bad;
  1336. }
  1337. /* Initialize and set key */
  1338. ret = crypt_set_key(cc, key);
  1339. if (ret < 0) {
  1340. ti->error = "Error decoding and setting key";
  1341. goto bad;
  1342. }
  1343. /* Allocate IV */
  1344. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
  1345. ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
  1346. if (ret < 0) {
  1347. ti->error = "Error creating IV";
  1348. goto bad;
  1349. }
  1350. }
  1351. /* Initialize IV (set keys for ESSIV etc) */
  1352. if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
  1353. ret = cc->iv_gen_ops->init(cc);
  1354. if (ret < 0) {
  1355. ti->error = "Error initialising IV";
  1356. goto bad;
  1357. }
  1358. }
  1359. ret = 0;
  1360. bad:
  1361. kfree(cipher_api);
  1362. return ret;
  1363. bad_mem:
  1364. ti->error = "Cannot allocate cipher strings";
  1365. return -ENOMEM;
  1366. }
  1367. /*
  1368. * Construct an encryption mapping:
  1369. * <cipher> <key> <iv_offset> <dev_path> <start>
  1370. */
  1371. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1372. {
  1373. struct crypt_config *cc;
  1374. unsigned int key_size, opt_params;
  1375. unsigned long long tmpll;
  1376. int ret;
  1377. struct dm_arg_set as;
  1378. const char *opt_string;
  1379. char dummy;
  1380. static struct dm_arg _args[] = {
  1381. {0, 1, "Invalid number of feature args"},
  1382. };
  1383. if (argc < 5) {
  1384. ti->error = "Not enough arguments";
  1385. return -EINVAL;
  1386. }
  1387. key_size = strlen(argv[1]) >> 1;
  1388. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  1389. if (!cc) {
  1390. ti->error = "Cannot allocate encryption context";
  1391. return -ENOMEM;
  1392. }
  1393. cc->key_size = key_size;
  1394. ti->private = cc;
  1395. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  1396. if (ret < 0)
  1397. goto bad;
  1398. ret = -ENOMEM;
  1399. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  1400. if (!cc->io_pool) {
  1401. ti->error = "Cannot allocate crypt io mempool";
  1402. goto bad;
  1403. }
  1404. cc->dmreq_start = sizeof(struct ablkcipher_request);
  1405. cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
  1406. cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
  1407. cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
  1408. ~(crypto_tfm_ctx_alignment() - 1);
  1409. cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
  1410. sizeof(struct dm_crypt_request) + cc->iv_size);
  1411. if (!cc->req_pool) {
  1412. ti->error = "Cannot allocate crypt request mempool";
  1413. goto bad;
  1414. }
  1415. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  1416. if (!cc->page_pool) {
  1417. ti->error = "Cannot allocate page mempool";
  1418. goto bad;
  1419. }
  1420. cc->bs = bioset_create(MIN_IOS, 0);
  1421. if (!cc->bs) {
  1422. ti->error = "Cannot allocate crypt bioset";
  1423. goto bad;
  1424. }
  1425. ret = -EINVAL;
  1426. if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
  1427. ti->error = "Invalid iv_offset sector";
  1428. goto bad;
  1429. }
  1430. cc->iv_offset = tmpll;
  1431. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
  1432. ti->error = "Device lookup failed";
  1433. goto bad;
  1434. }
  1435. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  1436. ti->error = "Invalid device sector";
  1437. goto bad;
  1438. }
  1439. cc->start = tmpll;
  1440. argv += 5;
  1441. argc -= 5;
  1442. /* Optional parameters */
  1443. if (argc) {
  1444. as.argc = argc;
  1445. as.argv = argv;
  1446. ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
  1447. if (ret)
  1448. goto bad;
  1449. opt_string = dm_shift_arg(&as);
  1450. if (opt_params == 1 && opt_string &&
  1451. !strcasecmp(opt_string, "allow_discards"))
  1452. ti->num_discard_bios = 1;
  1453. else if (opt_params) {
  1454. ret = -EINVAL;
  1455. ti->error = "Invalid feature arguments";
  1456. goto bad;
  1457. }
  1458. }
  1459. ret = -ENOMEM;
  1460. cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
  1461. if (!cc->io_queue) {
  1462. ti->error = "Couldn't create kcryptd io queue";
  1463. goto bad;
  1464. }
  1465. cc->crypt_queue = alloc_workqueue("kcryptd",
  1466. WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
  1467. if (!cc->crypt_queue) {
  1468. ti->error = "Couldn't create kcryptd queue";
  1469. goto bad;
  1470. }
  1471. ti->num_flush_bios = 1;
  1472. ti->discard_zeroes_data_unsupported = true;
  1473. return 0;
  1474. bad:
  1475. crypt_dtr(ti);
  1476. return ret;
  1477. }
  1478. static int crypt_map(struct dm_target *ti, struct bio *bio)
  1479. {
  1480. struct dm_crypt_io *io;
  1481. struct crypt_config *cc = ti->private;
  1482. /*
  1483. * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
  1484. * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
  1485. * - for REQ_DISCARD caller must use flush if IO ordering matters
  1486. */
  1487. if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
  1488. bio->bi_bdev = cc->dev->bdev;
  1489. if (bio_sectors(bio))
  1490. bio->bi_iter.bi_sector = cc->start +
  1491. dm_target_offset(ti, bio->bi_iter.bi_sector);
  1492. return DM_MAPIO_REMAPPED;
  1493. }
  1494. io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
  1495. if (bio_data_dir(io->base_bio) == READ) {
  1496. if (kcryptd_io_read(io, GFP_NOWAIT))
  1497. kcryptd_queue_io(io);
  1498. } else
  1499. kcryptd_queue_crypt(io);
  1500. return DM_MAPIO_SUBMITTED;
  1501. }
  1502. static void crypt_status(struct dm_target *ti, status_type_t type,
  1503. unsigned status_flags, char *result, unsigned maxlen)
  1504. {
  1505. struct crypt_config *cc = ti->private;
  1506. unsigned i, sz = 0;
  1507. switch (type) {
  1508. case STATUSTYPE_INFO:
  1509. result[0] = '\0';
  1510. break;
  1511. case STATUSTYPE_TABLE:
  1512. DMEMIT("%s ", cc->cipher_string);
  1513. if (cc->key_size > 0)
  1514. for (i = 0; i < cc->key_size; i++)
  1515. DMEMIT("%02x", cc->key[i]);
  1516. else
  1517. DMEMIT("-");
  1518. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  1519. cc->dev->name, (unsigned long long)cc->start);
  1520. if (ti->num_discard_bios)
  1521. DMEMIT(" 1 allow_discards");
  1522. break;
  1523. }
  1524. }
  1525. static void crypt_postsuspend(struct dm_target *ti)
  1526. {
  1527. struct crypt_config *cc = ti->private;
  1528. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1529. }
  1530. static int crypt_preresume(struct dm_target *ti)
  1531. {
  1532. struct crypt_config *cc = ti->private;
  1533. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  1534. DMERR("aborting resume - crypt key is not set.");
  1535. return -EAGAIN;
  1536. }
  1537. return 0;
  1538. }
  1539. static void crypt_resume(struct dm_target *ti)
  1540. {
  1541. struct crypt_config *cc = ti->private;
  1542. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1543. }
  1544. /* Message interface
  1545. * key set <key>
  1546. * key wipe
  1547. */
  1548. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  1549. {
  1550. struct crypt_config *cc = ti->private;
  1551. int ret = -EINVAL;
  1552. if (argc < 2)
  1553. goto error;
  1554. if (!strcasecmp(argv[0], "key")) {
  1555. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  1556. DMWARN("not suspended during key manipulation.");
  1557. return -EINVAL;
  1558. }
  1559. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  1560. ret = crypt_set_key(cc, argv[2]);
  1561. if (ret)
  1562. return ret;
  1563. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  1564. ret = cc->iv_gen_ops->init(cc);
  1565. return ret;
  1566. }
  1567. if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
  1568. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  1569. ret = cc->iv_gen_ops->wipe(cc);
  1570. if (ret)
  1571. return ret;
  1572. }
  1573. return crypt_wipe_key(cc);
  1574. }
  1575. }
  1576. error:
  1577. DMWARN("unrecognised message received.");
  1578. return -EINVAL;
  1579. }
  1580. static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1581. struct bio_vec *biovec, int max_size)
  1582. {
  1583. struct crypt_config *cc = ti->private;
  1584. struct request_queue *q = bdev_get_queue(cc->dev->bdev);
  1585. if (!q->merge_bvec_fn)
  1586. return max_size;
  1587. bvm->bi_bdev = cc->dev->bdev;
  1588. bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
  1589. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1590. }
  1591. static int crypt_iterate_devices(struct dm_target *ti,
  1592. iterate_devices_callout_fn fn, void *data)
  1593. {
  1594. struct crypt_config *cc = ti->private;
  1595. return fn(ti, cc->dev, cc->start, ti->len, data);
  1596. }
  1597. static struct target_type crypt_target = {
  1598. .name = "crypt",
  1599. .version = {1, 13, 0},
  1600. .module = THIS_MODULE,
  1601. .ctr = crypt_ctr,
  1602. .dtr = crypt_dtr,
  1603. .map = crypt_map,
  1604. .status = crypt_status,
  1605. .postsuspend = crypt_postsuspend,
  1606. .preresume = crypt_preresume,
  1607. .resume = crypt_resume,
  1608. .message = crypt_message,
  1609. .merge = crypt_merge,
  1610. .iterate_devices = crypt_iterate_devices,
  1611. };
  1612. static int __init dm_crypt_init(void)
  1613. {
  1614. int r;
  1615. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  1616. if (!_crypt_io_pool)
  1617. return -ENOMEM;
  1618. r = dm_register_target(&crypt_target);
  1619. if (r < 0) {
  1620. DMERR("register failed %d", r);
  1621. kmem_cache_destroy(_crypt_io_pool);
  1622. }
  1623. return r;
  1624. }
  1625. static void __exit dm_crypt_exit(void)
  1626. {
  1627. dm_unregister_target(&crypt_target);
  1628. kmem_cache_destroy(_crypt_io_pool);
  1629. }
  1630. module_init(dm_crypt_init);
  1631. module_exit(dm_crypt_exit);
  1632. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  1633. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  1634. MODULE_LICENSE("GPL");