crypto.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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, CRYPTO_TFM_REQ_WEAK_KEY);
  794. mutex_unlock(&crypt_stat->cs_tfm_mutex);
  795. rc = 0;
  796. out:
  797. return rc;
  798. }
  799. static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
  800. {
  801. int extent_size_tmp;
  802. crypt_stat->extent_mask = 0xFFFFFFFF;
  803. crypt_stat->extent_shift = 0;
  804. if (crypt_stat->extent_size == 0)
  805. return;
  806. extent_size_tmp = crypt_stat->extent_size;
  807. while ((extent_size_tmp & 0x01) == 0) {
  808. extent_size_tmp >>= 1;
  809. crypt_stat->extent_mask <<= 1;
  810. crypt_stat->extent_shift++;
  811. }
  812. }
  813. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
  814. {
  815. /* Default values; may be overwritten as we are parsing the
  816. * packets. */
  817. crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
  818. set_extent_mask_and_shift(crypt_stat);
  819. crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
  820. if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  821. crypt_stat->header_extent_size =
  822. ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
  823. } else
  824. crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
  825. crypt_stat->num_header_extents_at_front = 1;
  826. }
  827. /**
  828. * ecryptfs_compute_root_iv
  829. * @crypt_stats
  830. *
  831. * On error, sets the root IV to all 0's.
  832. */
  833. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
  834. {
  835. int rc = 0;
  836. char dst[MD5_DIGEST_SIZE];
  837. BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
  838. BUG_ON(crypt_stat->iv_bytes <= 0);
  839. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) {
  840. rc = -EINVAL;
  841. ecryptfs_printk(KERN_WARNING, "Session key not valid; "
  842. "cannot generate root IV\n");
  843. goto out;
  844. }
  845. rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
  846. crypt_stat->key_size);
  847. if (rc) {
  848. ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
  849. "MD5 while generating root IV\n");
  850. goto out;
  851. }
  852. memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
  853. out:
  854. if (rc) {
  855. memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
  856. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  857. ECRYPTFS_SECURITY_WARNING);
  858. }
  859. return rc;
  860. }
  861. static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
  862. {
  863. get_random_bytes(crypt_stat->key, crypt_stat->key_size);
  864. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  865. ecryptfs_compute_root_iv(crypt_stat);
  866. if (unlikely(ecryptfs_verbosity > 0)) {
  867. ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
  868. ecryptfs_dump_hex(crypt_stat->key,
  869. crypt_stat->key_size);
  870. }
  871. }
  872. /**
  873. * ecryptfs_set_default_crypt_stat_vals
  874. * @crypt_stat
  875. *
  876. * Default values in the event that policy does not override them.
  877. */
  878. static void ecryptfs_set_default_crypt_stat_vals(
  879. struct ecryptfs_crypt_stat *crypt_stat,
  880. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  881. {
  882. ecryptfs_set_default_sizes(crypt_stat);
  883. strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
  884. crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
  885. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  886. crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
  887. crypt_stat->mount_crypt_stat = mount_crypt_stat;
  888. }
  889. /**
  890. * ecryptfs_new_file_context
  891. * @ecryptfs_dentry
  892. *
  893. * If the crypto context for the file has not yet been established,
  894. * this is where we do that. Establishing a new crypto context
  895. * involves the following decisions:
  896. * - What cipher to use?
  897. * - What set of authentication tokens to use?
  898. * Here we just worry about getting enough information into the
  899. * authentication tokens so that we know that they are available.
  900. * We associate the available authentication tokens with the new file
  901. * via the set of signatures in the crypt_stat struct. Later, when
  902. * the headers are actually written out, we may again defer to
  903. * userspace to perform the encryption of the session key; for the
  904. * foreseeable future, this will be the case with public key packets.
  905. *
  906. * Returns zero on success; non-zero otherwise
  907. */
  908. /* Associate an authentication token(s) with the file */
  909. int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
  910. {
  911. int rc = 0;
  912. struct ecryptfs_crypt_stat *crypt_stat =
  913. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  914. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  915. &ecryptfs_superblock_to_private(
  916. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  917. int cipher_name_len;
  918. ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
  919. /* See if there are mount crypt options */
  920. if (mount_crypt_stat->global_auth_tok) {
  921. ecryptfs_printk(KERN_DEBUG, "Initializing context for new "
  922. "file using mount_crypt_stat\n");
  923. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  924. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  925. memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++],
  926. mount_crypt_stat->global_auth_tok_sig,
  927. ECRYPTFS_SIG_SIZE_HEX);
  928. cipher_name_len =
  929. strlen(mount_crypt_stat->global_default_cipher_name);
  930. memcpy(crypt_stat->cipher,
  931. mount_crypt_stat->global_default_cipher_name,
  932. cipher_name_len);
  933. crypt_stat->cipher[cipher_name_len] = '\0';
  934. crypt_stat->key_size =
  935. mount_crypt_stat->global_default_cipher_key_size;
  936. ecryptfs_generate_new_key(crypt_stat);
  937. } else
  938. /* We should not encounter this scenario since we
  939. * should detect lack of global_auth_tok at mount time
  940. * TODO: Applies to 0.1 release only; remove in future
  941. * release */
  942. BUG();
  943. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  944. if (rc)
  945. ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
  946. "context for cipher [%s]: rc = [%d]\n",
  947. crypt_stat->cipher, rc);
  948. return rc;
  949. }
  950. /**
  951. * contains_ecryptfs_marker - check for the ecryptfs marker
  952. * @data: The data block in which to check
  953. *
  954. * Returns one if marker found; zero if not found
  955. */
  956. int contains_ecryptfs_marker(char *data)
  957. {
  958. u32 m_1, m_2;
  959. memcpy(&m_1, data, 4);
  960. m_1 = be32_to_cpu(m_1);
  961. memcpy(&m_2, (data + 4), 4);
  962. m_2 = be32_to_cpu(m_2);
  963. if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
  964. return 1;
  965. ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
  966. "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
  967. MAGIC_ECRYPTFS_MARKER);
  968. ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
  969. "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
  970. return 0;
  971. }
  972. struct ecryptfs_flag_map_elem {
  973. u32 file_flag;
  974. u32 local_flag;
  975. };
  976. /* Add support for additional flags by adding elements here. */
  977. static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
  978. {0x00000001, ECRYPTFS_ENABLE_HMAC},
  979. {0x00000002, ECRYPTFS_ENCRYPTED}
  980. };
  981. /**
  982. * ecryptfs_process_flags
  983. * @crypt_stat
  984. * @page_virt: Source data to be parsed
  985. * @bytes_read: Updated with the number of bytes read
  986. *
  987. * Returns zero on success; non-zero if the flag set is invalid
  988. */
  989. static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
  990. char *page_virt, int *bytes_read)
  991. {
  992. int rc = 0;
  993. int i;
  994. u32 flags;
  995. memcpy(&flags, page_virt, 4);
  996. flags = be32_to_cpu(flags);
  997. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  998. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  999. if (flags & ecryptfs_flag_map[i].file_flag) {
  1000. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  1001. ecryptfs_flag_map[i].local_flag);
  1002. } else
  1003. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
  1004. ecryptfs_flag_map[i].local_flag);
  1005. /* Version is in top 8 bits of the 32-bit flag vector */
  1006. crypt_stat->file_version = ((flags >> 24) & 0xFF);
  1007. (*bytes_read) = 4;
  1008. return rc;
  1009. }
  1010. /**
  1011. * write_ecryptfs_marker
  1012. * @page_virt: The pointer to in a page to begin writing the marker
  1013. * @written: Number of bytes written
  1014. *
  1015. * Marker = 0x3c81b7f5
  1016. */
  1017. static void write_ecryptfs_marker(char *page_virt, size_t *written)
  1018. {
  1019. u32 m_1, m_2;
  1020. get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1021. m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
  1022. m_1 = cpu_to_be32(m_1);
  1023. memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1024. m_2 = cpu_to_be32(m_2);
  1025. memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
  1026. (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
  1027. (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1028. }
  1029. static void
  1030. write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
  1031. size_t *written)
  1032. {
  1033. u32 flags = 0;
  1034. int i;
  1035. for (i = 0; i < ((sizeof(ecryptfs_flag_map)
  1036. / sizeof(struct ecryptfs_flag_map_elem))); i++)
  1037. if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  1038. ecryptfs_flag_map[i].local_flag))
  1039. flags |= ecryptfs_flag_map[i].file_flag;
  1040. /* Version is in top 8 bits of the 32-bit flag vector */
  1041. flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
  1042. flags = cpu_to_be32(flags);
  1043. memcpy(page_virt, &flags, 4);
  1044. (*written) = 4;
  1045. }
  1046. struct ecryptfs_cipher_code_str_map_elem {
  1047. char cipher_str[16];
  1048. u16 cipher_code;
  1049. };
  1050. /* Add support for additional ciphers by adding elements here. The
  1051. * cipher_code is whatever OpenPGP applicatoins use to identify the
  1052. * ciphers. List in order of probability. */
  1053. static struct ecryptfs_cipher_code_str_map_elem
  1054. ecryptfs_cipher_code_str_map[] = {
  1055. {"aes",RFC2440_CIPHER_AES_128 },
  1056. {"blowfish", RFC2440_CIPHER_BLOWFISH},
  1057. {"des3_ede", RFC2440_CIPHER_DES3_EDE},
  1058. {"cast5", RFC2440_CIPHER_CAST_5},
  1059. {"twofish", RFC2440_CIPHER_TWOFISH},
  1060. {"cast6", RFC2440_CIPHER_CAST_6},
  1061. {"aes", RFC2440_CIPHER_AES_192},
  1062. {"aes", RFC2440_CIPHER_AES_256}
  1063. };
  1064. /**
  1065. * ecryptfs_code_for_cipher_string
  1066. * @str: The string representing the cipher name
  1067. *
  1068. * Returns zero on no match, or the cipher code on match
  1069. */
  1070. u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
  1071. {
  1072. int i;
  1073. u16 code = 0;
  1074. struct ecryptfs_cipher_code_str_map_elem *map =
  1075. ecryptfs_cipher_code_str_map;
  1076. if (strcmp(crypt_stat->cipher, "aes") == 0) {
  1077. switch (crypt_stat->key_size) {
  1078. case 16:
  1079. code = RFC2440_CIPHER_AES_128;
  1080. break;
  1081. case 24:
  1082. code = RFC2440_CIPHER_AES_192;
  1083. break;
  1084. case 32:
  1085. code = RFC2440_CIPHER_AES_256;
  1086. }
  1087. } else {
  1088. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1089. if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
  1090. code = map[i].cipher_code;
  1091. break;
  1092. }
  1093. }
  1094. return code;
  1095. }
  1096. /**
  1097. * ecryptfs_cipher_code_to_string
  1098. * @str: Destination to write out the cipher name
  1099. * @cipher_code: The code to convert to cipher name string
  1100. *
  1101. * Returns zero on success
  1102. */
  1103. int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
  1104. {
  1105. int rc = 0;
  1106. int i;
  1107. str[0] = '\0';
  1108. for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
  1109. if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
  1110. strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
  1111. if (str[0] == '\0') {
  1112. ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
  1113. "[%d]\n", cipher_code);
  1114. rc = -EINVAL;
  1115. }
  1116. return rc;
  1117. }
  1118. /**
  1119. * ecryptfs_read_header_region
  1120. * @data
  1121. * @dentry
  1122. * @nd
  1123. *
  1124. * Returns zero on success; non-zero otherwise
  1125. */
  1126. int ecryptfs_read_header_region(char *data, struct dentry *dentry,
  1127. struct vfsmount *mnt)
  1128. {
  1129. struct file *lower_file;
  1130. mm_segment_t oldfs;
  1131. int rc;
  1132. if ((rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt,
  1133. O_RDONLY))) {
  1134. printk(KERN_ERR
  1135. "Error opening lower_file to read header region\n");
  1136. goto out;
  1137. }
  1138. lower_file->f_pos = 0;
  1139. oldfs = get_fs();
  1140. set_fs(get_ds());
  1141. /* For releases 0.1 and 0.2, all of the header information
  1142. * fits in the first data extent-sized region. */
  1143. rc = lower_file->f_op->read(lower_file, (char __user *)data,
  1144. ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos);
  1145. set_fs(oldfs);
  1146. if ((rc = ecryptfs_close_lower_file(lower_file))) {
  1147. printk(KERN_ERR "Error closing lower_file\n");
  1148. goto out;
  1149. }
  1150. rc = 0;
  1151. out:
  1152. return rc;
  1153. }
  1154. static void
  1155. write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat,
  1156. size_t *written)
  1157. {
  1158. u32 header_extent_size;
  1159. u16 num_header_extents_at_front;
  1160. header_extent_size = (u32)crypt_stat->header_extent_size;
  1161. num_header_extents_at_front =
  1162. (u16)crypt_stat->num_header_extents_at_front;
  1163. header_extent_size = cpu_to_be32(header_extent_size);
  1164. memcpy(virt, &header_extent_size, 4);
  1165. virt += 4;
  1166. num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
  1167. memcpy(virt, &num_header_extents_at_front, 2);
  1168. (*written) = 6;
  1169. }
  1170. struct kmem_cache *ecryptfs_header_cache_0;
  1171. struct kmem_cache *ecryptfs_header_cache_1;
  1172. struct kmem_cache *ecryptfs_header_cache_2;
  1173. /**
  1174. * ecryptfs_write_headers_virt
  1175. * @page_virt
  1176. * @crypt_stat
  1177. * @ecryptfs_dentry
  1178. *
  1179. * Format version: 1
  1180. *
  1181. * Header Extent:
  1182. * Octets 0-7: Unencrypted file size (big-endian)
  1183. * Octets 8-15: eCryptfs special marker
  1184. * Octets 16-19: Flags
  1185. * Octet 16: File format version number (between 0 and 255)
  1186. * Octets 17-18: Reserved
  1187. * Octet 19: Bit 1 (lsb): Reserved
  1188. * Bit 2: Encrypted?
  1189. * Bits 3-8: Reserved
  1190. * Octets 20-23: Header extent size (big-endian)
  1191. * Octets 24-25: Number of header extents at front of file
  1192. * (big-endian)
  1193. * Octet 26: Begin RFC 2440 authentication token packet set
  1194. * Data Extent 0:
  1195. * Lower data (CBC encrypted)
  1196. * Data Extent 1:
  1197. * Lower data (CBC encrypted)
  1198. * ...
  1199. *
  1200. * Returns zero on success
  1201. */
  1202. int ecryptfs_write_headers_virt(char *page_virt,
  1203. struct ecryptfs_crypt_stat *crypt_stat,
  1204. struct dentry *ecryptfs_dentry)
  1205. {
  1206. int rc;
  1207. size_t written;
  1208. size_t offset;
  1209. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1210. write_ecryptfs_marker((page_virt + offset), &written);
  1211. offset += written;
  1212. write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
  1213. offset += written;
  1214. write_header_metadata((page_virt + offset), crypt_stat, &written);
  1215. offset += written;
  1216. rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
  1217. ecryptfs_dentry, &written,
  1218. PAGE_CACHE_SIZE - offset);
  1219. if (rc)
  1220. ecryptfs_printk(KERN_WARNING, "Error generating key packet "
  1221. "set; rc = [%d]\n", rc);
  1222. return rc;
  1223. }
  1224. /**
  1225. * ecryptfs_write_headers
  1226. * @lower_file: The lower file struct, which was returned from dentry_open
  1227. *
  1228. * Write the file headers out. This will likely involve a userspace
  1229. * callout, in which the session key is encrypted with one or more
  1230. * public keys and/or the passphrase necessary to do the encryption is
  1231. * retrieved via a prompt. Exactly what happens at this point should
  1232. * be policy-dependent.
  1233. *
  1234. * Returns zero on success; non-zero on error
  1235. */
  1236. int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
  1237. struct file *lower_file)
  1238. {
  1239. mm_segment_t oldfs;
  1240. struct ecryptfs_crypt_stat *crypt_stat;
  1241. char *page_virt;
  1242. int current_header_page;
  1243. int header_pages;
  1244. int rc = 0;
  1245. crypt_stat = &ecryptfs_inode_to_private(
  1246. ecryptfs_dentry->d_inode)->crypt_stat;
  1247. if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  1248. ECRYPTFS_ENCRYPTED))) {
  1249. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  1250. ECRYPTFS_KEY_VALID)) {
  1251. ecryptfs_printk(KERN_DEBUG, "Key is "
  1252. "invalid; bailing out\n");
  1253. rc = -EINVAL;
  1254. goto out;
  1255. }
  1256. } else {
  1257. rc = -EINVAL;
  1258. ecryptfs_printk(KERN_WARNING,
  1259. "Called with crypt_stat->encrypted == 0\n");
  1260. goto out;
  1261. }
  1262. /* Released in this function */
  1263. page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, GFP_USER);
  1264. if (!page_virt) {
  1265. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  1266. rc = -ENOMEM;
  1267. goto out;
  1268. }
  1269. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1270. rc = ecryptfs_write_headers_virt(page_virt, crypt_stat,
  1271. ecryptfs_dentry);
  1272. if (unlikely(rc)) {
  1273. ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
  1274. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1275. goto out_free;
  1276. }
  1277. ecryptfs_printk(KERN_DEBUG,
  1278. "Writing key packet set to underlying file\n");
  1279. lower_file->f_pos = 0;
  1280. oldfs = get_fs();
  1281. set_fs(get_ds());
  1282. ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
  1283. "write() w/ header page; lower_file->f_pos = "
  1284. "[0x%.16x]\n", lower_file->f_pos);
  1285. lower_file->f_op->write(lower_file, (char __user *)page_virt,
  1286. PAGE_CACHE_SIZE, &lower_file->f_pos);
  1287. header_pages = ((crypt_stat->header_extent_size
  1288. * crypt_stat->num_header_extents_at_front)
  1289. / PAGE_CACHE_SIZE);
  1290. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1291. current_header_page = 1;
  1292. while (current_header_page < header_pages) {
  1293. ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
  1294. "write() w/ zero'd page; lower_file->f_pos = "
  1295. "[0x%.16x]\n", lower_file->f_pos);
  1296. lower_file->f_op->write(lower_file, (char __user *)page_virt,
  1297. PAGE_CACHE_SIZE, &lower_file->f_pos);
  1298. current_header_page++;
  1299. }
  1300. set_fs(oldfs);
  1301. ecryptfs_printk(KERN_DEBUG,
  1302. "Done writing key packet set to underlying file.\n");
  1303. out_free:
  1304. kmem_cache_free(ecryptfs_header_cache_0, page_virt);
  1305. out:
  1306. return rc;
  1307. }
  1308. static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
  1309. char *virt, int *bytes_read)
  1310. {
  1311. int rc = 0;
  1312. u32 header_extent_size;
  1313. u16 num_header_extents_at_front;
  1314. memcpy(&header_extent_size, virt, 4);
  1315. header_extent_size = be32_to_cpu(header_extent_size);
  1316. virt += 4;
  1317. memcpy(&num_header_extents_at_front, virt, 2);
  1318. num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
  1319. crypt_stat->header_extent_size = (int)header_extent_size;
  1320. crypt_stat->num_header_extents_at_front =
  1321. (int)num_header_extents_at_front;
  1322. (*bytes_read) = 6;
  1323. if ((crypt_stat->header_extent_size
  1324. * crypt_stat->num_header_extents_at_front)
  1325. < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  1326. rc = -EINVAL;
  1327. ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
  1328. "[%d]\n", crypt_stat->header_extent_size);
  1329. }
  1330. return rc;
  1331. }
  1332. /**
  1333. * set_default_header_data
  1334. *
  1335. * For version 0 file format; this function is only for backwards
  1336. * compatibility for files created with the prior versions of
  1337. * eCryptfs.
  1338. */
  1339. static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
  1340. {
  1341. crypt_stat->header_extent_size = 4096;
  1342. crypt_stat->num_header_extents_at_front = 1;
  1343. }
  1344. /**
  1345. * ecryptfs_read_headers_virt
  1346. *
  1347. * Read/parse the header data. The header format is detailed in the
  1348. * comment block for the ecryptfs_write_headers_virt() function.
  1349. *
  1350. * Returns zero on success
  1351. */
  1352. static int ecryptfs_read_headers_virt(char *page_virt,
  1353. struct ecryptfs_crypt_stat *crypt_stat,
  1354. struct dentry *ecryptfs_dentry)
  1355. {
  1356. int rc = 0;
  1357. int offset;
  1358. int bytes_read;
  1359. ecryptfs_set_default_sizes(crypt_stat);
  1360. crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
  1361. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1362. offset = ECRYPTFS_FILE_SIZE_BYTES;
  1363. rc = contains_ecryptfs_marker(page_virt + offset);
  1364. if (rc == 0) {
  1365. rc = -EINVAL;
  1366. goto out;
  1367. }
  1368. offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
  1369. rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
  1370. &bytes_read);
  1371. if (rc) {
  1372. ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
  1373. goto out;
  1374. }
  1375. if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
  1376. ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
  1377. "file version [%d] is supported by this "
  1378. "version of eCryptfs\n",
  1379. crypt_stat->file_version,
  1380. ECRYPTFS_SUPPORTED_FILE_VERSION);
  1381. rc = -EINVAL;
  1382. goto out;
  1383. }
  1384. offset += bytes_read;
  1385. if (crypt_stat->file_version >= 1) {
  1386. rc = parse_header_metadata(crypt_stat, (page_virt + offset),
  1387. &bytes_read);
  1388. if (rc) {
  1389. ecryptfs_printk(KERN_WARNING, "Error reading header "
  1390. "metadata; rc = [%d]\n", rc);
  1391. }
  1392. offset += bytes_read;
  1393. } else
  1394. set_default_header_data(crypt_stat);
  1395. rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
  1396. ecryptfs_dentry);
  1397. out:
  1398. return rc;
  1399. }
  1400. /**
  1401. * ecryptfs_read_headers
  1402. *
  1403. * Returns zero if valid headers found and parsed; non-zero otherwise
  1404. */
  1405. int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
  1406. struct file *lower_file)
  1407. {
  1408. int rc = 0;
  1409. char *page_virt = NULL;
  1410. mm_segment_t oldfs;
  1411. ssize_t bytes_read;
  1412. struct ecryptfs_crypt_stat *crypt_stat =
  1413. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  1414. /* Read the first page from the underlying file */
  1415. page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
  1416. if (!page_virt) {
  1417. rc = -ENOMEM;
  1418. ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
  1419. goto out;
  1420. }
  1421. lower_file->f_pos = 0;
  1422. oldfs = get_fs();
  1423. set_fs(get_ds());
  1424. bytes_read = lower_file->f_op->read(lower_file,
  1425. (char __user *)page_virt,
  1426. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  1427. &lower_file->f_pos);
  1428. set_fs(oldfs);
  1429. if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
  1430. rc = -EINVAL;
  1431. goto out;
  1432. }
  1433. rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
  1434. ecryptfs_dentry);
  1435. if (rc) {
  1436. ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not "
  1437. "found\n");
  1438. rc = -EINVAL;
  1439. }
  1440. out:
  1441. if (page_virt) {
  1442. memset(page_virt, 0, PAGE_CACHE_SIZE);
  1443. kmem_cache_free(ecryptfs_header_cache_1, page_virt);
  1444. }
  1445. return rc;
  1446. }
  1447. /**
  1448. * ecryptfs_encode_filename - converts a plaintext file name to cipher text
  1449. * @crypt_stat: The crypt_stat struct associated with the file anem to encode
  1450. * @name: The plaintext name
  1451. * @length: The length of the plaintext
  1452. * @encoded_name: The encypted name
  1453. *
  1454. * Encrypts and encodes a filename into something that constitutes a
  1455. * valid filename for a filesystem, with printable characters.
  1456. *
  1457. * We assume that we have a properly initialized crypto context,
  1458. * pointed to by crypt_stat->tfm.
  1459. *
  1460. * TODO: Implement filename decoding and decryption here, in place of
  1461. * memcpy. We are keeping the framework around for now to (1)
  1462. * facilitate testing of the components needed to implement filename
  1463. * encryption and (2) to provide a code base from which other
  1464. * developers in the community can easily implement this feature.
  1465. *
  1466. * Returns the length of encoded filename; negative if error
  1467. */
  1468. int
  1469. ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1470. const char *name, int length, char **encoded_name)
  1471. {
  1472. int error = 0;
  1473. (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
  1474. if (!(*encoded_name)) {
  1475. error = -ENOMEM;
  1476. goto out;
  1477. }
  1478. /* TODO: Filename encryption is a scheduled feature for a
  1479. * future version of eCryptfs. This function is here only for
  1480. * the purpose of providing a framework for other developers
  1481. * to easily implement filename encryption. Hint: Replace this
  1482. * memcpy() with a call to encrypt and encode the
  1483. * filename, the set the length accordingly. */
  1484. memcpy((void *)(*encoded_name), (void *)name, length);
  1485. (*encoded_name)[length] = '\0';
  1486. error = length + 1;
  1487. out:
  1488. return error;
  1489. }
  1490. /**
  1491. * ecryptfs_decode_filename - converts the cipher text name to plaintext
  1492. * @crypt_stat: The crypt_stat struct associated with the file
  1493. * @name: The filename in cipher text
  1494. * @length: The length of the cipher text name
  1495. * @decrypted_name: The plaintext name
  1496. *
  1497. * Decodes and decrypts the filename.
  1498. *
  1499. * We assume that we have a properly initialized crypto context,
  1500. * pointed to by crypt_stat->tfm.
  1501. *
  1502. * TODO: Implement filename decoding and decryption here, in place of
  1503. * memcpy. We are keeping the framework around for now to (1)
  1504. * facilitate testing of the components needed to implement filename
  1505. * encryption and (2) to provide a code base from which other
  1506. * developers in the community can easily implement this feature.
  1507. *
  1508. * Returns the length of decoded filename; negative if error
  1509. */
  1510. int
  1511. ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
  1512. const char *name, int length, char **decrypted_name)
  1513. {
  1514. int error = 0;
  1515. (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
  1516. if (!(*decrypted_name)) {
  1517. error = -ENOMEM;
  1518. goto out;
  1519. }
  1520. /* TODO: Filename encryption is a scheduled feature for a
  1521. * future version of eCryptfs. This function is here only for
  1522. * the purpose of providing a framework for other developers
  1523. * to easily implement filename encryption. Hint: Replace this
  1524. * memcpy() with a call to decode and decrypt the
  1525. * filename, the set the length accordingly. */
  1526. memcpy((void *)(*decrypted_name), (void *)name, length);
  1527. (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
  1528. * in printing out the
  1529. * string in debug
  1530. * messages */
  1531. error = length;
  1532. out:
  1533. return error;
  1534. }
  1535. /**
  1536. * ecryptfs_process_cipher - Perform cipher initialization.
  1537. * @key_tfm: Crypto context for key material, set by this function
  1538. * @cipher_name: Name of the cipher
  1539. * @key_size: Size of the key in bytes
  1540. *
  1541. * Returns zero on success. Any crypto_tfm structs allocated here
  1542. * should be released by other functions, such as on a superblock put
  1543. * event, regardless of whether this function succeeds for fails.
  1544. */
  1545. int
  1546. ecryptfs_process_cipher(struct crypto_blkcipher **key_tfm, char *cipher_name,
  1547. size_t *key_size)
  1548. {
  1549. char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
  1550. char *full_alg_name;
  1551. int rc;
  1552. *key_tfm = NULL;
  1553. if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
  1554. rc = -EINVAL;
  1555. printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
  1556. "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
  1557. goto out;
  1558. }
  1559. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
  1560. "ecb");
  1561. if (rc)
  1562. goto out;
  1563. *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
  1564. kfree(full_alg_name);
  1565. if (IS_ERR(*key_tfm)) {
  1566. rc = PTR_ERR(*key_tfm);
  1567. printk(KERN_ERR "Unable to allocate crypto cipher with name "
  1568. "[%s]; rc = [%d]\n", cipher_name, rc);
  1569. goto out;
  1570. }
  1571. crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  1572. if (*key_size == 0) {
  1573. struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
  1574. *key_size = alg->max_keysize;
  1575. }
  1576. get_random_bytes(dummy_key, *key_size);
  1577. rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
  1578. if (rc) {
  1579. printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
  1580. "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
  1581. rc = -EINVAL;
  1582. goto out;
  1583. }
  1584. out:
  1585. return rc;
  1586. }