crypto.c (5632a9fbcd451892332d45553ce8b831d5143691) crypto.c (3767e255b390d72f9a33c08d9e86c5f21f25860f)
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-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>

--- 91 unchanged lines hidden (view full) ---

100 */
101static int ecryptfs_calculate_md5(char *dst,
102 struct ecryptfs_crypt_stat *crypt_stat,
103 char *src, int len)
104{
105 struct crypto_shash *tfm;
106 int rc = 0;
107
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-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>

--- 91 unchanged lines hidden (view full) ---

100 */
101static int ecryptfs_calculate_md5(char *dst,
102 struct ecryptfs_crypt_stat *crypt_stat,
103 char *src, int len)
104{
105 struct crypto_shash *tfm;
106 int rc = 0;
107
108 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
109 tfm = crypt_stat->hash_tfm;
108 tfm = crypt_stat->hash_tfm;
110 if (!tfm) {
111 tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
112 if (IS_ERR(tfm)) {
113 rc = PTR_ERR(tfm);
114 ecryptfs_printk(KERN_ERR, "Error attempting to "
115 "allocate crypto context; rc = [%d]\n",
116 rc);
117 goto out;
118 }
119 crypt_stat->hash_tfm = tfm;
120 }
121 rc = ecryptfs_hash_digest(tfm, src, len, dst);
122 if (rc) {
123 printk(KERN_ERR
124 "%s: Error computing crypto hash; rc = [%d]\n",
125 __func__, rc);
126 goto out;
127 }
128out:
109 rc = ecryptfs_hash_digest(tfm, src, len, dst);
110 if (rc) {
111 printk(KERN_ERR
112 "%s: Error computing crypto hash; rc = [%d]\n",
113 __func__, rc);
114 goto out;
115 }
116out:
129 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
130 return rc;
131}
132
133static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
134 char *cipher_name,
135 char *chaining_modifier)
136{
137 int cipher_name_len = strlen(cipher_name);

--- 64 unchanged lines hidden (view full) ---

202}
203
204/**
205 * ecryptfs_init_crypt_stat
206 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
207 *
208 * Initialize the crypt_stat structure.
209 */
117 return rc;
118}
119
120static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
121 char *cipher_name,
122 char *chaining_modifier)
123{
124 int cipher_name_len = strlen(cipher_name);

--- 64 unchanged lines hidden (view full) ---

189}
190
191/**
192 * ecryptfs_init_crypt_stat
193 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
194 *
195 * Initialize the crypt_stat structure.
196 */
210void
211ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
197int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
212{
198{
199 struct crypto_shash *tfm;
200 int rc;
201
202 tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
203 if (IS_ERR(tfm)) {
204 rc = PTR_ERR(tfm);
205 ecryptfs_printk(KERN_ERR, "Error attempting to "
206 "allocate crypto context; rc = [%d]\n",
207 rc);
208 return rc;
209 }
210
213 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
214 INIT_LIST_HEAD(&crypt_stat->keysig_list);
215 mutex_init(&crypt_stat->keysig_list_mutex);
216 mutex_init(&crypt_stat->cs_mutex);
217 mutex_init(&crypt_stat->cs_tfm_mutex);
211 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
212 INIT_LIST_HEAD(&crypt_stat->keysig_list);
213 mutex_init(&crypt_stat->keysig_list_mutex);
214 mutex_init(&crypt_stat->cs_mutex);
215 mutex_init(&crypt_stat->cs_tfm_mutex);
218 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
216 crypt_stat->hash_tfm = tfm;
219 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
217 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
218
219 return 0;
220}
221
222/**
223 * ecryptfs_destroy_crypt_stat
224 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
225 *
226 * Releases all memory associated with a crypt_stat struct.
227 */

--- 908 unchanged lines hidden (view full) ---

1136 "information to lower file; rc = [%d]\n", __func__, rc);
1137 else
1138 rc = 0;
1139 return rc;
1140}
1141
1142static int
1143ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
220}
221
222/**
223 * ecryptfs_destroy_crypt_stat
224 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
225 *
226 * Releases all memory associated with a crypt_stat struct.
227 */

