crypto.c 50 KB

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