dm-crypt.c 47 KB

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