dm-crypt.c 43 KB

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