dm-crypt.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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. char dummy;
  1155. /* Convert to crypto api definition? */
  1156. if (strchr(cipher_in, '(')) {
  1157. ti->error = "Bad cipher specification";
  1158. return -EINVAL;
  1159. }
  1160. cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
  1161. if (!cc->cipher_string)
  1162. goto bad_mem;
  1163. /*
  1164. * Legacy dm-crypt cipher specification
  1165. * cipher[:keycount]-mode-iv:ivopts
  1166. */
  1167. tmp = cipher_in;
  1168. keycount = strsep(&tmp, "-");
  1169. cipher = strsep(&keycount, ":");
  1170. if (!keycount)
  1171. cc->tfms_count = 1;
  1172. else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
  1173. !is_power_of_2(cc->tfms_count)) {
  1174. ti->error = "Bad cipher key count specification";
  1175. return -EINVAL;
  1176. }
  1177. cc->key_parts = cc->tfms_count;
  1178. cc->cipher = kstrdup(cipher, GFP_KERNEL);
  1179. if (!cc->cipher)
  1180. goto bad_mem;
  1181. chainmode = strsep(&tmp, "-");
  1182. ivopts = strsep(&tmp, "-");
  1183. ivmode = strsep(&ivopts, ":");
  1184. if (tmp)
  1185. DMWARN("Ignoring unexpected additional cipher options");
  1186. cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)) +
  1187. cc->tfms_count * sizeof(*(cc->cpu->tfms)),
  1188. __alignof__(struct crypt_cpu));
  1189. if (!cc->cpu) {
  1190. ti->error = "Cannot allocate per cpu state";
  1191. goto bad_mem;
  1192. }
  1193. /*
  1194. * For compatibility with the original dm-crypt mapping format, if
  1195. * only the cipher name is supplied, use cbc-plain.
  1196. */
  1197. if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
  1198. chainmode = "cbc";
  1199. ivmode = "plain";
  1200. }
  1201. if (strcmp(chainmode, "ecb") && !ivmode) {
  1202. ti->error = "IV mechanism required";
  1203. return -EINVAL;
  1204. }
  1205. cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
  1206. if (!cipher_api)
  1207. goto bad_mem;
  1208. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1209. "%s(%s)", chainmode, cipher);
  1210. if (ret < 0) {
  1211. kfree(cipher_api);
  1212. goto bad_mem;
  1213. }
  1214. /* Allocate cipher */
  1215. for_each_possible_cpu(cpu) {
  1216. ret = crypt_alloc_tfms(cc, cpu, cipher_api);
  1217. if (ret < 0) {
  1218. ti->error = "Error allocating crypto tfm";
  1219. goto bad;
  1220. }
  1221. }
  1222. /* Initialize and set key */
  1223. ret = crypt_set_key(cc, key);
  1224. if (ret < 0) {
  1225. ti->error = "Error decoding and setting key";
  1226. goto bad;
  1227. }
  1228. /* Initialize IV */
  1229. cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
  1230. if (cc->iv_size)
  1231. /* at least a 64 bit sector number should fit in our buffer */
  1232. cc->iv_size = max(cc->iv_size,
  1233. (unsigned int)(sizeof(u64) / sizeof(u8)));
  1234. else if (ivmode) {
  1235. DMWARN("Selected cipher does not support IVs");
  1236. ivmode = NULL;
  1237. }
  1238. /* Choose ivmode, see comments at iv code. */
  1239. if (ivmode == NULL)
  1240. cc->iv_gen_ops = NULL;
  1241. else if (strcmp(ivmode, "plain") == 0)
  1242. cc->iv_gen_ops = &crypt_iv_plain_ops;
  1243. else if (strcmp(ivmode, "plain64") == 0)
  1244. cc->iv_gen_ops = &crypt_iv_plain64_ops;
  1245. else if (strcmp(ivmode, "essiv") == 0)
  1246. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  1247. else if (strcmp(ivmode, "benbi") == 0)
  1248. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  1249. else if (strcmp(ivmode, "null") == 0)
  1250. cc->iv_gen_ops = &crypt_iv_null_ops;
  1251. else if (strcmp(ivmode, "lmk") == 0) {
  1252. cc->iv_gen_ops = &crypt_iv_lmk_ops;
  1253. /* Version 2 and 3 is recognised according
  1254. * to length of provided multi-key string.
  1255. * If present (version 3), last key is used as IV seed.
  1256. */
  1257. if (cc->key_size % cc->key_parts)
  1258. cc->key_parts++;
  1259. } else {
  1260. ret = -EINVAL;
  1261. ti->error = "Invalid IV mode";
  1262. goto bad;
  1263. }
  1264. /* Allocate IV */
  1265. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
  1266. ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
  1267. if (ret < 0) {
  1268. ti->error = "Error creating IV";
  1269. goto bad;
  1270. }
  1271. }
  1272. /* Initialize IV (set keys for ESSIV etc) */
  1273. if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
  1274. ret = cc->iv_gen_ops->init(cc);
  1275. if (ret < 0) {
  1276. ti->error = "Error initialising IV";
  1277. goto bad;
  1278. }
  1279. }
  1280. ret = 0;
  1281. bad:
  1282. kfree(cipher_api);
  1283. return ret;
  1284. bad_mem:
  1285. ti->error = "Cannot allocate cipher strings";
  1286. return -ENOMEM;
  1287. }
  1288. /*
  1289. * Construct an encryption mapping:
  1290. * <cipher> <key> <iv_offset> <dev_path> <start>
  1291. */
  1292. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1293. {
  1294. struct crypt_config *cc;
  1295. unsigned int key_size, opt_params;
  1296. unsigned long long tmpll;
  1297. int ret;
  1298. struct dm_arg_set as;
  1299. const char *opt_string;
  1300. char dummy;
  1301. static struct dm_arg _args[] = {
  1302. {0, 1, "Invalid number of feature args"},
  1303. };
  1304. if (argc < 5) {
  1305. ti->error = "Not enough arguments";
  1306. return -EINVAL;
  1307. }
  1308. key_size = strlen(argv[1]) >> 1;
  1309. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  1310. if (!cc) {
  1311. ti->error = "Cannot allocate encryption context";
  1312. return -ENOMEM;
  1313. }
  1314. cc->key_size = key_size;
  1315. ti->private = cc;
  1316. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  1317. if (ret < 0)
  1318. goto bad;
  1319. ret = -ENOMEM;
  1320. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  1321. if (!cc->io_pool) {
  1322. ti->error = "Cannot allocate crypt io mempool";
  1323. goto bad;
  1324. }
  1325. cc->dmreq_start = sizeof(struct ablkcipher_request);
  1326. cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
  1327. cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
  1328. cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
  1329. ~(crypto_tfm_ctx_alignment() - 1);
  1330. cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
  1331. sizeof(struct dm_crypt_request) + cc->iv_size);
  1332. if (!cc->req_pool) {
  1333. ti->error = "Cannot allocate crypt request mempool";
  1334. goto bad;
  1335. }
  1336. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  1337. if (!cc->page_pool) {
  1338. ti->error = "Cannot allocate page mempool";
  1339. goto bad;
  1340. }
  1341. cc->bs = bioset_create(MIN_IOS, 0);
  1342. if (!cc->bs) {
  1343. ti->error = "Cannot allocate crypt bioset";
  1344. goto bad;
  1345. }
  1346. ret = -EINVAL;
  1347. if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
  1348. ti->error = "Invalid iv_offset sector";
  1349. goto bad;
  1350. }
  1351. cc->iv_offset = tmpll;
  1352. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
  1353. ti->error = "Device lookup failed";
  1354. goto bad;
  1355. }
  1356. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  1357. ti->error = "Invalid device sector";
  1358. goto bad;
  1359. }
  1360. cc->start = tmpll;
  1361. argv += 5;
  1362. argc -= 5;
  1363. /* Optional parameters */
  1364. if (argc) {
  1365. as.argc = argc;
  1366. as.argv = argv;
  1367. ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
  1368. if (ret)
  1369. goto bad;
  1370. opt_string = dm_shift_arg(&as);
  1371. if (opt_params == 1 && opt_string &&
  1372. !strcasecmp(opt_string, "allow_discards"))
  1373. ti->num_discard_requests = 1;
  1374. else if (opt_params) {
  1375. ret = -EINVAL;
  1376. ti->error = "Invalid feature arguments";
  1377. goto bad;
  1378. }
  1379. }
  1380. ret = -ENOMEM;
  1381. cc->io_queue = alloc_workqueue("kcryptd_io",
  1382. WQ_NON_REENTRANT|
  1383. WQ_MEM_RECLAIM,
  1384. 1);
  1385. if (!cc->io_queue) {
  1386. ti->error = "Couldn't create kcryptd io queue";
  1387. goto bad;
  1388. }
  1389. cc->crypt_queue = alloc_workqueue("kcryptd",
  1390. WQ_NON_REENTRANT|
  1391. WQ_CPU_INTENSIVE|
  1392. WQ_MEM_RECLAIM,
  1393. 1);
  1394. if (!cc->crypt_queue) {
  1395. ti->error = "Couldn't create kcryptd queue";
  1396. goto bad;
  1397. }
  1398. ti->num_flush_requests = 1;
  1399. ti->discard_zeroes_data_unsupported = 1;
  1400. return 0;
  1401. bad:
  1402. crypt_dtr(ti);
  1403. return ret;
  1404. }
  1405. static int crypt_map(struct dm_target *ti, struct bio *bio,
  1406. union map_info *map_context)
  1407. {
  1408. struct dm_crypt_io *io;
  1409. struct crypt_config *cc;
  1410. /*
  1411. * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
  1412. * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
  1413. * - for REQ_DISCARD caller must use flush if IO ordering matters
  1414. */
  1415. if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
  1416. cc = ti->private;
  1417. bio->bi_bdev = cc->dev->bdev;
  1418. if (bio_sectors(bio))
  1419. bio->bi_sector = cc->start + dm_target_offset(ti, bio->bi_sector);
  1420. return DM_MAPIO_REMAPPED;
  1421. }
  1422. io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
  1423. if (bio_data_dir(io->base_bio) == READ) {
  1424. if (kcryptd_io_read(io, GFP_NOWAIT))
  1425. kcryptd_queue_io(io);
  1426. } else
  1427. kcryptd_queue_crypt(io);
  1428. return DM_MAPIO_SUBMITTED;
  1429. }
  1430. static int crypt_status(struct dm_target *ti, status_type_t type,
  1431. char *result, unsigned int maxlen)
  1432. {
  1433. struct crypt_config *cc = ti->private;
  1434. unsigned int sz = 0;
  1435. switch (type) {
  1436. case STATUSTYPE_INFO:
  1437. result[0] = '\0';
  1438. break;
  1439. case STATUSTYPE_TABLE:
  1440. DMEMIT("%s ", cc->cipher_string);
  1441. if (cc->key_size > 0) {
  1442. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  1443. return -ENOMEM;
  1444. crypt_encode_key(result + sz, cc->key, cc->key_size);
  1445. sz += cc->key_size << 1;
  1446. } else {
  1447. if (sz >= maxlen)
  1448. return -ENOMEM;
  1449. result[sz++] = '-';
  1450. }
  1451. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  1452. cc->dev->name, (unsigned long long)cc->start);
  1453. if (ti->num_discard_requests)
  1454. DMEMIT(" 1 allow_discards");
  1455. break;
  1456. }
  1457. return 0;
  1458. }
  1459. static void crypt_postsuspend(struct dm_target *ti)
  1460. {
  1461. struct crypt_config *cc = ti->private;
  1462. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1463. }
  1464. static int crypt_preresume(struct dm_target *ti)
  1465. {
  1466. struct crypt_config *cc = ti->private;
  1467. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  1468. DMERR("aborting resume - crypt key is not set.");
  1469. return -EAGAIN;
  1470. }
  1471. return 0;
  1472. }
  1473. static void crypt_resume(struct dm_target *ti)
  1474. {
  1475. struct crypt_config *cc = ti->private;
  1476. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1477. }
  1478. /* Message interface
  1479. * key set <key>
  1480. * key wipe
  1481. */
  1482. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  1483. {
  1484. struct crypt_config *cc = ti->private;
  1485. int ret = -EINVAL;
  1486. if (argc < 2)
  1487. goto error;
  1488. if (!strcasecmp(argv[0], "key")) {
  1489. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  1490. DMWARN("not suspended during key manipulation.");
  1491. return -EINVAL;
  1492. }
  1493. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  1494. ret = crypt_set_key(cc, argv[2]);
  1495. if (ret)
  1496. return ret;
  1497. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  1498. ret = cc->iv_gen_ops->init(cc);
  1499. return ret;
  1500. }
  1501. if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
  1502. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  1503. ret = cc->iv_gen_ops->wipe(cc);
  1504. if (ret)
  1505. return ret;
  1506. }
  1507. return crypt_wipe_key(cc);
  1508. }
  1509. }
  1510. error:
  1511. DMWARN("unrecognised message received.");
  1512. return -EINVAL;
  1513. }
  1514. static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1515. struct bio_vec *biovec, int max_size)
  1516. {
  1517. struct crypt_config *cc = ti->private;
  1518. struct request_queue *q = bdev_get_queue(cc->dev->bdev);
  1519. if (!q->merge_bvec_fn)
  1520. return max_size;
  1521. bvm->bi_bdev = cc->dev->bdev;
  1522. bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
  1523. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1524. }
  1525. static int crypt_iterate_devices(struct dm_target *ti,
  1526. iterate_devices_callout_fn fn, void *data)
  1527. {
  1528. struct crypt_config *cc = ti->private;
  1529. return fn(ti, cc->dev, cc->start, ti->len, data);
  1530. }
  1531. static struct target_type crypt_target = {
  1532. .name = "crypt",
  1533. .version = {1, 11, 0},
  1534. .module = THIS_MODULE,
  1535. .ctr = crypt_ctr,
  1536. .dtr = crypt_dtr,
  1537. .map = crypt_map,
  1538. .status = crypt_status,
  1539. .postsuspend = crypt_postsuspend,
  1540. .preresume = crypt_preresume,
  1541. .resume = crypt_resume,
  1542. .message = crypt_message,
  1543. .merge = crypt_merge,
  1544. .iterate_devices = crypt_iterate_devices,
  1545. };
  1546. static int __init dm_crypt_init(void)
  1547. {
  1548. int r;
  1549. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  1550. if (!_crypt_io_pool)
  1551. return -ENOMEM;
  1552. r = dm_register_target(&crypt_target);
  1553. if (r < 0) {
  1554. DMERR("register failed %d", r);
  1555. kmem_cache_destroy(_crypt_io_pool);
  1556. }
  1557. return r;
  1558. }
  1559. static void __exit dm_crypt_exit(void)
  1560. {
  1561. dm_unregister_target(&crypt_target);
  1562. kmem_cache_destroy(_crypt_io_pool);
  1563. }
  1564. module_init(dm_crypt_init);
  1565. module_exit(dm_crypt_exit);
  1566. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  1567. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  1568. MODULE_LICENSE("GPL");