--- 908 unchanged lines hidden (view full) ---

1136 "information to lower file; rc = [%d]\n", __func__, rc);
1137 else
1138 rc = 0;
1139 return rc;
1140}
1141
1142static int
1143ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1144 struct inode *ecryptfs_inode,
1144 char *page_virt, size_t size)
1145{
1146 int rc;
1147
1145 char *page_virt, size_t size)
1146{
1147 int rc;
1148
1148 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1149 size, 0);
1149 rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
1150 ECRYPTFS_XATTR_NAME, page_virt, size, 0);
1150 return rc;
1151}
1152
1153static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1154 unsigned int order)
1155{
1156 struct page *page;
1157

--- 52 unchanged lines hidden (view full) ---

1210 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1211 ecryptfs_dentry);
1212 if (unlikely(rc)) {
1213 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1214 __func__, rc);
1215 goto out_free;
1216 }
1217 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1151 return rc;
1152}
1153
1154static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
1155 unsigned int order)
1156{
1157 struct page *page;
1158

--- 52 unchanged lines hidden (view full) ---

1211 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1212 ecryptfs_dentry);
1213 if (unlikely(rc)) {
1214 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1215 __func__, rc);
1216 goto out_free;
1217 }
1218 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1218 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
1219 size);
1219 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
1220 virt, size);
1220 else
1221 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
1222 virt_len);
1223 if (rc) {
1224 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1225 "rc = [%d]\n", __func__, rc);
1226 goto out_free;
1227 }

--- 136 unchanged lines hidden (view full) ---

1364 */
1365int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1366{
1367 struct dentry *lower_dentry =
1368 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
1369 ssize_t size;
1370 int rc = 0;
1371
1221 else
1222 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
1223 virt_len);
1224 if (rc) {
1225 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1226 "rc = [%d]\n", __func__, rc);
1227 goto out_free;
1228 }

--- 136 unchanged lines hidden (view full) ---

1365 */
1366int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1367{
1368 struct dentry *lower_dentry =
1369 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
1370 ssize_t size;
1371 int rc = 0;
1372
1372 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1373 size = ecryptfs_getxattr_lower(lower_dentry,
1374 ecryptfs_inode_to_lower(ecryptfs_inode),
1375 ECRYPTFS_XATTR_NAME,
1373 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1374 if (size < 0) {
1375 if (unlikely(ecryptfs_verbosity > 0))
1376 printk(KERN_INFO "Error attempting to read the [%s] "
1377 "xattr from the lower file; return value = "
1378 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1379 rc = -EINVAL;
1380 goto out;

--- 5 unchanged lines hidden (view full) ---

1386int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
1387 struct inode *inode)
1388{
1389 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1390 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1391 int rc;
1392
1393 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1376 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1377 if (size < 0) {
1378 if (unlikely(ecryptfs_verbosity > 0))
1379 printk(KERN_INFO "Error attempting to read the [%s] "
1380 "xattr from the lower file; return value = "
1381 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1382 rc = -EINVAL;
1383 goto out;

--- 5 unchanged lines hidden (view full) ---

1389int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
1390 struct inode *inode)
1391{
1392 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1393 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1394 int rc;
1395
1396 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1397 ecryptfs_inode_to_lower(inode),
1394 ECRYPTFS_XATTR_NAME, file_size,
1395 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1396 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1397 return rc >= 0 ? -EINVAL : rc;
1398 rc = ecryptfs_validate_marker(marker);
1399 if (!rc)
1400 ecryptfs_i_size_init(file_size, inode);
1401 return rc;

--- 744 unchanged lines hidden ---
1398 ECRYPTFS_XATTR_NAME, file_size,
1399 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1400 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1401 return rc >= 0 ? -EINVAL : rc;
1402 rc = ecryptfs_validate_marker(marker);
1403 if (!rc)
1404 ecryptfs_i_size_init(file_size, inode);
1405 return rc;

--- 744 unchanged lines hidden ---