crypto.c 48 KB

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