dm-crypt.c 48 KB

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