dm-crypt.c 44 KB

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