dm-crypt.c 44 KB

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