dm-crypt.c 43 KB

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