crypto.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2004 Erez Zadok
  5. * Copyright (C) 2001-2004 Stony Brook University
  6. * Copyright (C) 2004-2006 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  8. * Michael C. Thompson <mcthomps@us.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/random.h>
  29. #include <linux/compiler.h>
  30. #include <linux/key.h>
  31. #include <linux/namei.h>
  32. #include <linux/crypto.h>
  33. #include <linux/file.h>
  34. #include <linux/scatterlist.h>
  35. #include "ecryptfs_kernel.h"
  36. static int
  37. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  38. struct page *dst_page, int dst_offset,
  39. struct page *src_page, int src_offset, int size,
  40. unsigned char *iv);
  41. static int
  42. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  43. struct page *dst_page, int dst_offset,
  44. struct page *src_page, int src_offset, int size,
  45. unsigned char *iv);
  46. /**
  47. * ecryptfs_to_hex
  48. * @dst: Buffer to take hex character representation of contents of
  49. * src; must be at least of size (src_size * 2)
  50. * @src: Buffer to be converted to a hex string respresentation
  51. * @src_size: number of bytes to convert
  52. */
  53. void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  54. {
  55. int x;
  56. for (x = 0; x < src_size; x++)
  57. sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
  58. }
  59. /**
  60. * ecryptfs_from_hex
  61. * @dst: Buffer to take the bytes from src hex; must be at least of
  62. * size (src_size / 2)
  63. * @src: Buffer to be converted from a hex string respresentation to raw value
  64. * @dst_size: size of dst buffer, or number of hex characters pairs to convert
  65. */
  66. void ecryptfs_from_hex(char *dst, char *src, int dst_size)
  67. {
  68. int x;
  69. char tmp[3] = { 0, };
  70. for (x = 0; x < dst_size; x++) {
  71. tmp[0] = src[x * 2];
  72. tmp[1] = src[x * 2 + 1];
  73. dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
  74. }
  75. }
  76. /**
  77. * ecryptfs_calculate_md5 - calculates the md5 of @src
  78. * @dst: Pointer to 16 bytes of allocated memory
  79. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  80. * @src: Data to be md5'd
  81. * @len: Length of @src
  82. *
  83. * Uses the allocated crypto context that crypt_stat references to
  84. * generate the MD5 sum of the contents of src.
  85. */
  86. static int ecryptfs_calculate_md5(char *dst,
  87. struct ecryptfs_crypt_stat *crypt_stat,
  88. char *src, int len)
  89. {
  90. int rc = 0;
  91. struct scatterlist sg;
  92. mutex_lock(&crypt_stat->cs_md5_tfm_mutex);
  93. sg_init_one(&sg, (u8 *)src, len);
  94. if (!crypt_stat->md5_tfm) {
  95. crypt_stat->md5_tfm =
  96. crypto_alloc_tfm("md5", CRYPTO_TFM_REQ_MAY_SLEEP);
  97. if (!crypt_stat->md5_tfm) {
  98. rc = -ENOMEM;
  99. ecryptfs_printk(KERN_ERR, "Error attempting to "
  100. "allocate crypto context\n");
  101. goto out;
  102. }
  103. }
  104. crypto_digest_init(crypt_stat->md5_tfm);
  105. crypto_digest_update(crypt_stat->md5_tfm, &sg, 1);
  106. crypto_digest_final(crypt_stat->md5_tfm, dst);
  107. mutex_unlock(&crypt_stat->cs_md5_tfm_mutex);
  108. out:
  109. return rc;
  110. }
  111. /**
  112. * ecryptfs_derive_iv
  113. * @iv: destination for the derived iv vale
  114. * @crypt_stat: Pointer to crypt_stat struct for the current inode
  115. * @offset: Offset of the page whose's iv we are to derive
  116. *
  117. * Generate the initialization vector from the given root IV and page
  118. * offset.
  119. *
  120. * Returns zero on success; non-zero on error.
  121. */
  122. static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
  123. pgoff_t offset)
  124. {
  125. int rc = 0;
  126. char dst[MD5_DIGEST_SIZE];
  127. char src[ECRYPTFS_MAX_IV_BYTES + 16];
  128. if (unlikely(ecryptfs_verbosity > 0)) {
  129. ecryptfs_printk(KERN_DEBUG, "root iv:\n");
  130. ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
  131. }
  132. /* TODO: It is probably secure to just cast the least
  133. * significant bits of the root IV into an unsigned long and
  134. * add the offset to that rather than go through all this
  135. * hashing business. -Halcrow */
  136. memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
  137. memset((src + crypt_stat->iv_bytes), 0, 16);
  138. snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset);
  139. if (unlikely(ecryptfs_verbosity > 0)) {
  140. ecryptfs_printk(KERN_DEBUG, "source:\n");
  141. ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
  142. }
  143. rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
  144. (crypt_stat->iv_bytes + 16));
  145. if (rc) {
  146. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  147. "MD5 while generating IV for a page\n");
  148. goto out;
  149. }
  150. memcpy(iv, dst, crypt_stat->iv_bytes);
  151. if (unlikely(ecryptfs_verbosity > 0)) {
  152. ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
  153. ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
  154. }
  155. out:
  156. return rc;
  157. }
  158. /**
  159. * ecryptfs_init_crypt_stat
  160. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  161. *
  162. * Initialize the crypt_stat structure.
  163. */
  164. void
  165. ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  166. {
  167. memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  168. mutex_init(&crypt_stat->cs_mutex);
  169. mutex_init(&crypt_stat->cs_tfm_mutex);
  170. mutex_init(&crypt_stat->cs_md5_tfm_mutex);
  171. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_STRUCT_INITIALIZED);
  172. }
  173. /**
  174. * ecryptfs_destruct_crypt_stat
  175. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  176. *
  177. * Releases all memory associated with a crypt_stat struct.
  178. */
  179. void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
  180. {
  181. if (crypt_stat->tfm)
  182. crypto_free_tfm(crypt_stat->tfm);
  183. if (crypt_stat->md5_tfm)
  184. crypto_free_tfm(crypt_stat->md5_tfm);
  185. memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
  186. }
  187. void ecryptfs_destruct_mount_crypt_stat(
  188. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  189. {
  190. if (mount_crypt_stat->global_auth_tok_key)
  191. key_put(mount_crypt_stat->global_auth_tok_key);
  192. if (mount_crypt_stat->global_key_tfm)
  193. crypto_free_tfm(mount_crypt_stat->global_key_tfm);
  194. memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
  195. }
  196. /**
  197. * virt_to_scatterlist
  198. * @addr: Virtual address
  199. * @size: Size of data; should be an even multiple of the block size
  200. * @sg: Pointer to scatterlist array; set to NULL to obtain only
  201. * the number of scatterlist structs required in array
  202. * @sg_size: Max array size
  203. *
  204. * Fills in a scatterlist array with page references for a passed
  205. * virtual address.
  206. *
  207. * Returns the number of scatterlist structs in array used
  208. */
  209. int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
  210. int sg_size)
  211. {
  212. int i = 0;
  213. struct page *pg;
  214. int offset;
  215. int remainder_of_page;
  216. while (size > 0 && i < sg_size) {
  217. pg = virt_to_page(addr);
  218. offset = offset_in_page(addr);
  219. if (sg) {
  220. sg[i].page = pg;
  221. sg[i].offset = offset;
  222. }
  223. remainder_of_page = PAGE_CACHE_SIZE - offset;
  224. if (size >= remainder_of_page) {
  225. if (sg)
  226. sg[i].length = remainder_of_page;
  227. addr += remainder_of_page;
  228. size -= remainder_of_page;
  229. } else {
  230. if (sg)
  231. sg[i].length = size;
  232. addr += size;
  233. size = 0;
  234. }
  235. i++;
  236. }
  237. if (size > 0)
  238. return -ENOMEM;
  239. return i;
  240. }
  241. /**
  242. * encrypt_scatterlist
  243. * @crypt_stat: Pointer to the crypt_stat struct to initialize.
  244. * @dest_sg: Destination of encrypted data
  245. * @src_sg: Data to be encrypted
  246. * @size: Length of data to be encrypted
  247. * @iv: iv to use during encryption
  248. *
  249. * Returns the number of bytes encrypted; negative value on error
  250. */
  251. static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  252. struct scatterlist *dest_sg,
  253. struct scatterlist *src_sg, int size,
  254. unsigned char *iv)
  255. {
  256. int rc = 0;
  257. BUG_ON(!crypt_stat || !crypt_stat->tfm
  258. || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  259. ECRYPTFS_STRUCT_INITIALIZED));
  260. if (unlikely(ecryptfs_verbosity > 0)) {
  261. ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
  262. crypt_stat->key_size);
  263. ecryptfs_dump_hex(crypt_stat->key,
  264. crypt_stat->key_size);
  265. }
  266. /* Consider doing this once, when the file is opened */
  267. mutex_lock(&crypt_stat->cs_tfm_mutex);
  268. rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
  269. crypt_stat->key_size);
  270. if (rc) {
  271. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  272. rc);
  273. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  274. rc = -EINVAL;
  275. goto out;
  276. }
  277. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
  278. crypto_cipher_encrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size, iv);
  279. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  280. out:
  281. return rc;
  282. }
  283. static void
  284. ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
  285. int *byte_offset,
  286. struct ecryptfs_crypt_stat *crypt_stat,
  287. unsigned long extent_num)
  288. {
  289. unsigned long lower_extent_num;
  290. int extents_occupied_by_headers_at_front;
  291. int bytes_occupied_by_headers_at_front;
  292. int extent_offset;
  293. int extents_per_page;
  294. bytes_occupied_by_headers_at_front =
  295. ( crypt_stat->header_extent_size
  296. * crypt_stat->num_header_extents_at_front );
  297. extents_occupied_by_headers_at_front =
  298. ( bytes_occupied_by_headers_at_front
  299. / crypt_stat->extent_size );
  300. lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
  301. extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
  302. (*lower_page_idx) = lower_extent_num / extents_per_page;
  303. extent_offset = lower_extent_num % extents_per_page;
  304. (*byte_offset) = extent_offset * crypt_stat->extent_size;
  305. ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
  306. "[%d]\n", crypt_stat->header_extent_size);
  307. ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
  308. "num_header_extents_at_front = [%d]\n",
  309. crypt_stat->num_header_extents_at_front);
  310. ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
  311. "front = [%d]\n", extents_occupied_by_headers_at_front);
  312. ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
  313. lower_extent_num);
  314. ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
  315. extents_per_page);
  316. ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
  317. (*lower_page_idx));
  318. ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
  319. extent_offset);
  320. ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
  321. (*byte_offset));
  322. }
  323. static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
  324. struct page *lower_page,
  325. struct inode *lower_inode,
  326. int byte_offset_in_page, int bytes_to_write)
  327. {
  328. int rc = 0;
  329. if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
  330. rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
  331. ctx->param.lower_file,
  332. byte_offset_in_page,
  333. bytes_to_write);
  334. if (rc) {
  335. ecryptfs_printk(KERN_ERR, "Error calling lower "
  336. "commit; rc = [%d]\n", rc);
  337. goto out;
  338. }
  339. } else {
  340. rc = ecryptfs_writepage_and_release_lower_page(lower_page,
  341. lower_inode,
  342. ctx->param.wbc);
  343. if (rc) {
  344. ecryptfs_printk(KERN_ERR, "Error calling lower "
  345. "writepage(); rc = [%d]\n", rc);
  346. goto out;
  347. }
  348. }
  349. out:
  350. return rc;
  351. }
  352. static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
  353. struct page **lower_page,
  354. struct inode *lower_inode,
  355. unsigned long lower_page_idx,
  356. int byte_offset_in_page)
  357. {
  358. int rc = 0;
  359. if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
  360. /* TODO: Limit this to only the data extents that are
  361. * needed */
  362. rc = ecryptfs_get_lower_page(lower_page, lower_inode,
  363. ctx->param.lower_file,
  364. lower_page_idx,
  365. byte_offset_in_page,
  366. (PAGE_CACHE_SIZE
  367. - byte_offset_in_page));
  368. if (rc) {
  369. ecryptfs_printk(
  370. KERN_ERR, "Error attempting to grab, map, "
  371. "and prepare_write lower page with index "
  372. "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
  373. goto out;
  374. }
  375. } else {
  376. rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL,
  377. lower_inode,
  378. lower_page_idx);
  379. if (rc) {
  380. ecryptfs_printk(
  381. KERN_ERR, "Error attempting to grab and map "
  382. "lower page with index [0x%.16x]; rc = [%d]\n",
  383. lower_page_idx, rc);
  384. goto out;
  385. }
  386. }
  387. out:
  388. return rc;
  389. }
  390. /**
  391. * ecryptfs_encrypt_page
  392. * @ctx: The context of the page
  393. *
  394. * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
  395. * that eCryptfs pages may straddle the lower pages -- for instance,
  396. * if the file was created on a machine with an 8K page size
  397. * (resulting in an 8K header), and then the file is copied onto a
  398. * host with a 32K page size, then when reading page 0 of the eCryptfs
  399. * file, 24K of page 0 of the lower file will be read and decrypted,
  400. * and then 8K of page 1 of the lower file will be read and decrypted.
  401. *
  402. * The actual operations performed on each page depends on the
  403. * contents of the ecryptfs_page_crypt_context struct.
  404. *
  405. * Returns zero on success; negative on error
  406. */
  407. int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
  408. {
  409. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  410. unsigned long base_extent;
  411. unsigned long extent_offset = 0;
  412. unsigned long lower_page_idx = 0;
  413. unsigned long prior_lower_page_idx = 0;
  414. struct page *lower_page;
  415. struct inode *lower_inode;
  416. struct ecryptfs_inode_info *inode_info;
  417. struct ecryptfs_crypt_stat *crypt_stat;
  418. int rc = 0;
  419. int lower_byte_offset = 0;
  420. int orig_byte_offset = 0;
  421. int num_extents_per_page;
  422. #define ECRYPTFS_PAGE_STATE_UNREAD 0
  423. #define ECRYPTFS_PAGE_STATE_READ 1
  424. #define ECRYPTFS_PAGE_STATE_MODIFIED 2
  425. #define ECRYPTFS_PAGE_STATE_WRITTEN 3
  426. int page_state;
  427. lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
  428. inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
  429. crypt_stat = &inode_info->crypt_stat;
  430. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
  431. rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
  432. ctx->param.lower_file);
  433. if (rc)
  434. ecryptfs_printk(KERN_ERR, "Error attempting to copy "
  435. "page at index [0x%.16x]\n",
  436. ctx->page->index);
  437. goto out;
  438. }
  439. num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
  440. base_extent = (ctx->page->index * num_extents_per_page);
  441. page_state = ECRYPTFS_PAGE_STATE_UNREAD;
  442. while (extent_offset < num_extents_per_page) {
  443. ecryptfs_extent_to_lwr_pg_idx_and_offset(
  444. &lower_page_idx, &lower_byte_offset, crypt_stat,
  445. (base_extent + extent_offset));
  446. if (prior_lower_page_idx != lower_page_idx
  447. && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
  448. rc = ecryptfs_write_out_page(ctx, lower_page,
  449. lower_inode,
  450. orig_byte_offset,
  451. (PAGE_CACHE_SIZE
  452. - orig_byte_offset));
  453. if (rc) {
  454. ecryptfs_printk(KERN_ERR, "Error attempting "
  455. "to write out page; rc = [%d]"
  456. "\n", rc);
  457. goto out;
  458. }
  459. page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
  460. }
  461. if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
  462. || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
  463. rc = ecryptfs_read_in_page(ctx, &lower_page,
  464. lower_inode, lower_page_idx,
  465. lower_byte_offset);
  466. if (rc) {
  467. ecryptfs_printk(KERN_ERR, "Error attempting "
  468. "to read in lower page with "
  469. "index [0x%.16x]; rc = [%d]\n",
  470. lower_page_idx, rc);
  471. goto out;
  472. }
  473. orig_byte_offset = lower_byte_offset;
  474. prior_lower_page_idx = lower_page_idx;
  475. page_state = ECRYPTFS_PAGE_STATE_READ;
  476. }
  477. BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
  478. || page_state == ECRYPTFS_PAGE_STATE_READ));
  479. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  480. (base_extent + extent_offset));
  481. if (rc) {
  482. ecryptfs_printk(KERN_ERR, "Error attempting to "
  483. "derive IV for extent [0x%.16x]; "
  484. "rc = [%d]\n",
  485. (base_extent + extent_offset), rc);
  486. goto out;
  487. }
  488. if (unlikely(ecryptfs_verbosity > 0)) {
  489. ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
  490. "with iv:\n");
  491. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  492. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  493. "encryption:\n");
  494. ecryptfs_dump_hex((char *)
  495. (page_address(ctx->page)
  496. + (extent_offset
  497. * crypt_stat->extent_size)), 8);
  498. }
  499. rc = ecryptfs_encrypt_page_offset(
  500. crypt_stat, lower_page, lower_byte_offset, ctx->page,
  501. (extent_offset * crypt_stat->extent_size),
  502. crypt_stat->extent_size, extent_iv);
  503. ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
  504. "rc = [%d]\n",
  505. (base_extent + extent_offset), rc);
  506. if (unlikely(ecryptfs_verbosity > 0)) {
  507. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  508. "encryption:\n");
  509. ecryptfs_dump_hex((char *)(page_address(lower_page)
  510. + lower_byte_offset), 8);
  511. }
  512. page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
  513. extent_offset++;
  514. }
  515. BUG_ON(orig_byte_offset != 0);
  516. rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
  517. (lower_byte_offset
  518. + crypt_stat->extent_size));
  519. if (rc) {
  520. ecryptfs_printk(KERN_ERR, "Error attempting to write out "
  521. "page; rc = [%d]\n", rc);
  522. goto out;
  523. }
  524. out:
  525. return rc;
  526. }
  527. /**
  528. * ecryptfs_decrypt_page
  529. * @file: The ecryptfs file
  530. * @page: The page in ecryptfs to decrypt
  531. *
  532. * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
  533. * that eCryptfs pages may straddle the lower pages -- for instance,
  534. * if the file was created on a machine with an 8K page size
  535. * (resulting in an 8K header), and then the file is copied onto a
  536. * host with a 32K page size, then when reading page 0 of the eCryptfs
  537. * file, 24K of page 0 of the lower file will be read and decrypted,
  538. * and then 8K of page 1 of the lower file will be read and decrypted.
  539. *
  540. * Returns zero on success; negative on error
  541. */
  542. int ecryptfs_decrypt_page(struct file *file, struct page *page)
  543. {
  544. char extent_iv[ECRYPTFS_MAX_IV_BYTES];
  545. unsigned long base_extent;
  546. unsigned long extent_offset = 0;
  547. unsigned long lower_page_idx = 0;
  548. unsigned long prior_lower_page_idx = 0;
  549. struct page *lower_page;
  550. char *lower_page_virt = NULL;
  551. struct inode *lower_inode;
  552. struct ecryptfs_crypt_stat *crypt_stat;
  553. int rc = 0;
  554. int byte_offset;
  555. int num_extents_per_page;
  556. int page_state;
  557. crypt_stat = &(ecryptfs_inode_to_private(
  558. page->mapping->host)->crypt_stat);
  559. lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
  560. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
  561. rc = ecryptfs_do_readpage(file, page, page->index);
  562. if (rc)
  563. ecryptfs_printk(KERN_ERR, "Error attempting to copy "
  564. "page at index [0x%.16x]\n",
  565. page->index);
  566. goto out;
  567. }
  568. num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
  569. base_extent = (page->index * num_extents_per_page);
  570. lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
  571. SLAB_KERNEL);
  572. if (!lower_page_virt) {
  573. rc = -ENOMEM;
  574. ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
  575. "lower page(s)\n");
  576. goto out;
  577. }
  578. lower_page = virt_to_page(lower_page_virt);
  579. page_state = ECRYPTFS_PAGE_STATE_UNREAD;
  580. while (extent_offset < num_extents_per_page) {
  581. ecryptfs_extent_to_lwr_pg_idx_and_offset(
  582. &lower_page_idx, &byte_offset, crypt_stat,
  583. (base_extent + extent_offset));
  584. if (prior_lower_page_idx != lower_page_idx
  585. || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
  586. rc = ecryptfs_do_readpage(file, lower_page,
  587. lower_page_idx);
  588. if (rc) {
  589. ecryptfs_printk(KERN_ERR, "Error reading "
  590. "lower encrypted page; rc = "
  591. "[%d]\n", rc);
  592. goto out;
  593. }
  594. prior_lower_page_idx = lower_page_idx;
  595. page_state = ECRYPTFS_PAGE_STATE_READ;
  596. }
  597. rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
  598. (base_extent + extent_offset));
  599. if (rc) {
  600. ecryptfs_printk(KERN_ERR, "Error attempting to "
  601. "derive IV for extent [0x%.16x]; rc = "
  602. "[%d]\n",
  603. (base_extent + extent_offset), rc);
  604. goto out;
  605. }
  606. if (unlikely(ecryptfs_verbosity > 0)) {
  607. ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
  608. "with iv:\n");
  609. ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
  610. ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
  611. "decryption:\n");
  612. ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
  613. }
  614. rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
  615. (extent_offset
  616. * crypt_stat->extent_size),
  617. lower_page, byte_offset,
  618. crypt_stat->extent_size,
  619. extent_iv);
  620. if (rc != crypt_stat->extent_size) {
  621. ecryptfs_printk(KERN_ERR, "Error attempting to "
  622. "decrypt extent [0x%.16x]\n",
  623. (base_extent + extent_offset));
  624. goto out;
  625. }
  626. rc = 0;
  627. if (unlikely(ecryptfs_verbosity > 0)) {
  628. ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
  629. "decryption:\n");
  630. ecryptfs_dump_hex((char *)(page_address(page)
  631. + byte_offset), 8);
  632. }
  633. extent_offset++;
  634. }
  635. out:
  636. if (lower_page_virt)
  637. kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
  638. return rc;
  639. }
  640. /**
  641. * decrypt_scatterlist
  642. *
  643. * Returns the number of bytes decrypted; negative value on error
  644. */
  645. static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
  646. struct scatterlist *dest_sg,
  647. struct scatterlist *src_sg, int size,
  648. unsigned char *iv)
  649. {
  650. int rc = 0;
  651. /* Consider doing this once, when the file is opened */
  652. mutex_lock(&crypt_stat->cs_tfm_mutex);
  653. rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
  654. crypt_stat->key_size);
  655. if (rc) {
  656. ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
  657. rc);
  658. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  659. rc = -EINVAL;
  660. goto out;
  661. }
  662. ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
  663. rc = crypto_cipher_decrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size,
  664. iv);
  665. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  666. if (rc) {
  667. ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
  668. rc);
  669. goto out;
  670. }
  671. rc = size;
  672. out:
  673. return rc;
  674. }
  675. /**
  676. * ecryptfs_encrypt_page_offset
  677. *
  678. * Returns the number of bytes encrypted
  679. */
  680. static int
  681. ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  682. struct page *dst_page, int dst_offset,
  683. struct page *src_page, int src_offset, int size,
  684. unsigned char *iv)
  685. {
  686. struct scatterlist src_sg, dst_sg;
  687. src_sg.page = src_page;
  688. src_sg.offset = src_offset;
  689. src_sg.length = size;
  690. dst_sg.page = dst_page;
  691. dst_sg.offset = dst_offset;
  692. dst_sg.length = size;
  693. return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  694. }
  695. /**
  696. * ecryptfs_decrypt_page_offset
  697. *
  698. * Returns the number of bytes decrypted
  699. */
  700. static int
  701. ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
  702. struct page *dst_page, int dst_offset,
  703. struct page *src_page, int src_offset, int size,
  704. unsigned char *iv)
  705. {
  706. struct scatterlist src_sg, dst_sg;
  707. src_sg.page = src_page;
  708. src_sg.offset = src_offset;
  709. src_sg.length = size;
  710. dst_sg.page = dst_page;
  711. dst_sg.offset = dst_offset;
  712. dst_sg.length = size;
  713. return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
  714. }
  715. #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
  716. /**
  717. * ecryptfs_init_crypt_ctx
  718. * @crypt_stat: Uninitilized crypt stats structure
  719. *
  720. * Initialize the crypto context.
  721. *
  722. * TODO: Performance: Keep a cache of initialized cipher contexts;
  723. * only init if needed
  724. */
  725. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
  726. {
  727. int rc = -EINVAL;
  728. if (!crypt_stat->cipher) {
  729. ecryptfs_printk(KERN_ERR, "No cipher specified\n");
  730. goto out;
  731. }
  732. ecryptfs_printk(KERN_DEBUG,
  733. "Initializing cipher [%s]; strlen = [%d]; "
  734. "key_size_bits = [%d]\n",
  735. crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
  736. crypt_stat->key_size << 3);
  737. if (crypt_stat->tfm) {
  738. rc = 0;
  739. goto out;
  740. }
  741. mutex_lock(&crypt_stat->cs_tfm_mutex);
  742. crypt_stat->tfm = crypto_alloc_tfm(crypt_stat->cipher,
  743. ECRYPTFS_DEFAULT_CHAINING_MODE
  744. | CRYPTO_TFM_REQ_WEAK_KEY);
  745. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  746. if (!crypt_stat->tfm) {
  747. ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
  748. "Error initializing cipher [%s]\n",
  749. crypt_stat->cipher);
  750. goto out;
  751. }
  752. rc = 0;
  753. out:
  754. return rc;
  755. }
  756. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  757. {
  758. int extent_size_tmp;
  759. crypt_stat->extent_mask = 0xFFFFFFFF;
  760. crypt_stat->extent_shift = 0;
  761. if (crypt_stat->extent_size == 0)
  762. return;
  763. extent_size_tmp = crypt_stat->extent_size;
  764. while ((extent_size_tmp & 0x01) == 0) {
  765. extent_size_tmp >>= 1;
  766. crypt_stat->extent_mask <<= 1;
  767. crypt_stat->extent_shift++;
  768. }
  769. }
  770. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  771. {
  772. /* Default values; may be overwritten as we are parsing the
  773. * packets. */
  774. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  775. set_extent_mask_and_shift(crypt_stat);
  776. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  777. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  778. crypt_stat->header_extent_size =
  779. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  780. } else
  781. crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
  782. crypt_stat->num_header_extents_at_front = 1;
  783. }
  784. /**
  785. * ecryptfs_compute_root_iv
  786. * @crypt_stats
  787. *
  788. * On error, sets the root IV to all 0's.
  789. */
  790. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  791. {
  792. int rc = 0;
  793. char dst[MD5_DIGEST_SIZE];
  794. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  795. BUG_ON(crypt_stat->iv_bytes <= 0);
  796. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) {
  797. rc = -EINVAL;
  798. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  799. "cannot generate root IV\n");
  800. goto out;
  801. }
  802. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  803. crypt_stat->key_size);
  804. if (rc) {
  805. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  806. "MD5 while generating root IV\n");
  807. goto out;
  808. }
  809. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  810. out:
  811. if (rc) {
  812. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  813. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  814. ECRYPTFS_SECURITY_WARNING);
  815. }
  816. return rc;
  817. }
  818. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  819. {
  820. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  821. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  822. ecryptfs_compute_root_iv(crypt_stat);
  823. if (unlikely(ecryptfs_verbosity > 0)) {
  824. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  825. ecryptfs_dump_hex(crypt_stat->key,
  826. crypt_stat->key_size);
  827. }
  828. }
  829. /**
  830. * ecryptfs_set_default_crypt_stat_vals
  831. * @crypt_stat
  832. *
  833. * Default values in the event that policy does not override them.
  834. */
  835. static void ecryptfs_set_default_crypt_stat_vals(
  836. struct ecryptfs_crypt_stat *crypt_stat,
  837. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  838. {
  839. ecryptfs_set_default_sizes(crypt_stat);
  840. strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
  841. crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
  842. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  843. crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
  844. crypt_stat->mount_crypt_stat = mount_crypt_stat;
  845. }
  846. /**
  847. * ecryptfs_new_file_context
  848. * @ecryptfs_dentry
  849. *
  850. * If the crypto context for the file has not yet been established,
  851. * this is where we do that. Establishing a new crypto context
  852. * involves the following decisions:
  853. * - What cipher to use?
  854. * - What set of authentication tokens to use?
  855. * Here we just worry about getting enough information into the
  856. * authentication tokens so that we know that they are available.
  857. * We associate the available authentication tokens with the new file
  858. * via the set of signatures in the crypt_stat struct. Later, when
  859. * the headers are actually written out, we may again defer to
  860. * userspace to perform the encryption of the session key; for the
  861. * foreseeable future, this will be the case with public key packets.
  862. *
  863. * Returns zero on success; non-zero otherwise
  864. */
  865. /* Associate an authentication token(s) with the file */
  866. int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
  867. {
  868. int rc = 0;
  869. struct ecryptfs_crypt_stat *crypt_stat =
  870. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  871. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  872. &ecryptfs_superblock_to_private(
  873. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  874. int cipher_name_len;
  875. ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
  876. /* See if there are mount crypt options */
  877. if (mount_crypt_stat->global_auth_tok) {
  878. ecryptfs_printk(KERN_DEBUG, "Initializing context for new "
  879. "file using mount_crypt_stat\n");
  880. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  881. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  882. memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++],
  883. mount_crypt_stat->global_auth_tok_sig,
  884. ECRYPTFS_SIG_SIZE_HEX);
  885. cipher_name_len =
  886. strlen(mount_crypt_stat->global_default_cipher_name);
  887. memcpy(crypt_stat->cipher,
  888. mount_crypt_stat->global_default_cipher_name,
  889. cipher_name_len);
  890. crypt_stat->cipher[cipher_name_len] = '\0';
  891. crypt_stat->key_size =
  892. mount_crypt_stat->global_default_cipher_key_size;
  893. ecryptfs_generate_new_key(crypt_stat);
  894. } else
  895. /* We should not encounter this scenario since we
  896. * should detect lack of global_auth_tok at mount time
  897. * TODO: Applies to 0.1 release only; remove in future
  898. * release */
  899. BUG();
  900. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  901. if (rc)
  902. ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
  903. "context for cipher [%s]: rc = [%d]\n",
  904. crypt_stat->cipher, rc);
  905. return rc;
  906. }
  907. /**
  908. * contains_ecryptfs_marker - check for the ecryptfs marker
  909. * @data: The data block in which to check
  910. *
  911. * Returns one if marker found; zero if not found
  912. */
  913. int contains_ecryptfs_marker(char *data)
  914. {
  915. u32 m_1, m_2;
  916. memcpy(&m_1, data, 4);
  917. m_1 = be32_to_cpu(m_1);
  918. memcpy(&m_2, (data + 4), 4);
  919. m_2 = be32_to_cpu(m_2);
  920. if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
  921. return 1;
  922. ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
  923. "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
  924. MAGIC_ECRYPTFS_MARKER);
  925. ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
  926. "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
  927. return 0;
  928. }
  929. struct ecryptfs_flag_map_elem {
  930. u32 file_flag;
  931. u32 local_flag;
  932. };
  933. /* Add support for additional flags by adding elements here. */
  934. static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
  935. {0x00000001, ECRYPTFS_ENABLE_HMAC},
  936. {0x00000002, ECRYPTFS_ENCRYPTED}
  937. };
  938. /**
  939. * ecryptfs_process_flags
  940. * @crypt_stat
  941. * @page_virt: Source data to be parsed
  942. * @bytes_read: Updated with the number of bytes read
  943. *
  944. * Returns zero on success; non-zero if the flag set is invalid
  945. */
  946. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  947. char *page_virt, int *bytes_read)
  948. {
  949. int rc = 0;
  950. int i;
  951. u32 flags;
  952. memcpy(&flags, page_virt, 4);
  953. flags = be32_to_cpu(flags);
  954. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  955. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  956. if (flags & ecryptfs_flag_map[i].file_flag) {
  957. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  958. ecryptfs_flag_map[i].local_flag);
  959. } else
  960. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
  961. ecryptfs_flag_map[i].local_flag);
  962. /* Version is in top 8 bits of the 32-bit flag vector */
  963. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  964. (*bytes_read) = 4;
  965. return rc;
  966. }
  967. /**
  968. * write_ecryptfs_marker
  969. * @page_virt: The pointer to in a page to begin writing the marker
  970. * @written: Number of bytes written
  971. *
  972. * Marker = 0x3c81b7f5
  973. */
  974. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  975. {
  976. u32 m_1, m_2;
  977. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  978. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  979. m_1 = cpu_to_be32(m_1);
  980. memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  981. m_2 = cpu_to_be32(m_2);
  982. memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
  983. (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  984. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  985. }
  986. static void
  987. write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
  988. size_t *written)
  989. {
  990. u32 flags = 0;
  991. int i;
  992. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  993. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  994. if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  995. ecryptfs_flag_map[i].local_flag))
  996. flags |= ecryptfs_flag_map[i].file_flag;
  997. /* Version is in top 8 bits of the 32-bit flag vector */
  998. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  999. flags = cpu_to_be32(flags);
  1000. memcpy(page_virt, &flags, 4);
  1001. (*written) = 4;
  1002. }
  1003. struct ecryptfs_cipher_code_str_map_elem {
  1004. char cipher_str[16];
  1005. u16 cipher_code;
  1006. };
  1007. /* Add support for additional ciphers by adding elements here. The
  1008. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1009. * ciphers. List in order of probability. */
  1010. static struct ecryptfs_cipher_code_str_map_elem
  1011. ecryptfs_cipher_code_str_map[] = {
  1012. {"aes",RFC2440_CIPHER_AES_128 },
  1013. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1014. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1015. {"cast5", RFC2440_CIPHER_CAST_5},
  1016. {"twofish", RFC2440_CIPHER_TWOFISH},
  1017. {"cast6", RFC2440_CIPHER_CAST_6},
  1018. {"aes", RFC2440_CIPHER_AES_192},
  1019. {"aes", RFC2440_CIPHER_AES_256}
  1020. };
  1021. /**
  1022. * ecryptfs_code_for_cipher_string
  1023. * @str: The string representing the cipher name
  1024. *
  1025. * Returns zero on no match, or the cipher code on match
  1026. */
  1027. u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1028. {
  1029. int i;
  1030. u16 code = 0;
  1031. struct ecryptfs_cipher_code_str_map_elem *map =
  1032. ecryptfs_cipher_code_str_map;
  1033. if (strcmp(crypt_stat->cipher, "aes") == 0) {
  1034. switch (crypt_stat->key_size) {
  1035. case 16:
  1036. code = RFC2440_CIPHER_AES_128;
  1037. break;
  1038. case 24:
  1039. code = RFC2440_CIPHER_AES_192;
  1040. break;
  1041. case 32:
  1042. code = RFC2440_CIPHER_AES_256;
  1043. }
  1044. } else {
  1045. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1046. if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
  1047. code = map[i].cipher_code;
  1048. break;
  1049. }
  1050. }
  1051. return code;
  1052. }
  1053. /**
  1054. * ecryptfs_cipher_code_to_string
  1055. * @str: Destination to write out the cipher name
  1056. * @cipher_code: The code to convert to cipher name string
  1057. *
  1058. * Returns zero on success
  1059. */
  1060. int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
  1061. {
  1062. int rc = 0;
  1063. int i;
  1064. str[0] = '\0';
  1065. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1066. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1067. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1068. if (str[0] == '\0') {
  1069. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1070. "[%d]\n", cipher_code);
  1071. rc = -EINVAL;
  1072. }
  1073. return rc;
  1074. }
  1075. /**
  1076. * ecryptfs_read_header_region
  1077. * @data
  1078. * @dentry
  1079. * @nd
  1080. *
  1081. * Returns zero on success; non-zero otherwise
  1082. */
  1083. int ecryptfs_read_header_region(char *data, struct dentry *dentry,
  1084. struct vfsmount *mnt)
  1085. {
  1086. struct file *file;
  1087. mm_segment_t oldfs;
  1088. int rc;
  1089. mnt = mntget(mnt);
  1090. file = dentry_open(dentry, mnt, O_RDONLY);
  1091. if (IS_ERR(file)) {
  1092. ecryptfs_printk(KERN_DEBUG, "Error opening file to "
  1093. "read header region\n");
  1094. mntput(mnt);
  1095. rc = PTR_ERR(file);
  1096. goto out;
  1097. }
  1098. file->f_pos = 0;
  1099. oldfs = get_fs();
  1100. set_fs(get_ds());
  1101. /* For releases 0.1 and 0.2, all of the header information
  1102. * fits in the first data extent-sized region. */
  1103. rc = file->f_op->read(file, (char __user *)data,
  1104. ECRYPTFS_DEFAULT_EXTENT_SIZE, &file->f_pos);
  1105. set_fs(oldfs);
  1106. fput(file);
  1107. rc = 0;
  1108. out:
  1109. return rc;
  1110. }
  1111. static void
  1112. write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat,
  1113. size_t *written)
  1114. {
  1115. u32 header_extent_size;
  1116. u16 num_header_extents_at_front;
  1117. header_extent_size = (u32)crypt_stat->header_extent_size;
  1118. num_header_extents_at_front =
  1119. (u16)crypt_stat->num_header_extents_at_front;
  1120. header_extent_size = cpu_to_be32(header_extent_size);
  1121. memcpy(virt, &header_extent_size, 4);
  1122. virt += 4;
  1123. num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
  1124. memcpy(virt, &num_header_extents_at_front, 2);
  1125. (*written) = 6;
  1126. }
  1127. struct kmem_cache *ecryptfs_header_cache_0;
  1128. struct kmem_cache *ecryptfs_header_cache_1;
  1129. struct kmem_cache *ecryptfs_header_cache_2;
  1130. /**
  1131. * ecryptfs_write_headers_virt
  1132. * @page_virt
  1133. * @crypt_stat
  1134. * @ecryptfs_dentry
  1135. *
  1136. * Format version: 1
  1137. *
  1138. * Header Extent:
  1139. * Octets 0-7: Unencrypted file size (big-endian)
  1140. * Octets 8-15: eCryptfs special marker
  1141. * Octets 16-19: Flags
  1142. * Octet 16: File format version number (between 0 and 255)
  1143. * Octets 17-18: Reserved
  1144. * Octet 19: Bit 1 (lsb): Reserved
  1145. * Bit 2: Encrypted?
  1146. * Bits 3-8: Reserved
  1147. * Octets 20-23: Header extent size (big-endian)
  1148. * Octets 24-25: Number of header extents at front of file
  1149. * (big-endian)
  1150. * Octet 26: Begin RFC 2440 authentication token packet set
  1151. * Data Extent 0:
  1152. * Lower data (CBC encrypted)
  1153. * Data Extent 1:
  1154. * Lower data (CBC encrypted)
  1155. * ...
  1156. *
  1157. * Returns zero on success
  1158. */
  1159. int ecryptfs_write_headers_virt(char *page_virt,
  1160. struct ecryptfs_crypt_stat *crypt_stat,
  1161. struct dentry *ecryptfs_dentry)
  1162. {
  1163. int rc;
  1164. size_t written;
  1165. size_t offset;
  1166. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1167. write_ecryptfs_marker((page_virt + offset), &written);
  1168. offset += written;
  1169. write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
  1170. offset += written;
  1171. write_header_metadata((page_virt + offset), crypt_stat, &written);
  1172. offset += written;
  1173. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1174. ecryptfs_dentry, &written,
  1175. PAGE_CACHE_SIZE - offset);
  1176. if (rc)
  1177. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1178. "set; rc = [%d]\n", rc);
  1179. return rc;
  1180. }
  1181. /**
  1182. * ecryptfs_write_headers
  1183. * @lower_file: The lower file struct, which was returned from dentry_open
  1184. *
  1185. * Write the file headers out. This will likely involve a userspace
  1186. * callout, in which the session key is encrypted with one or more
  1187. * public keys and/or the passphrase necessary to do the encryption is
  1188. * retrieved via a prompt. Exactly what happens at this point should
  1189. * be policy-dependent.
  1190. *
  1191. * Returns zero on success; non-zero on error
  1192. */
  1193. int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
  1194. struct file *lower_file)
  1195. {
  1196. mm_segment_t oldfs;
  1197. struct ecryptfs_crypt_stat *crypt_stat;
  1198. char *page_virt;
  1199. int current_header_page;
  1200. int header_pages;
  1201. int rc = 0;
  1202. crypt_stat = &ecryptfs_inode_to_private(
  1203. ecryptfs_dentry->d_inode)->crypt_stat;
  1204. if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  1205. ECRYPTFS_ENCRYPTED))) {
  1206. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  1207. ECRYPTFS_KEY_VALID)) {
  1208. ecryptfs_printk(KERN_DEBUG, "Key is "
  1209. "invalid; bailing out\n");
  1210. rc = -EINVAL;
  1211. goto out;
  1212. }
  1213. } else {
  1214. rc = -EINVAL;
  1215. ecryptfs_printk(KERN_WARNING,
  1216. "Called with crypt_stat->encrypted == 0\n");
  1217. goto out;
  1218. }
  1219. /* Released in this function */
  1220. page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, SLAB_USER);
  1221. if (!page_virt) {
  1222. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  1223. rc = -ENOMEM;
  1224. goto out;
  1225. }
  1226. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1227. rc = ecryptfs_write_headers_virt(page_virt, crypt_stat,
  1228. ecryptfs_dentry);
  1229. if (unlikely(rc)) {
  1230. ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
  1231. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1232. goto out_free;
  1233. }
  1234. ecryptfs_printk(KERN_DEBUG,
  1235. "Writing key packet set to underlying file\n");
  1236. lower_file->f_pos = 0;
  1237. oldfs = get_fs();
  1238. set_fs(get_ds());
  1239. ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
  1240. "write() w/ header page; lower_file->f_pos = "
  1241. "[0x%.16x]\n", lower_file->f_pos);
  1242. lower_file->f_op->write(lower_file, (char __user *)page_virt,
  1243. PAGE_CACHE_SIZE, &lower_file->f_pos);
  1244. header_pages = ((crypt_stat->header_extent_size
  1245. * crypt_stat->num_header_extents_at_front)
  1246. / PAGE_CACHE_SIZE);
  1247. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1248. current_header_page = 1;
  1249. while (current_header_page < header_pages) {
  1250. ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
  1251. "write() w/ zero'd page; lower_file->f_pos = "
  1252. "[0x%.16x]\n", lower_file->f_pos);
  1253. lower_file->f_op->write(lower_file, (char __user *)page_virt,
  1254. PAGE_CACHE_SIZE, &lower_file->f_pos);
  1255. current_header_page++;
  1256. }
  1257. set_fs(oldfs);
  1258. ecryptfs_printk(KERN_DEBUG,
  1259. "Done writing key packet set to underlying file.\n");
  1260. out_free:
  1261. kmem_cache_free(ecryptfs_header_cache_0, page_virt);
  1262. out:
  1263. return rc;
  1264. }
  1265. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1266. char *virt, int *bytes_read)
  1267. {
  1268. int rc = 0;
  1269. u32 header_extent_size;
  1270. u16 num_header_extents_at_front;
  1271. memcpy(&header_extent_size, virt, 4);
  1272. header_extent_size = be32_to_cpu(header_extent_size);
  1273. virt += 4;
  1274. memcpy(&num_header_extents_at_front, virt, 2);
  1275. num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
  1276. crypt_stat->header_extent_size = (int)header_extent_size;
  1277. crypt_stat->num_header_extents_at_front =
  1278. (int)num_header_extents_at_front;
  1279. (*bytes_read) = 6;
  1280. if ((crypt_stat->header_extent_size
  1281. * crypt_stat->num_header_extents_at_front)
  1282. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  1283. rc = -EINVAL;
  1284. ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
  1285. "[%d]\n", crypt_stat->header_extent_size);
  1286. }
  1287. return rc;
  1288. }
  1289. /**
  1290. * set_default_header_data
  1291. *
  1292. * For version 0 file format; this function is only for backwards
  1293. * compatibility for files created with the prior versions of
  1294. * eCryptfs.
  1295. */
  1296. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1297. {
  1298. crypt_stat->header_extent_size = 4096;
  1299. crypt_stat->num_header_extents_at_front = 1;
  1300. }
  1301. /**
  1302. * ecryptfs_read_headers_virt
  1303. *
  1304. * Read/parse the header data. The header format is detailed in the
  1305. * comment block for the ecryptfs_write_headers_virt() function.
  1306. *
  1307. * Returns zero on success
  1308. */
  1309. static int ecryptfs_read_headers_virt(char *page_virt,
  1310. struct ecryptfs_crypt_stat *crypt_stat,
  1311. struct dentry *ecryptfs_dentry)
  1312. {
  1313. int rc = 0;
  1314. int offset;
  1315. int bytes_read;
  1316. ecryptfs_set_default_sizes(crypt_stat);
  1317. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1318. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1319. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1320. rc = contains_ecryptfs_marker(page_virt + offset);
  1321. if (rc == 0) {
  1322. rc = -EINVAL;
  1323. goto out;
  1324. }
  1325. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1326. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1327. &bytes_read);
  1328. if (rc) {
  1329. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1330. goto out;
  1331. }
  1332. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1333. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1334. "file version [%d] is supported by this "
  1335. "version of eCryptfs\n",
  1336. crypt_stat->file_version,
  1337. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1338. rc = -EINVAL;
  1339. goto out;
  1340. }
  1341. offset += bytes_read;
  1342. if (crypt_stat->file_version >= 1) {
  1343. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1344. &bytes_read);
  1345. if (rc) {
  1346. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1347. "metadata; rc = [%d]\n", rc);
  1348. }
  1349. offset += bytes_read;
  1350. } else
  1351. set_default_header_data(crypt_stat);
  1352. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1353. ecryptfs_dentry);
  1354. out:
  1355. return rc;
  1356. }
  1357. /**
  1358. * ecryptfs_read_headers
  1359. *
  1360. * Returns zero if valid headers found and parsed; non-zero otherwise
  1361. */
  1362. int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
  1363. struct file *lower_file)
  1364. {
  1365. int rc = 0;
  1366. char *page_virt = NULL;
  1367. mm_segment_t oldfs;
  1368. ssize_t bytes_read;
  1369. struct ecryptfs_crypt_stat *crypt_stat =
  1370. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1371. /* Read the first page from the underlying file */
  1372. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, SLAB_USER);
  1373. if (!page_virt) {
  1374. rc = -ENOMEM;
  1375. ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
  1376. goto out;
  1377. }
  1378. lower_file->f_pos = 0;
  1379. oldfs = get_fs();
  1380. set_fs(get_ds());
  1381. bytes_read = lower_file->f_op->read(lower_file,
  1382. (char __user *)page_virt,
  1383. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  1384. &lower_file->f_pos);
  1385. set_fs(oldfs);
  1386. if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
  1387. rc = -EINVAL;
  1388. goto out;
  1389. }
  1390. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1391. ecryptfs_dentry);
  1392. if (rc) {
  1393. ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not "
  1394. "found\n");
  1395. rc = -EINVAL;
  1396. }
  1397. out:
  1398. if (page_virt) {
  1399. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1400. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1401. }
  1402. return rc;
  1403. }
  1404. /**
  1405. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1406. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1407. * @name: The plaintext name
  1408. * @length: The length of the plaintext
  1409. * @encoded_name: The encypted name
  1410. *
  1411. * Encrypts and encodes a filename into something that constitutes a
  1412. * valid filename for a filesystem, with printable characters.
  1413. *
  1414. * We assume that we have a properly initialized crypto context,
  1415. * pointed to by crypt_stat->tfm.
  1416. *
  1417. * TODO: Implement filename decoding and decryption here, in place of
  1418. * memcpy. We are keeping the framework around for now to (1)
  1419. * facilitate testing of the components needed to implement filename
  1420. * encryption and (2) to provide a code base from which other
  1421. * developers in the community can easily implement this feature.
  1422. *
  1423. * Returns the length of encoded filename; negative if error
  1424. */
  1425. int
  1426. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1427. const char *name, int length, char **encoded_name)
  1428. {
  1429. int error = 0;
  1430. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1431. if (!(*encoded_name)) {
  1432. error = -ENOMEM;
  1433. goto out;
  1434. }
  1435. /* TODO: Filename encryption is a scheduled feature for a
  1436. * future version of eCryptfs. This function is here only for
  1437. * the purpose of providing a framework for other developers
  1438. * to easily implement filename encryption. Hint: Replace this
  1439. * memcpy() with a call to encrypt and encode the
  1440. * filename, the set the length accordingly. */
  1441. memcpy((void *)(*encoded_name), (void *)name, length);
  1442. (*encoded_name)[length] = '\0';
  1443. error = length + 1;
  1444. out:
  1445. return error;
  1446. }
  1447. /**
  1448. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1449. * @crypt_stat: The crypt_stat struct associated with the file
  1450. * @name: The filename in cipher text
  1451. * @length: The length of the cipher text name
  1452. * @decrypted_name: The plaintext name
  1453. *
  1454. * Decodes and decrypts the filename.
  1455. *
  1456. * We assume that we have a properly initialized crypto context,
  1457. * pointed to by crypt_stat->tfm.
  1458. *
  1459. * TODO: Implement filename decoding and decryption here, in place of
  1460. * memcpy. We are keeping the framework around for now to (1)
  1461. * facilitate testing of the components needed to implement filename
  1462. * encryption and (2) to provide a code base from which other
  1463. * developers in the community can easily implement this feature.
  1464. *
  1465. * Returns the length of decoded filename; negative if error
  1466. */
  1467. int
  1468. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1469. const char *name, int length, char **decrypted_name)
  1470. {
  1471. int error = 0;
  1472. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1473. if (!(*decrypted_name)) {
  1474. error = -ENOMEM;
  1475. goto out;
  1476. }
  1477. /* TODO: Filename encryption is a scheduled feature for a
  1478. * future version of eCryptfs. This function is here only for
  1479. * the purpose of providing a framework for other developers
  1480. * to easily implement filename encryption. Hint: Replace this
  1481. * memcpy() with a call to decode and decrypt the
  1482. * filename, the set the length accordingly. */
  1483. memcpy((void *)(*decrypted_name), (void *)name, length);
  1484. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1485. * in printing out the
  1486. * string in debug
  1487. * messages */
  1488. error = length;
  1489. out:
  1490. return error;
  1491. }
  1492. /**
  1493. * ecryptfs_process_cipher - Perform cipher initialization.
  1494. * @tfm: Crypto context set by this function
  1495. * @key_tfm: Crypto context for key material, set by this function
  1496. * @cipher_name: Name of the cipher.
  1497. * @key_size: Size of the key in bytes.
  1498. *
  1499. * Returns zero on success. Any crypto_tfm structs allocated here
  1500. * should be released by other functions, such as on a superblock put
  1501. * event, regardless of whether this function succeeds for fails.
  1502. */
  1503. int
  1504. ecryptfs_process_cipher(struct crypto_tfm **tfm, struct crypto_tfm **key_tfm,
  1505. char *cipher_name, size_t key_size)
  1506. {
  1507. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1508. int rc;
  1509. *tfm = *key_tfm = NULL;
  1510. if (key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1511. rc = -EINVAL;
  1512. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1513. "allowable is [%d]\n", key_size, ECRYPTFS_MAX_KEY_BYTES);
  1514. goto out;
  1515. }
  1516. *tfm = crypto_alloc_tfm(cipher_name, (ECRYPTFS_DEFAULT_CHAINING_MODE
  1517. | CRYPTO_TFM_REQ_WEAK_KEY));
  1518. if (!(*tfm)) {
  1519. rc = -EINVAL;
  1520. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1521. "[%s]\n", cipher_name);
  1522. goto out;
  1523. }
  1524. *key_tfm = crypto_alloc_tfm(cipher_name, CRYPTO_TFM_REQ_WEAK_KEY);
  1525. if (!(*key_tfm)) {
  1526. rc = -EINVAL;
  1527. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1528. "[%s]\n", cipher_name);
  1529. goto out;
  1530. }
  1531. if (key_size < crypto_tfm_alg_min_keysize(*tfm)) {
  1532. rc = -EINVAL;
  1533. printk(KERN_ERR "Request key size is [%Zd]; minimum key size "
  1534. "supported by cipher [%s] is [%d]\n", key_size,
  1535. cipher_name, crypto_tfm_alg_min_keysize(*tfm));
  1536. goto out;
  1537. }
  1538. if (key_size < crypto_tfm_alg_min_keysize(*key_tfm)) {
  1539. rc = -EINVAL;
  1540. printk(KERN_ERR "Request key size is [%Zd]; minimum key size "
  1541. "supported by cipher [%s] is [%d]\n", key_size,
  1542. cipher_name, crypto_tfm_alg_min_keysize(*key_tfm));
  1543. goto out;
  1544. }
  1545. if (key_size > crypto_tfm_alg_max_keysize(*tfm)) {
  1546. rc = -EINVAL;
  1547. printk(KERN_ERR "Request key size is [%Zd]; maximum key size "
  1548. "supported by cipher [%s] is [%d]\n", key_size,
  1549. cipher_name, crypto_tfm_alg_min_keysize(*tfm));
  1550. goto out;
  1551. }
  1552. if (key_size > crypto_tfm_alg_max_keysize(*key_tfm)) {
  1553. rc = -EINVAL;
  1554. printk(KERN_ERR "Request key size is [%Zd]; maximum key size "
  1555. "supported by cipher [%s] is [%d]\n", key_size,
  1556. cipher_name, crypto_tfm_alg_min_keysize(*key_tfm));
  1557. goto out;
  1558. }
  1559. get_random_bytes(dummy_key, key_size);
  1560. rc = crypto_cipher_setkey(*tfm, dummy_key, key_size);
  1561. if (rc) {
  1562. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1563. "cipher [%s]; rc = [%d]\n", key_size, cipher_name, rc);
  1564. rc = -EINVAL;
  1565. goto out;
  1566. }
  1567. rc = crypto_cipher_setkey(*key_tfm, dummy_key, key_size);
  1568. if (rc) {
  1569. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1570. "cipher [%s]; rc = [%d]\n", key_size, cipher_name, rc);
  1571. rc = -EINVAL;
  1572. goto out;
  1573. }
  1574. out:
  1575. return rc;
  1576. }