dm-crypt.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. /*
  2. * Copyright (C) 2003 Jana Saout <jana@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/key.h>
  15. #include <linux/bio.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/mempool.h>
  18. #include <linux/slab.h>
  19. #include <linux/crypto.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/kthread.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/atomic.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/rbtree.h>
  26. #include <linux/ctype.h>
  27. #include <asm/page.h>
  28. #include <asm/unaligned.h>
  29. #include <crypto/hash.h>
  30. #include <crypto/md5.h>
  31. #include <crypto/algapi.h>
  32. #include <crypto/skcipher.h>
  33. #include <crypto/aead.h>
  34. #include <crypto/authenc.h>
  35. #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
  36. #include <keys/user-type.h>
  37. #include <linux/device-mapper.h>
  38. #define DM_MSG_PREFIX "crypt"
  39. /*
  40. * context holding the current state of a multi-part conversion
  41. */
  42. struct convert_context {
  43. struct completion restart;
  44. struct bio *bio_in;
  45. struct bio *bio_out;
  46. struct bvec_iter iter_in;
  47. struct bvec_iter iter_out;
  48. sector_t cc_sector;
  49. atomic_t cc_pending;
  50. union {
  51. struct skcipher_request *req;
  52. struct aead_request *req_aead;
  53. } r;
  54. };
  55. /*
  56. * per bio private data
  57. */
  58. struct dm_crypt_io {
  59. struct crypt_config *cc;
  60. struct bio *base_bio;
  61. u8 *integrity_metadata;
  62. bool integrity_metadata_from_pool;
  63. struct work_struct work;
  64. struct convert_context ctx;
  65. atomic_t io_pending;
  66. int error;
  67. sector_t sector;
  68. struct rb_node rb_node;
  69. } CRYPTO_MINALIGN_ATTR;
  70. struct dm_crypt_request {
  71. struct convert_context *ctx;
  72. struct scatterlist sg_in[4];
  73. struct scatterlist sg_out[4];
  74. sector_t iv_sector;
  75. };
  76. struct crypt_config;
  77. struct crypt_iv_operations {
  78. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  79. const char *opts);
  80. void (*dtr)(struct crypt_config *cc);
  81. int (*init)(struct crypt_config *cc);
  82. int (*wipe)(struct crypt_config *cc);
  83. int (*generator)(struct crypt_config *cc, u8 *iv,
  84. struct dm_crypt_request *dmreq);
  85. int (*post)(struct crypt_config *cc, u8 *iv,
  86. struct dm_crypt_request *dmreq);
  87. };
  88. struct iv_essiv_private {
  89. struct crypto_ahash *hash_tfm;
  90. u8 *salt;
  91. };
  92. struct iv_benbi_private {
  93. int shift;
  94. };
  95. #define LMK_SEED_SIZE 64 /* hash + 0 */
  96. struct iv_lmk_private {
  97. struct crypto_shash *hash_tfm;
  98. u8 *seed;
  99. };
  100. #define TCW_WHITENING_SIZE 16
  101. struct iv_tcw_private {
  102. struct crypto_shash *crc32_tfm;
  103. u8 *iv_seed;
  104. u8 *whitening;
  105. };
  106. /*
  107. * Crypt: maps a linear range of a block device
  108. * and encrypts / decrypts at the same time.
  109. */
  110. enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
  111. DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
  112. enum cipher_flags {
  113. CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
  114. CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
  115. };
  116. /*
  117. * The fields in here must be read only after initialization.
  118. */
  119. struct crypt_config {
  120. struct dm_dev *dev;
  121. sector_t start;
  122. /*
  123. * pool for per bio private data, crypto requests,
  124. * encryption requeusts/buffer pages and integrity tags
  125. */
  126. mempool_t *req_pool;
  127. mempool_t *page_pool;
  128. mempool_t *tag_pool;
  129. unsigned tag_pool_max_sectors;
  130. struct bio_set *bs;
  131. struct mutex bio_alloc_lock;
  132. struct workqueue_struct *io_queue;
  133. struct workqueue_struct *crypt_queue;
  134. struct task_struct *write_thread;
  135. wait_queue_head_t write_thread_wait;
  136. struct rb_root write_tree;
  137. char *cipher;
  138. char *cipher_string;
  139. char *cipher_auth;
  140. char *key_string;
  141. const struct crypt_iv_operations *iv_gen_ops;
  142. union {
  143. struct iv_essiv_private essiv;
  144. struct iv_benbi_private benbi;
  145. struct iv_lmk_private lmk;
  146. struct iv_tcw_private tcw;
  147. } iv_gen_private;
  148. sector_t iv_offset;
  149. unsigned int iv_size;
  150. unsigned short int sector_size;
  151. unsigned char sector_shift;
  152. /* ESSIV: struct crypto_cipher *essiv_tfm */
  153. void *iv_private;
  154. union {
  155. struct crypto_skcipher **tfms;
  156. struct crypto_aead **tfms_aead;
  157. } cipher_tfm;
  158. unsigned tfms_count;
  159. unsigned long cipher_flags;
  160. /*
  161. * Layout of each crypto request:
  162. *
  163. * struct skcipher_request
  164. * context
  165. * padding
  166. * struct dm_crypt_request
  167. * padding
  168. * IV
  169. *
  170. * The padding is added so that dm_crypt_request and the IV are
  171. * correctly aligned.
  172. */
  173. unsigned int dmreq_start;
  174. unsigned int per_bio_data_size;
  175. unsigned long flags;
  176. unsigned int key_size;
  177. unsigned int key_parts; /* independent parts in key buffer */
  178. unsigned int key_extra_size; /* additional keys length */
  179. unsigned int key_mac_size; /* MAC key size for authenc(...) */
  180. unsigned int integrity_tag_size;
  181. unsigned int integrity_iv_size;
  182. unsigned int on_disk_tag_size;
  183. u8 *authenc_key; /* space for keys in authenc() format (if used) */
  184. u8 key[0];
  185. };
  186. #define MIN_IOS 64
  187. #define MAX_TAG_SIZE 480
  188. #define POOL_ENTRY_SIZE 512
  189. static void clone_init(struct dm_crypt_io *, struct bio *);
  190. static void kcryptd_queue_crypt(struct dm_crypt_io *io);
  191. static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
  192. struct scatterlist *sg);
  193. /*
  194. * Use this to access cipher attributes that are independent of the key.
  195. */
  196. static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
  197. {
  198. return cc->cipher_tfm.tfms[0];
  199. }
  200. static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
  201. {
  202. return cc->cipher_tfm.tfms_aead[0];
  203. }
  204. /*
  205. * Different IV generation algorithms:
  206. *
  207. * plain: the initial vector is the 32-bit little-endian version of the sector
  208. * number, padded with zeros if necessary.
  209. *
  210. * plain64: the initial vector is the 64-bit little-endian version of the sector
  211. * number, padded with zeros if necessary.
  212. *
  213. * essiv: "encrypted sector|salt initial vector", the sector number is
  214. * encrypted with the bulk cipher using a salt as key. The salt
  215. * should be derived from the bulk cipher's key via hashing.
  216. *
  217. * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
  218. * (needed for LRW-32-AES and possible other narrow block modes)
  219. *
  220. * null: the initial vector is always zero. Provides compatibility with
  221. * obsolete loop_fish2 devices. Do not use for new devices.
  222. *
  223. * lmk: Compatible implementation of the block chaining mode used
  224. * by the Loop-AES block device encryption system
  225. * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
  226. * It operates on full 512 byte sectors and uses CBC
  227. * with an IV derived from the sector number, the data and
  228. * optionally extra IV seed.
  229. * This means that after decryption the first block
  230. * of sector must be tweaked according to decrypted data.
  231. * Loop-AES can use three encryption schemes:
  232. * version 1: is plain aes-cbc mode
  233. * version 2: uses 64 multikey scheme with lmk IV generator
  234. * version 3: the same as version 2 with additional IV seed
  235. * (it uses 65 keys, last key is used as IV seed)
  236. *
  237. * tcw: Compatible implementation of the block chaining mode used
  238. * by the TrueCrypt device encryption system (prior to version 4.1).
  239. * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
  240. * It operates on full 512 byte sectors and uses CBC
  241. * with an IV derived from initial key and the sector number.
  242. * In addition, whitening value is applied on every sector, whitening
  243. * is calculated from initial key, sector number and mixed using CRC32.
  244. * Note that this encryption scheme is vulnerable to watermarking attacks
  245. * and should be used for old compatible containers access only.
  246. *
  247. * plumb: unimplemented, see:
  248. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  249. */
  250. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
  251. struct dm_crypt_request *dmreq)
  252. {
  253. memset(iv, 0, cc->iv_size);
  254. *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
  255. return 0;
  256. }
  257. static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
  258. struct dm_crypt_request *dmreq)
  259. {
  260. memset(iv, 0, cc->iv_size);
  261. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  262. return 0;
  263. }
  264. /* Initialise ESSIV - compute salt but no local memory allocations */
  265. static int crypt_iv_essiv_init(struct crypt_config *cc)
  266. {
  267. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  268. AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
  269. struct scatterlist sg;
  270. struct crypto_cipher *essiv_tfm;
  271. int err;
  272. sg_init_one(&sg, cc->key, cc->key_size);
  273. ahash_request_set_tfm(req, essiv->hash_tfm);
  274. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  275. ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
  276. err = crypto_ahash_digest(req);
  277. ahash_request_zero(req);
  278. if (err)
  279. return err;
  280. essiv_tfm = cc->iv_private;
  281. err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
  282. crypto_ahash_digestsize(essiv->hash_tfm));
  283. if (err)
  284. return err;
  285. return 0;
  286. }
  287. /* Wipe salt and reset key derived from volume key */
  288. static int crypt_iv_essiv_wipe(struct crypt_config *cc)
  289. {
  290. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  291. unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
  292. struct crypto_cipher *essiv_tfm;
  293. int r, err = 0;
  294. memset(essiv->salt, 0, salt_size);
  295. essiv_tfm = cc->iv_private;
  296. r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
  297. if (r)
  298. err = r;
  299. return err;
  300. }
  301. /* Allocate the cipher for ESSIV */
  302. static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
  303. struct dm_target *ti,
  304. const u8 *salt,
  305. unsigned int saltsize)
  306. {
  307. struct crypto_cipher *essiv_tfm;
  308. int err;
  309. /* Setup the essiv_tfm with the given salt */
  310. essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  311. if (IS_ERR(essiv_tfm)) {
  312. ti->error = "Error allocating crypto tfm for ESSIV";
  313. return essiv_tfm;
  314. }
  315. if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
  316. ti->error = "Block size of ESSIV cipher does "
  317. "not match IV size of block cipher";
  318. crypto_free_cipher(essiv_tfm);
  319. return ERR_PTR(-EINVAL);
  320. }
  321. err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
  322. if (err) {
  323. ti->error = "Failed to set key for ESSIV cipher";
  324. crypto_free_cipher(essiv_tfm);
  325. return ERR_PTR(err);
  326. }
  327. return essiv_tfm;
  328. }
  329. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  330. {
  331. struct crypto_cipher *essiv_tfm;
  332. struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
  333. crypto_free_ahash(essiv->hash_tfm);
  334. essiv->hash_tfm = NULL;
  335. kzfree(essiv->salt);
  336. essiv->salt = NULL;
  337. essiv_tfm = cc->iv_private;
  338. if (essiv_tfm)
  339. crypto_free_cipher(essiv_tfm);
  340. cc->iv_private = NULL;
  341. }
  342. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  343. const char *opts)
  344. {
  345. struct crypto_cipher *essiv_tfm = NULL;
  346. struct crypto_ahash *hash_tfm = NULL;
  347. u8 *salt = NULL;
  348. int err;
  349. if (!opts) {
  350. ti->error = "Digest algorithm missing for ESSIV mode";
  351. return -EINVAL;
  352. }
  353. /* Allocate hash algorithm */
  354. hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
  355. if (IS_ERR(hash_tfm)) {
  356. ti->error = "Error initializing ESSIV hash";
  357. err = PTR_ERR(hash_tfm);
  358. goto bad;
  359. }
  360. salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
  361. if (!salt) {
  362. ti->error = "Error kmallocing salt storage in ESSIV";
  363. err = -ENOMEM;
  364. goto bad;
  365. }
  366. cc->iv_gen_private.essiv.salt = salt;
  367. cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
  368. essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
  369. crypto_ahash_digestsize(hash_tfm));
  370. if (IS_ERR(essiv_tfm)) {
  371. crypt_iv_essiv_dtr(cc);
  372. return PTR_ERR(essiv_tfm);
  373. }
  374. cc->iv_private = essiv_tfm;
  375. return 0;
  376. bad:
  377. if (hash_tfm && !IS_ERR(hash_tfm))
  378. crypto_free_ahash(hash_tfm);
  379. kfree(salt);
  380. return err;
  381. }
  382. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
  383. struct dm_crypt_request *dmreq)
  384. {
  385. struct crypto_cipher *essiv_tfm = cc->iv_private;
  386. memset(iv, 0, cc->iv_size);
  387. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  388. crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
  389. return 0;
  390. }
  391. static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
  392. const char *opts)
  393. {
  394. unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
  395. int log = ilog2(bs);
  396. /* we need to calculate how far we must shift the sector count
  397. * to get the cipher block count, we use this shift in _gen */
  398. if (1 << log != bs) {
  399. ti->error = "cypher blocksize is not a power of 2";
  400. return -EINVAL;
  401. }
  402. if (log > 9) {
  403. ti->error = "cypher blocksize is > 512";
  404. return -EINVAL;
  405. }
  406. cc->iv_gen_private.benbi.shift = 9 - log;
  407. return 0;
  408. }
  409. static void crypt_iv_benbi_dtr(struct crypt_config *cc)
  410. {
  411. }
  412. static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
  413. struct dm_crypt_request *dmreq)
  414. {
  415. __be64 val;
  416. memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
  417. val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
  418. put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
  419. return 0;
  420. }
  421. static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
  422. struct dm_crypt_request *dmreq)
  423. {
  424. memset(iv, 0, cc->iv_size);
  425. return 0;
  426. }
  427. static void crypt_iv_lmk_dtr(struct crypt_config *cc)
  428. {
  429. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  430. if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
  431. crypto_free_shash(lmk->hash_tfm);
  432. lmk->hash_tfm = NULL;
  433. kzfree(lmk->seed);
  434. lmk->seed = NULL;
  435. }
  436. static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
  437. const char *opts)
  438. {
  439. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  440. if (cc->sector_size != (1 << SECTOR_SHIFT)) {
  441. ti->error = "Unsupported sector size for LMK";
  442. return -EINVAL;
  443. }
  444. lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
  445. if (IS_ERR(lmk->hash_tfm)) {
  446. ti->error = "Error initializing LMK hash";
  447. return PTR_ERR(lmk->hash_tfm);
  448. }
  449. /* No seed in LMK version 2 */
  450. if (cc->key_parts == cc->tfms_count) {
  451. lmk->seed = NULL;
  452. return 0;
  453. }
  454. lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
  455. if (!lmk->seed) {
  456. crypt_iv_lmk_dtr(cc);
  457. ti->error = "Error kmallocing seed storage in LMK";
  458. return -ENOMEM;
  459. }
  460. return 0;
  461. }
  462. static int crypt_iv_lmk_init(struct crypt_config *cc)
  463. {
  464. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  465. int subkey_size = cc->key_size / cc->key_parts;
  466. /* LMK seed is on the position of LMK_KEYS + 1 key */
  467. if (lmk->seed)
  468. memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
  469. crypto_shash_digestsize(lmk->hash_tfm));
  470. return 0;
  471. }
  472. static int crypt_iv_lmk_wipe(struct crypt_config *cc)
  473. {
  474. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  475. if (lmk->seed)
  476. memset(lmk->seed, 0, LMK_SEED_SIZE);
  477. return 0;
  478. }
  479. static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
  480. struct dm_crypt_request *dmreq,
  481. u8 *data)
  482. {
  483. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  484. SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
  485. struct md5_state md5state;
  486. __le32 buf[4];
  487. int i, r;
  488. desc->tfm = lmk->hash_tfm;
  489. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  490. r = crypto_shash_init(desc);
  491. if (r)
  492. return r;
  493. if (lmk->seed) {
  494. r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
  495. if (r)
  496. return r;
  497. }
  498. /* Sector is always 512B, block size 16, add data of blocks 1-31 */
  499. r = crypto_shash_update(desc, data + 16, 16 * 31);
  500. if (r)
  501. return r;
  502. /* Sector is cropped to 56 bits here */
  503. buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
  504. buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
  505. buf[2] = cpu_to_le32(4024);
  506. buf[3] = 0;
  507. r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
  508. if (r)
  509. return r;
  510. /* No MD5 padding here */
  511. r = crypto_shash_export(desc, &md5state);
  512. if (r)
  513. return r;
  514. for (i = 0; i < MD5_HASH_WORDS; i++)
  515. __cpu_to_le32s(&md5state.hash[i]);
  516. memcpy(iv, &md5state.hash, cc->iv_size);
  517. return 0;
  518. }
  519. static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
  520. struct dm_crypt_request *dmreq)
  521. {
  522. struct scatterlist *sg;
  523. u8 *src;
  524. int r = 0;
  525. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
  526. sg = crypt_get_sg_data(cc, dmreq->sg_in);
  527. src = kmap_atomic(sg_page(sg));
  528. r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
  529. kunmap_atomic(src);
  530. } else
  531. memset(iv, 0, cc->iv_size);
  532. return r;
  533. }
  534. static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
  535. struct dm_crypt_request *dmreq)
  536. {
  537. struct scatterlist *sg;
  538. u8 *dst;
  539. int r;
  540. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
  541. return 0;
  542. sg = crypt_get_sg_data(cc, dmreq->sg_out);
  543. dst = kmap_atomic(sg_page(sg));
  544. r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
  545. /* Tweak the first block of plaintext sector */
  546. if (!r)
  547. crypto_xor(dst + sg->offset, iv, cc->iv_size);
  548. kunmap_atomic(dst);
  549. return r;
  550. }
  551. static void crypt_iv_tcw_dtr(struct crypt_config *cc)
  552. {
  553. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  554. kzfree(tcw->iv_seed);
  555. tcw->iv_seed = NULL;
  556. kzfree(tcw->whitening);
  557. tcw->whitening = NULL;
  558. if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
  559. crypto_free_shash(tcw->crc32_tfm);
  560. tcw->crc32_tfm = NULL;
  561. }
  562. static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
  563. const char *opts)
  564. {
  565. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  566. if (cc->sector_size != (1 << SECTOR_SHIFT)) {
  567. ti->error = "Unsupported sector size for TCW";
  568. return -EINVAL;
  569. }
  570. if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
  571. ti->error = "Wrong key size for TCW";
  572. return -EINVAL;
  573. }
  574. tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
  575. if (IS_ERR(tcw->crc32_tfm)) {
  576. ti->error = "Error initializing CRC32 in TCW";
  577. return PTR_ERR(tcw->crc32_tfm);
  578. }
  579. tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
  580. tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
  581. if (!tcw->iv_seed || !tcw->whitening) {
  582. crypt_iv_tcw_dtr(cc);
  583. ti->error = "Error allocating seed storage in TCW";
  584. return -ENOMEM;
  585. }
  586. return 0;
  587. }
  588. static int crypt_iv_tcw_init(struct crypt_config *cc)
  589. {
  590. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  591. int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
  592. memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
  593. memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
  594. TCW_WHITENING_SIZE);
  595. return 0;
  596. }
  597. static int crypt_iv_tcw_wipe(struct crypt_config *cc)
  598. {
  599. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  600. memset(tcw->iv_seed, 0, cc->iv_size);
  601. memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
  602. return 0;
  603. }
  604. static int crypt_iv_tcw_whitening(struct crypt_config *cc,
  605. struct dm_crypt_request *dmreq,
  606. u8 *data)
  607. {
  608. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  609. __le64 sector = cpu_to_le64(dmreq->iv_sector);
  610. u8 buf[TCW_WHITENING_SIZE];
  611. SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
  612. int i, r;
  613. /* xor whitening with sector number */
  614. memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
  615. crypto_xor(buf, (u8 *)&sector, 8);
  616. crypto_xor(&buf[8], (u8 *)&sector, 8);
  617. /* calculate crc32 for every 32bit part and xor it */
  618. desc->tfm = tcw->crc32_tfm;
  619. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  620. for (i = 0; i < 4; i++) {
  621. r = crypto_shash_init(desc);
  622. if (r)
  623. goto out;
  624. r = crypto_shash_update(desc, &buf[i * 4], 4);
  625. if (r)
  626. goto out;
  627. r = crypto_shash_final(desc, &buf[i * 4]);
  628. if (r)
  629. goto out;
  630. }
  631. crypto_xor(&buf[0], &buf[12], 4);
  632. crypto_xor(&buf[4], &buf[8], 4);
  633. /* apply whitening (8 bytes) to whole sector */
  634. for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
  635. crypto_xor(data + i * 8, buf, 8);
  636. out:
  637. memzero_explicit(buf, sizeof(buf));
  638. return r;
  639. }
  640. static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
  641. struct dm_crypt_request *dmreq)
  642. {
  643. struct scatterlist *sg;
  644. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  645. __le64 sector = cpu_to_le64(dmreq->iv_sector);
  646. u8 *src;
  647. int r = 0;
  648. /* Remove whitening from ciphertext */
  649. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
  650. sg = crypt_get_sg_data(cc, dmreq->sg_in);
  651. src = kmap_atomic(sg_page(sg));
  652. r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
  653. kunmap_atomic(src);
  654. }
  655. /* Calculate IV */
  656. memcpy(iv, tcw->iv_seed, cc->iv_size);
  657. crypto_xor(iv, (u8 *)&sector, 8);
  658. if (cc->iv_size > 8)
  659. crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
  660. return r;
  661. }
  662. static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
  663. struct dm_crypt_request *dmreq)
  664. {
  665. struct scatterlist *sg;
  666. u8 *dst;
  667. int r;
  668. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
  669. return 0;
  670. /* Apply whitening on ciphertext */
  671. sg = crypt_get_sg_data(cc, dmreq->sg_out);
  672. dst = kmap_atomic(sg_page(sg));
  673. r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
  674. kunmap_atomic(dst);
  675. return r;
  676. }
  677. static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
  678. struct dm_crypt_request *dmreq)
  679. {
  680. /* Used only for writes, there must be an additional space to store IV */
  681. get_random_bytes(iv, cc->iv_size);
  682. return 0;
  683. }
  684. static const struct crypt_iv_operations crypt_iv_plain_ops = {
  685. .generator = crypt_iv_plain_gen
  686. };
  687. static const struct crypt_iv_operations crypt_iv_plain64_ops = {
  688. .generator = crypt_iv_plain64_gen
  689. };
  690. static const struct crypt_iv_operations crypt_iv_essiv_ops = {
  691. .ctr = crypt_iv_essiv_ctr,
  692. .dtr = crypt_iv_essiv_dtr,
  693. .init = crypt_iv_essiv_init,
  694. .wipe = crypt_iv_essiv_wipe,
  695. .generator = crypt_iv_essiv_gen
  696. };
  697. static const struct crypt_iv_operations crypt_iv_benbi_ops = {
  698. .ctr = crypt_iv_benbi_ctr,
  699. .dtr = crypt_iv_benbi_dtr,
  700. .generator = crypt_iv_benbi_gen
  701. };
  702. static const struct crypt_iv_operations crypt_iv_null_ops = {
  703. .generator = crypt_iv_null_gen
  704. };
  705. static const struct crypt_iv_operations crypt_iv_lmk_ops = {
  706. .ctr = crypt_iv_lmk_ctr,
  707. .dtr = crypt_iv_lmk_dtr,
  708. .init = crypt_iv_lmk_init,
  709. .wipe = crypt_iv_lmk_wipe,
  710. .generator = crypt_iv_lmk_gen,
  711. .post = crypt_iv_lmk_post
  712. };
  713. static const struct crypt_iv_operations crypt_iv_tcw_ops = {
  714. .ctr = crypt_iv_tcw_ctr,
  715. .dtr = crypt_iv_tcw_dtr,
  716. .init = crypt_iv_tcw_init,
  717. .wipe = crypt_iv_tcw_wipe,
  718. .generator = crypt_iv_tcw_gen,
  719. .post = crypt_iv_tcw_post
  720. };
  721. static struct crypt_iv_operations crypt_iv_random_ops = {
  722. .generator = crypt_iv_random_gen
  723. };
  724. /*
  725. * Integrity extensions
  726. */
  727. static bool crypt_integrity_aead(struct crypt_config *cc)
  728. {
  729. return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
  730. }
  731. static bool crypt_integrity_hmac(struct crypt_config *cc)
  732. {
  733. return crypt_integrity_aead(cc) && cc->key_mac_size;
  734. }
  735. /* Get sg containing data */
  736. static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
  737. struct scatterlist *sg)
  738. {
  739. if (unlikely(crypt_integrity_aead(cc)))
  740. return &sg[2];
  741. return sg;
  742. }
  743. static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
  744. {
  745. struct bio_integrity_payload *bip;
  746. unsigned int tag_len;
  747. int ret;
  748. if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
  749. return 0;
  750. bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
  751. if (IS_ERR(bip))
  752. return PTR_ERR(bip);
  753. tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
  754. bip->bip_iter.bi_size = tag_len;
  755. bip->bip_iter.bi_sector = io->cc->start + io->sector;
  756. /* We own the metadata, do not let bio_free to release it */
  757. bip->bip_flags &= ~BIP_BLOCK_INTEGRITY;
  758. ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
  759. tag_len, offset_in_page(io->integrity_metadata));
  760. if (unlikely(ret != tag_len))
  761. return -ENOMEM;
  762. return 0;
  763. }
  764. static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
  765. {
  766. #ifdef CONFIG_BLK_DEV_INTEGRITY
  767. struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
  768. /* From now we require underlying device with our integrity profile */
  769. if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
  770. ti->error = "Integrity profile not supported.";
  771. return -EINVAL;
  772. }
  773. if (bi->tag_size != cc->on_disk_tag_size ||
  774. bi->tuple_size != cc->on_disk_tag_size) {
  775. ti->error = "Integrity profile tag size mismatch.";
  776. return -EINVAL;
  777. }
  778. if (1 << bi->interval_exp != cc->sector_size) {
  779. ti->error = "Integrity profile sector size mismatch.";
  780. return -EINVAL;
  781. }
  782. if (crypt_integrity_aead(cc)) {
  783. cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
  784. DMINFO("Integrity AEAD, tag size %u, IV size %u.",
  785. cc->integrity_tag_size, cc->integrity_iv_size);
  786. if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
  787. ti->error = "Integrity AEAD auth tag size is not supported.";
  788. return -EINVAL;
  789. }
  790. } else if (cc->integrity_iv_size)
  791. DMINFO("Additional per-sector space %u bytes for IV.",
  792. cc->integrity_iv_size);
  793. if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
  794. ti->error = "Not enough space for integrity tag in the profile.";
  795. return -EINVAL;
  796. }
  797. return 0;
  798. #else
  799. ti->error = "Integrity profile not supported.";
  800. return -EINVAL;
  801. #endif
  802. }
  803. static void crypt_convert_init(struct crypt_config *cc,
  804. struct convert_context *ctx,
  805. struct bio *bio_out, struct bio *bio_in,
  806. sector_t sector)
  807. {
  808. ctx->bio_in = bio_in;
  809. ctx->bio_out = bio_out;
  810. if (bio_in)
  811. ctx->iter_in = bio_in->bi_iter;
  812. if (bio_out)
  813. ctx->iter_out = bio_out->bi_iter;
  814. ctx->cc_sector = sector + cc->iv_offset;
  815. init_completion(&ctx->restart);
  816. }
  817. static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
  818. void *req)
  819. {
  820. return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
  821. }
  822. static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
  823. {
  824. return (void *)((char *)dmreq - cc->dmreq_start);
  825. }
  826. static u8 *iv_of_dmreq(struct crypt_config *cc,
  827. struct dm_crypt_request *dmreq)
  828. {
  829. if (crypt_integrity_aead(cc))
  830. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  831. crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
  832. else
  833. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  834. crypto_skcipher_alignmask(any_tfm(cc)) + 1);
  835. }
  836. static u8 *org_iv_of_dmreq(struct crypt_config *cc,
  837. struct dm_crypt_request *dmreq)
  838. {
  839. return iv_of_dmreq(cc, dmreq) + cc->iv_size;
  840. }
  841. static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
  842. struct dm_crypt_request *dmreq)
  843. {
  844. u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
  845. return (uint64_t*) ptr;
  846. }
  847. static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
  848. struct dm_crypt_request *dmreq)
  849. {
  850. u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
  851. cc->iv_size + sizeof(uint64_t);
  852. return (unsigned int*)ptr;
  853. }
  854. static void *tag_from_dmreq(struct crypt_config *cc,
  855. struct dm_crypt_request *dmreq)
  856. {
  857. struct convert_context *ctx = dmreq->ctx;
  858. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  859. return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
  860. cc->on_disk_tag_size];
  861. }
  862. static void *iv_tag_from_dmreq(struct crypt_config *cc,
  863. struct dm_crypt_request *dmreq)
  864. {
  865. return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
  866. }
  867. static int crypt_convert_block_aead(struct crypt_config *cc,
  868. struct convert_context *ctx,
  869. struct aead_request *req,
  870. unsigned int tag_offset)
  871. {
  872. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  873. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  874. struct dm_crypt_request *dmreq;
  875. u8 *iv, *org_iv, *tag_iv, *tag;
  876. uint64_t *sector;
  877. int r = 0;
  878. BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
  879. /* Reject unexpected unaligned bio. */
  880. if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
  881. return -EIO;
  882. dmreq = dmreq_of_req(cc, req);
  883. dmreq->iv_sector = ctx->cc_sector;
  884. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  885. dmreq->iv_sector >>= cc->sector_shift;
  886. dmreq->ctx = ctx;
  887. *org_tag_of_dmreq(cc, dmreq) = tag_offset;
  888. sector = org_sector_of_dmreq(cc, dmreq);
  889. *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
  890. iv = iv_of_dmreq(cc, dmreq);
  891. org_iv = org_iv_of_dmreq(cc, dmreq);
  892. tag = tag_from_dmreq(cc, dmreq);
  893. tag_iv = iv_tag_from_dmreq(cc, dmreq);
  894. /* AEAD request:
  895. * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
  896. * | (authenticated) | (auth+encryption) | |
  897. * | sector_LE | IV | sector in/out | tag in/out |
  898. */
  899. sg_init_table(dmreq->sg_in, 4);
  900. sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
  901. sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
  902. sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
  903. sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
  904. sg_init_table(dmreq->sg_out, 4);
  905. sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
  906. sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
  907. sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
  908. sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
  909. if (cc->iv_gen_ops) {
  910. /* For READs use IV stored in integrity metadata */
  911. if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
  912. memcpy(org_iv, tag_iv, cc->iv_size);
  913. } else {
  914. r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
  915. if (r < 0)
  916. return r;
  917. /* Store generated IV in integrity metadata */
  918. if (cc->integrity_iv_size)
  919. memcpy(tag_iv, org_iv, cc->iv_size);
  920. }
  921. /* Working copy of IV, to be modified in crypto API */
  922. memcpy(iv, org_iv, cc->iv_size);
  923. }
  924. aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
  925. if (bio_data_dir(ctx->bio_in) == WRITE) {
  926. aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
  927. cc->sector_size, iv);
  928. r = crypto_aead_encrypt(req);
  929. if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
  930. memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
  931. cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
  932. } else {
  933. aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
  934. cc->sector_size + cc->integrity_tag_size, iv);
  935. r = crypto_aead_decrypt(req);
  936. }
  937. if (r == -EBADMSG)
  938. DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
  939. (unsigned long long)le64_to_cpu(*sector));
  940. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  941. r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
  942. bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
  943. bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
  944. return r;
  945. }
  946. static int crypt_convert_block_skcipher(struct crypt_config *cc,
  947. struct convert_context *ctx,
  948. struct skcipher_request *req,
  949. unsigned int tag_offset)
  950. {
  951. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  952. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  953. struct scatterlist *sg_in, *sg_out;
  954. struct dm_crypt_request *dmreq;
  955. u8 *iv, *org_iv, *tag_iv;
  956. uint64_t *sector;
  957. int r = 0;
  958. /* Reject unexpected unaligned bio. */
  959. if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
  960. return -EIO;
  961. dmreq = dmreq_of_req(cc, req);
  962. dmreq->iv_sector = ctx->cc_sector;
  963. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  964. dmreq->iv_sector >>= cc->sector_shift;
  965. dmreq->ctx = ctx;
  966. *org_tag_of_dmreq(cc, dmreq) = tag_offset;
  967. iv = iv_of_dmreq(cc, dmreq);
  968. org_iv = org_iv_of_dmreq(cc, dmreq);
  969. tag_iv = iv_tag_from_dmreq(cc, dmreq);
  970. sector = org_sector_of_dmreq(cc, dmreq);
  971. *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
  972. /* For skcipher we use only the first sg item */
  973. sg_in = &dmreq->sg_in[0];
  974. sg_out = &dmreq->sg_out[0];
  975. sg_init_table(sg_in, 1);
  976. sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
  977. sg_init_table(sg_out, 1);
  978. sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
  979. if (cc->iv_gen_ops) {
  980. /* For READs use IV stored in integrity metadata */
  981. if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
  982. memcpy(org_iv, tag_iv, cc->integrity_iv_size);
  983. } else {
  984. r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
  985. if (r < 0)
  986. return r;
  987. /* Store generated IV in integrity metadata */
  988. if (cc->integrity_iv_size)
  989. memcpy(tag_iv, org_iv, cc->integrity_iv_size);
  990. }
  991. /* Working copy of IV, to be modified in crypto API */
  992. memcpy(iv, org_iv, cc->iv_size);
  993. }
  994. skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
  995. if (bio_data_dir(ctx->bio_in) == WRITE)
  996. r = crypto_skcipher_encrypt(req);
  997. else
  998. r = crypto_skcipher_decrypt(req);
  999. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  1000. r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
  1001. bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
  1002. bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
  1003. return r;
  1004. }
  1005. static void kcryptd_async_done(struct crypto_async_request *async_req,
  1006. int error);
  1007. static void crypt_alloc_req_skcipher(struct crypt_config *cc,
  1008. struct convert_context *ctx)
  1009. {
  1010. unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
  1011. if (!ctx->r.req)
  1012. ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
  1013. skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
  1014. /*
  1015. * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
  1016. * requests if driver request queue is full.
  1017. */
  1018. skcipher_request_set_callback(ctx->r.req,
  1019. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  1020. kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
  1021. }
  1022. static void crypt_alloc_req_aead(struct crypt_config *cc,
  1023. struct convert_context *ctx)
  1024. {
  1025. if (!ctx->r.req_aead)
  1026. ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
  1027. aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
  1028. /*
  1029. * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
  1030. * requests if driver request queue is full.
  1031. */
  1032. aead_request_set_callback(ctx->r.req_aead,
  1033. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  1034. kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
  1035. }
  1036. static void crypt_alloc_req(struct crypt_config *cc,
  1037. struct convert_context *ctx)
  1038. {
  1039. if (crypt_integrity_aead(cc))
  1040. crypt_alloc_req_aead(cc, ctx);
  1041. else
  1042. crypt_alloc_req_skcipher(cc, ctx);
  1043. }
  1044. static void crypt_free_req_skcipher(struct crypt_config *cc,
  1045. struct skcipher_request *req, struct bio *base_bio)
  1046. {
  1047. struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
  1048. if ((struct skcipher_request *)(io + 1) != req)
  1049. mempool_free(req, cc->req_pool);
  1050. }
  1051. static void crypt_free_req_aead(struct crypt_config *cc,
  1052. struct aead_request *req, struct bio *base_bio)
  1053. {
  1054. struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
  1055. if ((struct aead_request *)(io + 1) != req)
  1056. mempool_free(req, cc->req_pool);
  1057. }
  1058. static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
  1059. {
  1060. if (crypt_integrity_aead(cc))
  1061. crypt_free_req_aead(cc, req, base_bio);
  1062. else
  1063. crypt_free_req_skcipher(cc, req, base_bio);
  1064. }
  1065. /*
  1066. * Encrypt / decrypt data from one bio to another one (can be the same one)
  1067. */
  1068. static int crypt_convert(struct crypt_config *cc,
  1069. struct convert_context *ctx)
  1070. {
  1071. unsigned int tag_offset = 0;
  1072. unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
  1073. int r;
  1074. atomic_set(&ctx->cc_pending, 1);
  1075. while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
  1076. crypt_alloc_req(cc, ctx);
  1077. atomic_inc(&ctx->cc_pending);
  1078. if (crypt_integrity_aead(cc))
  1079. r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
  1080. else
  1081. r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
  1082. switch (r) {
  1083. /*
  1084. * The request was queued by a crypto driver
  1085. * but the driver request queue is full, let's wait.
  1086. */
  1087. case -EBUSY:
  1088. wait_for_completion(&ctx->restart);
  1089. reinit_completion(&ctx->restart);
  1090. /* fall through */
  1091. /*
  1092. * The request is queued and processed asynchronously,
  1093. * completion function kcryptd_async_done() will be called.
  1094. */
  1095. case -EINPROGRESS:
  1096. ctx->r.req = NULL;
  1097. ctx->cc_sector += sector_step;
  1098. tag_offset++;
  1099. continue;
  1100. /*
  1101. * The request was already processed (synchronously).
  1102. */
  1103. case 0:
  1104. atomic_dec(&ctx->cc_pending);
  1105. ctx->cc_sector += sector_step;
  1106. tag_offset++;
  1107. cond_resched();
  1108. continue;
  1109. /*
  1110. * There was a data integrity error.
  1111. */
  1112. case -EBADMSG:
  1113. atomic_dec(&ctx->cc_pending);
  1114. return -EILSEQ;
  1115. /*
  1116. * There was an error while processing the request.
  1117. */
  1118. default:
  1119. atomic_dec(&ctx->cc_pending);
  1120. return -EIO;
  1121. }
  1122. }
  1123. return 0;
  1124. }
  1125. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
  1126. /*
  1127. * Generate a new unfragmented bio with the given size
  1128. * This should never violate the device limitations (but only because
  1129. * max_segment_size is being constrained to PAGE_SIZE).
  1130. *
  1131. * This function may be called concurrently. If we allocate from the mempool
  1132. * concurrently, there is a possibility of deadlock. For example, if we have
  1133. * mempool of 256 pages, two processes, each wanting 256, pages allocate from
  1134. * the mempool concurrently, it may deadlock in a situation where both processes
  1135. * have allocated 128 pages and the mempool is exhausted.
  1136. *
  1137. * In order to avoid this scenario we allocate the pages under a mutex.
  1138. *
  1139. * In order to not degrade performance with excessive locking, we try
  1140. * non-blocking allocations without a mutex first but on failure we fallback
  1141. * to blocking allocations with a mutex.
  1142. */
  1143. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
  1144. {
  1145. struct crypt_config *cc = io->cc;
  1146. struct bio *clone;
  1147. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1148. gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
  1149. unsigned i, len, remaining_size;
  1150. struct page *page;
  1151. retry:
  1152. if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
  1153. mutex_lock(&cc->bio_alloc_lock);
  1154. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  1155. if (!clone)
  1156. goto out;
  1157. clone_init(io, clone);
  1158. remaining_size = size;
  1159. for (i = 0; i < nr_iovecs; i++) {
  1160. page = mempool_alloc(cc->page_pool, gfp_mask);
  1161. if (!page) {
  1162. crypt_free_buffer_pages(cc, clone);
  1163. bio_put(clone);
  1164. gfp_mask |= __GFP_DIRECT_RECLAIM;
  1165. goto retry;
  1166. }
  1167. len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
  1168. bio_add_page(clone, page, len, 0);
  1169. remaining_size -= len;
  1170. }
  1171. /* Allocate space for integrity tags */
  1172. if (dm_crypt_integrity_io_alloc(io, clone)) {
  1173. crypt_free_buffer_pages(cc, clone);
  1174. bio_put(clone);
  1175. clone = NULL;
  1176. }
  1177. out:
  1178. if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
  1179. mutex_unlock(&cc->bio_alloc_lock);
  1180. return clone;
  1181. }
  1182. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  1183. {
  1184. unsigned int i;
  1185. struct bio_vec *bv;
  1186. bio_for_each_segment_all(bv, clone, i) {
  1187. BUG_ON(!bv->bv_page);
  1188. mempool_free(bv->bv_page, cc->page_pool);
  1189. bv->bv_page = NULL;
  1190. }
  1191. }
  1192. static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
  1193. struct bio *bio, sector_t sector)
  1194. {
  1195. io->cc = cc;
  1196. io->base_bio = bio;
  1197. io->sector = sector;
  1198. io->error = 0;
  1199. io->ctx.r.req = NULL;
  1200. io->integrity_metadata = NULL;
  1201. io->integrity_metadata_from_pool = false;
  1202. atomic_set(&io->io_pending, 0);
  1203. }
  1204. static void crypt_inc_pending(struct dm_crypt_io *io)
  1205. {
  1206. atomic_inc(&io->io_pending);
  1207. }
  1208. /*
  1209. * One of the bios was finished. Check for completion of
  1210. * the whole request and correctly clean up the buffer.
  1211. */
  1212. static void crypt_dec_pending(struct dm_crypt_io *io)
  1213. {
  1214. struct crypt_config *cc = io->cc;
  1215. struct bio *base_bio = io->base_bio;
  1216. int error = io->error;
  1217. if (!atomic_dec_and_test(&io->io_pending))
  1218. return;
  1219. if (io->ctx.r.req)
  1220. crypt_free_req(cc, io->ctx.r.req, base_bio);
  1221. if (unlikely(io->integrity_metadata_from_pool))
  1222. mempool_free(io->integrity_metadata, io->cc->tag_pool);
  1223. else
  1224. kfree(io->integrity_metadata);
  1225. base_bio->bi_error = error;
  1226. bio_endio(base_bio);
  1227. }
  1228. /*
  1229. * kcryptd/kcryptd_io:
  1230. *
  1231. * Needed because it would be very unwise to do decryption in an
  1232. * interrupt context.
  1233. *
  1234. * kcryptd performs the actual encryption or decryption.
  1235. *
  1236. * kcryptd_io performs the IO submission.
  1237. *
  1238. * They must be separated as otherwise the final stages could be
  1239. * starved by new requests which can block in the first stages due
  1240. * to memory allocation.
  1241. *
  1242. * The work is done per CPU global for all dm-crypt instances.
  1243. * They should not depend on each other and do not block.
  1244. */
  1245. static void crypt_endio(struct bio *clone)
  1246. {
  1247. struct dm_crypt_io *io = clone->bi_private;
  1248. struct crypt_config *cc = io->cc;
  1249. unsigned rw = bio_data_dir(clone);
  1250. int error;
  1251. /*
  1252. * free the processed pages
  1253. */
  1254. if (rw == WRITE)
  1255. crypt_free_buffer_pages(cc, clone);
  1256. error = clone->bi_error;
  1257. bio_put(clone);
  1258. if (rw == READ && !error) {
  1259. kcryptd_queue_crypt(io);
  1260. return;
  1261. }
  1262. if (unlikely(error))
  1263. io->error = error;
  1264. crypt_dec_pending(io);
  1265. }
  1266. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  1267. {
  1268. struct crypt_config *cc = io->cc;
  1269. clone->bi_private = io;
  1270. clone->bi_end_io = crypt_endio;
  1271. clone->bi_bdev = cc->dev->bdev;
  1272. clone->bi_opf = io->base_bio->bi_opf;
  1273. }
  1274. static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
  1275. {
  1276. struct crypt_config *cc = io->cc;
  1277. struct bio *clone;
  1278. /*
  1279. * We need the original biovec array in order to decrypt
  1280. * the whole bio data *afterwards* -- thanks to immutable
  1281. * biovecs we don't need to worry about the block layer
  1282. * modifying the biovec array; so leverage bio_clone_fast().
  1283. */
  1284. clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
  1285. if (!clone)
  1286. return 1;
  1287. crypt_inc_pending(io);
  1288. clone_init(io, clone);
  1289. clone->bi_iter.bi_sector = cc->start + io->sector;
  1290. if (dm_crypt_integrity_io_alloc(io, clone)) {
  1291. crypt_dec_pending(io);
  1292. bio_put(clone);
  1293. return 1;
  1294. }
  1295. generic_make_request(clone);
  1296. return 0;
  1297. }
  1298. static void kcryptd_io_read_work(struct work_struct *work)
  1299. {
  1300. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1301. crypt_inc_pending(io);
  1302. if (kcryptd_io_read(io, GFP_NOIO))
  1303. io->error = -ENOMEM;
  1304. crypt_dec_pending(io);
  1305. }
  1306. static void kcryptd_queue_read(struct dm_crypt_io *io)
  1307. {
  1308. struct crypt_config *cc = io->cc;
  1309. INIT_WORK(&io->work, kcryptd_io_read_work);
  1310. queue_work(cc->io_queue, &io->work);
  1311. }
  1312. static void kcryptd_io_write(struct dm_crypt_io *io)
  1313. {
  1314. struct bio *clone = io->ctx.bio_out;
  1315. generic_make_request(clone);
  1316. }
  1317. #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
  1318. static int dmcrypt_write(void *data)
  1319. {
  1320. struct crypt_config *cc = data;
  1321. struct dm_crypt_io *io;
  1322. while (1) {
  1323. struct rb_root write_tree;
  1324. struct blk_plug plug;
  1325. DECLARE_WAITQUEUE(wait, current);
  1326. spin_lock_irq(&cc->write_thread_wait.lock);
  1327. continue_locked:
  1328. if (!RB_EMPTY_ROOT(&cc->write_tree))
  1329. goto pop_from_list;
  1330. set_current_state(TASK_INTERRUPTIBLE);
  1331. __add_wait_queue(&cc->write_thread_wait, &wait);
  1332. spin_unlock_irq(&cc->write_thread_wait.lock);
  1333. if (unlikely(kthread_should_stop())) {
  1334. set_current_state(TASK_RUNNING);
  1335. remove_wait_queue(&cc->write_thread_wait, &wait);
  1336. break;
  1337. }
  1338. schedule();
  1339. set_current_state(TASK_RUNNING);
  1340. spin_lock_irq(&cc->write_thread_wait.lock);
  1341. __remove_wait_queue(&cc->write_thread_wait, &wait);
  1342. goto continue_locked;
  1343. pop_from_list:
  1344. write_tree = cc->write_tree;
  1345. cc->write_tree = RB_ROOT;
  1346. spin_unlock_irq(&cc->write_thread_wait.lock);
  1347. BUG_ON(rb_parent(write_tree.rb_node));
  1348. /*
  1349. * Note: we cannot walk the tree here with rb_next because
  1350. * the structures may be freed when kcryptd_io_write is called.
  1351. */
  1352. blk_start_plug(&plug);
  1353. do {
  1354. io = crypt_io_from_node(rb_first(&write_tree));
  1355. rb_erase(&io->rb_node, &write_tree);
  1356. kcryptd_io_write(io);
  1357. } while (!RB_EMPTY_ROOT(&write_tree));
  1358. blk_finish_plug(&plug);
  1359. }
  1360. return 0;
  1361. }
  1362. static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
  1363. {
  1364. struct bio *clone = io->ctx.bio_out;
  1365. struct crypt_config *cc = io->cc;
  1366. unsigned long flags;
  1367. sector_t sector;
  1368. struct rb_node **rbp, *parent;
  1369. if (unlikely(io->error < 0)) {
  1370. crypt_free_buffer_pages(cc, clone);
  1371. bio_put(clone);
  1372. crypt_dec_pending(io);
  1373. return;
  1374. }
  1375. /* crypt_convert should have filled the clone bio */
  1376. BUG_ON(io->ctx.iter_out.bi_size);
  1377. clone->bi_iter.bi_sector = cc->start + io->sector;
  1378. if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
  1379. generic_make_request(clone);
  1380. return;
  1381. }
  1382. spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
  1383. rbp = &cc->write_tree.rb_node;
  1384. parent = NULL;
  1385. sector = io->sector;
  1386. while (*rbp) {
  1387. parent = *rbp;
  1388. if (sector < crypt_io_from_node(parent)->sector)
  1389. rbp = &(*rbp)->rb_left;
  1390. else
  1391. rbp = &(*rbp)->rb_right;
  1392. }
  1393. rb_link_node(&io->rb_node, parent, rbp);
  1394. rb_insert_color(&io->rb_node, &cc->write_tree);
  1395. wake_up_locked(&cc->write_thread_wait);
  1396. spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
  1397. }
  1398. static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
  1399. {
  1400. struct crypt_config *cc = io->cc;
  1401. struct bio *clone;
  1402. int crypt_finished;
  1403. sector_t sector = io->sector;
  1404. int r;
  1405. /*
  1406. * Prevent io from disappearing until this function completes.
  1407. */
  1408. crypt_inc_pending(io);
  1409. crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
  1410. clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
  1411. if (unlikely(!clone)) {
  1412. io->error = -EIO;
  1413. goto dec;
  1414. }
  1415. io->ctx.bio_out = clone;
  1416. io->ctx.iter_out = clone->bi_iter;
  1417. sector += bio_sectors(clone);
  1418. crypt_inc_pending(io);
  1419. r = crypt_convert(cc, &io->ctx);
  1420. if (r < 0)
  1421. io->error = r;
  1422. crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
  1423. /* Encryption was already finished, submit io now */
  1424. if (crypt_finished) {
  1425. kcryptd_crypt_write_io_submit(io, 0);
  1426. io->sector = sector;
  1427. }
  1428. dec:
  1429. crypt_dec_pending(io);
  1430. }
  1431. static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
  1432. {
  1433. crypt_dec_pending(io);
  1434. }
  1435. static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
  1436. {
  1437. struct crypt_config *cc = io->cc;
  1438. int r = 0;
  1439. crypt_inc_pending(io);
  1440. crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
  1441. io->sector);
  1442. r = crypt_convert(cc, &io->ctx);
  1443. if (r < 0)
  1444. io->error = r;
  1445. if (atomic_dec_and_test(&io->ctx.cc_pending))
  1446. kcryptd_crypt_read_done(io);
  1447. crypt_dec_pending(io);
  1448. }
  1449. static void kcryptd_async_done(struct crypto_async_request *async_req,
  1450. int error)
  1451. {
  1452. struct dm_crypt_request *dmreq = async_req->data;
  1453. struct convert_context *ctx = dmreq->ctx;
  1454. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  1455. struct crypt_config *cc = io->cc;
  1456. /*
  1457. * A request from crypto driver backlog is going to be processed now,
  1458. * finish the completion and continue in crypt_convert().
  1459. * (Callback will be called for the second time for this request.)
  1460. */
  1461. if (error == -EINPROGRESS) {
  1462. complete(&ctx->restart);
  1463. return;
  1464. }
  1465. if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
  1466. error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
  1467. if (error == -EBADMSG) {
  1468. DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
  1469. (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
  1470. io->error = -EILSEQ;
  1471. } else if (error < 0)
  1472. io->error = -EIO;
  1473. crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
  1474. if (!atomic_dec_and_test(&ctx->cc_pending))
  1475. return;
  1476. if (bio_data_dir(io->base_bio) == READ)
  1477. kcryptd_crypt_read_done(io);
  1478. else
  1479. kcryptd_crypt_write_io_submit(io, 1);
  1480. }
  1481. static void kcryptd_crypt(struct work_struct *work)
  1482. {
  1483. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1484. if (bio_data_dir(io->base_bio) == READ)
  1485. kcryptd_crypt_read_convert(io);
  1486. else
  1487. kcryptd_crypt_write_convert(io);
  1488. }
  1489. static void kcryptd_queue_crypt(struct dm_crypt_io *io)
  1490. {
  1491. struct crypt_config *cc = io->cc;
  1492. INIT_WORK(&io->work, kcryptd_crypt);
  1493. queue_work(cc->crypt_queue, &io->work);
  1494. }
  1495. static void crypt_free_tfms_aead(struct crypt_config *cc)
  1496. {
  1497. if (!cc->cipher_tfm.tfms_aead)
  1498. return;
  1499. if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
  1500. crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
  1501. cc->cipher_tfm.tfms_aead[0] = NULL;
  1502. }
  1503. kfree(cc->cipher_tfm.tfms_aead);
  1504. cc->cipher_tfm.tfms_aead = NULL;
  1505. }
  1506. static void crypt_free_tfms_skcipher(struct crypt_config *cc)
  1507. {
  1508. unsigned i;
  1509. if (!cc->cipher_tfm.tfms)
  1510. return;
  1511. for (i = 0; i < cc->tfms_count; i++)
  1512. if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
  1513. crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
  1514. cc->cipher_tfm.tfms[i] = NULL;
  1515. }
  1516. kfree(cc->cipher_tfm.tfms);
  1517. cc->cipher_tfm.tfms = NULL;
  1518. }
  1519. static void crypt_free_tfms(struct crypt_config *cc)
  1520. {
  1521. if (crypt_integrity_aead(cc))
  1522. crypt_free_tfms_aead(cc);
  1523. else
  1524. crypt_free_tfms_skcipher(cc);
  1525. }
  1526. static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
  1527. {
  1528. unsigned i;
  1529. int err;
  1530. cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
  1531. sizeof(struct crypto_skcipher *), GFP_KERNEL);
  1532. if (!cc->cipher_tfm.tfms)
  1533. return -ENOMEM;
  1534. for (i = 0; i < cc->tfms_count; i++) {
  1535. cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
  1536. if (IS_ERR(cc->cipher_tfm.tfms[i])) {
  1537. err = PTR_ERR(cc->cipher_tfm.tfms[i]);
  1538. crypt_free_tfms(cc);
  1539. return err;
  1540. }
  1541. }
  1542. return 0;
  1543. }
  1544. static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
  1545. {
  1546. int err;
  1547. cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
  1548. if (!cc->cipher_tfm.tfms)
  1549. return -ENOMEM;
  1550. cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
  1551. if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
  1552. err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
  1553. crypt_free_tfms(cc);
  1554. return err;
  1555. }
  1556. return 0;
  1557. }
  1558. static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
  1559. {
  1560. if (crypt_integrity_aead(cc))
  1561. return crypt_alloc_tfms_aead(cc, ciphermode);
  1562. else
  1563. return crypt_alloc_tfms_skcipher(cc, ciphermode);
  1564. }
  1565. static unsigned crypt_subkey_size(struct crypt_config *cc)
  1566. {
  1567. return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
  1568. }
  1569. static unsigned crypt_authenckey_size(struct crypt_config *cc)
  1570. {
  1571. return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
  1572. }
  1573. /*
  1574. * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
  1575. * the key must be for some reason in special format.
  1576. * This funcion converts cc->key to this special format.
  1577. */
  1578. static void crypt_copy_authenckey(char *p, const void *key,
  1579. unsigned enckeylen, unsigned authkeylen)
  1580. {
  1581. struct crypto_authenc_key_param *param;
  1582. struct rtattr *rta;
  1583. rta = (struct rtattr *)p;
  1584. param = RTA_DATA(rta);
  1585. param->enckeylen = cpu_to_be32(enckeylen);
  1586. rta->rta_len = RTA_LENGTH(sizeof(*param));
  1587. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  1588. p += RTA_SPACE(sizeof(*param));
  1589. memcpy(p, key + enckeylen, authkeylen);
  1590. p += authkeylen;
  1591. memcpy(p, key, enckeylen);
  1592. }
  1593. static int crypt_setkey(struct crypt_config *cc)
  1594. {
  1595. unsigned subkey_size;
  1596. int err = 0, i, r;
  1597. /* Ignore extra keys (which are used for IV etc) */
  1598. subkey_size = crypt_subkey_size(cc);
  1599. if (crypt_integrity_hmac(cc))
  1600. crypt_copy_authenckey(cc->authenc_key, cc->key,
  1601. subkey_size - cc->key_mac_size,
  1602. cc->key_mac_size);
  1603. for (i = 0; i < cc->tfms_count; i++) {
  1604. if (crypt_integrity_hmac(cc))
  1605. r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
  1606. cc->authenc_key, crypt_authenckey_size(cc));
  1607. else if (crypt_integrity_aead(cc))
  1608. r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
  1609. cc->key + (i * subkey_size),
  1610. subkey_size);
  1611. else
  1612. r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
  1613. cc->key + (i * subkey_size),
  1614. subkey_size);
  1615. if (r)
  1616. err = r;
  1617. }
  1618. if (crypt_integrity_hmac(cc))
  1619. memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
  1620. return err;
  1621. }
  1622. #ifdef CONFIG_KEYS
  1623. static bool contains_whitespace(const char *str)
  1624. {
  1625. while (*str)
  1626. if (isspace(*str++))
  1627. return true;
  1628. return false;
  1629. }
  1630. static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
  1631. {
  1632. char *new_key_string, *key_desc;
  1633. int ret;
  1634. struct key *key;
  1635. const struct user_key_payload *ukp;
  1636. /*
  1637. * Reject key_string with whitespace. dm core currently lacks code for
  1638. * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
  1639. */
  1640. if (contains_whitespace(key_string)) {
  1641. DMERR("whitespace chars not allowed in key string");
  1642. return -EINVAL;
  1643. }
  1644. /* look for next ':' separating key_type from key_description */
  1645. key_desc = strpbrk(key_string, ":");
  1646. if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
  1647. return -EINVAL;
  1648. if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
  1649. strncmp(key_string, "user:", key_desc - key_string + 1))
  1650. return -EINVAL;
  1651. new_key_string = kstrdup(key_string, GFP_KERNEL);
  1652. if (!new_key_string)
  1653. return -ENOMEM;
  1654. key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
  1655. key_desc + 1, NULL);
  1656. if (IS_ERR(key)) {
  1657. kzfree(new_key_string);
  1658. return PTR_ERR(key);
  1659. }
  1660. down_read(&key->sem);
  1661. ukp = user_key_payload_locked(key);
  1662. if (!ukp) {
  1663. up_read(&key->sem);
  1664. key_put(key);
  1665. kzfree(new_key_string);
  1666. return -EKEYREVOKED;
  1667. }
  1668. if (cc->key_size != ukp->datalen) {
  1669. up_read(&key->sem);
  1670. key_put(key);
  1671. kzfree(new_key_string);
  1672. return -EINVAL;
  1673. }
  1674. memcpy(cc->key, ukp->data, cc->key_size);
  1675. up_read(&key->sem);
  1676. key_put(key);
  1677. /* clear the flag since following operations may invalidate previously valid key */
  1678. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1679. ret = crypt_setkey(cc);
  1680. /* wipe the kernel key payload copy in each case */
  1681. memset(cc->key, 0, cc->key_size * sizeof(u8));
  1682. if (!ret) {
  1683. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1684. kzfree(cc->key_string);
  1685. cc->key_string = new_key_string;
  1686. } else
  1687. kzfree(new_key_string);
  1688. return ret;
  1689. }
  1690. static int get_key_size(char **key_string)
  1691. {
  1692. char *colon, dummy;
  1693. int ret;
  1694. if (*key_string[0] != ':')
  1695. return strlen(*key_string) >> 1;
  1696. /* look for next ':' in key string */
  1697. colon = strpbrk(*key_string + 1, ":");
  1698. if (!colon)
  1699. return -EINVAL;
  1700. if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
  1701. return -EINVAL;
  1702. *key_string = colon;
  1703. /* remaining key string should be :<logon|user>:<key_desc> */
  1704. return ret;
  1705. }
  1706. #else
  1707. static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
  1708. {
  1709. return -EINVAL;
  1710. }
  1711. static int get_key_size(char **key_string)
  1712. {
  1713. return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
  1714. }
  1715. #endif
  1716. static int crypt_set_key(struct crypt_config *cc, char *key)
  1717. {
  1718. int r = -EINVAL;
  1719. int key_string_len = strlen(key);
  1720. /* Hyphen (which gives a key_size of zero) means there is no key. */
  1721. if (!cc->key_size && strcmp(key, "-"))
  1722. goto out;
  1723. /* ':' means the key is in kernel keyring, short-circuit normal key processing */
  1724. if (key[0] == ':') {
  1725. r = crypt_set_keyring_key(cc, key + 1);
  1726. goto out;
  1727. }
  1728. /* clear the flag since following operations may invalidate previously valid key */
  1729. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1730. /* wipe references to any kernel keyring key */
  1731. kzfree(cc->key_string);
  1732. cc->key_string = NULL;
  1733. /* Decode key from its hex representation. */
  1734. if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
  1735. goto out;
  1736. r = crypt_setkey(cc);
  1737. if (!r)
  1738. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1739. out:
  1740. /* Hex key string not needed after here, so wipe it. */
  1741. memset(key, '0', key_string_len);
  1742. return r;
  1743. }
  1744. static int crypt_wipe_key(struct crypt_config *cc)
  1745. {
  1746. int r;
  1747. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1748. get_random_bytes(&cc->key, cc->key_size);
  1749. kzfree(cc->key_string);
  1750. cc->key_string = NULL;
  1751. r = crypt_setkey(cc);
  1752. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  1753. return r;
  1754. }
  1755. static void crypt_dtr(struct dm_target *ti)
  1756. {
  1757. struct crypt_config *cc = ti->private;
  1758. ti->private = NULL;
  1759. if (!cc)
  1760. return;
  1761. if (cc->write_thread)
  1762. kthread_stop(cc->write_thread);
  1763. if (cc->io_queue)
  1764. destroy_workqueue(cc->io_queue);
  1765. if (cc->crypt_queue)
  1766. destroy_workqueue(cc->crypt_queue);
  1767. crypt_free_tfms(cc);
  1768. if (cc->bs)
  1769. bioset_free(cc->bs);
  1770. mempool_destroy(cc->page_pool);
  1771. mempool_destroy(cc->req_pool);
  1772. mempool_destroy(cc->tag_pool);
  1773. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  1774. cc->iv_gen_ops->dtr(cc);
  1775. if (cc->dev)
  1776. dm_put_device(ti, cc->dev);
  1777. kzfree(cc->cipher);
  1778. kzfree(cc->cipher_string);
  1779. kzfree(cc->key_string);
  1780. kzfree(cc->cipher_auth);
  1781. kzfree(cc->authenc_key);
  1782. /* Must zero key material before freeing */
  1783. kzfree(cc);
  1784. }
  1785. static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
  1786. {
  1787. struct crypt_config *cc = ti->private;
  1788. if (crypt_integrity_aead(cc))
  1789. cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
  1790. else
  1791. cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
  1792. if (cc->iv_size)
  1793. /* at least a 64 bit sector number should fit in our buffer */
  1794. cc->iv_size = max(cc->iv_size,
  1795. (unsigned int)(sizeof(u64) / sizeof(u8)));
  1796. else if (ivmode) {
  1797. DMWARN("Selected cipher does not support IVs");
  1798. ivmode = NULL;
  1799. }
  1800. /* Choose ivmode, see comments at iv code. */
  1801. if (ivmode == NULL)
  1802. cc->iv_gen_ops = NULL;
  1803. else if (strcmp(ivmode, "plain") == 0)
  1804. cc->iv_gen_ops = &crypt_iv_plain_ops;
  1805. else if (strcmp(ivmode, "plain64") == 0)
  1806. cc->iv_gen_ops = &crypt_iv_plain64_ops;
  1807. else if (strcmp(ivmode, "essiv") == 0)
  1808. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  1809. else if (strcmp(ivmode, "benbi") == 0)
  1810. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  1811. else if (strcmp(ivmode, "null") == 0)
  1812. cc->iv_gen_ops = &crypt_iv_null_ops;
  1813. else if (strcmp(ivmode, "lmk") == 0) {
  1814. cc->iv_gen_ops = &crypt_iv_lmk_ops;
  1815. /*
  1816. * Version 2 and 3 is recognised according
  1817. * to length of provided multi-key string.
  1818. * If present (version 3), last key is used as IV seed.
  1819. * All keys (including IV seed) are always the same size.
  1820. */
  1821. if (cc->key_size % cc->key_parts) {
  1822. cc->key_parts++;
  1823. cc->key_extra_size = cc->key_size / cc->key_parts;
  1824. }
  1825. } else if (strcmp(ivmode, "tcw") == 0) {
  1826. cc->iv_gen_ops = &crypt_iv_tcw_ops;
  1827. cc->key_parts += 2; /* IV + whitening */
  1828. cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
  1829. } else if (strcmp(ivmode, "random") == 0) {
  1830. cc->iv_gen_ops = &crypt_iv_random_ops;
  1831. /* Need storage space in integrity fields. */
  1832. cc->integrity_iv_size = cc->iv_size;
  1833. } else {
  1834. ti->error = "Invalid IV mode";
  1835. return -EINVAL;
  1836. }
  1837. return 0;
  1838. }
  1839. /*
  1840. * Workaround to parse cipher algorithm from crypto API spec.
  1841. * The cc->cipher is currently used only in ESSIV.
  1842. * This should be probably done by crypto-api calls (once available...)
  1843. */
  1844. static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
  1845. {
  1846. const char *alg_name = NULL;
  1847. char *start, *end;
  1848. if (crypt_integrity_aead(cc)) {
  1849. alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
  1850. if (!alg_name)
  1851. return -EINVAL;
  1852. if (crypt_integrity_hmac(cc)) {
  1853. alg_name = strchr(alg_name, ',');
  1854. if (!alg_name)
  1855. return -EINVAL;
  1856. }
  1857. alg_name++;
  1858. } else {
  1859. alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
  1860. if (!alg_name)
  1861. return -EINVAL;
  1862. }
  1863. start = strchr(alg_name, '(');
  1864. end = strchr(alg_name, ')');
  1865. if (!start && !end) {
  1866. cc->cipher = kstrdup(alg_name, GFP_KERNEL);
  1867. return cc->cipher ? 0 : -ENOMEM;
  1868. }
  1869. if (!start || !end || ++start >= end)
  1870. return -EINVAL;
  1871. cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
  1872. if (!cc->cipher)
  1873. return -ENOMEM;
  1874. strncpy(cc->cipher, start, end - start);
  1875. return 0;
  1876. }
  1877. /*
  1878. * Workaround to parse HMAC algorithm from AEAD crypto API spec.
  1879. * The HMAC is needed to calculate tag size (HMAC digest size).
  1880. * This should be probably done by crypto-api calls (once available...)
  1881. */
  1882. static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
  1883. {
  1884. char *start, *end, *mac_alg = NULL;
  1885. struct crypto_ahash *mac;
  1886. if (!strstarts(cipher_api, "authenc("))
  1887. return 0;
  1888. start = strchr(cipher_api, '(');
  1889. end = strchr(cipher_api, ',');
  1890. if (!start || !end || ++start > end)
  1891. return -EINVAL;
  1892. mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
  1893. if (!mac_alg)
  1894. return -ENOMEM;
  1895. strncpy(mac_alg, start, end - start);
  1896. mac = crypto_alloc_ahash(mac_alg, 0, 0);
  1897. kfree(mac_alg);
  1898. if (IS_ERR(mac))
  1899. return PTR_ERR(mac);
  1900. cc->key_mac_size = crypto_ahash_digestsize(mac);
  1901. crypto_free_ahash(mac);
  1902. cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
  1903. if (!cc->authenc_key)
  1904. return -ENOMEM;
  1905. return 0;
  1906. }
  1907. static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
  1908. char **ivmode, char **ivopts)
  1909. {
  1910. struct crypt_config *cc = ti->private;
  1911. char *tmp, *cipher_api;
  1912. int ret = -EINVAL;
  1913. cc->tfms_count = 1;
  1914. /*
  1915. * New format (capi: prefix)
  1916. * capi:cipher_api_spec-iv:ivopts
  1917. */
  1918. tmp = &cipher_in[strlen("capi:")];
  1919. cipher_api = strsep(&tmp, "-");
  1920. *ivmode = strsep(&tmp, ":");
  1921. *ivopts = tmp;
  1922. if (*ivmode && !strcmp(*ivmode, "lmk"))
  1923. cc->tfms_count = 64;
  1924. cc->key_parts = cc->tfms_count;
  1925. /* Allocate cipher */
  1926. ret = crypt_alloc_tfms(cc, cipher_api);
  1927. if (ret < 0) {
  1928. ti->error = "Error allocating crypto tfm";
  1929. return ret;
  1930. }
  1931. /* Alloc AEAD, can be used only in new format. */
  1932. if (crypt_integrity_aead(cc)) {
  1933. ret = crypt_ctr_auth_cipher(cc, cipher_api);
  1934. if (ret < 0) {
  1935. ti->error = "Invalid AEAD cipher spec";
  1936. return -ENOMEM;
  1937. }
  1938. cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
  1939. } else
  1940. cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
  1941. ret = crypt_ctr_blkdev_cipher(cc);
  1942. if (ret < 0) {
  1943. ti->error = "Cannot allocate cipher string";
  1944. return -ENOMEM;
  1945. }
  1946. return 0;
  1947. }
  1948. static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
  1949. char **ivmode, char **ivopts)
  1950. {
  1951. struct crypt_config *cc = ti->private;
  1952. char *tmp, *cipher, *chainmode, *keycount;
  1953. char *cipher_api = NULL;
  1954. int ret = -EINVAL;
  1955. char dummy;
  1956. if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
  1957. ti->error = "Bad cipher specification";
  1958. return -EINVAL;
  1959. }
  1960. /*
  1961. * Legacy dm-crypt cipher specification
  1962. * cipher[:keycount]-mode-iv:ivopts
  1963. */
  1964. tmp = cipher_in;
  1965. keycount = strsep(&tmp, "-");
  1966. cipher = strsep(&keycount, ":");
  1967. if (!keycount)
  1968. cc->tfms_count = 1;
  1969. else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
  1970. !is_power_of_2(cc->tfms_count)) {
  1971. ti->error = "Bad cipher key count specification";
  1972. return -EINVAL;
  1973. }
  1974. cc->key_parts = cc->tfms_count;
  1975. cc->cipher = kstrdup(cipher, GFP_KERNEL);
  1976. if (!cc->cipher)
  1977. goto bad_mem;
  1978. chainmode = strsep(&tmp, "-");
  1979. *ivopts = strsep(&tmp, "-");
  1980. *ivmode = strsep(&*ivopts, ":");
  1981. if (tmp)
  1982. DMWARN("Ignoring unexpected additional cipher options");
  1983. /*
  1984. * For compatibility with the original dm-crypt mapping format, if
  1985. * only the cipher name is supplied, use cbc-plain.
  1986. */
  1987. if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
  1988. chainmode = "cbc";
  1989. *ivmode = "plain";
  1990. }
  1991. if (strcmp(chainmode, "ecb") && !*ivmode) {
  1992. ti->error = "IV mechanism required";
  1993. return -EINVAL;
  1994. }
  1995. cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
  1996. if (!cipher_api)
  1997. goto bad_mem;
  1998. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1999. "%s(%s)", chainmode, cipher);
  2000. if (ret < 0) {
  2001. kfree(cipher_api);
  2002. goto bad_mem;
  2003. }
  2004. /* Allocate cipher */
  2005. ret = crypt_alloc_tfms(cc, cipher_api);
  2006. if (ret < 0) {
  2007. ti->error = "Error allocating crypto tfm";
  2008. kfree(cipher_api);
  2009. return ret;
  2010. }
  2011. return 0;
  2012. bad_mem:
  2013. ti->error = "Cannot allocate cipher strings";
  2014. return -ENOMEM;
  2015. }
  2016. static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
  2017. {
  2018. struct crypt_config *cc = ti->private;
  2019. char *ivmode = NULL, *ivopts = NULL;
  2020. int ret;
  2021. cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
  2022. if (!cc->cipher_string) {
  2023. ti->error = "Cannot allocate cipher strings";
  2024. return -ENOMEM;
  2025. }
  2026. if (strstarts(cipher_in, "capi:"))
  2027. ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
  2028. else
  2029. ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
  2030. if (ret)
  2031. return ret;
  2032. /* Initialize IV */
  2033. ret = crypt_ctr_ivmode(ti, ivmode);
  2034. if (ret < 0)
  2035. return ret;
  2036. /* Initialize and set key */
  2037. ret = crypt_set_key(cc, key);
  2038. if (ret < 0) {
  2039. ti->error = "Error decoding and setting key";
  2040. return ret;
  2041. }
  2042. /* Allocate IV */
  2043. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
  2044. ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
  2045. if (ret < 0) {
  2046. ti->error = "Error creating IV";
  2047. return ret;
  2048. }
  2049. }
  2050. /* Initialize IV (set keys for ESSIV etc) */
  2051. if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
  2052. ret = cc->iv_gen_ops->init(cc);
  2053. if (ret < 0) {
  2054. ti->error = "Error initialising IV";
  2055. return ret;
  2056. }
  2057. }
  2058. return ret;
  2059. }
  2060. static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
  2061. {
  2062. struct crypt_config *cc = ti->private;
  2063. struct dm_arg_set as;
  2064. static struct dm_arg _args[] = {
  2065. {0, 6, "Invalid number of feature args"},
  2066. };
  2067. unsigned int opt_params, val;
  2068. const char *opt_string, *sval;
  2069. char dummy;
  2070. int ret;
  2071. /* Optional parameters */
  2072. as.argc = argc;
  2073. as.argv = argv;
  2074. ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
  2075. if (ret)
  2076. return ret;
  2077. while (opt_params--) {
  2078. opt_string = dm_shift_arg(&as);
  2079. if (!opt_string) {
  2080. ti->error = "Not enough feature arguments";
  2081. return -EINVAL;
  2082. }
  2083. if (!strcasecmp(opt_string, "allow_discards"))
  2084. ti->num_discard_bios = 1;
  2085. else if (!strcasecmp(opt_string, "same_cpu_crypt"))
  2086. set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
  2087. else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
  2088. set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
  2089. else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
  2090. if (val == 0 || val > MAX_TAG_SIZE) {
  2091. ti->error = "Invalid integrity arguments";
  2092. return -EINVAL;
  2093. }
  2094. cc->on_disk_tag_size = val;
  2095. sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
  2096. if (!strcasecmp(sval, "aead")) {
  2097. set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
  2098. } else if (strcasecmp(sval, "none")) {
  2099. ti->error = "Unknown integrity profile";
  2100. return -EINVAL;
  2101. }
  2102. cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
  2103. if (!cc->cipher_auth)
  2104. return -ENOMEM;
  2105. } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
  2106. if (cc->sector_size < (1 << SECTOR_SHIFT) ||
  2107. cc->sector_size > 4096 ||
  2108. (cc->sector_size & (cc->sector_size - 1))) {
  2109. ti->error = "Invalid feature value for sector_size";
  2110. return -EINVAL;
  2111. }
  2112. cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
  2113. } else if (!strcasecmp(opt_string, "iv_large_sectors"))
  2114. set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
  2115. else {
  2116. ti->error = "Invalid feature arguments";
  2117. return -EINVAL;
  2118. }
  2119. }
  2120. return 0;
  2121. }
  2122. /*
  2123. * Construct an encryption mapping:
  2124. * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
  2125. */
  2126. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  2127. {
  2128. struct crypt_config *cc;
  2129. int key_size;
  2130. unsigned int align_mask;
  2131. unsigned long long tmpll;
  2132. int ret;
  2133. size_t iv_size_padding, additional_req_size;
  2134. char dummy;
  2135. if (argc < 5) {
  2136. ti->error = "Not enough arguments";
  2137. return -EINVAL;
  2138. }
  2139. key_size = get_key_size(&argv[1]);
  2140. if (key_size < 0) {
  2141. ti->error = "Cannot parse key size";
  2142. return -EINVAL;
  2143. }
  2144. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  2145. if (!cc) {
  2146. ti->error = "Cannot allocate encryption context";
  2147. return -ENOMEM;
  2148. }
  2149. cc->key_size = key_size;
  2150. cc->sector_size = (1 << SECTOR_SHIFT);
  2151. cc->sector_shift = 0;
  2152. ti->private = cc;
  2153. /* Optional parameters need to be read before cipher constructor */
  2154. if (argc > 5) {
  2155. ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
  2156. if (ret)
  2157. goto bad;
  2158. }
  2159. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  2160. if (ret < 0)
  2161. goto bad;
  2162. if (crypt_integrity_aead(cc)) {
  2163. cc->dmreq_start = sizeof(struct aead_request);
  2164. cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
  2165. align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
  2166. } else {
  2167. cc->dmreq_start = sizeof(struct skcipher_request);
  2168. cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
  2169. align_mask = crypto_skcipher_alignmask(any_tfm(cc));
  2170. }
  2171. cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
  2172. if (align_mask < CRYPTO_MINALIGN) {
  2173. /* Allocate the padding exactly */
  2174. iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
  2175. & align_mask;
  2176. } else {
  2177. /*
  2178. * If the cipher requires greater alignment than kmalloc
  2179. * alignment, we don't know the exact position of the
  2180. * initialization vector. We must assume worst case.
  2181. */
  2182. iv_size_padding = align_mask;
  2183. }
  2184. ret = -ENOMEM;
  2185. /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
  2186. additional_req_size = sizeof(struct dm_crypt_request) +
  2187. iv_size_padding + cc->iv_size +
  2188. cc->iv_size +
  2189. sizeof(uint64_t) +
  2190. sizeof(unsigned int);
  2191. cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
  2192. if (!cc->req_pool) {
  2193. ti->error = "Cannot allocate crypt request mempool";
  2194. goto bad;
  2195. }
  2196. cc->per_bio_data_size = ti->per_io_data_size =
  2197. ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
  2198. ARCH_KMALLOC_MINALIGN);
  2199. cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
  2200. if (!cc->page_pool) {
  2201. ti->error = "Cannot allocate page mempool";
  2202. goto bad;
  2203. }
  2204. cc->bs = bioset_create(MIN_IOS, 0);
  2205. if (!cc->bs) {
  2206. ti->error = "Cannot allocate crypt bioset";
  2207. goto bad;
  2208. }
  2209. mutex_init(&cc->bio_alloc_lock);
  2210. ret = -EINVAL;
  2211. if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
  2212. (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
  2213. ti->error = "Invalid iv_offset sector";
  2214. goto bad;
  2215. }
  2216. cc->iv_offset = tmpll;
  2217. ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
  2218. if (ret) {
  2219. ti->error = "Device lookup failed";
  2220. goto bad;
  2221. }
  2222. ret = -EINVAL;
  2223. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  2224. ti->error = "Invalid device sector";
  2225. goto bad;
  2226. }
  2227. cc->start = tmpll;
  2228. if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
  2229. ret = crypt_integrity_ctr(cc, ti);
  2230. if (ret)
  2231. goto bad;
  2232. cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
  2233. if (!cc->tag_pool_max_sectors)
  2234. cc->tag_pool_max_sectors = 1;
  2235. cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
  2236. cc->tag_pool_max_sectors * cc->on_disk_tag_size);
  2237. if (!cc->tag_pool) {
  2238. ti->error = "Cannot allocate integrity tags mempool";
  2239. goto bad;
  2240. }
  2241. cc->tag_pool_max_sectors <<= cc->sector_shift;
  2242. }
  2243. ret = -ENOMEM;
  2244. cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
  2245. if (!cc->io_queue) {
  2246. ti->error = "Couldn't create kcryptd io queue";
  2247. goto bad;
  2248. }
  2249. if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
  2250. cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
  2251. else
  2252. cc->crypt_queue = alloc_workqueue("kcryptd",
  2253. WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
  2254. num_online_cpus());
  2255. if (!cc->crypt_queue) {
  2256. ti->error = "Couldn't create kcryptd queue";
  2257. goto bad;
  2258. }
  2259. init_waitqueue_head(&cc->write_thread_wait);
  2260. cc->write_tree = RB_ROOT;
  2261. cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
  2262. if (IS_ERR(cc->write_thread)) {
  2263. ret = PTR_ERR(cc->write_thread);
  2264. cc->write_thread = NULL;
  2265. ti->error = "Couldn't spawn write thread";
  2266. goto bad;
  2267. }
  2268. wake_up_process(cc->write_thread);
  2269. ti->num_flush_bios = 1;
  2270. return 0;
  2271. bad:
  2272. crypt_dtr(ti);
  2273. return ret;
  2274. }
  2275. static int crypt_map(struct dm_target *ti, struct bio *bio)
  2276. {
  2277. struct dm_crypt_io *io;
  2278. struct crypt_config *cc = ti->private;
  2279. /*
  2280. * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
  2281. * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
  2282. * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
  2283. */
  2284. if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
  2285. bio_op(bio) == REQ_OP_DISCARD)) {
  2286. bio->bi_bdev = cc->dev->bdev;
  2287. if (bio_sectors(bio))
  2288. bio->bi_iter.bi_sector = cc->start +
  2289. dm_target_offset(ti, bio->bi_iter.bi_sector);
  2290. return DM_MAPIO_REMAPPED;
  2291. }
  2292. /*
  2293. * Check if bio is too large, split as needed.
  2294. */
  2295. if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
  2296. (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
  2297. dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
  2298. /*
  2299. * Ensure that bio is a multiple of internal sector encryption size
  2300. * and is aligned to this size as defined in IO hints.
  2301. */
  2302. if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
  2303. return -EIO;
  2304. if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
  2305. return -EIO;
  2306. io = dm_per_bio_data(bio, cc->per_bio_data_size);
  2307. crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
  2308. if (cc->on_disk_tag_size) {
  2309. unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
  2310. if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
  2311. unlikely(!(io->integrity_metadata = kmalloc(tag_len,
  2312. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
  2313. if (bio_sectors(bio) > cc->tag_pool_max_sectors)
  2314. dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
  2315. io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
  2316. io->integrity_metadata_from_pool = true;
  2317. }
  2318. }
  2319. if (crypt_integrity_aead(cc))
  2320. io->ctx.r.req_aead = (struct aead_request *)(io + 1);
  2321. else
  2322. io->ctx.r.req = (struct skcipher_request *)(io + 1);
  2323. if (bio_data_dir(io->base_bio) == READ) {
  2324. if (kcryptd_io_read(io, GFP_NOWAIT))
  2325. kcryptd_queue_read(io);
  2326. } else
  2327. kcryptd_queue_crypt(io);
  2328. return DM_MAPIO_SUBMITTED;
  2329. }
  2330. static void crypt_status(struct dm_target *ti, status_type_t type,
  2331. unsigned status_flags, char *result, unsigned maxlen)
  2332. {
  2333. struct crypt_config *cc = ti->private;
  2334. unsigned i, sz = 0;
  2335. int num_feature_args = 0;
  2336. switch (type) {
  2337. case STATUSTYPE_INFO:
  2338. result[0] = '\0';
  2339. break;
  2340. case STATUSTYPE_TABLE:
  2341. DMEMIT("%s ", cc->cipher_string);
  2342. if (cc->key_size > 0) {
  2343. if (cc->key_string)
  2344. DMEMIT(":%u:%s", cc->key_size, cc->key_string);
  2345. else
  2346. for (i = 0; i < cc->key_size; i++)
  2347. DMEMIT("%02x", cc->key[i]);
  2348. } else
  2349. DMEMIT("-");
  2350. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  2351. cc->dev->name, (unsigned long long)cc->start);
  2352. num_feature_args += !!ti->num_discard_bios;
  2353. num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
  2354. num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
  2355. num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
  2356. num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
  2357. if (cc->on_disk_tag_size)
  2358. num_feature_args++;
  2359. if (num_feature_args) {
  2360. DMEMIT(" %d", num_feature_args);
  2361. if (ti->num_discard_bios)
  2362. DMEMIT(" allow_discards");
  2363. if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
  2364. DMEMIT(" same_cpu_crypt");
  2365. if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
  2366. DMEMIT(" submit_from_crypt_cpus");
  2367. if (cc->on_disk_tag_size)
  2368. DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
  2369. if (cc->sector_size != (1 << SECTOR_SHIFT))
  2370. DMEMIT(" sector_size:%d", cc->sector_size);
  2371. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  2372. DMEMIT(" iv_large_sectors");
  2373. }
  2374. break;
  2375. }
  2376. }
  2377. static void crypt_postsuspend(struct dm_target *ti)
  2378. {
  2379. struct crypt_config *cc = ti->private;
  2380. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  2381. }
  2382. static int crypt_preresume(struct dm_target *ti)
  2383. {
  2384. struct crypt_config *cc = ti->private;
  2385. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  2386. DMERR("aborting resume - crypt key is not set.");
  2387. return -EAGAIN;
  2388. }
  2389. return 0;
  2390. }
  2391. static void crypt_resume(struct dm_target *ti)
  2392. {
  2393. struct crypt_config *cc = ti->private;
  2394. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  2395. }
  2396. /* Message interface
  2397. * key set <key>
  2398. * key wipe
  2399. */
  2400. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  2401. {
  2402. struct crypt_config *cc = ti->private;
  2403. int key_size, ret = -EINVAL;
  2404. if (argc < 2)
  2405. goto error;
  2406. if (!strcasecmp(argv[0], "key")) {
  2407. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  2408. DMWARN("not suspended during key manipulation.");
  2409. return -EINVAL;
  2410. }
  2411. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  2412. /* The key size may not be changed. */
  2413. key_size = get_key_size(&argv[2]);
  2414. if (key_size < 0 || cc->key_size != key_size) {
  2415. memset(argv[2], '0', strlen(argv[2]));
  2416. return -EINVAL;
  2417. }
  2418. ret = crypt_set_key(cc, argv[2]);
  2419. if (ret)
  2420. return ret;
  2421. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  2422. ret = cc->iv_gen_ops->init(cc);
  2423. return ret;
  2424. }
  2425. if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
  2426. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  2427. ret = cc->iv_gen_ops->wipe(cc);
  2428. if (ret)
  2429. return ret;
  2430. }
  2431. return crypt_wipe_key(cc);
  2432. }
  2433. }
  2434. error:
  2435. DMWARN("unrecognised message received.");
  2436. return -EINVAL;
  2437. }
  2438. static int crypt_iterate_devices(struct dm_target *ti,
  2439. iterate_devices_callout_fn fn, void *data)
  2440. {
  2441. struct crypt_config *cc = ti->private;
  2442. return fn(ti, cc->dev, cc->start, ti->len, data);
  2443. }
  2444. static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2445. {
  2446. struct crypt_config *cc = ti->private;
  2447. /*
  2448. * Unfortunate constraint that is required to avoid the potential
  2449. * for exceeding underlying device's max_segments limits -- due to
  2450. * crypt_alloc_buffer() possibly allocating pages for the encryption
  2451. * bio that are not as physically contiguous as the original bio.
  2452. */
  2453. limits->max_segment_size = PAGE_SIZE;
  2454. if (cc->sector_size != (1 << SECTOR_SHIFT)) {
  2455. limits->logical_block_size = cc->sector_size;
  2456. limits->physical_block_size = cc->sector_size;
  2457. blk_limits_io_min(limits, cc->sector_size);
  2458. }
  2459. }
  2460. static struct target_type crypt_target = {
  2461. .name = "crypt",
  2462. .version = {1, 17, 0},
  2463. .module = THIS_MODULE,
  2464. .ctr = crypt_ctr,
  2465. .dtr = crypt_dtr,
  2466. .map = crypt_map,
  2467. .status = crypt_status,
  2468. .postsuspend = crypt_postsuspend,
  2469. .preresume = crypt_preresume,
  2470. .resume = crypt_resume,
  2471. .message = crypt_message,
  2472. .iterate_devices = crypt_iterate_devices,
  2473. .io_hints = crypt_io_hints,
  2474. };
  2475. static int __init dm_crypt_init(void)
  2476. {
  2477. int r;
  2478. r = dm_register_target(&crypt_target);
  2479. if (r < 0)
  2480. DMERR("register failed %d", r);
  2481. return r;
  2482. }
  2483. static void __exit dm_crypt_exit(void)
  2484. {
  2485. dm_unregister_target(&crypt_target);
  2486. }
  2487. module_init(dm_crypt_init);
  2488. module_exit(dm_crypt_exit);
  2489. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  2490. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  2491. MODULE_LICENSE("GPL");