dm-crypt.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
  437. struct md5_state md5state;
  438. __le32 buf[4];
  439. int i, r;
  440. desc->tfm = lmk->hash_tfm;
  441. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  442. r = crypto_shash_init(desc);
  443. if (r)
  444. return r;
  445. if (lmk->seed) {
  446. r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
  447. if (r)
  448. return r;
  449. }
  450. /* Sector is always 512B, block size 16, add data of blocks 1-31 */
  451. r = crypto_shash_update(desc, data + 16, 16 * 31);
  452. if (r)
  453. return r;
  454. /* Sector is cropped to 56 bits here */
  455. buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
  456. buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
  457. buf[2] = cpu_to_le32(4024);
  458. buf[3] = 0;
  459. r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
  460. if (r)
  461. return r;
  462. /* No MD5 padding here */
  463. r = crypto_shash_export(desc, &md5state);
  464. if (r)
  465. return r;
  466. for (i = 0; i < MD5_HASH_WORDS; i++)
  467. __cpu_to_le32s(&md5state.hash[i]);
  468. memcpy(iv, &md5state.hash, cc->iv_size);
  469. return 0;
  470. }
  471. static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
  472. struct dm_crypt_request *dmreq)
  473. {
  474. u8 *src;
  475. int r = 0;
  476. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
  477. src = kmap_atomic(sg_page(&dmreq->sg_in));
  478. r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
  479. kunmap_atomic(src);
  480. } else
  481. memset(iv, 0, cc->iv_size);
  482. return r;
  483. }
  484. static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
  485. struct dm_crypt_request *dmreq)
  486. {
  487. u8 *dst;
  488. int r;
  489. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
  490. return 0;
  491. dst = kmap_atomic(sg_page(&dmreq->sg_out));
  492. r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
  493. /* Tweak the first block of plaintext sector */
  494. if (!r)
  495. crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
  496. kunmap_atomic(dst);
  497. return r;
  498. }
  499. static void crypt_iv_tcw_dtr(struct crypt_config *cc)
  500. {
  501. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  502. kzfree(tcw->iv_seed);
  503. tcw->iv_seed = NULL;
  504. kzfree(tcw->whitening);
  505. tcw->whitening = NULL;
  506. if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
  507. crypto_free_shash(tcw->crc32_tfm);
  508. tcw->crc32_tfm = NULL;
  509. }
  510. static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
  511. const char *opts)
  512. {
  513. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  514. if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
  515. ti->error = "Wrong key size for TCW";
  516. return -EINVAL;
  517. }
  518. tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
  519. if (IS_ERR(tcw->crc32_tfm)) {
  520. ti->error = "Error initializing CRC32 in TCW";
  521. return PTR_ERR(tcw->crc32_tfm);
  522. }
  523. tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
  524. tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
  525. if (!tcw->iv_seed || !tcw->whitening) {
  526. crypt_iv_tcw_dtr(cc);
  527. ti->error = "Error allocating seed storage in TCW";
  528. return -ENOMEM;
  529. }
  530. return 0;
  531. }
  532. static int crypt_iv_tcw_init(struct crypt_config *cc)
  533. {
  534. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  535. int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
  536. memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
  537. memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
  538. TCW_WHITENING_SIZE);
  539. return 0;
  540. }
  541. static int crypt_iv_tcw_wipe(struct crypt_config *cc)
  542. {
  543. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  544. memset(tcw->iv_seed, 0, cc->iv_size);
  545. memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
  546. return 0;
  547. }
  548. static int crypt_iv_tcw_whitening(struct crypt_config *cc,
  549. struct dm_crypt_request *dmreq,
  550. u8 *data)
  551. {
  552. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  553. u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
  554. u8 buf[TCW_WHITENING_SIZE];
  555. SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
  556. int i, r;
  557. /* xor whitening with sector number */
  558. memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
  559. crypto_xor(buf, (u8 *)&sector, 8);
  560. crypto_xor(&buf[8], (u8 *)&sector, 8);
  561. /* calculate crc32 for every 32bit part and xor it */
  562. desc->tfm = tcw->crc32_tfm;
  563. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  564. for (i = 0; i < 4; i++) {
  565. r = crypto_shash_init(desc);
  566. if (r)
  567. goto out;
  568. r = crypto_shash_update(desc, &buf[i * 4], 4);
  569. if (r)
  570. goto out;
  571. r = crypto_shash_final(desc, &buf[i * 4]);
  572. if (r)
  573. goto out;
  574. }
  575. crypto_xor(&buf[0], &buf[12], 4);
  576. crypto_xor(&buf[4], &buf[8], 4);
  577. /* apply whitening (8 bytes) to whole sector */
  578. for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
  579. crypto_xor(data + i * 8, buf, 8);
  580. out:
  581. memset(buf, 0, sizeof(buf));
  582. return r;
  583. }
  584. static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
  585. struct dm_crypt_request *dmreq)
  586. {
  587. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  588. u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
  589. u8 *src;
  590. int r = 0;
  591. /* Remove whitening from ciphertext */
  592. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
  593. src = kmap_atomic(sg_page(&dmreq->sg_in));
  594. r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
  595. kunmap_atomic(src);
  596. }
  597. /* Calculate IV */
  598. memcpy(iv, tcw->iv_seed, cc->iv_size);
  599. crypto_xor(iv, (u8 *)&sector, 8);
  600. if (cc->iv_size > 8)
  601. crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
  602. return r;
  603. }
  604. static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
  605. struct dm_crypt_request *dmreq)
  606. {
  607. u8 *dst;
  608. int r;
  609. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
  610. return 0;
  611. /* Apply whitening on ciphertext */
  612. dst = kmap_atomic(sg_page(&dmreq->sg_out));
  613. r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
  614. kunmap_atomic(dst);
  615. return r;
  616. }
  617. static struct crypt_iv_operations crypt_iv_plain_ops = {
  618. .generator = crypt_iv_plain_gen
  619. };
  620. static struct crypt_iv_operations crypt_iv_plain64_ops = {
  621. .generator = crypt_iv_plain64_gen
  622. };
  623. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  624. .ctr = crypt_iv_essiv_ctr,
  625. .dtr = crypt_iv_essiv_dtr,
  626. .init = crypt_iv_essiv_init,
  627. .wipe = crypt_iv_essiv_wipe,
  628. .generator = crypt_iv_essiv_gen
  629. };
  630. static struct crypt_iv_operations crypt_iv_benbi_ops = {
  631. .ctr = crypt_iv_benbi_ctr,
  632. .dtr = crypt_iv_benbi_dtr,
  633. .generator = crypt_iv_benbi_gen
  634. };
  635. static struct crypt_iv_operations crypt_iv_null_ops = {
  636. .generator = crypt_iv_null_gen
  637. };
  638. static struct crypt_iv_operations crypt_iv_lmk_ops = {
  639. .ctr = crypt_iv_lmk_ctr,
  640. .dtr = crypt_iv_lmk_dtr,
  641. .init = crypt_iv_lmk_init,
  642. .wipe = crypt_iv_lmk_wipe,
  643. .generator = crypt_iv_lmk_gen,
  644. .post = crypt_iv_lmk_post
  645. };
  646. static struct crypt_iv_operations crypt_iv_tcw_ops = {
  647. .ctr = crypt_iv_tcw_ctr,
  648. .dtr = crypt_iv_tcw_dtr,
  649. .init = crypt_iv_tcw_init,
  650. .wipe = crypt_iv_tcw_wipe,
  651. .generator = crypt_iv_tcw_gen,
  652. .post = crypt_iv_tcw_post
  653. };
  654. static void crypt_convert_init(struct crypt_config *cc,
  655. struct convert_context *ctx,
  656. struct bio *bio_out, struct bio *bio_in,
  657. sector_t sector)
  658. {
  659. ctx->bio_in = bio_in;
  660. ctx->bio_out = bio_out;
  661. if (bio_in)
  662. ctx->iter_in = bio_in->bi_iter;
  663. if (bio_out)
  664. ctx->iter_out = bio_out->bi_iter;
  665. ctx->cc_sector = sector + cc->iv_offset;
  666. init_completion(&ctx->restart);
  667. }
  668. static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
  669. struct ablkcipher_request *req)
  670. {
  671. return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
  672. }
  673. static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
  674. struct dm_crypt_request *dmreq)
  675. {
  676. return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
  677. }
  678. static u8 *iv_of_dmreq(struct crypt_config *cc,
  679. struct dm_crypt_request *dmreq)
  680. {
  681. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  682. crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
  683. }
  684. static int crypt_convert_block(struct crypt_config *cc,
  685. struct convert_context *ctx,
  686. struct ablkcipher_request *req)
  687. {
  688. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  689. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  690. struct dm_crypt_request *dmreq;
  691. u8 *iv;
  692. int r;
  693. dmreq = dmreq_of_req(cc, req);
  694. iv = iv_of_dmreq(cc, dmreq);
  695. dmreq->iv_sector = ctx->cc_sector;
  696. dmreq->ctx = ctx;
  697. sg_init_table(&dmreq->sg_in, 1);
  698. sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
  699. bv_in.bv_offset);
  700. sg_init_table(&dmreq->sg_out, 1);
  701. sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
  702. bv_out.bv_offset);
  703. bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
  704. bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
  705. if (cc->iv_gen_ops) {
  706. r = cc->iv_gen_ops->generator(cc, iv, dmreq);
  707. if (r < 0)
  708. return r;
  709. }
  710. ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
  711. 1 << SECTOR_SHIFT, iv);
  712. if (bio_data_dir(ctx->bio_in) == WRITE)
  713. r = crypto_ablkcipher_encrypt(req);
  714. else
  715. r = crypto_ablkcipher_decrypt(req);
  716. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  717. r = cc->iv_gen_ops->post(cc, iv, dmreq);
  718. return r;
  719. }
  720. static void kcryptd_async_done(struct crypto_async_request *async_req,
  721. int error);
  722. static void crypt_alloc_req(struct crypt_config *cc,
  723. struct convert_context *ctx)
  724. {
  725. unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
  726. if (!ctx->req)
  727. ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
  728. ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
  729. ablkcipher_request_set_callback(ctx->req,
  730. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  731. kcryptd_async_done, dmreq_of_req(cc, ctx->req));
  732. }
  733. static void crypt_free_req(struct crypt_config *cc,
  734. struct ablkcipher_request *req, struct bio *base_bio)
  735. {
  736. struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
  737. if ((struct ablkcipher_request *)(io + 1) != req)
  738. mempool_free(req, cc->req_pool);
  739. }
  740. /*
  741. * Encrypt / decrypt data from one bio to another one (can be the same one)
  742. */
  743. static int crypt_convert(struct crypt_config *cc,
  744. struct convert_context *ctx)
  745. {
  746. int r;
  747. atomic_set(&ctx->cc_pending, 1);
  748. while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
  749. crypt_alloc_req(cc, ctx);
  750. atomic_inc(&ctx->cc_pending);
  751. r = crypt_convert_block(cc, ctx, ctx->req);
  752. switch (r) {
  753. /* async */
  754. case -EBUSY:
  755. wait_for_completion(&ctx->restart);
  756. reinit_completion(&ctx->restart);
  757. /* fall through*/
  758. case -EINPROGRESS:
  759. ctx->req = NULL;
  760. ctx->cc_sector++;
  761. continue;
  762. /* sync */
  763. case 0:
  764. atomic_dec(&ctx->cc_pending);
  765. ctx->cc_sector++;
  766. cond_resched();
  767. continue;
  768. /* error */
  769. default:
  770. atomic_dec(&ctx->cc_pending);
  771. return r;
  772. }
  773. }
  774. return 0;
  775. }
  776. /*
  777. * Generate a new unfragmented bio with the given size
  778. * This should never violate the device limitations
  779. * May return a smaller bio when running out of pages, indicated by
  780. * *out_of_pages set to 1.
  781. */
  782. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
  783. unsigned *out_of_pages)
  784. {
  785. struct crypt_config *cc = io->cc;
  786. struct bio *clone;
  787. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  788. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  789. unsigned i, len;
  790. struct page *page;
  791. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  792. if (!clone)
  793. return NULL;
  794. clone_init(io, clone);
  795. *out_of_pages = 0;
  796. for (i = 0; i < nr_iovecs; i++) {
  797. page = mempool_alloc(cc->page_pool, gfp_mask);
  798. if (!page) {
  799. *out_of_pages = 1;
  800. break;
  801. }
  802. /*
  803. * If additional pages cannot be allocated without waiting,
  804. * return a partially-allocated bio. The caller will then try
  805. * to allocate more bios while submitting this partial bio.
  806. */
  807. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  808. len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  809. if (!bio_add_page(clone, page, len, 0)) {
  810. mempool_free(page, cc->page_pool);
  811. break;
  812. }
  813. size -= len;
  814. }
  815. if (!clone->bi_iter.bi_size) {
  816. bio_put(clone);
  817. return NULL;
  818. }
  819. return clone;
  820. }
  821. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  822. {
  823. unsigned int i;
  824. struct bio_vec *bv;
  825. bio_for_each_segment_all(bv, clone, i) {
  826. BUG_ON(!bv->bv_page);
  827. mempool_free(bv->bv_page, cc->page_pool);
  828. bv->bv_page = NULL;
  829. }
  830. }
  831. static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
  832. struct bio *bio, sector_t sector)
  833. {
  834. io->cc = cc;
  835. io->base_bio = bio;
  836. io->sector = sector;
  837. io->error = 0;
  838. io->base_io = NULL;
  839. io->ctx.req = NULL;
  840. atomic_set(&io->io_pending, 0);
  841. }
  842. static void crypt_inc_pending(struct dm_crypt_io *io)
  843. {
  844. atomic_inc(&io->io_pending);
  845. }
  846. /*
  847. * One of the bios was finished. Check for completion of
  848. * the whole request and correctly clean up the buffer.
  849. * If base_io is set, wait for the last fragment to complete.
  850. */
  851. static void crypt_dec_pending(struct dm_crypt_io *io)
  852. {
  853. struct crypt_config *cc = io->cc;
  854. struct bio *base_bio = io->base_bio;
  855. struct dm_crypt_io *base_io = io->base_io;
  856. int error = io->error;
  857. if (!atomic_dec_and_test(&io->io_pending))
  858. return;
  859. if (io->ctx.req)
  860. crypt_free_req(cc, io->ctx.req, base_bio);
  861. if (io != dm_per_bio_data(base_bio, cc->per_bio_data_size))
  862. mempool_free(io, cc->io_pool);
  863. if (likely(!base_io))
  864. bio_endio(base_bio, error);
  865. else {
  866. if (error && !base_io->error)
  867. base_io->error = error;
  868. crypt_dec_pending(base_io);
  869. }
  870. }
  871. /*
  872. * kcryptd/kcryptd_io:
  873. *
  874. * Needed because it would be very unwise to do decryption in an
  875. * interrupt context.
  876. *
  877. * kcryptd performs the actual encryption or decryption.
  878. *
  879. * kcryptd_io performs the IO submission.
  880. *
  881. * They must be separated as otherwise the final stages could be
  882. * starved by new requests which can block in the first stages due
  883. * to memory allocation.
  884. *
  885. * The work is done per CPU global for all dm-crypt instances.
  886. * They should not depend on each other and do not block.
  887. */
  888. static void crypt_endio(struct bio *clone, int error)
  889. {
  890. struct dm_crypt_io *io = clone->bi_private;
  891. struct crypt_config *cc = io->cc;
  892. unsigned rw = bio_data_dir(clone);
  893. if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
  894. error = -EIO;
  895. /*
  896. * free the processed pages
  897. */
  898. if (rw == WRITE)
  899. crypt_free_buffer_pages(cc, clone);
  900. bio_put(clone);
  901. if (rw == READ && !error) {
  902. kcryptd_queue_crypt(io);
  903. return;
  904. }
  905. if (unlikely(error))
  906. io->error = error;
  907. crypt_dec_pending(io);
  908. }
  909. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  910. {
  911. struct crypt_config *cc = io->cc;
  912. clone->bi_private = io;
  913. clone->bi_end_io = crypt_endio;
  914. clone->bi_bdev = cc->dev->bdev;
  915. clone->bi_rw = io->base_bio->bi_rw;
  916. }
  917. static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
  918. {
  919. struct crypt_config *cc = io->cc;
  920. struct bio *base_bio = io->base_bio;
  921. struct bio *clone;
  922. /*
  923. * The block layer might modify the bvec array, so always
  924. * copy the required bvecs because we need the original
  925. * one in order to decrypt the whole bio data *afterwards*.
  926. */
  927. clone = bio_clone_bioset(base_bio, gfp, cc->bs);
  928. if (!clone)
  929. return 1;
  930. crypt_inc_pending(io);
  931. clone_init(io, clone);
  932. clone->bi_iter.bi_sector = cc->start + io->sector;
  933. generic_make_request(clone);
  934. return 0;
  935. }
  936. static void kcryptd_io_write(struct dm_crypt_io *io)
  937. {
  938. struct bio *clone = io->ctx.bio_out;
  939. generic_make_request(clone);
  940. }
  941. static void kcryptd_io(struct work_struct *work)
  942. {
  943. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  944. if (bio_data_dir(io->base_bio) == READ) {
  945. crypt_inc_pending(io);
  946. if (kcryptd_io_read(io, GFP_NOIO))
  947. io->error = -ENOMEM;
  948. crypt_dec_pending(io);
  949. } else
  950. kcryptd_io_write(io);
  951. }
  952. static void kcryptd_queue_io(struct dm_crypt_io *io)
  953. {
  954. struct crypt_config *cc = io->cc;
  955. INIT_WORK(&io->work, kcryptd_io);
  956. queue_work(cc->io_queue, &io->work);
  957. }
  958. static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
  959. {
  960. struct bio *clone = io->ctx.bio_out;
  961. struct crypt_config *cc = io->cc;
  962. if (unlikely(io->error < 0)) {
  963. crypt_free_buffer_pages(cc, clone);
  964. bio_put(clone);
  965. crypt_dec_pending(io);
  966. return;
  967. }
  968. /* crypt_convert should have filled the clone bio */
  969. BUG_ON(io->ctx.iter_out.bi_size);
  970. clone->bi_iter.bi_sector = cc->start + io->sector;
  971. if (async)
  972. kcryptd_queue_io(io);
  973. else
  974. generic_make_request(clone);
  975. }
  976. static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
  977. {
  978. struct crypt_config *cc = io->cc;
  979. struct bio *clone;
  980. struct dm_crypt_io *new_io;
  981. int crypt_finished;
  982. unsigned out_of_pages = 0;
  983. unsigned remaining = io->base_bio->bi_iter.bi_size;
  984. sector_t sector = io->sector;
  985. int r;
  986. /*
  987. * Prevent io from disappearing until this function completes.
  988. */
  989. crypt_inc_pending(io);
  990. crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
  991. /*
  992. * The allocated buffers can be smaller than the whole bio,
  993. * so repeat the whole process until all the data can be handled.
  994. */
  995. while (remaining) {
  996. clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
  997. if (unlikely(!clone)) {
  998. io->error = -ENOMEM;
  999. break;
  1000. }
  1001. io->ctx.bio_out = clone;
  1002. io->ctx.iter_out = clone->bi_iter;
  1003. remaining -= clone->bi_iter.bi_size;
  1004. sector += bio_sectors(clone);
  1005. crypt_inc_pending(io);
  1006. r = crypt_convert(cc, &io->ctx);
  1007. if (r < 0)
  1008. io->error = -EIO;
  1009. crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
  1010. /* Encryption was already finished, submit io now */
  1011. if (crypt_finished) {
  1012. kcryptd_crypt_write_io_submit(io, 0);
  1013. /*
  1014. * If there was an error, do not try next fragments.
  1015. * For async, error is processed in async handler.
  1016. */
  1017. if (unlikely(r < 0))
  1018. break;
  1019. io->sector = sector;
  1020. }
  1021. /*
  1022. * Out of memory -> run queues
  1023. * But don't wait if split was due to the io size restriction
  1024. */
  1025. if (unlikely(out_of_pages))
  1026. congestion_wait(BLK_RW_ASYNC, HZ/100);
  1027. /*
  1028. * With async crypto it is unsafe to share the crypto context
  1029. * between fragments, so switch to a new dm_crypt_io structure.
  1030. */
  1031. if (unlikely(!crypt_finished && remaining)) {
  1032. new_io = mempool_alloc(cc->io_pool, GFP_NOIO);
  1033. crypt_io_init(new_io, io->cc, io->base_bio, sector);
  1034. crypt_inc_pending(new_io);
  1035. crypt_convert_init(cc, &new_io->ctx, NULL,
  1036. io->base_bio, sector);
  1037. new_io->ctx.iter_in = io->ctx.iter_in;
  1038. /*
  1039. * Fragments after the first use the base_io
  1040. * pending count.
  1041. */
  1042. if (!io->base_io)
  1043. new_io->base_io = io;
  1044. else {
  1045. new_io->base_io = io->base_io;
  1046. crypt_inc_pending(io->base_io);
  1047. crypt_dec_pending(io);
  1048. }
  1049. io = new_io;
  1050. }
  1051. }
  1052. crypt_dec_pending(io);
  1053. }
  1054. static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
  1055. {
  1056. crypt_dec_pending(io);
  1057. }
  1058. static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
  1059. {
  1060. struct crypt_config *cc = io->cc;
  1061. int r = 0;
  1062. crypt_inc_pending(io);
  1063. crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
  1064. io->sector);
  1065. r = crypt_convert(cc, &io->ctx);
  1066. if (r < 0)
  1067. io->error = -EIO;
  1068. if (atomic_dec_and_test(&io->ctx.cc_pending))
  1069. kcryptd_crypt_read_done(io);
  1070. crypt_dec_pending(io);
  1071. }
  1072. static void kcryptd_async_done(struct crypto_async_request *async_req,
  1073. int error)
  1074. {
  1075. struct dm_crypt_request *dmreq = async_req->data;
  1076. struct convert_context *ctx = dmreq->ctx;
  1077. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  1078. struct crypt_config *cc = io->cc;
  1079. if (error == -EINPROGRESS) {
  1080. complete(&ctx->restart);
  1081. return;
  1082. }
  1083. if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
  1084. error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
  1085. if (error < 0)
  1086. io->error = -EIO;
  1087. crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
  1088. if (!atomic_dec_and_test(&ctx->cc_pending))
  1089. return;
  1090. if (bio_data_dir(io->base_bio) == READ)
  1091. kcryptd_crypt_read_done(io);
  1092. else
  1093. kcryptd_crypt_write_io_submit(io, 1);
  1094. }
  1095. static void kcryptd_crypt(struct work_struct *work)
  1096. {
  1097. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1098. if (bio_data_dir(io->base_bio) == READ)
  1099. kcryptd_crypt_read_convert(io);
  1100. else
  1101. kcryptd_crypt_write_convert(io);
  1102. }
  1103. static void kcryptd_queue_crypt(struct dm_crypt_io *io)
  1104. {
  1105. struct crypt_config *cc = io->cc;
  1106. INIT_WORK(&io->work, kcryptd_crypt);
  1107. queue_work(cc->crypt_queue, &io->work);
  1108. }
  1109. /*
  1110. * Decode key from its hex representation
  1111. */
  1112. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  1113. {
  1114. char buffer[3];
  1115. unsigned int i;
  1116. buffer[2] = '\0';
  1117. for (i = 0; i < size; i++) {
  1118. buffer[0] = *hex++;
  1119. buffer[1] = *hex++;
  1120. if (kstrtou8(buffer, 16, &key[i]))
  1121. return -EINVAL;
  1122. }
  1123. if (*hex != '\0')
  1124. return -EINVAL;
  1125. return 0;
  1126. }
  1127. static void crypt_free_tfms(struct crypt_config *cc)
  1128. {
  1129. unsigned i;
  1130. if (!cc->tfms)
  1131. return;
  1132. for (i = 0; i < cc->tfms_count; i++)
  1133. if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
  1134. crypto_free_ablkcipher(cc->tfms[i]);
  1135. cc->tfms[i] = NULL;
  1136. }
  1137. kfree(cc->tfms);
  1138. cc->tfms = NULL;
  1139. }
  1140. static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
  1141. {
  1142. unsigned i;
  1143. int err;
  1144. cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
  1145. GFP_KERNEL);
  1146. if (!cc->tfms)
  1147. return -ENOMEM;
  1148. for (i = 0; i < cc->tfms_count; i++) {
  1149. cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
  1150. if (IS_ERR(cc->tfms[i])) {
  1151. err = PTR_ERR(cc->tfms[i]);
  1152. crypt_free_tfms(cc);
  1153. return err;
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. static int crypt_setkey_allcpus(struct crypt_config *cc)
  1159. {
  1160. unsigned subkey_size;
  1161. int err = 0, i, r;
  1162. /* Ignore extra keys (which are used for IV etc) */
  1163. subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
  1164. for (i = 0; i < cc->tfms_count; i++) {
  1165. r = crypto_ablkcipher_setkey(cc->tfms[i],
  1166. cc->key + (i * subkey_size),
  1167. subkey_size);
  1168. if (r)
  1169. err = r;
  1170. }
  1171. return err;
  1172. }
  1173. static int crypt_set_key(struct crypt_config *cc, char *key)
  1174. {
  1175. int r = -EINVAL;
  1176. int key_string_len = strlen(key);
  1177. /* The key size may not be changed. */
  1178. if (cc->key_size != (key_string_len >> 1))
  1179. goto out;
  1180. /* Hyphen (which gives a key_size of zero) means there is no key. */
  1181. if (!cc->key_size && strcmp(key, "-"))
  1182. goto out;
  1183. if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
  1184. goto out;
  1185. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1186. r = crypt_setkey_allcpus(cc);
  1187. out:
  1188. /* Hex key string not needed after here, so wipe it. */
  1189. memset(key, '0', key_string_len);
  1190. return r;
  1191. }
  1192. static int crypt_wipe_key(struct crypt_config *cc)
  1193. {
  1194. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1195. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  1196. return crypt_setkey_allcpus(cc);
  1197. }
  1198. static void crypt_dtr(struct dm_target *ti)
  1199. {
  1200. struct crypt_config *cc = ti->private;
  1201. ti->private = NULL;
  1202. if (!cc)
  1203. return;
  1204. if (cc->io_queue)
  1205. destroy_workqueue(cc->io_queue);
  1206. if (cc->crypt_queue)
  1207. destroy_workqueue(cc->crypt_queue);
  1208. crypt_free_tfms(cc);
  1209. if (cc->bs)
  1210. bioset_free(cc->bs);
  1211. if (cc->page_pool)
  1212. mempool_destroy(cc->page_pool);
  1213. if (cc->req_pool)
  1214. mempool_destroy(cc->req_pool);
  1215. if (cc->io_pool)
  1216. mempool_destroy(cc->io_pool);
  1217. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  1218. cc->iv_gen_ops->dtr(cc);
  1219. if (cc->dev)
  1220. dm_put_device(ti, cc->dev);
  1221. kzfree(cc->cipher);
  1222. kzfree(cc->cipher_string);
  1223. /* Must zero key material before freeing */
  1224. kzfree(cc);
  1225. }
  1226. static int crypt_ctr_cipher(struct dm_target *ti,
  1227. char *cipher_in, char *key)
  1228. {
  1229. struct crypt_config *cc = ti->private;
  1230. char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
  1231. char *cipher_api = NULL;
  1232. int ret = -EINVAL;
  1233. char dummy;
  1234. /* Convert to crypto api definition? */
  1235. if (strchr(cipher_in, '(')) {
  1236. ti->error = "Bad cipher specification";
  1237. return -EINVAL;
  1238. }
  1239. cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
  1240. if (!cc->cipher_string)
  1241. goto bad_mem;
  1242. /*
  1243. * Legacy dm-crypt cipher specification
  1244. * cipher[:keycount]-mode-iv:ivopts
  1245. */
  1246. tmp = cipher_in;
  1247. keycount = strsep(&tmp, "-");
  1248. cipher = strsep(&keycount, ":");
  1249. if (!keycount)
  1250. cc->tfms_count = 1;
  1251. else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
  1252. !is_power_of_2(cc->tfms_count)) {
  1253. ti->error = "Bad cipher key count specification";
  1254. return -EINVAL;
  1255. }
  1256. cc->key_parts = cc->tfms_count;
  1257. cc->key_extra_size = 0;
  1258. cc->cipher = kstrdup(cipher, GFP_KERNEL);
  1259. if (!cc->cipher)
  1260. goto bad_mem;
  1261. chainmode = strsep(&tmp, "-");
  1262. ivopts = strsep(&tmp, "-");
  1263. ivmode = strsep(&ivopts, ":");
  1264. if (tmp)
  1265. DMWARN("Ignoring unexpected additional cipher options");
  1266. /*
  1267. * For compatibility with the original dm-crypt mapping format, if
  1268. * only the cipher name is supplied, use cbc-plain.
  1269. */
  1270. if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
  1271. chainmode = "cbc";
  1272. ivmode = "plain";
  1273. }
  1274. if (strcmp(chainmode, "ecb") && !ivmode) {
  1275. ti->error = "IV mechanism required";
  1276. return -EINVAL;
  1277. }
  1278. cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
  1279. if (!cipher_api)
  1280. goto bad_mem;
  1281. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1282. "%s(%s)", chainmode, cipher);
  1283. if (ret < 0) {
  1284. kfree(cipher_api);
  1285. goto bad_mem;
  1286. }
  1287. /* Allocate cipher */
  1288. ret = crypt_alloc_tfms(cc, cipher_api);
  1289. if (ret < 0) {
  1290. ti->error = "Error allocating crypto tfm";
  1291. goto bad;
  1292. }
  1293. /* Initialize IV */
  1294. cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
  1295. if (cc->iv_size)
  1296. /* at least a 64 bit sector number should fit in our buffer */
  1297. cc->iv_size = max(cc->iv_size,
  1298. (unsigned int)(sizeof(u64) / sizeof(u8)));
  1299. else if (ivmode) {
  1300. DMWARN("Selected cipher does not support IVs");
  1301. ivmode = NULL;
  1302. }
  1303. /* Choose ivmode, see comments at iv code. */
  1304. if (ivmode == NULL)
  1305. cc->iv_gen_ops = NULL;
  1306. else if (strcmp(ivmode, "plain") == 0)
  1307. cc->iv_gen_ops = &crypt_iv_plain_ops;
  1308. else if (strcmp(ivmode, "plain64") == 0)
  1309. cc->iv_gen_ops = &crypt_iv_plain64_ops;
  1310. else if (strcmp(ivmode, "essiv") == 0)
  1311. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  1312. else if (strcmp(ivmode, "benbi") == 0)
  1313. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  1314. else if (strcmp(ivmode, "null") == 0)
  1315. cc->iv_gen_ops = &crypt_iv_null_ops;
  1316. else if (strcmp(ivmode, "lmk") == 0) {
  1317. cc->iv_gen_ops = &crypt_iv_lmk_ops;
  1318. /*
  1319. * Version 2 and 3 is recognised according
  1320. * to length of provided multi-key string.
  1321. * If present (version 3), last key is used as IV seed.
  1322. * All keys (including IV seed) are always the same size.
  1323. */
  1324. if (cc->key_size % cc->key_parts) {
  1325. cc->key_parts++;
  1326. cc->key_extra_size = cc->key_size / cc->key_parts;
  1327. }
  1328. } else if (strcmp(ivmode, "tcw") == 0) {
  1329. cc->iv_gen_ops = &crypt_iv_tcw_ops;
  1330. cc->key_parts += 2; /* IV + whitening */
  1331. cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
  1332. } else {
  1333. ret = -EINVAL;
  1334. ti->error = "Invalid IV mode";
  1335. goto bad;
  1336. }
  1337. /* Initialize and set key */
  1338. ret = crypt_set_key(cc, key);
  1339. if (ret < 0) {
  1340. ti->error = "Error decoding and setting key";
  1341. goto bad;
  1342. }
  1343. /* Allocate IV */
  1344. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
  1345. ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
  1346. if (ret < 0) {
  1347. ti->error = "Error creating IV";
  1348. goto bad;
  1349. }
  1350. }
  1351. /* Initialize IV (set keys for ESSIV etc) */
  1352. if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
  1353. ret = cc->iv_gen_ops->init(cc);
  1354. if (ret < 0) {
  1355. ti->error = "Error initialising IV";
  1356. goto bad;
  1357. }
  1358. }
  1359. ret = 0;
  1360. bad:
  1361. kfree(cipher_api);
  1362. return ret;
  1363. bad_mem:
  1364. ti->error = "Cannot allocate cipher strings";
  1365. return -ENOMEM;
  1366. }
  1367. /*
  1368. * Construct an encryption mapping:
  1369. * <cipher> <key> <iv_offset> <dev_path> <start>
  1370. */
  1371. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1372. {
  1373. struct crypt_config *cc;
  1374. unsigned int key_size, opt_params;
  1375. unsigned long long tmpll;
  1376. int ret;
  1377. size_t iv_size_padding;
  1378. struct dm_arg_set as;
  1379. const char *opt_string;
  1380. char dummy;
  1381. static struct dm_arg _args[] = {
  1382. {0, 1, "Invalid number of feature args"},
  1383. };
  1384. if (argc < 5) {
  1385. ti->error = "Not enough arguments";
  1386. return -EINVAL;
  1387. }
  1388. key_size = strlen(argv[1]) >> 1;
  1389. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  1390. if (!cc) {
  1391. ti->error = "Cannot allocate encryption context";
  1392. return -ENOMEM;
  1393. }
  1394. cc->key_size = key_size;
  1395. ti->private = cc;
  1396. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  1397. if (ret < 0)
  1398. goto bad;
  1399. ret = -ENOMEM;
  1400. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  1401. if (!cc->io_pool) {
  1402. ti->error = "Cannot allocate crypt io mempool";
  1403. goto bad;
  1404. }
  1405. cc->dmreq_start = sizeof(struct ablkcipher_request);
  1406. cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
  1407. cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
  1408. if (crypto_ablkcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
  1409. /* Allocate the padding exactly */
  1410. iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
  1411. & crypto_ablkcipher_alignmask(any_tfm(cc));
  1412. } else {
  1413. /*
  1414. * If the cipher requires greater alignment than kmalloc
  1415. * alignment, we don't know the exact position of the
  1416. * initialization vector. We must assume worst case.
  1417. */
  1418. iv_size_padding = crypto_ablkcipher_alignmask(any_tfm(cc));
  1419. }
  1420. cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
  1421. sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
  1422. if (!cc->req_pool) {
  1423. ti->error = "Cannot allocate crypt request mempool";
  1424. goto bad;
  1425. }
  1426. cc->per_bio_data_size = ti->per_bio_data_size =
  1427. ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
  1428. sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
  1429. ARCH_KMALLOC_MINALIGN);
  1430. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  1431. if (!cc->page_pool) {
  1432. ti->error = "Cannot allocate page mempool";
  1433. goto bad;
  1434. }
  1435. cc->bs = bioset_create(MIN_IOS, 0);
  1436. if (!cc->bs) {
  1437. ti->error = "Cannot allocate crypt bioset";
  1438. goto bad;
  1439. }
  1440. ret = -EINVAL;
  1441. if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
  1442. ti->error = "Invalid iv_offset sector";
  1443. goto bad;
  1444. }
  1445. cc->iv_offset = tmpll;
  1446. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
  1447. ti->error = "Device lookup failed";
  1448. goto bad;
  1449. }
  1450. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  1451. ti->error = "Invalid device sector";
  1452. goto bad;
  1453. }
  1454. cc->start = tmpll;
  1455. argv += 5;
  1456. argc -= 5;
  1457. /* Optional parameters */
  1458. if (argc) {
  1459. as.argc = argc;
  1460. as.argv = argv;
  1461. ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
  1462. if (ret)
  1463. goto bad;
  1464. opt_string = dm_shift_arg(&as);
  1465. if (opt_params == 1 && opt_string &&
  1466. !strcasecmp(opt_string, "allow_discards"))
  1467. ti->num_discard_bios = 1;
  1468. else if (opt_params) {
  1469. ret = -EINVAL;
  1470. ti->error = "Invalid feature arguments";
  1471. goto bad;
  1472. }
  1473. }
  1474. ret = -ENOMEM;
  1475. cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
  1476. if (!cc->io_queue) {
  1477. ti->error = "Couldn't create kcryptd io queue";
  1478. goto bad;
  1479. }
  1480. cc->crypt_queue = alloc_workqueue("kcryptd",
  1481. WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
  1482. if (!cc->crypt_queue) {
  1483. ti->error = "Couldn't create kcryptd queue";
  1484. goto bad;
  1485. }
  1486. ti->num_flush_bios = 1;
  1487. ti->discard_zeroes_data_unsupported = true;
  1488. return 0;
  1489. bad:
  1490. crypt_dtr(ti);
  1491. return ret;
  1492. }
  1493. static int crypt_map(struct dm_target *ti, struct bio *bio)
  1494. {
  1495. struct dm_crypt_io *io;
  1496. struct crypt_config *cc = ti->private;
  1497. /*
  1498. * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
  1499. * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
  1500. * - for REQ_DISCARD caller must use flush if IO ordering matters
  1501. */
  1502. if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
  1503. bio->bi_bdev = cc->dev->bdev;
  1504. if (bio_sectors(bio))
  1505. bio->bi_iter.bi_sector = cc->start +
  1506. dm_target_offset(ti, bio->bi_iter.bi_sector);
  1507. return DM_MAPIO_REMAPPED;
  1508. }
  1509. io = dm_per_bio_data(bio, cc->per_bio_data_size);
  1510. crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
  1511. io->ctx.req = (struct ablkcipher_request *)(io + 1);
  1512. if (bio_data_dir(io->base_bio) == READ) {
  1513. if (kcryptd_io_read(io, GFP_NOWAIT))
  1514. kcryptd_queue_io(io);
  1515. } else
  1516. kcryptd_queue_crypt(io);
  1517. return DM_MAPIO_SUBMITTED;
  1518. }
  1519. static void crypt_status(struct dm_target *ti, status_type_t type,
  1520. unsigned status_flags, char *result, unsigned maxlen)
  1521. {
  1522. struct crypt_config *cc = ti->private;
  1523. unsigned i, sz = 0;
  1524. switch (type) {
  1525. case STATUSTYPE_INFO:
  1526. result[0] = '\0';
  1527. break;
  1528. case STATUSTYPE_TABLE:
  1529. DMEMIT("%s ", cc->cipher_string);
  1530. if (cc->key_size > 0)
  1531. for (i = 0; i < cc->key_size; i++)
  1532. DMEMIT("%02x", cc->key[i]);
  1533. else
  1534. DMEMIT("-");
  1535. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  1536. cc->dev->name, (unsigned long long)cc->start);
  1537. if (ti->num_discard_bios)
  1538. DMEMIT(" 1 allow_discards");
  1539. break;
  1540. }
  1541. }
  1542. static void crypt_postsuspend(struct dm_target *ti)
  1543. {
  1544. struct crypt_config *cc = ti->private;
  1545. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1546. }
  1547. static int crypt_preresume(struct dm_target *ti)
  1548. {
  1549. struct crypt_config *cc = ti->private;
  1550. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  1551. DMERR("aborting resume - crypt key is not set.");
  1552. return -EAGAIN;
  1553. }
  1554. return 0;
  1555. }
  1556. static void crypt_resume(struct dm_target *ti)
  1557. {
  1558. struct crypt_config *cc = ti->private;
  1559. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  1560. }
  1561. /* Message interface
  1562. * key set <key>
  1563. * key wipe
  1564. */
  1565. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  1566. {
  1567. struct crypt_config *cc = ti->private;
  1568. int ret = -EINVAL;
  1569. if (argc < 2)
  1570. goto error;
  1571. if (!strcasecmp(argv[0], "key")) {
  1572. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  1573. DMWARN("not suspended during key manipulation.");
  1574. return -EINVAL;
  1575. }
  1576. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  1577. ret = crypt_set_key(cc, argv[2]);
  1578. if (ret)
  1579. return ret;
  1580. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  1581. ret = cc->iv_gen_ops->init(cc);
  1582. return ret;
  1583. }
  1584. if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
  1585. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  1586. ret = cc->iv_gen_ops->wipe(cc);
  1587. if (ret)
  1588. return ret;
  1589. }
  1590. return crypt_wipe_key(cc);
  1591. }
  1592. }
  1593. error:
  1594. DMWARN("unrecognised message received.");
  1595. return -EINVAL;
  1596. }
  1597. static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1598. struct bio_vec *biovec, int max_size)
  1599. {
  1600. struct crypt_config *cc = ti->private;
  1601. struct request_queue *q = bdev_get_queue(cc->dev->bdev);
  1602. if (!q->merge_bvec_fn)
  1603. return max_size;
  1604. bvm->bi_bdev = cc->dev->bdev;
  1605. bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
  1606. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1607. }
  1608. static int crypt_iterate_devices(struct dm_target *ti,
  1609. iterate_devices_callout_fn fn, void *data)
  1610. {
  1611. struct crypt_config *cc = ti->private;
  1612. return fn(ti, cc->dev, cc->start, ti->len, data);
  1613. }
  1614. static struct target_type crypt_target = {
  1615. .name = "crypt",
  1616. .version = {1, 13, 0},
  1617. .module = THIS_MODULE,
  1618. .ctr = crypt_ctr,
  1619. .dtr = crypt_dtr,
  1620. .map = crypt_map,
  1621. .status = crypt_status,
  1622. .postsuspend = crypt_postsuspend,
  1623. .preresume = crypt_preresume,
  1624. .resume = crypt_resume,
  1625. .message = crypt_message,
  1626. .merge = crypt_merge,
  1627. .iterate_devices = crypt_iterate_devices,
  1628. };
  1629. static int __init dm_crypt_init(void)
  1630. {
  1631. int r;
  1632. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  1633. if (!_crypt_io_pool)
  1634. return -ENOMEM;
  1635. r = dm_register_target(&crypt_target);
  1636. if (r < 0) {
  1637. DMERR("register failed %d", r);
  1638. kmem_cache_destroy(_crypt_io_pool);
  1639. }
  1640. return r;
  1641. }
  1642. static void __exit dm_crypt_exit(void)
  1643. {
  1644. dm_unregister_target(&crypt_target);
  1645. kmem_cache_destroy(_crypt_io_pool);
  1646. }
  1647. module_init(dm_crypt_init);
  1648. module_exit(dm_crypt_exit);
  1649. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  1650. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  1651. MODULE_LICENSE("GPL");