dm-crypt.c 44 KB

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