dm-crypt.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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. #define MIN_BIO_PAGES 8
  156. static struct kmem_cache *_crypt_io_pool;
  157. static void clone_init(struct dm_crypt_io *, struct bio *);
  158. static void kcryptd_queue_crypt(struct dm_crypt_io *io);
  159. static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
  160. static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
  161. {
  162. return this_cpu_ptr(cc->cpu);
  163. }
  164. /*
  165. * Use this to access cipher attributes that are the same for each CPU.
  166. */
  167. static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
  168. {
  169. return __this_cpu_ptr(cc->cpu)->tfms[0];
  170. }
  171. /*
  172. * Different IV generation algorithms:
  173. *
  174. * plain: the initial vector is the 32-bit little-endian version of the sector
  175. * number, padded with zeros if necessary.
  176. *
  177. * plain64: the initial vector is the 64-bit little-endian version of the sector
  178. * number, padded with zeros if necessary.
  179. *
  180. * essiv: "encrypted sector|salt initial vector", the sector number is
  181. * encrypted with the bulk cipher using a salt as key. The salt
  182. * should be derived from the bulk cipher's key via hashing.
  183. *
  184. * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
  185. * (needed for LRW-32-AES and possible other narrow block modes)
  186. *
  187. * null: the initial vector is always zero. Provides compatibility with
  188. * obsolete loop_fish2 devices. Do not use for new devices.
  189. *
  190. * lmk: Compatible implementation of the block chaining mode used
  191. * by the Loop-AES block device encryption system
  192. * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
  193. * It operates on full 512 byte sectors and uses CBC
  194. * with an IV derived from the sector number, the data and
  195. * optionally extra IV seed.
  196. * This means that after decryption the first block
  197. * of sector must be tweaked according to decrypted data.
  198. * Loop-AES can use three encryption schemes:
  199. * version 1: is plain aes-cbc mode
  200. * version 2: uses 64 multikey scheme with lmk IV generator
  201. * version 3: the same as version 2 with additional IV seed
  202. * (it uses 65 keys, last key is used as IV seed)
  203. *
  204. * plumb: unimplemented, see:
  205. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  206. */
  207. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
  208. struct dm_crypt_request *dmreq)
  209. {
  210. memset(iv, 0, cc->iv_size);
  211. *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
  212. return 0;
  213. }
  214. static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
  215. struct dm_crypt_request *dmreq)
  216. {
  217. memset(iv, 0, cc->iv_size);
  218. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  219. return 0;
  220. }
  221. /* Initialise ESSIV - compute salt but no local memory allocations */
  222. static int crypt_iv_essiv_init(struct crypt_config *cc)
  223. {
  224. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  225. struct hash_desc desc;
  226. struct scatterlist sg;
  227. struct crypto_cipher *essiv_tfm;
  228. int err, cpu;
  229. sg_init_one(&sg, cc->key, cc->key_size);
  230. desc.tfm = essiv->hash_tfm;
  231. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  232. err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
  233. if (err)
  234. return err;
  235. for_each_possible_cpu(cpu) {
  236. essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private,
  237. err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
  238. crypto_hash_digestsize(essiv->hash_tfm));
  239. if (err)
  240. return err;
  241. }
  242. return 0;
  243. }
  244. /* Wipe salt and reset key derived from volume key */
  245. static int crypt_iv_essiv_wipe(struct crypt_config *cc)
  246. {
  247. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  248. unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
  249. struct crypto_cipher *essiv_tfm;
  250. int cpu, r, err = 0;
  251. memset(essiv->salt, 0, salt_size);
  252. for_each_possible_cpu(cpu) {
  253. essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private;
  254. r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
  255. if (r)
  256. err = r;
  257. }
  258. return err;
  259. }
  260. /* Set up per cpu cipher state */
  261. static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
  262. struct dm_target *ti,
  263. u8 *salt, unsigned saltsize)
  264. {
  265. struct crypto_cipher *essiv_tfm;
  266. int err;
  267. /* Setup the essiv_tfm with the given salt */
  268. essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  269. if (IS_ERR(essiv_tfm)) {
  270. ti->error = "Error allocating crypto tfm for ESSIV";
  271. return essiv_tfm;
  272. }
  273. if (crypto_cipher_blocksize(essiv_tfm) !=
  274. crypto_ablkcipher_ivsize(any_tfm(cc))) {
  275. ti->error = "Block size of ESSIV cipher does "
  276. "not match IV size of block cipher";
  277. crypto_free_cipher(essiv_tfm);
  278. return ERR_PTR(-EINVAL);
  279. }
  280. err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
  281. if (err) {
  282. ti->error = "Failed to set key for ESSIV cipher";
  283. crypto_free_cipher(essiv_tfm);
  284. return ERR_PTR(err);
  285. }
  286. return essiv_tfm;
  287. }
  288. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  289. {
  290. int cpu;
  291. struct crypt_cpu *cpu_cc;
  292. struct crypto_cipher *essiv_tfm;
  293. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  294. crypto_free_hash(essiv->hash_tfm);
  295. essiv->hash_tfm = NULL;
  296. kzfree(essiv->salt);
  297. essiv->salt = NULL;
  298. for_each_possible_cpu(cpu) {
  299. cpu_cc = per_cpu_ptr(cc->cpu, cpu);
  300. essiv_tfm = cpu_cc->iv_private;
  301. if (essiv_tfm)
  302. crypto_free_cipher(essiv_tfm);
  303. cpu_cc->iv_private = NULL;
  304. }
  305. }
  306. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  307. const char *opts)
  308. {
  309. struct crypto_cipher *essiv_tfm = NULL;
  310. struct crypto_hash *hash_tfm = NULL;
  311. u8 *salt = NULL;
  312. int err, cpu;
  313. if (!opts) {
  314. ti->error = "Digest algorithm missing for ESSIV mode";
  315. return -EINVAL;
  316. }
  317. /* Allocate hash algorithm */
  318. hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
  319. if (IS_ERR(hash_tfm)) {
  320. ti->error = "Error initializing ESSIV hash";
  321. err = PTR_ERR(hash_tfm);
  322. goto bad;
  323. }
  324. salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
  325. if (!salt) {
  326. ti->error = "Error kmallocing salt storage in ESSIV";
  327. err = -ENOMEM;
  328. goto bad;
  329. }
  330. cc->iv_gen_private.essiv.salt = salt;
  331. cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
  332. for_each_possible_cpu(cpu) {
  333. essiv_tfm = setup_essiv_cpu(cc, ti, salt,
  334. crypto_hash_digestsize(hash_tfm));
  335. if (IS_ERR(essiv_tfm)) {
  336. crypt_iv_essiv_dtr(cc);
  337. return PTR_ERR(essiv_tfm);
  338. }
  339. per_cpu_ptr(cc->cpu, cpu)->iv_private = essiv_tfm;
  340. }
  341. return 0;
  342. bad:
  343. if (hash_tfm && !IS_ERR(hash_tfm))
  344. crypto_free_hash(hash_tfm);
  345. kfree(salt);
  346. return err;
  347. }
  348. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
  349. struct dm_crypt_request *dmreq)
  350. {
  351. struct crypto_cipher *essiv_tfm = this_crypt_config(cc)->iv_private;
  352. memset(iv, 0, cc->iv_size);
  353. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  354. crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
  355. return 0;
  356. }
  357. static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
  358. const char *opts)
  359. {
  360. unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
  361. int log = ilog2(bs);
  362. /* we need to calculate how far we must shift the sector count
  363. * to get the cipher block count, we use this shift in _gen */
  364. if (1 << log != bs) {
  365. ti->error = "cypher blocksize is not a power of 2";
  366. return -EINVAL;
  367. }
  368. if (log > 9) {
  369. ti->error = "cypher blocksize is > 512";
  370. return -EINVAL;
  371. }
  372. cc->iv_gen_private.benbi.shift = 9 - log;
  373. return 0;
  374. }
  375. static void crypt_iv_benbi_dtr(struct crypt_config *cc)
  376. {
  377. }
  378. static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
  379. struct dm_crypt_request *dmreq)
  380. {
  381. __be64 val;
  382. memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
  383. val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
  384. put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
  385. return 0;
  386. }
  387. static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
  388. struct dm_crypt_request *dmreq)
  389. {
  390. memset(iv, 0, cc->iv_size);
  391. return 0;
  392. }
  393. static void crypt_iv_lmk_dtr(struct crypt_config *cc)
  394. {
  395. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  396. if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
  397. crypto_free_shash(lmk->hash_tfm);
  398. lmk->hash_tfm = NULL;
  399. kzfree(lmk->seed);
  400. lmk->seed = NULL;
  401. }
  402. static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
  403. const char *opts)
  404. {
  405. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  406. lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
  407. if (IS_ERR(lmk->hash_tfm)) {
  408. ti->error = "Error initializing LMK hash";
  409. return PTR_ERR(lmk->hash_tfm);
  410. }
  411. /* No seed in LMK version 2 */
  412. if (cc->key_parts == cc->tfms_count) {
  413. lmk->seed = NULL;
  414. return 0;
  415. }
  416. lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
  417. if (!lmk->seed) {
  418. crypt_iv_lmk_dtr(cc);
  419. ti->error = "Error kmallocing seed storage in LMK";
  420. return -ENOMEM;
  421. }
  422. return 0;
  423. }
  424. static int crypt_iv_lmk_init(struct crypt_config *cc)
  425. {
  426. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  427. int subkey_size = cc->key_size / cc->key_parts;
  428. /* LMK seed is on the position of LMK_KEYS + 1 key */
  429. if (lmk->seed)
  430. memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
  431. crypto_shash_digestsize(lmk->hash_tfm));
  432. return 0;
  433. }
  434. static int crypt_iv_lmk_wipe(struct crypt_config *cc)
  435. {
  436. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  437. if (lmk->seed)
  438. memset(lmk->seed, 0, LMK_SEED_SIZE);
  439. return 0;
  440. }
  441. static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
  442. struct dm_crypt_request *dmreq,
  443. u8 *data)
  444. {
  445. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  446. struct {
  447. struct shash_desc desc;
  448. char ctx[crypto_shash_descsize(lmk->hash_tfm)];
  449. } sdesc;
  450. struct md5_state md5state;
  451. u32 buf[4];
  452. int i, r;
  453. sdesc.desc.tfm = lmk->hash_tfm;
  454. sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  455. r = crypto_shash_init(&sdesc.desc);
  456. if (r)
  457. return r;
  458. if (lmk->seed) {
  459. r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
  460. if (r)
  461. return r;
  462. }
  463. /* Sector is always 512B, block size 16, add data of blocks 1-31 */
  464. r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
  465. if (r)
  466. return r;
  467. /* Sector is cropped to 56 bits here */
  468. buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
  469. buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
  470. buf[2] = cpu_to_le32(4024);
  471. buf[3] = 0;
  472. r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
  473. if (r)
  474. return r;
  475. /* No MD5 padding here */
  476. r = crypto_shash_export(&sdesc.desc, &md5state);
  477. if (r)
  478. return r;
  479. for (i = 0; i < MD5_HASH_WORDS; i++)
  480. __cpu_to_le32s(&md5state.hash[i]);
  481. memcpy(iv, &md5state.hash, cc->iv_size);
  482. return 0;
  483. }
  484. static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
  485. struct dm_crypt_request *dmreq)
  486. {
  487. u8 *src;
  488. int r = 0;
  489. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
  490. src = kmap_atomic(sg_page(&dmreq->sg_in), KM_USER0);
  491. r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
  492. kunmap_atomic(src, KM_USER0);
  493. } else
  494. memset(iv, 0, cc->iv_size);
  495. return r;
  496. }
  497. static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
  498. struct dm_crypt_request *dmreq)
  499. {
  500. u8 *dst;
  501. int r;
  502. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
  503. return 0;
  504. dst = kmap_atomic(sg_page(&dmreq->sg_out), KM_USER0);
  505. r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
  506. /* Tweak the first block of plaintext sector */
  507. if (!r)
  508. crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
  509. kunmap_atomic(dst, KM_USER0);
  510. return r;
  511. }
  512. static struct crypt_iv_operations crypt_iv_plain_ops = {
  513. .generator = crypt_iv_plain_gen
  514. };
  515. static struct crypt_iv_operations crypt_iv_plain64_ops = {
  516. .generator = crypt_iv_plain64_gen
  517. };
  518. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  519. .ctr = crypt_iv_essiv_ctr,
  520. .dtr = crypt_iv_essiv_dtr,
  521. .init = crypt_iv_essiv_init,
  522. .wipe = crypt_iv_essiv_wipe,
  523. .generator = crypt_iv_essiv_gen
  524. };
  525. static struct crypt_iv_operations crypt_iv_benbi_ops = {
  526. .ctr = crypt_iv_benbi_ctr,
  527. .dtr = crypt_iv_benbi_dtr,
  528. .generator = crypt_iv_benbi_gen
  529. };
  530. static struct crypt_iv_operations crypt_iv_null_ops = {
  531. .generator = crypt_iv_null_gen
  532. };
  533. static struct crypt_iv_operations crypt_iv_lmk_ops = {
  534. .ctr = crypt_iv_lmk_ctr,
  535. .dtr = crypt_iv_lmk_dtr,
  536. .init = crypt_iv_lmk_init,
  537. .wipe = crypt_iv_lmk_wipe,
  538. .generator = crypt_iv_lmk_gen,
  539. .post = crypt_iv_lmk_post
  540. };
  541. static void crypt_convert_init(struct crypt_config *cc,
  542. struct convert_context *ctx,
  543. struct bio *bio_out, struct bio *bio_in,
  544. sector_t sector)
  545. {
  546. ctx->bio_in = bio_in;
  547. ctx->bio_out = bio_out;
  548. ctx->offset_in = 0;
  549. ctx->offset_out = 0;
  550. ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
  551. ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
  552. ctx->sector = sector + cc->iv_offset;
  553. init_completion(&ctx->restart);
  554. }
  555. static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
  556. struct ablkcipher_request *req)
  557. {
  558. return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
  559. }
  560. static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
  561. struct dm_crypt_request *dmreq)
  562. {
  563. return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
  564. }
  565. static u8 *iv_of_dmreq(struct crypt_config *cc,
  566. struct dm_crypt_request *dmreq)
  567. {
  568. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  569. crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
  570. }
  571. static int crypt_convert_block(struct crypt_config *cc,
  572. struct convert_context *ctx,
  573. struct ablkcipher_request *req)
  574. {
  575. struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
  576. struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
  577. struct dm_crypt_request *dmreq;
  578. u8 *iv;
  579. int r = 0;
  580. dmreq = dmreq_of_req(cc, req);
  581. iv = iv_of_dmreq(cc, dmreq);
  582. dmreq->iv_sector = ctx->sector;
  583. dmreq->ctx = ctx;
  584. sg_init_table(&dmreq->sg_in, 1);
  585. sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
  586. bv_in->bv_offset + ctx->offset_in);
  587. sg_init_table(&dmreq->sg_out, 1);
  588. sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
  589. bv_out->bv_offset + ctx->offset_out);
  590. ctx->offset_in += 1 << SECTOR_SHIFT;
  591. if (ctx->offset_in >= bv_in->bv_len) {
  592. ctx->offset_in = 0;
  593. ctx->idx_in++;
  594. }
  595. ctx->offset_out += 1 << SECTOR_SHIFT;
  596. if (ctx->offset_out >= bv_out->bv_len) {
  597. ctx->offset_out = 0;
  598. ctx->idx_out++;
  599. }
  600. if (cc->iv_gen_ops) {
  601. r = cc->iv_gen_ops->generator(cc, iv, dmreq);
  602. if (r < 0)
  603. return r;
  604. }
  605. ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
  606. 1 << SECTOR_SHIFT, iv);
  607. if (bio_data_dir(ctx->bio_in) == WRITE)
  608. r = crypto_ablkcipher_encrypt(req);
  609. else
  610. r = crypto_ablkcipher_decrypt(req);
  611. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  612. r = cc->iv_gen_ops->post(cc, iv, dmreq);
  613. return r;
  614. }
  615. static void kcryptd_async_done(struct crypto_async_request *async_req,
  616. int error);
  617. static void crypt_alloc_req(struct crypt_config *cc,
  618. struct convert_context *ctx)
  619. {
  620. struct crypt_cpu *this_cc = this_crypt_config(cc);
  621. unsigned key_index = ctx->sector & (cc->tfms_count - 1);
  622. if (!this_cc->req)
  623. this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
  624. ablkcipher_request_set_tfm(this_cc->req, this_cc->tfms[key_index]);
  625. ablkcipher_request_set_callback(this_cc->req,
  626. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  627. kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
  628. }
  629. /*
  630. * Encrypt / decrypt data from one bio to another one (can be the same one)
  631. */
  632. static int crypt_convert(struct crypt_config *cc,
  633. struct convert_context *ctx)
  634. {
  635. struct crypt_cpu *this_cc = this_crypt_config(cc);
  636. int r;
  637. atomic_set(&ctx->pending, 1);
  638. while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
  639. ctx->idx_out < ctx->bio_out->bi_vcnt) {
  640. crypt_alloc_req(cc, ctx);
  641. atomic_inc(&ctx->pending);
  642. r = crypt_convert_block(cc, ctx, this_cc->req);
  643. switch (r) {
  644. /* async */
  645. case -EBUSY:
  646. wait_for_completion(&ctx->restart);
  647. INIT_COMPLETION(ctx->restart);
  648. /* fall through*/
  649. case -EINPROGRESS:
  650. this_cc->req = NULL;
  651. ctx->sector++;
  652. continue;
  653. /* sync */
  654. case 0:
  655. atomic_dec(&ctx->pending);
  656. ctx->sector++;
  657. cond_resched();
  658. continue;
  659. /* error */
  660. default:
  661. atomic_dec(&ctx->pending);
  662. return r;
  663. }
  664. }
  665. return 0;
  666. }
  667. static void dm_crypt_bio_destructor(struct bio *bio)
  668. {
  669. struct dm_crypt_io *io = bio->bi_private;
  670. struct crypt_config *cc = io->target->private;
  671. bio_free(bio, cc->bs);
  672. }
  673. /*
  674. * Generate a new unfragmented bio with the given size
  675. * This should never violate the device limitations
  676. * May return a smaller bio when running out of pages, indicated by
  677. * *out_of_pages set to 1.
  678. */
  679. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
  680. unsigned *out_of_pages)
  681. {
  682. struct crypt_config *cc = io->target->private;
  683. struct bio *clone;
  684. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  685. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  686. unsigned i, len;
  687. struct page *page;
  688. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  689. if (!clone)
  690. return NULL;
  691. clone_init(io, clone);
  692. *out_of_pages = 0;
  693. for (i = 0; i < nr_iovecs; i++) {
  694. page = mempool_alloc(cc->page_pool, gfp_mask);
  695. if (!page) {
  696. *out_of_pages = 1;
  697. break;
  698. }
  699. /*
  700. * if additional pages cannot be allocated without waiting,
  701. * return a partially allocated bio, the caller will then try
  702. * to allocate additional bios while submitting this partial bio
  703. */
  704. if (i == (MIN_BIO_PAGES - 1))
  705. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  706. len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  707. if (!bio_add_page(clone, page, len, 0)) {
  708. mempool_free(page, cc->page_pool);
  709. break;
  710. }
  711. size -= len;
  712. }
  713. if (!clone->bi_size) {
  714. bio_put(clone);
  715. return NULL;
  716. }
  717. return clone;
  718. }
  719. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  720. {
  721. unsigned int i;
  722. struct bio_vec *bv;
  723. for (i = 0; i < clone->bi_vcnt; i++) {
  724. bv = bio_iovec_idx(clone, i);
  725. BUG_ON(!bv->bv_page);
  726. mempool_free(bv->bv_page, cc->page_pool);
  727. bv->bv_page = NULL;
  728. }
  729. }
  730. static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
  731. struct bio *bio, sector_t sector)
  732. {
  733. struct crypt_config *cc = ti->private;
  734. struct dm_crypt_io *io;
  735. io = mempool_alloc(cc->io_pool, GFP_NOIO);
  736. io->target = ti;
  737. io->base_bio = bio;
  738. io->sector = sector;
  739. io->error = 0;
  740. io->base_io = NULL;
  741. atomic_set(&io->pending, 0);
  742. return io;
  743. }
  744. static void crypt_inc_pending(struct dm_crypt_io *io)
  745. {
  746. atomic_inc(&io->pending);
  747. }
  748. /*
  749. * One of the bios was finished. Check for completion of
  750. * the whole request and correctly clean up the buffer.
  751. * If base_io is set, wait for the last fragment to complete.
  752. */
  753. static void crypt_dec_pending(struct dm_crypt_io *io)
  754. {
  755. struct crypt_config *cc = io->target->private;
  756. struct bio *base_bio = io->base_bio;
  757. struct dm_crypt_io *base_io = io->base_io;
  758. int error = io->error;
  759. if (!atomic_dec_and_test(&io->pending))
  760. return;
  761. mempool_free(io, cc->io_pool);
  762. if (likely(!base_io))
  763. bio_endio(base_bio, error);
  764. else {
  765. if (error && !base_io->error)
  766. base_io->error = error;
  767. crypt_dec_pending(base_io);
  768. }
  769. }
  770. /*
  771. * kcryptd/kcryptd_io:
  772. *
  773. * Needed because it would be very unwise to do decryption in an
  774. * interrupt context.
  775. *
  776. * kcryptd performs the actual encryption or decryption.
  777. *
  778. * kcryptd_io performs the IO submission.
  779. *
  780. * They must be separated as otherwise the final stages could be
  781. * starved by new requests which can block in the first stages due
  782. * to memory allocation.
  783. *
  784. * The work is done per CPU global for all dm-crypt instances.
  785. * They should not depend on each other and do not block.
  786. */
  787. static void crypt_endio(struct bio *clone, int error)
  788. {
  789. struct dm_crypt_io *io = clone->bi_private;
  790. struct crypt_config *cc = io->target->private;
  791. unsigned rw = bio_data_dir(clone);
  792. if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
  793. error = -EIO;
  794. /*
  795. * free the processed pages
  796. */
  797. if (rw == WRITE)
  798. crypt_free_buffer_pages(cc, clone);
  799. bio_put(clone);
  800. if (rw == READ && !error) {
  801. kcryptd_queue_crypt(io);
  802. return;
  803. }
  804. if (unlikely(error))
  805. io->error = error;
  806. crypt_dec_pending(io);
  807. }
  808. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  809. {
  810. struct crypt_config *cc = io->target->private;
  811. clone->bi_private = io;
  812. clone->bi_end_io = crypt_endio;
  813. clone->bi_bdev = cc->dev->bdev;
  814. clone->bi_rw = io->base_bio->bi_rw;
  815. clone->bi_destructor = dm_crypt_bio_destructor;
  816. }
  817. static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
  818. {
  819. struct crypt_config *cc = io->target->private;
  820. struct bio *base_bio = io->base_bio;
  821. struct bio *clone;
  822. /*
  823. * The block layer might modify the bvec array, so always
  824. * copy the required bvecs because we need the original
  825. * one in order to decrypt the whole bio data *afterwards*.
  826. */
  827. clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs);
  828. if (!clone)
  829. return 1;
  830. crypt_inc_pending(io);
  831. clone_init(io, clone);
  832. clone->bi_idx = 0;
  833. clone->bi_vcnt = bio_segments(base_bio);
  834. clone->bi_size = base_bio->bi_size;
  835. clone->bi_sector = cc->start + io->sector;
  836. memcpy(clone->bi_io_vec, bio_iovec(base_bio),
  837. sizeof(struct bio_vec) * clone->bi_vcnt);
  838. generic_make_request(clone);
  839. return 0;
  840. }
  841. static void kcryptd_io_write(struct dm_crypt_io *io)
  842. {
  843. struct bio *clone = io->ctx.bio_out;
  844. generic_make_request(clone);
  845. }
  846. static void kcryptd_io(struct work_struct *work)
  847. {
  848. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  849. if (bio_data_dir(io->base_bio) == READ) {
  850. crypt_inc_pending(io);
  851. if (kcryptd_io_read(io, GFP_NOIO))
  852. io->error = -ENOMEM;
  853. crypt_dec_pending(io);
  854. } else
  855. kcryptd_io_write(io);
  856. }
  857. static void kcryptd_queue_io(struct dm_crypt_io *io)
  858. {
  859. struct crypt_config *cc = io->target->private;
  860. INIT_WORK(&io->work, kcryptd_io);
  861. queue_work(cc->io_queue, &io->work);
  862. }
  863. static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io,
  864. int error, int async)
  865. {
  866. struct bio *clone = io->ctx.bio_out;
  867. struct crypt_config *cc = io->target->private;
  868. if (unlikely(error < 0)) {
  869. crypt_free_buffer_pages(cc, clone);
  870. bio_put(clone);
  871. io->error = -EIO;
  872. crypt_dec_pending(io);
  873. return;
  874. }
  875. /* crypt_convert should have filled the clone bio */
  876. BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
  877. clone->bi_sector = cc->start + io->sector;
  878. if (async)
  879. kcryptd_queue_io(io);
  880. else
  881. generic_make_request(clone);
  882. }
  883. static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
  884. {
  885. struct crypt_config *cc = io->target->private;
  886. struct bio *clone;
  887. struct dm_crypt_io *new_io;
  888. int crypt_finished;
  889. unsigned out_of_pages = 0;
  890. unsigned remaining = io->base_bio->bi_size;
  891. sector_t sector = io->sector;
  892. int r;
  893. /*
  894. * Prevent io from disappearing until this function completes.
  895. */
  896. crypt_inc_pending(io);
  897. crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
  898. /*
  899. * The allocated buffers can be smaller than the whole bio,
  900. * so repeat the whole process until all the data can be handled.
  901. */
  902. while (remaining) {
  903. clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
  904. if (unlikely(!clone)) {
  905. io->error = -ENOMEM;
  906. break;
  907. }
  908. io->ctx.bio_out = clone;
  909. io->ctx.idx_out = 0;
  910. remaining -= clone->bi_size;
  911. sector += bio_sectors(clone);
  912. crypt_inc_pending(io);
  913. r = crypt_convert(cc, &io->ctx);
  914. crypt_finished = atomic_dec_and_test(&io->ctx.pending);
  915. /* Encryption was already finished, submit io now */
  916. if (crypt_finished) {
  917. kcryptd_crypt_write_io_submit(io, r, 0);
  918. /*
  919. * If there was an error, do not try next fragments.
  920. * For async, error is processed in async handler.
  921. */
  922. if (unlikely(r < 0))
  923. break;
  924. io->sector = sector;
  925. }
  926. /*
  927. * Out of memory -> run queues
  928. * But don't wait if split was due to the io size restriction
  929. */
  930. if (unlikely(out_of_pages))
  931. congestion_wait(BLK_RW_ASYNC, HZ/100);
  932. /*
  933. * With async crypto it is unsafe to share the crypto context
  934. * between fragments, so switch to a new dm_crypt_io structure.
  935. */
  936. if (unlikely(!crypt_finished && remaining)) {
  937. new_io = crypt_io_alloc(io->target, io->base_bio,
  938. sector);
  939. crypt_inc_pending(new_io);
  940. crypt_convert_init(cc, &new_io->ctx, NULL,
  941. io->base_bio, sector);
  942. new_io->ctx.idx_in = io->ctx.idx_in;
  943. new_io->ctx.offset_in = io->ctx.offset_in;
  944. /*
  945. * Fragments after the first use the base_io
  946. * pending count.
  947. */
  948. if (!io->base_io)
  949. new_io->base_io = io;
  950. else {
  951. new_io->base_io = io->base_io;
  952. crypt_inc_pending(io->base_io);
  953. crypt_dec_pending(io);
  954. }
  955. io = new_io;
  956. }
  957. }
  958. crypt_dec_pending(io);
  959. }
  960. static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
  961. {
  962. if (unlikely(error < 0))
  963. io->error = -EIO;
  964. crypt_dec_pending(io);
  965. }
  966. static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
  967. {
  968. struct crypt_config *cc = io->target->private;
  969. int r = 0;
  970. crypt_inc_pending(io);
  971. crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
  972. io->sector);
  973. r = crypt_convert(cc, &io->ctx);
  974. if (atomic_dec_and_test(&io->ctx.pending))
  975. kcryptd_crypt_read_done(io, r);
  976. crypt_dec_pending(io);
  977. }
  978. static void kcryptd_async_done(struct crypto_async_request *async_req,
  979. int error)
  980. {
  981. struct dm_crypt_request *dmreq = async_req->data;
  982. struct convert_context *ctx = dmreq->ctx;
  983. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  984. struct crypt_config *cc = io->target->private;
  985. if (error == -EINPROGRESS) {
  986. complete(&ctx->restart);
  987. return;
  988. }
  989. if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
  990. error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
  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, error);
  996. else
  997. kcryptd_crypt_write_io_submit(io, error, 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;
  1295. unsigned long long tmpll;
  1296. int ret;
  1297. if (argc != 5) {
  1298. ti->error = "Not enough arguments";
  1299. return -EINVAL;
  1300. }
  1301. key_size = strlen(argv[1]) >> 1;
  1302. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  1303. if (!cc) {
  1304. ti->error = "Cannot allocate encryption context";
  1305. return -ENOMEM;
  1306. }
  1307. cc->key_size = key_size;
  1308. ti->private = cc;
  1309. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  1310. if (ret < 0)
  1311. goto bad;
  1312. ret = -ENOMEM;
  1313. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  1314. if (!cc->io_pool) {
  1315. ti->error = "Cannot allocate crypt io mempool";
  1316. goto bad;
  1317. }
  1318. cc->dmreq_start = sizeof(struct ablkcipher_request);
  1319. cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
  1320. cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
  1321. cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
  1322. ~(crypto_tfm_ctx_alignment() - 1);
  1323. cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
  1324. sizeof(struct dm_crypt_request) + cc->iv_size);
  1325. if (!cc->req_pool) {
  1326. ti->error = "Cannot allocate crypt request mempool";
  1327. goto bad;
  1328. }
  1329. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  1330. if (!cc->page_pool) {
  1331. ti->error = "Cannot allocate page mempool";
  1332. goto bad;
  1333. }
  1334. cc->bs = bioset_create(MIN_IOS, 0);
  1335. if (!cc->bs) {
  1336. ti->error = "Cannot allocate crypt bioset";
  1337. goto bad;
  1338. }
  1339. ret = -EINVAL;
  1340. if (sscanf(argv[2], "%llu", &tmpll) != 1) {
  1341. ti->error = "Invalid iv_offset sector";
  1342. goto bad;
  1343. }
  1344. cc->iv_offset = tmpll;
  1345. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
  1346. ti->error = "Device lookup failed";
  1347. goto bad;
  1348. }
  1349. if (sscanf(argv[4], "%llu", &tmpll) != 1) {
  1350. ti->error = "Invalid device sector";
  1351. goto bad;
  1352. }
  1353. cc->start = tmpll;
  1354. ret = -ENOMEM;
  1355. cc->io_queue = alloc_workqueue("kcryptd_io",
  1356. WQ_NON_REENTRANT|
  1357. WQ_MEM_RECLAIM,
  1358. 1);
  1359. if (!cc->io_queue) {
  1360. ti->error = "Couldn't create kcryptd io queue";
  1361. goto bad;
  1362. }
  1363. cc->crypt_queue = alloc_workqueue("kcryptd",
  1364. WQ_NON_REENTRANT|
  1365. WQ_CPU_INTENSIVE|
  1366. WQ_MEM_RECLAIM,
  1367. 1);
  1368. if (!cc->crypt_queue) {
  1369. ti->error = "Couldn't create kcryptd queue";
  1370. goto bad;
  1371. }
  1372. ti->num_flush_requests = 1;
  1373. return 0;
  1374. bad:
  1375. crypt_dtr(ti);
  1376. return ret;
  1377. }
  1378. static int crypt_map(struct dm_target *ti, struct bio *bio,
  1379. union map_info *map_context)
  1380. {
  1381. struct dm_crypt_io *io;
  1382. struct crypt_config *cc;
  1383. if (bio->bi_rw & REQ_FLUSH) {
  1384. cc = ti->private;
  1385. bio->bi_bdev = cc->dev->bdev;
  1386. return DM_MAPIO_REMAPPED;
  1387. }
  1388. io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
  1389. if (bio_data_dir(io->base_bio) == READ) {
  1390. if (kcryptd_io_read(io, GFP_NOWAIT))
  1391. kcryptd_queue_io(io);
  1392. } else
  1393. kcryptd_queue_crypt(io);
  1394. return DM_MAPIO_SUBMITTED;
  1395. }
  1396. static int crypt_status(struct dm_target *ti, status_type_t type,
  1397. char *result, unsigned int maxlen)
  1398. {
  1399. struct crypt_config *cc = ti->private;
  1400. unsigned int sz = 0;
  1401. switch (type) {
  1402. case STATUSTYPE_INFO:
  1403. result[0] = '\0';
  1404. break;
  1405. case STATUSTYPE_TABLE:
  1406. DMEMIT("%s ", cc->cipher_string);
  1407. if (cc->key_size > 0) {
  1408. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  1409. return -ENOMEM;
  1410. crypt_encode_key(result + sz, cc->key, cc->key_size);
  1411. sz += cc->key_size << 1;
  1412. } else {
  1413. if (sz >= maxlen)
  1414. return -ENOMEM;
  1415. result[sz++] = '-';
  1416. }
  1417. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  1418. cc->dev->name, (unsigned long long)cc->start);
  1419. break;
  1420. }
  1421. return 0;
  1422. }
  1423. static void crypt_postsuspend(struct dm_target *ti)
  1424. {
  1425. struct crypt_config *cc = ti->private;
  1426. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1427. }
  1428. static int crypt_preresume(struct dm_target *ti)
  1429. {
  1430. struct crypt_config *cc = ti->private;
  1431. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  1432. DMERR("aborting resume - crypt key is not set.");
  1433. return -EAGAIN;
  1434. }
  1435. return 0;
  1436. }
  1437. static void crypt_resume(struct dm_target *ti)
  1438. {
  1439. struct crypt_config *cc = ti->private;
  1440. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1441. }
  1442. /* Message interface
  1443. * key set <key>
  1444. * key wipe
  1445. */
  1446. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  1447. {
  1448. struct crypt_config *cc = ti->private;
  1449. int ret = -EINVAL;
  1450. if (argc < 2)
  1451. goto error;
  1452. if (!strcasecmp(argv[0], "key")) {
  1453. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  1454. DMWARN("not suspended during key manipulation.");
  1455. return -EINVAL;
  1456. }
  1457. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  1458. ret = crypt_set_key(cc, argv[2]);
  1459. if (ret)
  1460. return ret;
  1461. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  1462. ret = cc->iv_gen_ops->init(cc);
  1463. return ret;
  1464. }
  1465. if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
  1466. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  1467. ret = cc->iv_gen_ops->wipe(cc);
  1468. if (ret)
  1469. return ret;
  1470. }
  1471. return crypt_wipe_key(cc);
  1472. }
  1473. }
  1474. error:
  1475. DMWARN("unrecognised message received.");
  1476. return -EINVAL;
  1477. }
  1478. static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1479. struct bio_vec *biovec, int max_size)
  1480. {
  1481. struct crypt_config *cc = ti->private;
  1482. struct request_queue *q = bdev_get_queue(cc->dev->bdev);
  1483. if (!q->merge_bvec_fn)
  1484. return max_size;
  1485. bvm->bi_bdev = cc->dev->bdev;
  1486. bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
  1487. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1488. }
  1489. static int crypt_iterate_devices(struct dm_target *ti,
  1490. iterate_devices_callout_fn fn, void *data)
  1491. {
  1492. struct crypt_config *cc = ti->private;
  1493. return fn(ti, cc->dev, cc->start, ti->len, data);
  1494. }
  1495. static struct target_type crypt_target = {
  1496. .name = "crypt",
  1497. .version = {1, 10, 0},
  1498. .module = THIS_MODULE,
  1499. .ctr = crypt_ctr,
  1500. .dtr = crypt_dtr,
  1501. .map = crypt_map,
  1502. .status = crypt_status,
  1503. .postsuspend = crypt_postsuspend,
  1504. .preresume = crypt_preresume,
  1505. .resume = crypt_resume,
  1506. .message = crypt_message,
  1507. .merge = crypt_merge,
  1508. .iterate_devices = crypt_iterate_devices,
  1509. };
  1510. static int __init dm_crypt_init(void)
  1511. {
  1512. int r;
  1513. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  1514. if (!_crypt_io_pool)
  1515. return -ENOMEM;
  1516. r = dm_register_target(&crypt_target);
  1517. if (r < 0) {
  1518. DMERR("register failed %d", r);
  1519. kmem_cache_destroy(_crypt_io_pool);
  1520. }
  1521. return r;
  1522. }
  1523. static void __exit dm_crypt_exit(void)
  1524. {
  1525. dm_unregister_target(&crypt_target);
  1526. kmem_cache_destroy(_crypt_io_pool);
  1527. }
  1528. module_init(dm_crypt_init);
  1529. module_exit(dm_crypt_exit);
  1530. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  1531. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  1532. MODULE_LICENSE("GPL");