dm-crypt.c 43 KB

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