Skip to content
Snippets Groups Projects
crypto.c 48.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
     */
    static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
    				  char *page_virt, int *bytes_read)
    {
    	int rc = 0;
    	int i;
    	u32 flags;
    
    	memcpy(&flags, page_virt, 4);
    	flags = be32_to_cpu(flags);
    	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
    			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
    		if (flags & ecryptfs_flag_map[i].file_flag) {
    			ECRYPTFS_SET_FLAG(crypt_stat->flags,
    					  ecryptfs_flag_map[i].local_flag);
    		} else
    			ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
    					    ecryptfs_flag_map[i].local_flag);
    	/* Version is in top 8 bits of the 32-bit flag vector */
    	crypt_stat->file_version = ((flags >> 24) & 0xFF);
    	(*bytes_read) = 4;
    	return rc;
    }
    
    /**
     * write_ecryptfs_marker
     * @page_virt: The pointer to in a page to begin writing the marker
     * @written: Number of bytes written
     *
     * Marker = 0x3c81b7f5
     */
    static void write_ecryptfs_marker(char *page_virt, size_t *written)
    {
    	u32 m_1, m_2;
    
    	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
    	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
    	m_1 = cpu_to_be32(m_1);
    	memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
    	m_2 = cpu_to_be32(m_2);
    	memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
    	       (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
    	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
    }
    
    static void
    write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
    		     size_t *written)
    {
    	u32 flags = 0;
    	int i;
    
    	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
    			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
    		if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
    					ecryptfs_flag_map[i].local_flag))
    			flags |= ecryptfs_flag_map[i].file_flag;
    	/* Version is in top 8 bits of the 32-bit flag vector */
    	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
    	flags = cpu_to_be32(flags);
    	memcpy(page_virt, &flags, 4);
    	(*written) = 4;
    }
    
    struct ecryptfs_cipher_code_str_map_elem {
    	char cipher_str[16];
    	u16 cipher_code;
    };
    
    /* Add support for additional ciphers by adding elements here. The
     * cipher_code is whatever OpenPGP applicatoins use to identify the
     * ciphers. List in order of probability. */
    static struct ecryptfs_cipher_code_str_map_elem
    ecryptfs_cipher_code_str_map[] = {
    	{"aes",RFC2440_CIPHER_AES_128 },
    	{"blowfish", RFC2440_CIPHER_BLOWFISH},
    	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
    	{"cast5", RFC2440_CIPHER_CAST_5},
    	{"twofish", RFC2440_CIPHER_TWOFISH},
    	{"cast6", RFC2440_CIPHER_CAST_6},
    	{"aes", RFC2440_CIPHER_AES_192},
    	{"aes", RFC2440_CIPHER_AES_256}
    };
    
    /**
     * ecryptfs_code_for_cipher_string
     * @str: The string representing the cipher name
     *
     * Returns zero on no match, or the cipher code on match
     */
    u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
    {
    	int i;
    	u16 code = 0;
    	struct ecryptfs_cipher_code_str_map_elem *map =
    		ecryptfs_cipher_code_str_map;
    
    	if (strcmp(crypt_stat->cipher, "aes") == 0) {
    		switch (crypt_stat->key_size) {
    		case 16:
    			code = RFC2440_CIPHER_AES_128;
    			break;
    		case 24:
    			code = RFC2440_CIPHER_AES_192;
    			break;
    		case 32:
    			code = RFC2440_CIPHER_AES_256;
    		}
    	} else {
    		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
    			if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
    				code = map[i].cipher_code;
    				break;
    			}
    	}
    	return code;
    }
    
    /**
     * ecryptfs_cipher_code_to_string
     * @str: Destination to write out the cipher name
     * @cipher_code: The code to convert to cipher name string
     *
     * Returns zero on success
     */
    int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
    {
    	int rc = 0;
    	int i;
    
    	str[0] = '\0';
    	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
    		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
    			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
    	if (str[0] == '\0') {
    		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
    				"[%d]\n", cipher_code);
    		rc = -EINVAL;
    	}
    	return rc;
    }
    
    /**
     * ecryptfs_read_header_region
     * @data
     * @dentry
     * @nd
     *
     * Returns zero on success; non-zero otherwise
     */
    int ecryptfs_read_header_region(char *data, struct dentry *dentry,
    				struct vfsmount *mnt)
    {
    	struct file *file;
    	mm_segment_t oldfs;
    	int rc;
    
    	mnt = mntget(mnt);
    	file = dentry_open(dentry, mnt, O_RDONLY);
    	if (IS_ERR(file)) {
    		ecryptfs_printk(KERN_DEBUG, "Error opening file to "
    				"read header region\n");
    		mntput(mnt);
    		rc = PTR_ERR(file);
    		goto out;
    	}
    	file->f_pos = 0;
    	oldfs = get_fs();
    	set_fs(get_ds());
    	/* For releases 0.1 and 0.2, all of the header information
    	 * fits in the first data extent-sized region. */
    	rc = file->f_op->read(file, (char __user *)data,
    			      ECRYPTFS_DEFAULT_EXTENT_SIZE, &file->f_pos);
    	set_fs(oldfs);
    	fput(file);
    	rc = 0;
    out:
    	return rc;
    }
    
    static void
    write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat,
    		      size_t *written)
    {
    	u32 header_extent_size;
    	u16 num_header_extents_at_front;
    
    	header_extent_size = (u32)crypt_stat->header_extent_size;
    	num_header_extents_at_front =
    		(u16)crypt_stat->num_header_extents_at_front;
    	header_extent_size = cpu_to_be32(header_extent_size);
    	memcpy(virt, &header_extent_size, 4);
    	virt += 4;
    	num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
    	memcpy(virt, &num_header_extents_at_front, 2);
    	(*written) = 6;
    }
    
    struct kmem_cache *ecryptfs_header_cache_0;
    struct kmem_cache *ecryptfs_header_cache_1;
    struct kmem_cache *ecryptfs_header_cache_2;
    
    /**
     * ecryptfs_write_headers_virt
     * @page_virt
     * @crypt_stat
     * @ecryptfs_dentry
     *
     * Format version: 1
     *
     *   Header Extent:
     *     Octets 0-7:        Unencrypted file size (big-endian)
     *     Octets 8-15:       eCryptfs special marker
     *     Octets 16-19:      Flags
     *      Octet 16:         File format version number (between 0 and 255)
     *      Octets 17-18:     Reserved
     *      Octet 19:         Bit 1 (lsb): Reserved
     *                        Bit 2: Encrypted?
     *                        Bits 3-8: Reserved
     *     Octets 20-23:      Header extent size (big-endian)
     *     Octets 24-25:      Number of header extents at front of file
     *                        (big-endian)
     *     Octet  26:         Begin RFC 2440 authentication token packet set
     *   Data Extent 0:
     *     Lower data (CBC encrypted)
     *   Data Extent 1:
     *     Lower data (CBC encrypted)
     *   ...
     *
     * Returns zero on success
     */
    int ecryptfs_write_headers_virt(char *page_virt,
    				struct ecryptfs_crypt_stat *crypt_stat,
    				struct dentry *ecryptfs_dentry)
    {
    	int rc;
    	size_t written;
    	size_t offset;
    
    	offset = ECRYPTFS_FILE_SIZE_BYTES;
    	write_ecryptfs_marker((page_virt + offset), &written);
    	offset += written;
    	write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
    	offset += written;
    	write_header_metadata((page_virt + offset), crypt_stat, &written);
    	offset += written;
    	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
    					      ecryptfs_dentry, &written,
    					      PAGE_CACHE_SIZE - offset);
    	if (rc)
    		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
    				"set; rc = [%d]\n", rc);
    	return rc;
    }
    
    /**
     * ecryptfs_write_headers
     * @lower_file: The lower file struct, which was returned from dentry_open
     *
     * Write the file headers out.  This will likely involve a userspace
     * callout, in which the session key is encrypted with one or more
     * public keys and/or the passphrase necessary to do the encryption is
     * retrieved via a prompt.  Exactly what happens at this point should
     * be policy-dependent.
     *
     * Returns zero on success; non-zero on error
     */
    int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
    			   struct file *lower_file)
    {
    	mm_segment_t oldfs;
    	struct ecryptfs_crypt_stat *crypt_stat;
    	char *page_virt;
    	int current_header_page;
    	int header_pages;
    	int rc = 0;
    
    	crypt_stat = &ecryptfs_inode_to_private(
    		ecryptfs_dentry->d_inode)->crypt_stat;
    	if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
    				       ECRYPTFS_ENCRYPTED))) {
    		if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
    					 ECRYPTFS_KEY_VALID)) {
    			ecryptfs_printk(KERN_DEBUG, "Key is "
    					"invalid; bailing out\n");
    			rc = -EINVAL;
    			goto out;
    		}
    	} else {
    		rc = -EINVAL;
    		ecryptfs_printk(KERN_WARNING,
    				"Called with crypt_stat->encrypted == 0\n");
    		goto out;
    	}
    	/* Released in this function */
    	page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, SLAB_USER);
    	if (!page_virt) {
    		ecryptfs_printk(KERN_ERR, "Out of memory\n");
    		rc = -ENOMEM;
    		goto out;
    	}
    	memset(page_virt, 0, PAGE_CACHE_SIZE);
    	rc = ecryptfs_write_headers_virt(page_virt, crypt_stat,
    					 ecryptfs_dentry);
    	if (unlikely(rc)) {
    		ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
    		memset(page_virt, 0, PAGE_CACHE_SIZE);
    		goto out_free;
    	}
    	ecryptfs_printk(KERN_DEBUG,
    			"Writing key packet set to underlying file\n");
    	lower_file->f_pos = 0;
    	oldfs = get_fs();
    	set_fs(get_ds());
    	ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
    			"write() w/ header page; lower_file->f_pos = "
    			"[0x%.16x]\n", lower_file->f_pos);
    	lower_file->f_op->write(lower_file, (char __user *)page_virt,
    				PAGE_CACHE_SIZE, &lower_file->f_pos);
    	header_pages = ((crypt_stat->header_extent_size
    			 * crypt_stat->num_header_extents_at_front)
    			/ PAGE_CACHE_SIZE);
    	memset(page_virt, 0, PAGE_CACHE_SIZE);
    	current_header_page = 1;
    	while (current_header_page < header_pages) {
    		ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
    				"write() w/ zero'd page; lower_file->f_pos = "
    				"[0x%.16x]\n", lower_file->f_pos);
    		lower_file->f_op->write(lower_file, (char __user *)page_virt,
    					PAGE_CACHE_SIZE, &lower_file->f_pos);
    		current_header_page++;
    	}
    	set_fs(oldfs);
    	ecryptfs_printk(KERN_DEBUG,
    			"Done writing key packet set to underlying file.\n");
    out_free:
    	kmem_cache_free(ecryptfs_header_cache_0, page_virt);
    out:
    	return rc;
    }
    
    static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
    				 char *virt, int *bytes_read)
    {
    	int rc = 0;
    	u32 header_extent_size;
    	u16 num_header_extents_at_front;
    
    	memcpy(&header_extent_size, virt, 4);
    	header_extent_size = be32_to_cpu(header_extent_size);
    	virt += 4;
    	memcpy(&num_header_extents_at_front, virt, 2);
    	num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
    	crypt_stat->header_extent_size = (int)header_extent_size;
    	crypt_stat->num_header_extents_at_front =
    		(int)num_header_extents_at_front;
    	(*bytes_read) = 6;
    	if ((crypt_stat->header_extent_size
    	     * crypt_stat->num_header_extents_at_front)
    	    < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
    		rc = -EINVAL;
    		ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
    				"[%d]\n", crypt_stat->header_extent_size);
    	}
    	return rc;
    }
    
    /**
     * set_default_header_data
     *
     * For version 0 file format; this function is only for backwards
     * compatibility for files created with the prior versions of
     * eCryptfs.
     */
    static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
    {
    	crypt_stat->header_extent_size = 4096;
    	crypt_stat->num_header_extents_at_front = 1;
    }
    
    /**
     * ecryptfs_read_headers_virt
     *
     * Read/parse the header data. The header format is detailed in the
     * comment block for the ecryptfs_write_headers_virt() function.
     *
     * Returns zero on success
     */
    static int ecryptfs_read_headers_virt(char *page_virt,
    				      struct ecryptfs_crypt_stat *crypt_stat,
    				      struct dentry *ecryptfs_dentry)
    {
    	int rc = 0;
    	int offset;
    	int bytes_read;
    
    	ecryptfs_set_default_sizes(crypt_stat);
    	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
    		ecryptfs_dentry->d_sb)->mount_crypt_stat;
    	offset = ECRYPTFS_FILE_SIZE_BYTES;
    	rc = contains_ecryptfs_marker(page_virt + offset);
    	if (rc == 0) {
    		rc = -EINVAL;
    		goto out;
    	}
    	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
    	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
    				    &bytes_read);
    	if (rc) {
    		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
    		goto out;
    	}
    	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
    		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
    				"file version [%d] is supported by this "
    				"version of eCryptfs\n",
    				crypt_stat->file_version,
    				ECRYPTFS_SUPPORTED_FILE_VERSION);
    		rc = -EINVAL;
    		goto out;
    	}
    	offset += bytes_read;
    	if (crypt_stat->file_version >= 1) {
    		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
    					   &bytes_read);
    		if (rc) {
    			ecryptfs_printk(KERN_WARNING, "Error reading header "
    					"metadata; rc = [%d]\n", rc);
    		}
    		offset += bytes_read;
    	} else
    		set_default_header_data(crypt_stat);
    	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
    				       ecryptfs_dentry);
    out:
    	return rc;
    }
    
    /**
     * ecryptfs_read_headers
     *
     * Returns zero if valid headers found and parsed; non-zero otherwise
     */
    int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
    			  struct file *lower_file)
    {
    	int rc = 0;
    	char *page_virt = NULL;
    	mm_segment_t oldfs;
    	ssize_t bytes_read;
    	struct ecryptfs_crypt_stat *crypt_stat =
    	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
    
    	/* Read the first page from the underlying file */
    	page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, SLAB_USER);
    	if (!page_virt) {
    		rc = -ENOMEM;
    		ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
    		goto out;
    	}
    	lower_file->f_pos = 0;
    	oldfs = get_fs();
    	set_fs(get_ds());
    	bytes_read = lower_file->f_op->read(lower_file,
    					    (char __user *)page_virt,
    					    ECRYPTFS_DEFAULT_EXTENT_SIZE,
    					    &lower_file->f_pos);
    	set_fs(oldfs);
    	if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
    		rc = -EINVAL;
    		goto out;
    	}
    	rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
    					ecryptfs_dentry);
    	if (rc) {
    		ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not "
    				"found\n");
    		rc = -EINVAL;
    	}
    out:
    	if (page_virt) {
    		memset(page_virt, 0, PAGE_CACHE_SIZE);
    		kmem_cache_free(ecryptfs_header_cache_1, page_virt);
    	}
    	return rc;
    }
    
    /**
     * ecryptfs_encode_filename - converts a plaintext file name to cipher text
     * @crypt_stat: The crypt_stat struct associated with the file anem to encode
     * @name: The plaintext name
     * @length: The length of the plaintext
     * @encoded_name: The encypted name
     *
     * Encrypts and encodes a filename into something that constitutes a
     * valid filename for a filesystem, with printable characters.
     *
     * We assume that we have a properly initialized crypto context,
     * pointed to by crypt_stat->tfm.
     *
     * TODO: Implement filename decoding and decryption here, in place of
     * memcpy. We are keeping the framework around for now to (1)
     * facilitate testing of the components needed to implement filename
     * encryption and (2) to provide a code base from which other
     * developers in the community can easily implement this feature.
     *
     * Returns the length of encoded filename; negative if error
     */
    int
    ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
    			 const char *name, int length, char **encoded_name)
    {
    	int error = 0;
    
    	(*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
    	if (!(*encoded_name)) {
    		error = -ENOMEM;
    		goto out;
    	}
    	/* TODO: Filename encryption is a scheduled feature for a
    	 * future version of eCryptfs. This function is here only for
    	 * the purpose of providing a framework for other developers
    	 * to easily implement filename encryption. Hint: Replace this
    	 * memcpy() with a call to encrypt and encode the
    	 * filename, the set the length accordingly. */
    	memcpy((void *)(*encoded_name), (void *)name, length);
    	(*encoded_name)[length] = '\0';
    	error = length + 1;
    out:
    	return error;
    }
    
    /**
     * ecryptfs_decode_filename - converts the cipher text name to plaintext
     * @crypt_stat: The crypt_stat struct associated with the file
     * @name: The filename in cipher text
     * @length: The length of the cipher text name
     * @decrypted_name: The plaintext name
     *
     * Decodes and decrypts the filename.
     *
     * We assume that we have a properly initialized crypto context,
     * pointed to by crypt_stat->tfm.
     *
     * TODO: Implement filename decoding and decryption here, in place of
     * memcpy. We are keeping the framework around for now to (1)
     * facilitate testing of the components needed to implement filename
     * encryption and (2) to provide a code base from which other
     * developers in the community can easily implement this feature.
     *
     * Returns the length of decoded filename; negative if error
     */
    int
    ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
    			 const char *name, int length, char **decrypted_name)
    {
    	int error = 0;
    
    	(*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
    	if (!(*decrypted_name)) {
    		error = -ENOMEM;
    		goto out;
    	}
    	/* TODO: Filename encryption is a scheduled feature for a
    	 * future version of eCryptfs. This function is here only for
    	 * the purpose of providing a framework for other developers
    	 * to easily implement filename encryption. Hint: Replace this
    	 * memcpy() with a call to decode and decrypt the
    	 * filename, the set the length accordingly. */
    	memcpy((void *)(*decrypted_name), (void *)name, length);
    	(*decrypted_name)[length + 1] = '\0';	/* Only for convenience
    						 * in printing out the
    						 * string in debug
    						 * messages */
    	error = length;
    out:
    	return error;
    }
    
    /**
     * ecryptfs_process_cipher - Perform cipher initialization.
     * @key_tfm: Crypto context for key material, set by this function
    
     * @cipher_name: Name of the cipher
     * @key_size: Size of the key in bytes
    
     *
     * Returns zero on success. Any crypto_tfm structs allocated here
     * should be released by other functions, such as on a superblock put
     * event, regardless of whether this function succeeds for fails.
     */
    int
    
    ecryptfs_process_cipher(struct crypto_tfm **key_tfm, char *cipher_name,
    			size_t *key_size)
    
    {
    	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
    	int rc;
    
    
    	*key_tfm = NULL;
    	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
    
    		rc = -EINVAL;
    		printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
    
    		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
    
    		goto out;
    	}
    	*key_tfm = crypto_alloc_tfm(cipher_name, CRYPTO_TFM_REQ_WEAK_KEY);
    	if (!(*key_tfm)) {
    		rc = -EINVAL;
    		printk(KERN_ERR "Unable to allocate crypto cipher with name "
    		       "[%s]\n", cipher_name);
    		goto out;
    	}
    
    	if (*key_size == 0)
    		*key_size = crypto_tfm_alg_max_keysize(*key_tfm);
    	get_random_bytes(dummy_key, *key_size);
    	rc = crypto_cipher_setkey(*key_tfm, dummy_key, *key_size);
    
    	if (rc) {
    		printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
    
    		       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
    
    		rc = -EINVAL;
    		goto out;
    	}
    out:
    	return rc;
    }