dm-crypt.c 77 KB

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