xref: /openbmc/linux/fs/ecryptfs/keystore.c (revision f137f15072411618e37b338aa13e5ae43583bcf2)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  * In-kernel key management code.  Includes functions to parse and
4237fead6SMichael Halcrow  * write authentication token-related packets with the underlying
5237fead6SMichael Halcrow  * file.
6237fead6SMichael Halcrow  *
7237fead6SMichael Halcrow  * Copyright (C) 2004-2006 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
10dddfa461SMichael Halcrow  *              Trevor S. Highland <trevor.highland@gmail.com>
11237fead6SMichael Halcrow  *
12237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
13237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
14237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
15237fead6SMichael Halcrow  * License, or (at your option) any later version.
16237fead6SMichael Halcrow  *
17237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
18237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
19237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20237fead6SMichael Halcrow  * General Public License for more details.
21237fead6SMichael Halcrow  *
22237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
23237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
24237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25237fead6SMichael Halcrow  * 02111-1307, USA.
26237fead6SMichael Halcrow  */
27237fead6SMichael Halcrow 
28237fead6SMichael Halcrow #include <linux/string.h>
29237fead6SMichael Halcrow #include <linux/syscalls.h>
30237fead6SMichael Halcrow #include <linux/pagemap.h>
31237fead6SMichael Halcrow #include <linux/key.h>
32237fead6SMichael Halcrow #include <linux/random.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/scatterlist.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
36237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
37237fead6SMichael Halcrow 
38237fead6SMichael Halcrow /**
39237fead6SMichael Halcrow  * request_key returned an error instead of a valid key address;
40237fead6SMichael Halcrow  * determine the type of error, make appropriate log entries, and
41237fead6SMichael Halcrow  * return an error code.
42237fead6SMichael Halcrow  */
43cd9d67dfSMichael Halcrow static int process_request_key_err(long err_code)
44237fead6SMichael Halcrow {
45237fead6SMichael Halcrow 	int rc = 0;
46237fead6SMichael Halcrow 
47237fead6SMichael Halcrow 	switch (err_code) {
48982363c9SEric Sandeen 	case -ENOKEY:
49237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "No key\n");
50237fead6SMichael Halcrow 		rc = -ENOENT;
51237fead6SMichael Halcrow 		break;
52982363c9SEric Sandeen 	case -EKEYEXPIRED:
53237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Key expired\n");
54237fead6SMichael Halcrow 		rc = -ETIME;
55237fead6SMichael Halcrow 		break;
56982363c9SEric Sandeen 	case -EKEYREVOKED:
57237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Key revoked\n");
58237fead6SMichael Halcrow 		rc = -EINVAL;
59237fead6SMichael Halcrow 		break;
60237fead6SMichael Halcrow 	default:
61237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Unknown error code: "
62237fead6SMichael Halcrow 				"[0x%.16x]\n", err_code);
63237fead6SMichael Halcrow 		rc = -EINVAL;
64237fead6SMichael Halcrow 	}
65237fead6SMichael Halcrow 	return rc;
66237fead6SMichael Halcrow }
67237fead6SMichael Halcrow 
68237fead6SMichael Halcrow /**
69f66e883eSMichael Halcrow  * ecryptfs_parse_packet_length
70237fead6SMichael Halcrow  * @data: Pointer to memory containing length at offset
71237fead6SMichael Halcrow  * @size: This function writes the decoded size to this memory
72237fead6SMichael Halcrow  *        address; zero on error
73237fead6SMichael Halcrow  * @length_size: The number of bytes occupied by the encoded length
74237fead6SMichael Halcrow  *
7522e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
76237fead6SMichael Halcrow  */
77f66e883eSMichael Halcrow int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
78237fead6SMichael Halcrow 				 size_t *length_size)
79237fead6SMichael Halcrow {
80237fead6SMichael Halcrow 	int rc = 0;
81237fead6SMichael Halcrow 
82237fead6SMichael Halcrow 	(*length_size) = 0;
83237fead6SMichael Halcrow 	(*size) = 0;
84237fead6SMichael Halcrow 	if (data[0] < 192) {
85237fead6SMichael Halcrow 		/* One-byte length */
86dddfa461SMichael Halcrow 		(*size) = (unsigned char)data[0];
87237fead6SMichael Halcrow 		(*length_size) = 1;
88237fead6SMichael Halcrow 	} else if (data[0] < 224) {
89237fead6SMichael Halcrow 		/* Two-byte length */
90dddfa461SMichael Halcrow 		(*size) = (((unsigned char)(data[0]) - 192) * 256);
91dddfa461SMichael Halcrow 		(*size) += ((unsigned char)(data[1]) + 192);
92237fead6SMichael Halcrow 		(*length_size) = 2;
93237fead6SMichael Halcrow 	} else if (data[0] == 255) {
94237fead6SMichael Halcrow 		/* Five-byte length; we're not supposed to see this */
95237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
96237fead6SMichael Halcrow 				"supported\n");
97237fead6SMichael Halcrow 		rc = -EINVAL;
98237fead6SMichael Halcrow 		goto out;
99237fead6SMichael Halcrow 	} else {
100237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
101237fead6SMichael Halcrow 		rc = -EINVAL;
102237fead6SMichael Halcrow 		goto out;
103237fead6SMichael Halcrow 	}
104237fead6SMichael Halcrow out:
105237fead6SMichael Halcrow 	return rc;
106237fead6SMichael Halcrow }
107237fead6SMichael Halcrow 
108237fead6SMichael Halcrow /**
109f66e883eSMichael Halcrow  * ecryptfs_write_packet_length
11022e78fafSMichael Halcrow  * @dest: The byte array target into which to write the length. Must
11122e78fafSMichael Halcrow  *        have at least 5 bytes allocated.
112237fead6SMichael Halcrow  * @size: The length to write.
11322e78fafSMichael Halcrow  * @packet_size_length: The number of bytes used to encode the packet
11422e78fafSMichael Halcrow  *                      length is written to this address.
115237fead6SMichael Halcrow  *
116237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
117237fead6SMichael Halcrow  */
118f66e883eSMichael Halcrow int ecryptfs_write_packet_length(char *dest, size_t size,
119237fead6SMichael Halcrow 				 size_t *packet_size_length)
120237fead6SMichael Halcrow {
121237fead6SMichael Halcrow 	int rc = 0;
122237fead6SMichael Halcrow 
123237fead6SMichael Halcrow 	if (size < 192) {
124237fead6SMichael Halcrow 		dest[0] = size;
125237fead6SMichael Halcrow 		(*packet_size_length) = 1;
126237fead6SMichael Halcrow 	} else if (size < 65536) {
127237fead6SMichael Halcrow 		dest[0] = (((size - 192) / 256) + 192);
128237fead6SMichael Halcrow 		dest[1] = ((size - 192) % 256);
129237fead6SMichael Halcrow 		(*packet_size_length) = 2;
130237fead6SMichael Halcrow 	} else {
131237fead6SMichael Halcrow 		rc = -EINVAL;
132237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING,
133237fead6SMichael Halcrow 				"Unsupported packet size: [%d]\n", size);
134237fead6SMichael Halcrow 	}
135237fead6SMichael Halcrow 	return rc;
136237fead6SMichael Halcrow }
137237fead6SMichael Halcrow 
138dddfa461SMichael Halcrow static int
139dddfa461SMichael Halcrow write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
140dddfa461SMichael Halcrow 		    char **packet, size_t *packet_len)
141dddfa461SMichael Halcrow {
142dddfa461SMichael Halcrow 	size_t i = 0;
143dddfa461SMichael Halcrow 	size_t data_len;
144dddfa461SMichael Halcrow 	size_t packet_size_len;
145dddfa461SMichael Halcrow 	char *message;
146dddfa461SMichael Halcrow 	int rc;
147dddfa461SMichael Halcrow 
148dddfa461SMichael Halcrow 	/*
149dddfa461SMichael Halcrow 	 *              ***** TAG 64 Packet Format *****
150dddfa461SMichael Halcrow 	 *    | Content Type                       | 1 byte       |
151dddfa461SMichael Halcrow 	 *    | Key Identifier Size                | 1 or 2 bytes |
152dddfa461SMichael Halcrow 	 *    | Key Identifier                     | arbitrary    |
153dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
154dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key      | arbitrary    |
155dddfa461SMichael Halcrow 	 */
156dddfa461SMichael Halcrow 	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
157dddfa461SMichael Halcrow 		    + session_key->encrypted_key_size);
158dddfa461SMichael Halcrow 	*packet = kmalloc(data_len, GFP_KERNEL);
159dddfa461SMichael Halcrow 	message = *packet;
160dddfa461SMichael Halcrow 	if (!message) {
161dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
162dddfa461SMichael Halcrow 		rc = -ENOMEM;
163dddfa461SMichael Halcrow 		goto out;
164dddfa461SMichael Halcrow 	}
165dddfa461SMichael Halcrow 	message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
166f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
167dddfa461SMichael Halcrow 					  &packet_size_len);
168dddfa461SMichael Halcrow 	if (rc) {
169dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
170dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
171dddfa461SMichael Halcrow 		goto out;
172dddfa461SMichael Halcrow 	}
173dddfa461SMichael Halcrow 	i += packet_size_len;
174dddfa461SMichael Halcrow 	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
175dddfa461SMichael Halcrow 	i += ECRYPTFS_SIG_SIZE_HEX;
176f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i],
177f66e883eSMichael Halcrow 					  session_key->encrypted_key_size,
178dddfa461SMichael Halcrow 					  &packet_size_len);
179dddfa461SMichael Halcrow 	if (rc) {
180dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
181dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
182dddfa461SMichael Halcrow 		goto out;
183dddfa461SMichael Halcrow 	}
184dddfa461SMichael Halcrow 	i += packet_size_len;
185dddfa461SMichael Halcrow 	memcpy(&message[i], session_key->encrypted_key,
186dddfa461SMichael Halcrow 	       session_key->encrypted_key_size);
187dddfa461SMichael Halcrow 	i += session_key->encrypted_key_size;
188dddfa461SMichael Halcrow 	*packet_len = i;
189dddfa461SMichael Halcrow out:
190dddfa461SMichael Halcrow 	return rc;
191dddfa461SMichael Halcrow }
192dddfa461SMichael Halcrow 
193dddfa461SMichael Halcrow static int
19419e66a67STrevor Highland parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
195dddfa461SMichael Halcrow 		    struct ecryptfs_message *msg)
196dddfa461SMichael Halcrow {
197dddfa461SMichael Halcrow 	size_t i = 0;
198dddfa461SMichael Halcrow 	char *data;
199dddfa461SMichael Halcrow 	size_t data_len;
200dddfa461SMichael Halcrow 	size_t m_size;
201dddfa461SMichael Halcrow 	size_t message_len;
202dddfa461SMichael Halcrow 	u16 checksum = 0;
203dddfa461SMichael Halcrow 	u16 expected_checksum = 0;
204dddfa461SMichael Halcrow 	int rc;
205dddfa461SMichael Halcrow 
206dddfa461SMichael Halcrow 	/*
207dddfa461SMichael Halcrow 	 *              ***** TAG 65 Packet Format *****
208dddfa461SMichael Halcrow 	 *         | Content Type             | 1 byte       |
209dddfa461SMichael Halcrow 	 *         | Status Indicator         | 1 byte       |
210dddfa461SMichael Halcrow 	 *         | File Encryption Key Size | 1 or 2 bytes |
211dddfa461SMichael Halcrow 	 *         | File Encryption Key      | arbitrary    |
212dddfa461SMichael Halcrow 	 */
213dddfa461SMichael Halcrow 	message_len = msg->data_len;
214dddfa461SMichael Halcrow 	data = msg->data;
215dddfa461SMichael Halcrow 	if (message_len < 4) {
216dddfa461SMichael Halcrow 		rc = -EIO;
217dddfa461SMichael Halcrow 		goto out;
218dddfa461SMichael Halcrow 	}
219dddfa461SMichael Halcrow 	if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
220dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
221dddfa461SMichael Halcrow 		rc = -EIO;
222dddfa461SMichael Halcrow 		goto out;
223dddfa461SMichael Halcrow 	}
224dddfa461SMichael Halcrow 	if (data[i++]) {
225dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
226dddfa461SMichael Halcrow 				"[%d]\n", data[i-1]);
227dddfa461SMichael Halcrow 		rc = -EIO;
228dddfa461SMichael Halcrow 		goto out;
229dddfa461SMichael Halcrow 	}
230f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
231dddfa461SMichael Halcrow 	if (rc) {
232dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
233dddfa461SMichael Halcrow 				"rc = [%d]\n", rc);
234dddfa461SMichael Halcrow 		goto out;
235dddfa461SMichael Halcrow 	}
236dddfa461SMichael Halcrow 	i += data_len;
237dddfa461SMichael Halcrow 	if (message_len < (i + m_size)) {
238624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
239624ae528STyler Hicks 				"is shorter than expected\n");
240dddfa461SMichael Halcrow 		rc = -EIO;
241dddfa461SMichael Halcrow 		goto out;
242dddfa461SMichael Halcrow 	}
243dddfa461SMichael Halcrow 	if (m_size < 3) {
244dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
245dddfa461SMichael Halcrow 				"The decrypted key is not long enough to "
246dddfa461SMichael Halcrow 				"include a cipher code and checksum\n");
247dddfa461SMichael Halcrow 		rc = -EIO;
248dddfa461SMichael Halcrow 		goto out;
249dddfa461SMichael Halcrow 	}
250dddfa461SMichael Halcrow 	*cipher_code = data[i++];
251dddfa461SMichael Halcrow 	/* The decrypted key includes 1 byte cipher code and 2 byte checksum */
252dddfa461SMichael Halcrow 	session_key->decrypted_key_size = m_size - 3;
253dddfa461SMichael Halcrow 	if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
254dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
255dddfa461SMichael Halcrow 				"the maximum key size [%d]\n",
256dddfa461SMichael Halcrow 				session_key->decrypted_key_size,
257dddfa461SMichael Halcrow 				ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
258dddfa461SMichael Halcrow 		rc = -EIO;
259dddfa461SMichael Halcrow 		goto out;
260dddfa461SMichael Halcrow 	}
261dddfa461SMichael Halcrow 	memcpy(session_key->decrypted_key, &data[i],
262dddfa461SMichael Halcrow 	       session_key->decrypted_key_size);
263dddfa461SMichael Halcrow 	i += session_key->decrypted_key_size;
264dddfa461SMichael Halcrow 	expected_checksum += (unsigned char)(data[i++]) << 8;
265dddfa461SMichael Halcrow 	expected_checksum += (unsigned char)(data[i++]);
266dddfa461SMichael Halcrow 	for (i = 0; i < session_key->decrypted_key_size; i++)
267dddfa461SMichael Halcrow 		checksum += session_key->decrypted_key[i];
268dddfa461SMichael Halcrow 	if (expected_checksum != checksum) {
269dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
270dddfa461SMichael Halcrow 				"encryption  key; expected [%x]; calculated "
271dddfa461SMichael Halcrow 				"[%x]\n", expected_checksum, checksum);
272dddfa461SMichael Halcrow 		rc = -EIO;
273dddfa461SMichael Halcrow 	}
274dddfa461SMichael Halcrow out:
275dddfa461SMichael Halcrow 	return rc;
276dddfa461SMichael Halcrow }
277dddfa461SMichael Halcrow 
278dddfa461SMichael Halcrow 
279dddfa461SMichael Halcrow static int
28019e66a67STrevor Highland write_tag_66_packet(char *signature, u8 cipher_code,
281dddfa461SMichael Halcrow 		    struct ecryptfs_crypt_stat *crypt_stat, char **packet,
282dddfa461SMichael Halcrow 		    size_t *packet_len)
283dddfa461SMichael Halcrow {
284dddfa461SMichael Halcrow 	size_t i = 0;
285dddfa461SMichael Halcrow 	size_t j;
286dddfa461SMichael Halcrow 	size_t data_len;
287dddfa461SMichael Halcrow 	size_t checksum = 0;
288dddfa461SMichael Halcrow 	size_t packet_size_len;
289dddfa461SMichael Halcrow 	char *message;
290dddfa461SMichael Halcrow 	int rc;
291dddfa461SMichael Halcrow 
292dddfa461SMichael Halcrow 	/*
293dddfa461SMichael Halcrow 	 *              ***** TAG 66 Packet Format *****
294dddfa461SMichael Halcrow 	 *         | Content Type             | 1 byte       |
295dddfa461SMichael Halcrow 	 *         | Key Identifier Size      | 1 or 2 bytes |
296dddfa461SMichael Halcrow 	 *         | Key Identifier           | arbitrary    |
297dddfa461SMichael Halcrow 	 *         | File Encryption Key Size | 1 or 2 bytes |
298dddfa461SMichael Halcrow 	 *         | File Encryption Key      | arbitrary    |
299dddfa461SMichael Halcrow 	 */
300dddfa461SMichael Halcrow 	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
301dddfa461SMichael Halcrow 	*packet = kmalloc(data_len, GFP_KERNEL);
302dddfa461SMichael Halcrow 	message = *packet;
303dddfa461SMichael Halcrow 	if (!message) {
304dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
305dddfa461SMichael Halcrow 		rc = -ENOMEM;
306dddfa461SMichael Halcrow 		goto out;
307dddfa461SMichael Halcrow 	}
308dddfa461SMichael Halcrow 	message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
309f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
310dddfa461SMichael Halcrow 					  &packet_size_len);
311dddfa461SMichael Halcrow 	if (rc) {
312dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
313dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
314dddfa461SMichael Halcrow 		goto out;
315dddfa461SMichael Halcrow 	}
316dddfa461SMichael Halcrow 	i += packet_size_len;
317dddfa461SMichael Halcrow 	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
318dddfa461SMichael Halcrow 	i += ECRYPTFS_SIG_SIZE_HEX;
319dddfa461SMichael Halcrow 	/* The encrypted key includes 1 byte cipher code and 2 byte checksum */
320f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
321dddfa461SMichael Halcrow 					  &packet_size_len);
322dddfa461SMichael Halcrow 	if (rc) {
323dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
324dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
325dddfa461SMichael Halcrow 		goto out;
326dddfa461SMichael Halcrow 	}
327dddfa461SMichael Halcrow 	i += packet_size_len;
328dddfa461SMichael Halcrow 	message[i++] = cipher_code;
329dddfa461SMichael Halcrow 	memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
330dddfa461SMichael Halcrow 	i += crypt_stat->key_size;
331dddfa461SMichael Halcrow 	for (j = 0; j < crypt_stat->key_size; j++)
332dddfa461SMichael Halcrow 		checksum += crypt_stat->key[j];
333dddfa461SMichael Halcrow 	message[i++] = (checksum / 256) % 256;
334dddfa461SMichael Halcrow 	message[i++] = (checksum % 256);
335dddfa461SMichael Halcrow 	*packet_len = i;
336dddfa461SMichael Halcrow out:
337dddfa461SMichael Halcrow 	return rc;
338dddfa461SMichael Halcrow }
339dddfa461SMichael Halcrow 
340dddfa461SMichael Halcrow static int
341dddfa461SMichael Halcrow parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
342dddfa461SMichael Halcrow 		    struct ecryptfs_message *msg)
343dddfa461SMichael Halcrow {
344dddfa461SMichael Halcrow 	size_t i = 0;
345dddfa461SMichael Halcrow 	char *data;
346dddfa461SMichael Halcrow 	size_t data_len;
347dddfa461SMichael Halcrow 	size_t message_len;
348dddfa461SMichael Halcrow 	int rc;
349dddfa461SMichael Halcrow 
350dddfa461SMichael Halcrow 	/*
351dddfa461SMichael Halcrow 	 *              ***** TAG 65 Packet Format *****
352dddfa461SMichael Halcrow 	 *    | Content Type                       | 1 byte       |
353dddfa461SMichael Halcrow 	 *    | Status Indicator                   | 1 byte       |
354dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
355dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key      | arbitrary    |
356dddfa461SMichael Halcrow 	 */
357dddfa461SMichael Halcrow 	message_len = msg->data_len;
358dddfa461SMichael Halcrow 	data = msg->data;
359dddfa461SMichael Halcrow 	/* verify that everything through the encrypted FEK size is present */
360dddfa461SMichael Halcrow 	if (message_len < 4) {
361dddfa461SMichael Halcrow 		rc = -EIO;
362df261c52SMichael Halcrow 		printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable "
363f66e883eSMichael Halcrow 		       "message length is [%d]\n", __func__, message_len, 4);
364dddfa461SMichael Halcrow 		goto out;
365dddfa461SMichael Halcrow 	}
366dddfa461SMichael Halcrow 	if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
367dddfa461SMichael Halcrow 		rc = -EIO;
368f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
369f66e883eSMichael Halcrow 		       __func__);
370dddfa461SMichael Halcrow 		goto out;
371dddfa461SMichael Halcrow 	}
372dddfa461SMichael Halcrow 	if (data[i++]) {
373dddfa461SMichael Halcrow 		rc = -EIO;
374f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: Status indicator has non zero "
375f66e883eSMichael Halcrow 		       "value [%d]\n", __func__, data[i-1]);
376f66e883eSMichael Halcrow 
377dddfa461SMichael Halcrow 		goto out;
378dddfa461SMichael Halcrow 	}
379f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
380f66e883eSMichael Halcrow 					  &data_len);
381dddfa461SMichael Halcrow 	if (rc) {
382dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
383dddfa461SMichael Halcrow 				"rc = [%d]\n", rc);
384dddfa461SMichael Halcrow 		goto out;
385dddfa461SMichael Halcrow 	}
386dddfa461SMichael Halcrow 	i += data_len;
387dddfa461SMichael Halcrow 	if (message_len < (i + key_rec->enc_key_size)) {
388dddfa461SMichael Halcrow 		rc = -EIO;
389df261c52SMichael Halcrow 		printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n",
390f66e883eSMichael Halcrow 		       __func__, message_len, (i + key_rec->enc_key_size));
391dddfa461SMichael Halcrow 		goto out;
392dddfa461SMichael Halcrow 	}
393dddfa461SMichael Halcrow 	if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
394f66e883eSMichael Halcrow 		rc = -EIO;
395df261c52SMichael Halcrow 		printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than "
396f66e883eSMichael Halcrow 		       "the maximum key size [%d]\n", __func__,
397dddfa461SMichael Halcrow 		       key_rec->enc_key_size,
398dddfa461SMichael Halcrow 		       ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
399dddfa461SMichael Halcrow 		goto out;
400dddfa461SMichael Halcrow 	}
401dddfa461SMichael Halcrow 	memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
402dddfa461SMichael Halcrow out:
403dddfa461SMichael Halcrow 	return rc;
404dddfa461SMichael Halcrow }
405dddfa461SMichael Halcrow 
406cd9d67dfSMichael Halcrow static int
4079c79f34fSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig(
4089c79f34fSMichael Halcrow 	struct ecryptfs_global_auth_tok **global_auth_tok,
4099c79f34fSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
4109c79f34fSMichael Halcrow {
4119c79f34fSMichael Halcrow 	struct ecryptfs_global_auth_tok *walker;
4129c79f34fSMichael Halcrow 	int rc = 0;
4139c79f34fSMichael Halcrow 
4149c79f34fSMichael Halcrow 	(*global_auth_tok) = NULL;
4159c79f34fSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
4169c79f34fSMichael Halcrow 	list_for_each_entry(walker,
4179c79f34fSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
4189c79f34fSMichael Halcrow 			    mount_crypt_stat_list) {
4199c79f34fSMichael Halcrow 		if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
42038919598STyler Hicks 			rc = key_validate(walker->global_auth_tok_key);
42138919598STyler Hicks 			if (!rc)
4229c79f34fSMichael Halcrow 				(*global_auth_tok) = walker;
4239c79f34fSMichael Halcrow 			goto out;
4249c79f34fSMichael Halcrow 		}
4259c79f34fSMichael Halcrow 	}
4269c79f34fSMichael Halcrow 	rc = -EINVAL;
4279c79f34fSMichael Halcrow out:
4289c79f34fSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
4299c79f34fSMichael Halcrow 	return rc;
4309c79f34fSMichael Halcrow }
4319c79f34fSMichael Halcrow 
4329c79f34fSMichael Halcrow /**
4339c79f34fSMichael Halcrow  * ecryptfs_find_auth_tok_for_sig
4349c79f34fSMichael Halcrow  * @auth_tok: Set to the matching auth_tok; NULL if not found
4359c79f34fSMichael Halcrow  * @crypt_stat: inode crypt_stat crypto context
4369c79f34fSMichael Halcrow  * @sig: Sig of auth_tok to find
4379c79f34fSMichael Halcrow  *
4389c79f34fSMichael Halcrow  * For now, this function simply looks at the registered auth_tok's
4399c79f34fSMichael Halcrow  * linked off the mount_crypt_stat, so all the auth_toks that can be
4409c79f34fSMichael Halcrow  * used must be registered at mount time. This function could
4419c79f34fSMichael Halcrow  * potentially try a lot harder to find auth_tok's (e.g., by calling
4429c79f34fSMichael Halcrow  * out to ecryptfsd to dynamically retrieve an auth_tok object) so
4439c79f34fSMichael Halcrow  * that static registration of auth_tok's will no longer be necessary.
4449c79f34fSMichael Halcrow  *
4459c79f34fSMichael Halcrow  * Returns zero on no error; non-zero on error
4469c79f34fSMichael Halcrow  */
4479c79f34fSMichael Halcrow static int
4489c79f34fSMichael Halcrow ecryptfs_find_auth_tok_for_sig(
4499c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok **auth_tok,
4509c79f34fSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
4519c79f34fSMichael Halcrow 	char *sig)
4529c79f34fSMichael Halcrow {
4539c79f34fSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
4549c79f34fSMichael Halcrow 	int rc = 0;
4559c79f34fSMichael Halcrow 
4569c79f34fSMichael Halcrow 	(*auth_tok) = NULL;
4579c79f34fSMichael Halcrow 	if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
4589c79f34fSMichael Halcrow 						  mount_crypt_stat, sig)) {
4599c79f34fSMichael Halcrow 		struct key *auth_tok_key;
4609c79f34fSMichael Halcrow 
4619c79f34fSMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
4629c79f34fSMichael Halcrow 						       sig);
4639c79f34fSMichael Halcrow 	} else
4649c79f34fSMichael Halcrow 		(*auth_tok) = global_auth_tok->global_auth_tok;
4659c79f34fSMichael Halcrow 	return rc;
4669c79f34fSMichael Halcrow }
4679c79f34fSMichael Halcrow 
4689c79f34fSMichael Halcrow /**
4699c79f34fSMichael Halcrow  * write_tag_70_packet can gobble a lot of stack space. We stuff most
4709c79f34fSMichael Halcrow  * of the function's parameters in a kmalloc'd struct to help reduce
4719c79f34fSMichael Halcrow  * eCryptfs' overall stack usage.
4729c79f34fSMichael Halcrow  */
4739c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack {
4749c79f34fSMichael Halcrow 	u8 cipher_code;
4759c79f34fSMichael Halcrow 	size_t max_packet_size;
4769c79f34fSMichael Halcrow 	size_t packet_size_len;
4779c79f34fSMichael Halcrow 	size_t block_aligned_filename_size;
4789c79f34fSMichael Halcrow 	size_t block_size;
4799c79f34fSMichael Halcrow 	size_t i;
4809c79f34fSMichael Halcrow 	size_t j;
4819c79f34fSMichael Halcrow 	size_t num_rand_bytes;
4829c79f34fSMichael Halcrow 	struct mutex *tfm_mutex;
4839c79f34fSMichael Halcrow 	char *block_aligned_filename;
4849c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
4859c79f34fSMichael Halcrow 	struct scatterlist src_sg;
4869c79f34fSMichael Halcrow 	struct scatterlist dst_sg;
4879c79f34fSMichael Halcrow 	struct blkcipher_desc desc;
4889c79f34fSMichael Halcrow 	char iv[ECRYPTFS_MAX_IV_BYTES];
4899c79f34fSMichael Halcrow 	char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
4909c79f34fSMichael Halcrow 	char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
4919c79f34fSMichael Halcrow 	struct hash_desc hash_desc;
4929c79f34fSMichael Halcrow 	struct scatterlist hash_sg;
4939c79f34fSMichael Halcrow };
4949c79f34fSMichael Halcrow 
4959c79f34fSMichael Halcrow /**
4969c79f34fSMichael Halcrow  * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
4979c79f34fSMichael Halcrow  * @filename: NULL-terminated filename string
4989c79f34fSMichael Halcrow  *
4999c79f34fSMichael Halcrow  * This is the simplest mechanism for achieving filename encryption in
5009c79f34fSMichael Halcrow  * eCryptfs. It encrypts the given filename with the mount-wide
5019c79f34fSMichael Halcrow  * filename encryption key (FNEK) and stores it in a packet to @dest,
5029c79f34fSMichael Halcrow  * which the callee will encode and write directly into the dentry
5039c79f34fSMichael Halcrow  * name.
5049c79f34fSMichael Halcrow  */
5059c79f34fSMichael Halcrow int
5069c79f34fSMichael Halcrow ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
5079c79f34fSMichael Halcrow 			     size_t *packet_size,
5089c79f34fSMichael Halcrow 			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
5099c79f34fSMichael Halcrow 			     char *filename, size_t filename_size)
5109c79f34fSMichael Halcrow {
5119c79f34fSMichael Halcrow 	struct ecryptfs_write_tag_70_packet_silly_stack *s;
5129c79f34fSMichael Halcrow 	int rc = 0;
5139c79f34fSMichael Halcrow 
5149c79f34fSMichael Halcrow 	s = kmalloc(sizeof(*s), GFP_KERNEL);
5159c79f34fSMichael Halcrow 	if (!s) {
5169c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
517df261c52SMichael Halcrow 		       "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
518*f137f150SJulia Lawall 		rc = -ENOMEM;
5199c79f34fSMichael Halcrow 		goto out;
5209c79f34fSMichael Halcrow 	}
5219c79f34fSMichael Halcrow 	s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
5229c79f34fSMichael Halcrow 	(*packet_size) = 0;
5239c79f34fSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
5249c79f34fSMichael Halcrow 		&s->desc.tfm,
5259c79f34fSMichael Halcrow 		&s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
5269c79f34fSMichael Halcrow 	if (unlikely(rc)) {
5279c79f34fSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
5289c79f34fSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
5299c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_name, rc);
5309c79f34fSMichael Halcrow 		goto out;
5319c79f34fSMichael Halcrow 	}
5329c79f34fSMichael Halcrow 	mutex_lock(s->tfm_mutex);
5339c79f34fSMichael Halcrow 	s->block_size = crypto_blkcipher_blocksize(s->desc.tfm);
5349c79f34fSMichael Halcrow 	/* Plus one for the \0 separator between the random prefix
5359c79f34fSMichael Halcrow 	 * and the plaintext filename */
5369c79f34fSMichael Halcrow 	s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
5379c79f34fSMichael Halcrow 	s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
5389c79f34fSMichael Halcrow 	if ((s->block_aligned_filename_size % s->block_size) != 0) {
5399c79f34fSMichael Halcrow 		s->num_rand_bytes += (s->block_size
5409c79f34fSMichael Halcrow 				      - (s->block_aligned_filename_size
5419c79f34fSMichael Halcrow 					 % s->block_size));
5429c79f34fSMichael Halcrow 		s->block_aligned_filename_size = (s->num_rand_bytes
5439c79f34fSMichael Halcrow 						  + filename_size);
5449c79f34fSMichael Halcrow 	}
5459c79f34fSMichael Halcrow 	/* Octet 0: Tag 70 identifier
5469c79f34fSMichael Halcrow 	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
5479c79f34fSMichael Halcrow 	 *              and block-aligned encrypted filename size)
5489c79f34fSMichael Halcrow 	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
5499c79f34fSMichael Halcrow 	 * Octet N2-N3: Cipher identifier (1 octet)
5509c79f34fSMichael Halcrow 	 * Octets N3-N4: Block-aligned encrypted filename
5519c79f34fSMichael Halcrow 	 *  - Consists of a minimum number of random characters, a \0
5529c79f34fSMichael Halcrow 	 *    separator, and then the filename */
5539c79f34fSMichael Halcrow 	s->max_packet_size = (1                   /* Tag 70 identifier */
5549c79f34fSMichael Halcrow 			      + 3                 /* Max Tag 70 packet size */
5559c79f34fSMichael Halcrow 			      + ECRYPTFS_SIG_SIZE /* FNEK sig */
5569c79f34fSMichael Halcrow 			      + 1                 /* Cipher identifier */
5579c79f34fSMichael Halcrow 			      + s->block_aligned_filename_size);
5589c79f34fSMichael Halcrow 	if (dest == NULL) {
5599c79f34fSMichael Halcrow 		(*packet_size) = s->max_packet_size;
5609c79f34fSMichael Halcrow 		goto out_unlock;
5619c79f34fSMichael Halcrow 	}
5629c79f34fSMichael Halcrow 	if (s->max_packet_size > (*remaining_bytes)) {
563a8f12864SMichael Halcrow 		printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
564a8f12864SMichael Halcrow 		       "[%zd] available\n", __func__, s->max_packet_size,
5659c79f34fSMichael Halcrow 		       (*remaining_bytes));
5669c79f34fSMichael Halcrow 		rc = -EINVAL;
5679c79f34fSMichael Halcrow 		goto out_unlock;
5689c79f34fSMichael Halcrow 	}
5699c79f34fSMichael Halcrow 	s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
5709c79f34fSMichael Halcrow 					    GFP_KERNEL);
5719c79f34fSMichael Halcrow 	if (!s->block_aligned_filename) {
5729c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
573df261c52SMichael Halcrow 		       "kzalloc [%zd] bytes\n", __func__,
5749c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
5759c79f34fSMichael Halcrow 		rc = -ENOMEM;
5769c79f34fSMichael Halcrow 		goto out_unlock;
5779c79f34fSMichael Halcrow 	}
5789c79f34fSMichael Halcrow 	s->i = 0;
5799c79f34fSMichael Halcrow 	dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
5809c79f34fSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[s->i],
5819c79f34fSMichael Halcrow 					  (ECRYPTFS_SIG_SIZE
5829c79f34fSMichael Halcrow 					   + 1 /* Cipher code */
5839c79f34fSMichael Halcrow 					   + s->block_aligned_filename_size),
5849c79f34fSMichael Halcrow 					  &s->packet_size_len);
5859c79f34fSMichael Halcrow 	if (rc) {
5869c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error generating tag 70 packet "
5879c79f34fSMichael Halcrow 		       "header; cannot generate packet length; rc = [%d]\n",
5889c79f34fSMichael Halcrow 		       __func__, rc);
5899c79f34fSMichael Halcrow 		goto out_free_unlock;
5909c79f34fSMichael Halcrow 	}
5919c79f34fSMichael Halcrow 	s->i += s->packet_size_len;
5929c79f34fSMichael Halcrow 	ecryptfs_from_hex(&dest[s->i],
5939c79f34fSMichael Halcrow 			  mount_crypt_stat->global_default_fnek_sig,
5949c79f34fSMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
5959c79f34fSMichael Halcrow 	s->i += ECRYPTFS_SIG_SIZE;
5969c79f34fSMichael Halcrow 	s->cipher_code = ecryptfs_code_for_cipher_string(
5979c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_name,
5989c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
5999c79f34fSMichael Halcrow 	if (s->cipher_code == 0) {
6009c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Unable to generate code for "
601a8f12864SMichael Halcrow 		       "cipher [%s] with key bytes [%zd]\n", __func__,
6029c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_name,
6039c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
6049c79f34fSMichael Halcrow 		rc = -EINVAL;
6059c79f34fSMichael Halcrow 		goto out_free_unlock;
6069c79f34fSMichael Halcrow 	}
6079c79f34fSMichael Halcrow 	dest[s->i++] = s->cipher_code;
6089c79f34fSMichael Halcrow 	rc = ecryptfs_find_auth_tok_for_sig(
6099c79f34fSMichael Halcrow 		&s->auth_tok, mount_crypt_stat,
6109c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fnek_sig);
6119c79f34fSMichael Halcrow 	if (rc) {
6129c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to find auth tok for "
6139c79f34fSMichael Halcrow 		       "fnek sig [%s]; rc = [%d]\n", __func__,
6149c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fnek_sig, rc);
6159c79f34fSMichael Halcrow 		goto out_free_unlock;
6169c79f34fSMichael Halcrow 	}
6179c79f34fSMichael Halcrow 	/* TODO: Support other key modules than passphrase for
6189c79f34fSMichael Halcrow 	 * filename encryption */
619df6ad33bSTyler Hicks 	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
620df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
621df6ad33bSTyler Hicks 		printk(KERN_INFO "%s: Filename encryption only supports "
622df6ad33bSTyler Hicks 		       "password tokens\n", __func__);
623df6ad33bSTyler Hicks 		goto out_free_unlock;
624df6ad33bSTyler Hicks 	}
6259c79f34fSMichael Halcrow 	sg_init_one(
6269c79f34fSMichael Halcrow 		&s->hash_sg,
6279c79f34fSMichael Halcrow 		(u8 *)s->auth_tok->token.password.session_key_encryption_key,
6289c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key_bytes);
6299c79f34fSMichael Halcrow 	s->hash_desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
6309c79f34fSMichael Halcrow 	s->hash_desc.tfm = crypto_alloc_hash(ECRYPTFS_TAG_70_DIGEST, 0,
6319c79f34fSMichael Halcrow 					     CRYPTO_ALG_ASYNC);
6329c79f34fSMichael Halcrow 	if (IS_ERR(s->hash_desc.tfm)) {
6339c79f34fSMichael Halcrow 			rc = PTR_ERR(s->hash_desc.tfm);
6349c79f34fSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to "
6359c79f34fSMichael Halcrow 			       "allocate hash crypto context; rc = [%d]\n",
6369c79f34fSMichael Halcrow 			       __func__, rc);
6379c79f34fSMichael Halcrow 			goto out_free_unlock;
6389c79f34fSMichael Halcrow 	}
6399c79f34fSMichael Halcrow 	rc = crypto_hash_init(&s->hash_desc);
6409c79f34fSMichael Halcrow 	if (rc) {
6419c79f34fSMichael Halcrow 		printk(KERN_ERR
6429c79f34fSMichael Halcrow 		       "%s: Error initializing crypto hash; rc = [%d]\n",
6439c79f34fSMichael Halcrow 		       __func__, rc);
6449c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6459c79f34fSMichael Halcrow 	}
6469c79f34fSMichael Halcrow 	rc = crypto_hash_update(
6479c79f34fSMichael Halcrow 		&s->hash_desc, &s->hash_sg,
6489c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key_bytes);
6499c79f34fSMichael Halcrow 	if (rc) {
6509c79f34fSMichael Halcrow 		printk(KERN_ERR
6519c79f34fSMichael Halcrow 		       "%s: Error updating crypto hash; rc = [%d]\n",
6529c79f34fSMichael Halcrow 		       __func__, rc);
6539c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6549c79f34fSMichael Halcrow 	}
6559c79f34fSMichael Halcrow 	rc = crypto_hash_final(&s->hash_desc, s->hash);
6569c79f34fSMichael Halcrow 	if (rc) {
6579c79f34fSMichael Halcrow 		printk(KERN_ERR
6589c79f34fSMichael Halcrow 		       "%s: Error finalizing crypto hash; rc = [%d]\n",
6599c79f34fSMichael Halcrow 		       __func__, rc);
6609c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6619c79f34fSMichael Halcrow 	}
6629c79f34fSMichael Halcrow 	for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
6639c79f34fSMichael Halcrow 		s->block_aligned_filename[s->j] =
6649c79f34fSMichael Halcrow 			s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
6659c79f34fSMichael Halcrow 		if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
6669c79f34fSMichael Halcrow 		    == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
6679c79f34fSMichael Halcrow 			sg_init_one(&s->hash_sg, (u8 *)s->hash,
6689c79f34fSMichael Halcrow 				    ECRYPTFS_TAG_70_DIGEST_SIZE);
6699c79f34fSMichael Halcrow 			rc = crypto_hash_init(&s->hash_desc);
6709c79f34fSMichael Halcrow 			if (rc) {
6719c79f34fSMichael Halcrow 				printk(KERN_ERR
6729c79f34fSMichael Halcrow 				       "%s: Error initializing crypto hash; "
6739c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
6749c79f34fSMichael Halcrow 				goto out_release_free_unlock;
6759c79f34fSMichael Halcrow 			}
6769c79f34fSMichael Halcrow 			rc = crypto_hash_update(&s->hash_desc, &s->hash_sg,
6779c79f34fSMichael Halcrow 						ECRYPTFS_TAG_70_DIGEST_SIZE);
6789c79f34fSMichael Halcrow 			if (rc) {
6799c79f34fSMichael Halcrow 				printk(KERN_ERR
6809c79f34fSMichael Halcrow 				       "%s: Error updating crypto hash; "
6819c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
6829c79f34fSMichael Halcrow 				goto out_release_free_unlock;
6839c79f34fSMichael Halcrow 			}
6849c79f34fSMichael Halcrow 			rc = crypto_hash_final(&s->hash_desc, s->tmp_hash);
6859c79f34fSMichael Halcrow 			if (rc) {
6869c79f34fSMichael Halcrow 				printk(KERN_ERR
6879c79f34fSMichael Halcrow 				       "%s: Error finalizing crypto hash; "
6889c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
6899c79f34fSMichael Halcrow 				goto out_release_free_unlock;
6909c79f34fSMichael Halcrow 			}
6919c79f34fSMichael Halcrow 			memcpy(s->hash, s->tmp_hash,
6929c79f34fSMichael Halcrow 			       ECRYPTFS_TAG_70_DIGEST_SIZE);
6939c79f34fSMichael Halcrow 		}
6949c79f34fSMichael Halcrow 		if (s->block_aligned_filename[s->j] == '\0')
6959c79f34fSMichael Halcrow 			s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
6969c79f34fSMichael Halcrow 	}
6979c79f34fSMichael Halcrow 	memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
6989c79f34fSMichael Halcrow 	       filename_size);
6999c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(s->block_aligned_filename,
7009c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->src_sg, 1);
7019c79f34fSMichael Halcrow 	if (rc != 1) {
7029c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
7039c79f34fSMichael Halcrow 		       "convert filename memory to scatterlist; "
7049c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
705a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
7069c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
7079c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7089c79f34fSMichael Halcrow 	}
7099c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
7109c79f34fSMichael Halcrow 				 &s->dst_sg, 1);
7119c79f34fSMichael Halcrow 	if (rc != 1) {
7129c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
7139c79f34fSMichael Halcrow 		       "convert encrypted filename memory to scatterlist; "
7149c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
715a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
7169c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
7179c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7189c79f34fSMichael Halcrow 	}
7199c79f34fSMichael Halcrow 	/* The characters in the first block effectively do the job
7209c79f34fSMichael Halcrow 	 * of the IV here, so we just use 0's for the IV. Note the
7219c79f34fSMichael Halcrow 	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
7229c79f34fSMichael Halcrow 	 * >= ECRYPTFS_MAX_IV_BYTES. */
7239c79f34fSMichael Halcrow 	memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
7249c79f34fSMichael Halcrow 	s->desc.info = s->iv;
7259c79f34fSMichael Halcrow 	rc = crypto_blkcipher_setkey(
7269c79f34fSMichael Halcrow 		s->desc.tfm,
7279c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key,
7289c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
7299c79f34fSMichael Halcrow 	if (rc < 0) {
7309c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error setting key for crypto context; "
7319c79f34fSMichael Halcrow 		       "rc = [%d]. s->auth_tok->token.password.session_key_"
7329c79f34fSMichael Halcrow 		       "encryption_key = [0x%p]; mount_crypt_stat->"
733df261c52SMichael Halcrow 		       "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
7349c79f34fSMichael Halcrow 		       rc,
7359c79f34fSMichael Halcrow 		       s->auth_tok->token.password.session_key_encryption_key,
7369c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
7379c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7389c79f34fSMichael Halcrow 	}
7399c79f34fSMichael Halcrow 	rc = crypto_blkcipher_encrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
7409c79f34fSMichael Halcrow 					 s->block_aligned_filename_size);
7419c79f34fSMichael Halcrow 	if (rc) {
7429c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to encrypt filename; "
7439c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
7449c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7459c79f34fSMichael Halcrow 	}
7469c79f34fSMichael Halcrow 	s->i += s->block_aligned_filename_size;
7479c79f34fSMichael Halcrow 	(*packet_size) = s->i;
7489c79f34fSMichael Halcrow 	(*remaining_bytes) -= (*packet_size);
7499c79f34fSMichael Halcrow out_release_free_unlock:
7509c79f34fSMichael Halcrow 	crypto_free_hash(s->hash_desc.tfm);
7519c79f34fSMichael Halcrow out_free_unlock:
75200fcf2cbSJohannes Weiner 	kzfree(s->block_aligned_filename);
7539c79f34fSMichael Halcrow out_unlock:
7549c79f34fSMichael Halcrow 	mutex_unlock(s->tfm_mutex);
7559c79f34fSMichael Halcrow out:
7569c79f34fSMichael Halcrow 	kfree(s);
7579c79f34fSMichael Halcrow 	return rc;
7589c79f34fSMichael Halcrow }
7599c79f34fSMichael Halcrow 
7609c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack {
7619c79f34fSMichael Halcrow 	u8 cipher_code;
7629c79f34fSMichael Halcrow 	size_t max_packet_size;
7639c79f34fSMichael Halcrow 	size_t packet_size_len;
7649c79f34fSMichael Halcrow 	size_t parsed_tag_70_packet_size;
7659c79f34fSMichael Halcrow 	size_t block_aligned_filename_size;
7669c79f34fSMichael Halcrow 	size_t block_size;
7679c79f34fSMichael Halcrow 	size_t i;
7689c79f34fSMichael Halcrow 	struct mutex *tfm_mutex;
7699c79f34fSMichael Halcrow 	char *decrypted_filename;
7709c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
7719c79f34fSMichael Halcrow 	struct scatterlist src_sg;
7729c79f34fSMichael Halcrow 	struct scatterlist dst_sg;
7739c79f34fSMichael Halcrow 	struct blkcipher_desc desc;
7749c79f34fSMichael Halcrow 	char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
7759c79f34fSMichael Halcrow 	char iv[ECRYPTFS_MAX_IV_BYTES];
7769c79f34fSMichael Halcrow 	char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
7779c79f34fSMichael Halcrow };
7789c79f34fSMichael Halcrow 
7799c79f34fSMichael Halcrow /**
7809c79f34fSMichael Halcrow  * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
7819c79f34fSMichael Halcrow  * @filename: This function kmalloc's the memory for the filename
7827d8bc2beSMichael Halcrow  * @filename_size: This function sets this to the amount of memory
7837d8bc2beSMichael Halcrow  *                 kmalloc'd for the filename
7847d8bc2beSMichael Halcrow  * @packet_size: This function sets this to the the number of octets
7857d8bc2beSMichael Halcrow  *               in the packet parsed
7867d8bc2beSMichael Halcrow  * @mount_crypt_stat: The mount-wide cryptographic context
7877d8bc2beSMichael Halcrow  * @data: The memory location containing the start of the tag 70
7887d8bc2beSMichael Halcrow  *        packet
7897d8bc2beSMichael Halcrow  * @max_packet_size: The maximum legal size of the packet to be parsed
7907d8bc2beSMichael Halcrow  *                   from @data
7917d8bc2beSMichael Halcrow  *
7927d8bc2beSMichael Halcrow  * Returns zero on success; non-zero otherwise
7939c79f34fSMichael Halcrow  */
7949c79f34fSMichael Halcrow int
7959c79f34fSMichael Halcrow ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
7969c79f34fSMichael Halcrow 			     size_t *packet_size,
7979c79f34fSMichael Halcrow 			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
7989c79f34fSMichael Halcrow 			     char *data, size_t max_packet_size)
7999c79f34fSMichael Halcrow {
8009c79f34fSMichael Halcrow 	struct ecryptfs_parse_tag_70_packet_silly_stack *s;
8019c79f34fSMichael Halcrow 	int rc = 0;
8029c79f34fSMichael Halcrow 
8039c79f34fSMichael Halcrow 	(*packet_size) = 0;
8049c79f34fSMichael Halcrow 	(*filename_size) = 0;
8059c79f34fSMichael Halcrow 	(*filename) = NULL;
8069c79f34fSMichael Halcrow 	s = kmalloc(sizeof(*s), GFP_KERNEL);
8079c79f34fSMichael Halcrow 	if (!s) {
8089c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
809a8f12864SMichael Halcrow 		       "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
810*f137f150SJulia Lawall 		rc = -ENOMEM;
8119c79f34fSMichael Halcrow 		goto out;
8129c79f34fSMichael Halcrow 	}
8139c79f34fSMichael Halcrow 	s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
8149c79f34fSMichael Halcrow 	if (max_packet_size < (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)) {
815df261c52SMichael Halcrow 		printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
8169c79f34fSMichael Halcrow 		       "at least [%d]\n", __func__, max_packet_size,
8179c79f34fSMichael Halcrow 			(1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1));
8189c79f34fSMichael Halcrow 		rc = -EINVAL;
8199c79f34fSMichael Halcrow 		goto out;
8209c79f34fSMichael Halcrow 	}
8219c79f34fSMichael Halcrow 	/* Octet 0: Tag 70 identifier
8229c79f34fSMichael Halcrow 	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
8239c79f34fSMichael Halcrow 	 *              and block-aligned encrypted filename size)
8249c79f34fSMichael Halcrow 	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
8259c79f34fSMichael Halcrow 	 * Octet N2-N3: Cipher identifier (1 octet)
8269c79f34fSMichael Halcrow 	 * Octets N3-N4: Block-aligned encrypted filename
8279c79f34fSMichael Halcrow 	 *  - Consists of a minimum number of random numbers, a \0
8289c79f34fSMichael Halcrow 	 *    separator, and then the filename */
8299c79f34fSMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
8309c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
8319c79f34fSMichael Halcrow 		       "tag [0x%.2x]\n", __func__,
8329c79f34fSMichael Halcrow 		       data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
8339c79f34fSMichael Halcrow 		rc = -EINVAL;
8349c79f34fSMichael Halcrow 		goto out;
8359c79f34fSMichael Halcrow 	}
8369c79f34fSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
8379c79f34fSMichael Halcrow 					  &s->parsed_tag_70_packet_size,
8389c79f34fSMichael Halcrow 					  &s->packet_size_len);
8399c79f34fSMichael Halcrow 	if (rc) {
8409c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Error parsing packet length; "
8419c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
8429c79f34fSMichael Halcrow 		goto out;
8439c79f34fSMichael Halcrow 	}
8449c79f34fSMichael Halcrow 	s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
8459c79f34fSMichael Halcrow 					  - ECRYPTFS_SIG_SIZE - 1);
8469c79f34fSMichael Halcrow 	if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
8479c79f34fSMichael Halcrow 	    > max_packet_size) {
848a8f12864SMichael Halcrow 		printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
849a8f12864SMichael Halcrow 		       "size is [%zd]\n", __func__, max_packet_size,
8509c79f34fSMichael Halcrow 		       (1 + s->packet_size_len + 1
8519c79f34fSMichael Halcrow 			+ s->block_aligned_filename_size));
8529c79f34fSMichael Halcrow 		rc = -EINVAL;
8539c79f34fSMichael Halcrow 		goto out;
8549c79f34fSMichael Halcrow 	}
8559c79f34fSMichael Halcrow 	(*packet_size) += s->packet_size_len;
8569c79f34fSMichael Halcrow 	ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
8579c79f34fSMichael Halcrow 			ECRYPTFS_SIG_SIZE);
8589c79f34fSMichael Halcrow 	s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
8599c79f34fSMichael Halcrow 	(*packet_size) += ECRYPTFS_SIG_SIZE;
8609c79f34fSMichael Halcrow 	s->cipher_code = data[(*packet_size)++];
8619c79f34fSMichael Halcrow 	rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
8629c79f34fSMichael Halcrow 	if (rc) {
8639c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
8649c79f34fSMichael Halcrow 		       __func__, s->cipher_code);
8659c79f34fSMichael Halcrow 		goto out;
8669c79f34fSMichael Halcrow 	}
8679c79f34fSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
8689c79f34fSMichael Halcrow 							&s->tfm_mutex,
8699c79f34fSMichael Halcrow 							s->cipher_string);
8709c79f34fSMichael Halcrow 	if (unlikely(rc)) {
8719c79f34fSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
8729c79f34fSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
8739c79f34fSMichael Halcrow 		       s->cipher_string, rc);
8749c79f34fSMichael Halcrow 		goto out;
8759c79f34fSMichael Halcrow 	}
8769c79f34fSMichael Halcrow 	mutex_lock(s->tfm_mutex);
8779c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(&data[(*packet_size)],
8789c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->src_sg, 1);
8799c79f34fSMichael Halcrow 	if (rc != 1) {
8809c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
8819c79f34fSMichael Halcrow 		       "convert encrypted filename memory to scatterlist; "
8829c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
883a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
8849c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
8859c79f34fSMichael Halcrow 		goto out_unlock;
8869c79f34fSMichael Halcrow 	}
8879c79f34fSMichael Halcrow 	(*packet_size) += s->block_aligned_filename_size;
8889c79f34fSMichael Halcrow 	s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
8899c79f34fSMichael Halcrow 					GFP_KERNEL);
8909c79f34fSMichael Halcrow 	if (!s->decrypted_filename) {
8919c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst attempting to "
892a8f12864SMichael Halcrow 		       "kmalloc [%zd] bytes\n", __func__,
8939c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
8949c79f34fSMichael Halcrow 		rc = -ENOMEM;
8959c79f34fSMichael Halcrow 		goto out_unlock;
8969c79f34fSMichael Halcrow 	}
8979c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(s->decrypted_filename,
8989c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->dst_sg, 1);
8999c79f34fSMichael Halcrow 	if (rc != 1) {
9009c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
9019c79f34fSMichael Halcrow 		       "convert decrypted filename memory to scatterlist; "
9029c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
903a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
9049c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
9059c79f34fSMichael Halcrow 		goto out_free_unlock;
9069c79f34fSMichael Halcrow 	}
9079c79f34fSMichael Halcrow 	/* The characters in the first block effectively do the job of
9089c79f34fSMichael Halcrow 	 * the IV here, so we just use 0's for the IV. Note the
9099c79f34fSMichael Halcrow 	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
9109c79f34fSMichael Halcrow 	 * >= ECRYPTFS_MAX_IV_BYTES. */
9119c79f34fSMichael Halcrow 	memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
9129c79f34fSMichael Halcrow 	s->desc.info = s->iv;
9139c79f34fSMichael Halcrow 	rc = ecryptfs_find_auth_tok_for_sig(&s->auth_tok, mount_crypt_stat,
9149c79f34fSMichael Halcrow 					    s->fnek_sig_hex);
9159c79f34fSMichael Halcrow 	if (rc) {
9169c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to find auth tok for "
9179c79f34fSMichael Halcrow 		       "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
9189c79f34fSMichael Halcrow 		       rc);
9199c79f34fSMichael Halcrow 		goto out_free_unlock;
9209c79f34fSMichael Halcrow 	}
9219c79f34fSMichael Halcrow 	/* TODO: Support other key modules than passphrase for
9229c79f34fSMichael Halcrow 	 * filename encryption */
923df6ad33bSTyler Hicks 	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
924df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
925df6ad33bSTyler Hicks 		printk(KERN_INFO "%s: Filename encryption only supports "
926df6ad33bSTyler Hicks 		       "password tokens\n", __func__);
927df6ad33bSTyler Hicks 		goto out_free_unlock;
928df6ad33bSTyler Hicks 	}
9299c79f34fSMichael Halcrow 	rc = crypto_blkcipher_setkey(
9309c79f34fSMichael Halcrow 		s->desc.tfm,
9319c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key,
9329c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
9339c79f34fSMichael Halcrow 	if (rc < 0) {
9349c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error setting key for crypto context; "
9359c79f34fSMichael Halcrow 		       "rc = [%d]. s->auth_tok->token.password.session_key_"
9369c79f34fSMichael Halcrow 		       "encryption_key = [0x%p]; mount_crypt_stat->"
937df261c52SMichael Halcrow 		       "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
9389c79f34fSMichael Halcrow 		       rc,
9399c79f34fSMichael Halcrow 		       s->auth_tok->token.password.session_key_encryption_key,
9409c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
9419c79f34fSMichael Halcrow 		goto out_free_unlock;
9429c79f34fSMichael Halcrow 	}
9439c79f34fSMichael Halcrow 	rc = crypto_blkcipher_decrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
9449c79f34fSMichael Halcrow 					 s->block_aligned_filename_size);
9459c79f34fSMichael Halcrow 	if (rc) {
9469c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decrypt filename; "
9479c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
9489c79f34fSMichael Halcrow 		goto out_free_unlock;
9499c79f34fSMichael Halcrow 	}
9509c79f34fSMichael Halcrow 	s->i = 0;
9519c79f34fSMichael Halcrow 	while (s->decrypted_filename[s->i] != '\0'
9529c79f34fSMichael Halcrow 	       && s->i < s->block_aligned_filename_size)
9539c79f34fSMichael Halcrow 		s->i++;
9549c79f34fSMichael Halcrow 	if (s->i == s->block_aligned_filename_size) {
9559c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
9569c79f34fSMichael Halcrow 		       "find valid separator between random characters and "
9579c79f34fSMichael Halcrow 		       "the filename\n", __func__);
9589c79f34fSMichael Halcrow 		rc = -EINVAL;
9599c79f34fSMichael Halcrow 		goto out_free_unlock;
9609c79f34fSMichael Halcrow 	}
9619c79f34fSMichael Halcrow 	s->i++;
9629c79f34fSMichael Halcrow 	(*filename_size) = (s->block_aligned_filename_size - s->i);
9639c79f34fSMichael Halcrow 	if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
964df261c52SMichael Halcrow 		printk(KERN_WARNING "%s: Filename size is [%zd], which is "
9659c79f34fSMichael Halcrow 		       "invalid\n", __func__, (*filename_size));
9669c79f34fSMichael Halcrow 		rc = -EINVAL;
9679c79f34fSMichael Halcrow 		goto out_free_unlock;
9689c79f34fSMichael Halcrow 	}
9699c79f34fSMichael Halcrow 	(*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
9709c79f34fSMichael Halcrow 	if (!(*filename)) {
9719c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst attempting to "
972a8f12864SMichael Halcrow 		       "kmalloc [%zd] bytes\n", __func__,
9739c79f34fSMichael Halcrow 		       ((*filename_size) + 1));
9749c79f34fSMichael Halcrow 		rc = -ENOMEM;
9759c79f34fSMichael Halcrow 		goto out_free_unlock;
9769c79f34fSMichael Halcrow 	}
9779c79f34fSMichael Halcrow 	memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
9789c79f34fSMichael Halcrow 	(*filename)[(*filename_size)] = '\0';
9799c79f34fSMichael Halcrow out_free_unlock:
9809c79f34fSMichael Halcrow 	kfree(s->decrypted_filename);
9819c79f34fSMichael Halcrow out_unlock:
9829c79f34fSMichael Halcrow 	mutex_unlock(s->tfm_mutex);
9839c79f34fSMichael Halcrow out:
9849c79f34fSMichael Halcrow 	if (rc) {
9859c79f34fSMichael Halcrow 		(*packet_size) = 0;
9869c79f34fSMichael Halcrow 		(*filename_size) = 0;
9879c79f34fSMichael Halcrow 		(*filename) = NULL;
9889c79f34fSMichael Halcrow 	}
9899c79f34fSMichael Halcrow 	kfree(s);
9909c79f34fSMichael Halcrow 	return rc;
9919c79f34fSMichael Halcrow }
9929c79f34fSMichael Halcrow 
9939c79f34fSMichael Halcrow static int
994cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
995cd9d67dfSMichael Halcrow {
996cd9d67dfSMichael Halcrow 	int rc = 0;
997cd9d67dfSMichael Halcrow 
998cd9d67dfSMichael Halcrow 	(*sig) = NULL;
999cd9d67dfSMichael Halcrow 	switch (auth_tok->token_type) {
1000cd9d67dfSMichael Halcrow 	case ECRYPTFS_PASSWORD:
1001cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.password.signature;
1002cd9d67dfSMichael Halcrow 		break;
1003cd9d67dfSMichael Halcrow 	case ECRYPTFS_PRIVATE_KEY:
1004cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.private_key.signature;
1005cd9d67dfSMichael Halcrow 		break;
1006cd9d67dfSMichael Halcrow 	default:
1007cd9d67dfSMichael Halcrow 		printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1008cd9d67dfSMichael Halcrow 		       auth_tok->token_type);
1009cd9d67dfSMichael Halcrow 		rc = -EINVAL;
1010cd9d67dfSMichael Halcrow 	}
1011cd9d67dfSMichael Halcrow 	return rc;
1012cd9d67dfSMichael Halcrow }
1013cd9d67dfSMichael Halcrow 
1014dddfa461SMichael Halcrow /**
101522e78fafSMichael Halcrow  * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
101622e78fafSMichael Halcrow  * @auth_tok: The key authentication token used to decrypt the session key
101722e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1018dddfa461SMichael Halcrow  *
101922e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise.
1020dddfa461SMichael Halcrow  */
1021f4aad16aSMichael Halcrow static int
1022f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1023dddfa461SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat)
1024dddfa461SMichael Halcrow {
102519e66a67STrevor Highland 	u8 cipher_code = 0;
1026dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx;
1027dddfa461SMichael Halcrow 	struct ecryptfs_message *msg = NULL;
1028f4aad16aSMichael Halcrow 	char *auth_tok_sig;
1029624ae528STyler Hicks 	char *payload;
1030624ae528STyler Hicks 	size_t payload_len;
1031dddfa461SMichael Halcrow 	int rc;
1032dddfa461SMichael Halcrow 
10335dda6992SMichael Halcrow 	rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
10345dda6992SMichael Halcrow 	if (rc) {
1035f4aad16aSMichael Halcrow 		printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
1036f4aad16aSMichael Halcrow 		       auth_tok->token_type);
1037f4aad16aSMichael Halcrow 		goto out;
1038f4aad16aSMichael Halcrow 	}
1039f4aad16aSMichael Halcrow 	rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
1040624ae528STyler Hicks 				 &payload, &payload_len);
1041dddfa461SMichael Halcrow 	if (rc) {
1042f66e883eSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
1043dddfa461SMichael Halcrow 		goto out;
1044dddfa461SMichael Halcrow 	}
1045624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1046dddfa461SMichael Halcrow 	if (rc) {
1047624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
1048624ae528STyler Hicks 				"ecryptfsd\n");
1049dddfa461SMichael Halcrow 		goto out;
1050dddfa461SMichael Halcrow 	}
1051dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1052dddfa461SMichael Halcrow 	if (rc) {
1053dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
1054dddfa461SMichael Halcrow 				"from the user space daemon\n");
1055dddfa461SMichael Halcrow 		rc = -EIO;
1056dddfa461SMichael Halcrow 		goto out;
1057dddfa461SMichael Halcrow 	}
1058dddfa461SMichael Halcrow 	rc = parse_tag_65_packet(&(auth_tok->session_key),
1059dddfa461SMichael Halcrow 				 &cipher_code, msg);
1060dddfa461SMichael Halcrow 	if (rc) {
1061dddfa461SMichael Halcrow 		printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
1062dddfa461SMichael Halcrow 		       rc);
1063dddfa461SMichael Halcrow 		goto out;
1064dddfa461SMichael Halcrow 	}
1065dddfa461SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1066dddfa461SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1067dddfa461SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
1068dddfa461SMichael Halcrow 	crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
1069dddfa461SMichael Halcrow 	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
1070dddfa461SMichael Halcrow 	if (rc) {
1071dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
1072dddfa461SMichael Halcrow 				cipher_code)
1073dddfa461SMichael Halcrow 		goto out;
1074dddfa461SMichael Halcrow 	}
1075dddfa461SMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1076dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1077dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
1078dddfa461SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
1079dddfa461SMichael Halcrow 				  crypt_stat->key_size);
1080dddfa461SMichael Halcrow 	}
1081dddfa461SMichael Halcrow out:
1082dddfa461SMichael Halcrow 	if (msg)
1083dddfa461SMichael Halcrow 		kfree(msg);
1084dddfa461SMichael Halcrow 	return rc;
1085dddfa461SMichael Halcrow }
1086dddfa461SMichael Halcrow 
1087dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
1088dddfa461SMichael Halcrow {
1089dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1090e0869cc1SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1091dddfa461SMichael Halcrow 
1092e0869cc1SMichael Halcrow 	list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
1093e0869cc1SMichael Halcrow 				 auth_tok_list_head, list) {
1094e0869cc1SMichael Halcrow 		list_del(&auth_tok_list_item->list);
1095dddfa461SMichael Halcrow 		kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1096dddfa461SMichael Halcrow 				auth_tok_list_item);
1097dddfa461SMichael Halcrow 	}
1098dddfa461SMichael Halcrow }
1099dddfa461SMichael Halcrow 
1100dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
1101dddfa461SMichael Halcrow 
1102dddfa461SMichael Halcrow /**
1103dddfa461SMichael Halcrow  * parse_tag_1_packet
110422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet contents
1105dddfa461SMichael Halcrow  * @data: The raw bytes of the packet.
1106dddfa461SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
110722e78fafSMichael Halcrow  *                 a new authentication token will be placed at the
110822e78fafSMichael Halcrow  *                 end of this list for this packet.
1109dddfa461SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
1110dddfa461SMichael Halcrow  *                allocates; sets the memory address of the pointer to
1111dddfa461SMichael Halcrow  *                NULL on error. This object is added to the
1112dddfa461SMichael Halcrow  *                auth_tok_list.
1113dddfa461SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1114dddfa461SMichael Halcrow  *               into this memory location; zero on error.
111522e78fafSMichael Halcrow  * @max_packet_size: The maximum allowable packet size
1116dddfa461SMichael Halcrow  *
1117dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
1118dddfa461SMichael Halcrow  */
1119dddfa461SMichael Halcrow static int
1120dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
1121dddfa461SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
1122dddfa461SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
1123dddfa461SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
1124dddfa461SMichael Halcrow {
1125dddfa461SMichael Halcrow 	size_t body_size;
1126dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1127dddfa461SMichael Halcrow 	size_t length_size;
1128dddfa461SMichael Halcrow 	int rc = 0;
1129dddfa461SMichael Halcrow 
1130dddfa461SMichael Halcrow 	(*packet_size) = 0;
1131dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
113213218179SMichael Halcrow 	/**
113313218179SMichael Halcrow 	 * This format is inspired by OpenPGP; see RFC 2440
113413218179SMichael Halcrow 	 * packet tag 1
113513218179SMichael Halcrow 	 *
113613218179SMichael Halcrow 	 * Tag 1 identifier (1 byte)
113713218179SMichael Halcrow 	 * Max Tag 1 packet size (max 3 bytes)
113813218179SMichael Halcrow 	 * Version (1 byte)
113913218179SMichael Halcrow 	 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
114013218179SMichael Halcrow 	 * Cipher identifier (1 byte)
114113218179SMichael Halcrow 	 * Encrypted key size (arbitrary)
114213218179SMichael Halcrow 	 *
114313218179SMichael Halcrow 	 * 12 bytes minimum packet size
1144dddfa461SMichael Halcrow 	 */
114513218179SMichael Halcrow 	if (unlikely(max_packet_size < 12)) {
114613218179SMichael Halcrow 		printk(KERN_ERR "Invalid max packet size; must be >=12\n");
1147dddfa461SMichael Halcrow 		rc = -EINVAL;
1148dddfa461SMichael Halcrow 		goto out;
1149dddfa461SMichael Halcrow 	}
1150dddfa461SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
115113218179SMichael Halcrow 		printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
1152dddfa461SMichael Halcrow 		       ECRYPTFS_TAG_1_PACKET_TYPE);
1153dddfa461SMichael Halcrow 		rc = -EINVAL;
1154dddfa461SMichael Halcrow 		goto out;
1155dddfa461SMichael Halcrow 	}
1156dddfa461SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1157dddfa461SMichael Halcrow 	 * at end of function upon failure */
1158dddfa461SMichael Halcrow 	auth_tok_list_item =
115913218179SMichael Halcrow 		kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
1160dddfa461SMichael Halcrow 				  GFP_KERNEL);
1161dddfa461SMichael Halcrow 	if (!auth_tok_list_item) {
116213218179SMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
1163dddfa461SMichael Halcrow 		rc = -ENOMEM;
1164dddfa461SMichael Halcrow 		goto out;
1165dddfa461SMichael Halcrow 	}
1166dddfa461SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
1167f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
11685dda6992SMichael Halcrow 					  &length_size);
11695dda6992SMichael Halcrow 	if (rc) {
117013218179SMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; "
1171dddfa461SMichael Halcrow 		       "rc = [%d]\n", rc);
1172dddfa461SMichael Halcrow 		goto out_free;
1173dddfa461SMichael Halcrow 	}
117413218179SMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
117581acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1176dddfa461SMichael Halcrow 		rc = -EINVAL;
1177dddfa461SMichael Halcrow 		goto out_free;
1178dddfa461SMichael Halcrow 	}
1179dddfa461SMichael Halcrow 	(*packet_size) += length_size;
1180dddfa461SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
118113218179SMichael Halcrow 		printk(KERN_WARNING "Packet size exceeds max\n");
1182dddfa461SMichael Halcrow 		rc = -EINVAL;
1183dddfa461SMichael Halcrow 		goto out_free;
1184dddfa461SMichael Halcrow 	}
1185dddfa461SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
118613218179SMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
118713218179SMichael Halcrow 		       data[(*packet_size) - 1]);
1188dddfa461SMichael Halcrow 		rc = -EINVAL;
1189dddfa461SMichael Halcrow 		goto out_free;
1190dddfa461SMichael Halcrow 	}
1191dddfa461SMichael Halcrow 	ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
1192dddfa461SMichael Halcrow 			&data[(*packet_size)], ECRYPTFS_SIG_SIZE);
1193dddfa461SMichael Halcrow 	*packet_size += ECRYPTFS_SIG_SIZE;
1194dddfa461SMichael Halcrow 	/* This byte is skipped because the kernel does not need to
1195dddfa461SMichael Halcrow 	 * know which public key encryption algorithm was used */
1196dddfa461SMichael Halcrow 	(*packet_size)++;
1197dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
119813218179SMichael Halcrow 		body_size - (ECRYPTFS_SIG_SIZE + 2);
1199dddfa461SMichael Halcrow 	if ((*new_auth_tok)->session_key.encrypted_key_size
1200dddfa461SMichael Halcrow 	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
120113218179SMichael Halcrow 		printk(KERN_WARNING "Tag 1 packet contains key larger "
1202dddfa461SMichael Halcrow 		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
1203dddfa461SMichael Halcrow 		rc = -EINVAL;
1204dddfa461SMichael Halcrow 		goto out;
1205dddfa461SMichael Halcrow 	}
1206dddfa461SMichael Halcrow 	memcpy((*new_auth_tok)->session_key.encrypted_key,
120713218179SMichael Halcrow 	       &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
1208dddfa461SMichael Halcrow 	(*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
1209dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1210dddfa461SMichael Halcrow 		~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1211dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags |=
1212dddfa461SMichael Halcrow 		ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1213dddfa461SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
121413218179SMichael Halcrow 	(*new_auth_tok)->flags = 0;
1215e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1216e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1217e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1218e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1219dddfa461SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
1220dddfa461SMichael Halcrow 	goto out;
1221dddfa461SMichael Halcrow out_free:
1222dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
1223dddfa461SMichael Halcrow 	memset(auth_tok_list_item, 0,
1224dddfa461SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
1225dddfa461SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1226dddfa461SMichael Halcrow 			auth_tok_list_item);
1227dddfa461SMichael Halcrow out:
1228dddfa461SMichael Halcrow 	if (rc)
1229dddfa461SMichael Halcrow 		(*packet_size) = 0;
1230dddfa461SMichael Halcrow 	return rc;
1231dddfa461SMichael Halcrow }
1232dddfa461SMichael Halcrow 
1233237fead6SMichael Halcrow /**
1234237fead6SMichael Halcrow  * parse_tag_3_packet
1235237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet
1236237fead6SMichael Halcrow  *              contents.
1237237fead6SMichael Halcrow  * @data: The raw bytes of the packet.
1238237fead6SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
1239237fead6SMichael Halcrow  *                 a new authentication token will be placed at the end
1240237fead6SMichael Halcrow  *                 of this list for this packet.
1241237fead6SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
1242237fead6SMichael Halcrow  *                allocates; sets the memory address of the pointer to
1243237fead6SMichael Halcrow  *                NULL on error. This object is added to the
1244237fead6SMichael Halcrow  *                auth_tok_list.
1245237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1246237fead6SMichael Halcrow  *               into this memory location; zero on error.
1247237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
1248237fead6SMichael Halcrow  *
1249237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1250237fead6SMichael Halcrow  */
1251237fead6SMichael Halcrow static int
1252237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
1253237fead6SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
1254237fead6SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
1255237fead6SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
1256237fead6SMichael Halcrow {
1257237fead6SMichael Halcrow 	size_t body_size;
1258237fead6SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1259237fead6SMichael Halcrow 	size_t length_size;
1260dddfa461SMichael Halcrow 	int rc = 0;
1261237fead6SMichael Halcrow 
1262237fead6SMichael Halcrow 	(*packet_size) = 0;
1263237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
1264c59becfcSMichael Halcrow 	/**
1265c59becfcSMichael Halcrow 	 *This format is inspired by OpenPGP; see RFC 2440
1266c59becfcSMichael Halcrow 	 * packet tag 3
1267c59becfcSMichael Halcrow 	 *
1268c59becfcSMichael Halcrow 	 * Tag 3 identifier (1 byte)
1269c59becfcSMichael Halcrow 	 * Max Tag 3 packet size (max 3 bytes)
1270c59becfcSMichael Halcrow 	 * Version (1 byte)
1271c59becfcSMichael Halcrow 	 * Cipher code (1 byte)
1272c59becfcSMichael Halcrow 	 * S2K specifier (1 byte)
1273c59becfcSMichael Halcrow 	 * Hash identifier (1 byte)
1274c59becfcSMichael Halcrow 	 * Salt (ECRYPTFS_SALT_SIZE)
1275c59becfcSMichael Halcrow 	 * Hash iterations (1 byte)
1276c59becfcSMichael Halcrow 	 * Encrypted key (arbitrary)
1277c59becfcSMichael Halcrow 	 *
1278c59becfcSMichael Halcrow 	 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
1279237fead6SMichael Halcrow 	 */
1280c59becfcSMichael Halcrow 	if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
1281c59becfcSMichael Halcrow 		printk(KERN_ERR "Max packet size too large\n");
1282237fead6SMichael Halcrow 		rc = -EINVAL;
1283237fead6SMichael Halcrow 		goto out;
1284237fead6SMichael Halcrow 	}
1285237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
1286c59becfcSMichael Halcrow 		printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
1287237fead6SMichael Halcrow 		       ECRYPTFS_TAG_3_PACKET_TYPE);
1288237fead6SMichael Halcrow 		rc = -EINVAL;
1289237fead6SMichael Halcrow 		goto out;
1290237fead6SMichael Halcrow 	}
1291237fead6SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1292237fead6SMichael Halcrow 	 * at end of function upon failure */
1293237fead6SMichael Halcrow 	auth_tok_list_item =
1294c3762229SRobert P. J. Day 	    kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
1295237fead6SMichael Halcrow 	if (!auth_tok_list_item) {
1296c59becfcSMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
1297237fead6SMichael Halcrow 		rc = -ENOMEM;
1298237fead6SMichael Halcrow 		goto out;
1299237fead6SMichael Halcrow 	}
1300237fead6SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
1301f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
13025dda6992SMichael Halcrow 					  &length_size);
13035dda6992SMichael Halcrow 	if (rc) {
1304c59becfcSMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
1305c59becfcSMichael Halcrow 		       rc);
1306237fead6SMichael Halcrow 		goto out_free;
1307237fead6SMichael Halcrow 	}
1308c59becfcSMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
130981acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1310237fead6SMichael Halcrow 		rc = -EINVAL;
1311237fead6SMichael Halcrow 		goto out_free;
1312237fead6SMichael Halcrow 	}
1313237fead6SMichael Halcrow 	(*packet_size) += length_size;
1314237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
1315c59becfcSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
1316237fead6SMichael Halcrow 		rc = -EINVAL;
1317237fead6SMichael Halcrow 		goto out_free;
1318237fead6SMichael Halcrow 	}
1319237fead6SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
1320c59becfcSMichael Halcrow 		(body_size - (ECRYPTFS_SALT_SIZE + 5));
1321f151cd2cSRamon de Carvalho Valle 	if ((*new_auth_tok)->session_key.encrypted_key_size
1322f151cd2cSRamon de Carvalho Valle 	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
1323f151cd2cSRamon de Carvalho Valle 		printk(KERN_WARNING "Tag 3 packet contains key larger "
1324f151cd2cSRamon de Carvalho Valle 		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
1325f151cd2cSRamon de Carvalho Valle 		rc = -EINVAL;
1326f151cd2cSRamon de Carvalho Valle 		goto out_free;
1327f151cd2cSRamon de Carvalho Valle 	}
1328237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x04)) {
1329c59becfcSMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
1330c59becfcSMichael Halcrow 		       data[(*packet_size) - 1]);
1331237fead6SMichael Halcrow 		rc = -EINVAL;
1332237fead6SMichael Halcrow 		goto out_free;
1333237fead6SMichael Halcrow 	}
1334b0105eaeSTyler Hicks 	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1335237fead6SMichael Halcrow 					    (u16)data[(*packet_size)]);
1336b0105eaeSTyler Hicks 	if (rc)
1337b0105eaeSTyler Hicks 		goto out_free;
1338237fead6SMichael Halcrow 	/* A little extra work to differentiate among the AES key
1339237fead6SMichael Halcrow 	 * sizes; see RFC2440 */
1340237fead6SMichael Halcrow 	switch(data[(*packet_size)++]) {
1341237fead6SMichael Halcrow 	case RFC2440_CIPHER_AES_192:
1342237fead6SMichael Halcrow 		crypt_stat->key_size = 24;
1343237fead6SMichael Halcrow 		break;
1344237fead6SMichael Halcrow 	default:
1345237fead6SMichael Halcrow 		crypt_stat->key_size =
1346237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
1347237fead6SMichael Halcrow 	}
1348b0105eaeSTyler Hicks 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1349b0105eaeSTyler Hicks 	if (rc)
1350b0105eaeSTyler Hicks 		goto out_free;
1351237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
1352c59becfcSMichael Halcrow 		printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
1353237fead6SMichael Halcrow 		rc = -ENOSYS;
1354237fead6SMichael Halcrow 		goto out_free;
1355237fead6SMichael Halcrow 	}
1356237fead6SMichael Halcrow 	/* TODO: finish the hash mapping */
1357237fead6SMichael Halcrow 	switch (data[(*packet_size)++]) {
1358237fead6SMichael Halcrow 	case 0x01: /* See RFC2440 for these numbers and their mappings */
1359237fead6SMichael Halcrow 		/* Choose MD5 */
1360237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->token.password.salt,
1361237fead6SMichael Halcrow 		       &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
1362237fead6SMichael Halcrow 		(*packet_size) += ECRYPTFS_SALT_SIZE;
1363237fead6SMichael Halcrow 		/* This conversion was taken straight from RFC2440 */
1364237fead6SMichael Halcrow 		(*new_auth_tok)->token.password.hash_iterations =
1365237fead6SMichael Halcrow 			((u32) 16 + (data[(*packet_size)] & 15))
1366237fead6SMichael Halcrow 				<< ((data[(*packet_size)] >> 4) + 6);
1367237fead6SMichael Halcrow 		(*packet_size)++;
1368c59becfcSMichael Halcrow 		/* Friendly reminder:
1369c59becfcSMichael Halcrow 		 * (*new_auth_tok)->session_key.encrypted_key_size =
1370c59becfcSMichael Halcrow 		 *         (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
1371237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->session_key.encrypted_key,
1372237fead6SMichael Halcrow 		       &data[(*packet_size)],
1373237fead6SMichael Halcrow 		       (*new_auth_tok)->session_key.encrypted_key_size);
1374237fead6SMichael Halcrow 		(*packet_size) +=
1375237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
1376237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags &=
1377237fead6SMichael Halcrow 			~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1378237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags |=
1379237fead6SMichael Halcrow 			ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1380c59becfcSMichael Halcrow 		(*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
1381237fead6SMichael Halcrow 		break;
1382237fead6SMichael Halcrow 	default:
1383237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
1384237fead6SMichael Halcrow 				"[%d]\n", data[(*packet_size) - 1]);
1385237fead6SMichael Halcrow 		rc = -ENOSYS;
1386237fead6SMichael Halcrow 		goto out_free;
1387237fead6SMichael Halcrow 	}
1388237fead6SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
1389237fead6SMichael Halcrow 	/* TODO: Parametarize; we might actually want userspace to
1390237fead6SMichael Halcrow 	 * decrypt the session key. */
1391e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1392e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1393e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1394e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1395237fead6SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
1396237fead6SMichael Halcrow 	goto out;
1397237fead6SMichael Halcrow out_free:
1398237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
1399237fead6SMichael Halcrow 	memset(auth_tok_list_item, 0,
1400237fead6SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
1401237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1402237fead6SMichael Halcrow 			auth_tok_list_item);
1403237fead6SMichael Halcrow out:
1404237fead6SMichael Halcrow 	if (rc)
1405237fead6SMichael Halcrow 		(*packet_size) = 0;
1406237fead6SMichael Halcrow 	return rc;
1407237fead6SMichael Halcrow }
1408237fead6SMichael Halcrow 
1409237fead6SMichael Halcrow /**
1410237fead6SMichael Halcrow  * parse_tag_11_packet
1411237fead6SMichael Halcrow  * @data: The raw bytes of the packet
1412237fead6SMichael Halcrow  * @contents: This function writes the data contents of the literal
1413237fead6SMichael Halcrow  *            packet into this memory location
1414237fead6SMichael Halcrow  * @max_contents_bytes: The maximum number of bytes that this function
1415237fead6SMichael Halcrow  *                      is allowed to write into contents
1416237fead6SMichael Halcrow  * @tag_11_contents_size: This function writes the size of the parsed
1417237fead6SMichael Halcrow  *                        contents into this memory location; zero on
1418237fead6SMichael Halcrow  *                        error
1419237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1420237fead6SMichael Halcrow  *               into this memory location; zero on error
1421237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
1422237fead6SMichael Halcrow  *
1423237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1424237fead6SMichael Halcrow  */
1425237fead6SMichael Halcrow static int
1426237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents,
1427237fead6SMichael Halcrow 		    size_t max_contents_bytes, size_t *tag_11_contents_size,
1428237fead6SMichael Halcrow 		    size_t *packet_size, size_t max_packet_size)
1429237fead6SMichael Halcrow {
1430237fead6SMichael Halcrow 	size_t body_size;
1431237fead6SMichael Halcrow 	size_t length_size;
1432dddfa461SMichael Halcrow 	int rc = 0;
1433237fead6SMichael Halcrow 
1434237fead6SMichael Halcrow 	(*packet_size) = 0;
1435237fead6SMichael Halcrow 	(*tag_11_contents_size) = 0;
1436f648104aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1437f648104aSMichael Halcrow 	 * packet tag 11
1438f648104aSMichael Halcrow 	 *
1439f648104aSMichael Halcrow 	 * Tag 11 identifier (1 byte)
1440f648104aSMichael Halcrow 	 * Max Tag 11 packet size (max 3 bytes)
1441f648104aSMichael Halcrow 	 * Binary format specifier (1 byte)
1442f648104aSMichael Halcrow 	 * Filename length (1 byte)
1443f648104aSMichael Halcrow 	 * Filename ("_CONSOLE") (8 bytes)
1444f648104aSMichael Halcrow 	 * Modification date (4 bytes)
1445f648104aSMichael Halcrow 	 * Literal data (arbitrary)
1446f648104aSMichael Halcrow 	 *
1447f648104aSMichael Halcrow 	 * We need at least 16 bytes of data for the packet to even be
1448f648104aSMichael Halcrow 	 * valid.
1449237fead6SMichael Halcrow 	 */
1450f648104aSMichael Halcrow 	if (max_packet_size < 16) {
1451f648104aSMichael Halcrow 		printk(KERN_ERR "Maximum packet size too small\n");
1452237fead6SMichael Halcrow 		rc = -EINVAL;
1453237fead6SMichael Halcrow 		goto out;
1454237fead6SMichael Halcrow 	}
1455237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
1456f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
1457237fead6SMichael Halcrow 		rc = -EINVAL;
1458237fead6SMichael Halcrow 		goto out;
1459237fead6SMichael Halcrow 	}
1460f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
14615dda6992SMichael Halcrow 					  &length_size);
14625dda6992SMichael Halcrow 	if (rc) {
1463f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
1464f648104aSMichael Halcrow 		goto out;
1465f648104aSMichael Halcrow 	}
1466f648104aSMichael Halcrow 	if (body_size < 14) {
146781acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1468f648104aSMichael Halcrow 		rc = -EINVAL;
1469237fead6SMichael Halcrow 		goto out;
1470237fead6SMichael Halcrow 	}
1471237fead6SMichael Halcrow 	(*packet_size) += length_size;
1472f648104aSMichael Halcrow 	(*tag_11_contents_size) = (body_size - 14);
1473237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
1474f648104aSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
1475237fead6SMichael Halcrow 		rc = -EINVAL;
1476237fead6SMichael Halcrow 		goto out;
1477237fead6SMichael Halcrow 	}
14786352a293STyler Hicks 	if (unlikely((*tag_11_contents_size) > max_contents_bytes)) {
14796352a293STyler Hicks 		printk(KERN_ERR "Literal data section in tag 11 packet exceeds "
14806352a293STyler Hicks 		       "expected size\n");
14816352a293STyler Hicks 		rc = -EINVAL;
14826352a293STyler Hicks 		goto out;
14836352a293STyler Hicks 	}
1484237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x62) {
1485f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
1486237fead6SMichael Halcrow 		rc = -EINVAL;
1487237fead6SMichael Halcrow 		goto out;
1488237fead6SMichael Halcrow 	}
1489237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x08) {
1490f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
1491237fead6SMichael Halcrow 		rc = -EINVAL;
1492237fead6SMichael Halcrow 		goto out;
1493237fead6SMichael Halcrow 	}
1494f648104aSMichael Halcrow 	(*packet_size) += 12; /* Ignore filename and modification date */
1495237fead6SMichael Halcrow 	memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
1496237fead6SMichael Halcrow 	(*packet_size) += (*tag_11_contents_size);
1497237fead6SMichael Halcrow out:
1498237fead6SMichael Halcrow 	if (rc) {
1499237fead6SMichael Halcrow 		(*packet_size) = 0;
1500237fead6SMichael Halcrow 		(*tag_11_contents_size) = 0;
1501237fead6SMichael Halcrow 	}
1502237fead6SMichael Halcrow 	return rc;
1503237fead6SMichael Halcrow }
1504237fead6SMichael Halcrow 
1505237fead6SMichael Halcrow /**
1506f4aad16aSMichael Halcrow  * ecryptfs_verify_version
1507f4aad16aSMichael Halcrow  * @version: The version number to confirm
1508f4aad16aSMichael Halcrow  *
1509f4aad16aSMichael Halcrow  * Returns zero on good version; non-zero otherwise
1510f4aad16aSMichael Halcrow  */
1511f4aad16aSMichael Halcrow static int ecryptfs_verify_version(u16 version)
1512f4aad16aSMichael Halcrow {
1513f4aad16aSMichael Halcrow 	int rc = 0;
1514f4aad16aSMichael Halcrow 	unsigned char major;
1515f4aad16aSMichael Halcrow 	unsigned char minor;
1516f4aad16aSMichael Halcrow 
1517f4aad16aSMichael Halcrow 	major = ((version >> 8) & 0xFF);
1518f4aad16aSMichael Halcrow 	minor = (version & 0xFF);
1519f4aad16aSMichael Halcrow 	if (major != ECRYPTFS_VERSION_MAJOR) {
1520f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
1521f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
1522f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MAJOR, major);
1523f4aad16aSMichael Halcrow 		rc = -EINVAL;
1524f4aad16aSMichael Halcrow 		goto out;
1525f4aad16aSMichael Halcrow 	}
1526f4aad16aSMichael Halcrow 	if (minor != ECRYPTFS_VERSION_MINOR) {
1527f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
1528f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
1529f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MINOR, minor);
1530f4aad16aSMichael Halcrow 		rc = -EINVAL;
1531f4aad16aSMichael Halcrow 		goto out;
1532f4aad16aSMichael Halcrow 	}
1533f4aad16aSMichael Halcrow out:
1534f4aad16aSMichael Halcrow 	return rc;
1535f4aad16aSMichael Halcrow }
1536f4aad16aSMichael Halcrow 
1537f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
1538f4aad16aSMichael Halcrow 				      struct ecryptfs_auth_tok **auth_tok,
1539f4aad16aSMichael Halcrow 				      char *sig)
1540f4aad16aSMichael Halcrow {
1541f4aad16aSMichael Halcrow 	int rc = 0;
1542f4aad16aSMichael Halcrow 
1543f4aad16aSMichael Halcrow 	(*auth_tok_key) = request_key(&key_type_user, sig, NULL);
1544f4aad16aSMichael Halcrow 	if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1545f4aad16aSMichael Halcrow 		printk(KERN_ERR "Could not find key with description: [%s]\n",
1546f4aad16aSMichael Halcrow 		       sig);
1547982363c9SEric Sandeen 		rc = process_request_key_err(PTR_ERR(*auth_tok_key));
1548f4aad16aSMichael Halcrow 		goto out;
1549f4aad16aSMichael Halcrow 	}
1550f4aad16aSMichael Halcrow 	(*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
1551f4aad16aSMichael Halcrow 	if (ecryptfs_verify_version((*auth_tok)->version)) {
1552f4aad16aSMichael Halcrow 		printk(KERN_ERR
1553f4aad16aSMichael Halcrow 		       "Data structure version mismatch. "
1554f4aad16aSMichael Halcrow 		       "Userspace tools must match eCryptfs "
1555f4aad16aSMichael Halcrow 		       "kernel module with major version [%d] "
1556f4aad16aSMichael Halcrow 		       "and minor version [%d]\n",
1557f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MAJOR,
1558f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MINOR);
1559f4aad16aSMichael Halcrow 		rc = -EINVAL;
1560f4aad16aSMichael Halcrow 		goto out;
1561f4aad16aSMichael Halcrow 	}
1562f4aad16aSMichael Halcrow 	if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
1563f4aad16aSMichael Halcrow 	    && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
1564f4aad16aSMichael Halcrow 		printk(KERN_ERR "Invalid auth_tok structure "
1565f4aad16aSMichael Halcrow 		       "returned from key query\n");
1566f4aad16aSMichael Halcrow 		rc = -EINVAL;
1567f4aad16aSMichael Halcrow 		goto out;
1568f4aad16aSMichael Halcrow 	}
1569f4aad16aSMichael Halcrow out:
1570f4aad16aSMichael Halcrow 	return rc;
1571f4aad16aSMichael Halcrow }
1572f4aad16aSMichael Halcrow 
1573f4aad16aSMichael Halcrow /**
157422e78fafSMichael Halcrow  * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
157522e78fafSMichael Halcrow  * @auth_tok: The passphrase authentication token to use to encrypt the FEK
157622e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1577237fead6SMichael Halcrow  *
157822e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise
1579237fead6SMichael Halcrow  */
1580f4aad16aSMichael Halcrow static int
1581f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1582237fead6SMichael Halcrow 					 struct ecryptfs_crypt_stat *crypt_stat)
1583237fead6SMichael Halcrow {
1584ac97b9f9SMichael Halcrow 	struct scatterlist dst_sg[2];
1585ac97b9f9SMichael Halcrow 	struct scatterlist src_sg[2];
1586dd8e2902SMichael Halcrow 	struct mutex *tfm_mutex;
15878bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
15888bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
15898bba066fSMichael Halcrow 	};
15908bba066fSMichael Halcrow 	int rc = 0;
1591237fead6SMichael Halcrow 
1592f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1593f4aad16aSMichael Halcrow 		ecryptfs_printk(
1594f4aad16aSMichael Halcrow 			KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1595f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1596f4aad16aSMichael Halcrow 		ecryptfs_dump_hex(
1597f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key,
1598f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1599f4aad16aSMichael Halcrow 	}
1600f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1601f4aad16aSMichael Halcrow 							crypt_stat->cipher);
1602f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
1603f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
1604f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1605f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
1606237fead6SMichael Halcrow 		goto out;
1607237fead6SMichael Halcrow 	}
16085dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1609f4aad16aSMichael Halcrow 				 auth_tok->session_key.encrypted_key_size,
1610ac97b9f9SMichael Halcrow 				 src_sg, 2);
1611ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
1612f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1613f4aad16aSMichael Halcrow 			"auth_tok->session_key.encrypted_key to scatterlist; "
1614f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]. "
1615f4aad16aSMichael Halcrow 		       "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1616f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key_size);
1617f4aad16aSMichael Halcrow 		goto out;
1618237fead6SMichael Halcrow 	}
1619f4aad16aSMichael Halcrow 	auth_tok->session_key.decrypted_key_size =
1620f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
16215dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1622f4aad16aSMichael Halcrow 				 auth_tok->session_key.decrypted_key_size,
1623ac97b9f9SMichael Halcrow 				 dst_sg, 2);
1624ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
1625f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1626f4aad16aSMichael Halcrow 			"auth_tok->session_key.decrypted_key to scatterlist; "
1627f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]\n", rc);
1628f4aad16aSMichael Halcrow 		goto out;
1629f4aad16aSMichael Halcrow 	}
1630237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
1631f4aad16aSMichael Halcrow 	rc = crypto_blkcipher_setkey(
1632f4aad16aSMichael Halcrow 		desc.tfm, auth_tok->token.password.session_key_encryption_key,
1633237fead6SMichael Halcrow 		crypt_stat->key_size);
1634f4aad16aSMichael Halcrow 	if (unlikely(rc < 0)) {
1635f4aad16aSMichael Halcrow 		mutex_unlock(tfm_mutex);
1636e5d9cbdeSMichael Halcrow 		printk(KERN_ERR "Error setting key for crypto context\n");
1637e5d9cbdeSMichael Halcrow 		rc = -EINVAL;
1638f4aad16aSMichael Halcrow 		goto out;
1639e5d9cbdeSMichael Halcrow 	}
1640ac97b9f9SMichael Halcrow 	rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
1641237fead6SMichael Halcrow 				      auth_tok->session_key.encrypted_key_size);
1642f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
1643f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
16448bba066fSMichael Halcrow 		printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
1645f4aad16aSMichael Halcrow 		goto out;
16468bba066fSMichael Halcrow 	}
1647237fead6SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1648237fead6SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1649237fead6SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
1650e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1651f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1652f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1653f4aad16aSMichael Halcrow 				crypt_stat->key_size);
1654237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
1655237fead6SMichael Halcrow 				  crypt_stat->key_size);
1656f4aad16aSMichael Halcrow 	}
1657237fead6SMichael Halcrow out:
1658237fead6SMichael Halcrow 	return rc;
1659237fead6SMichael Halcrow }
1660237fead6SMichael Halcrow 
1661237fead6SMichael Halcrow /**
1662237fead6SMichael Halcrow  * ecryptfs_parse_packet_set
166322e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
166422e78fafSMichael Halcrow  * @src: Virtual address of region of memory containing the packets
166522e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
1666237fead6SMichael Halcrow  *
1667237fead6SMichael Halcrow  * Get crypt_stat to have the file's session key if the requisite key
1668237fead6SMichael Halcrow  * is available to decrypt the session key.
1669237fead6SMichael Halcrow  *
1670237fead6SMichael Halcrow  * Returns Zero if a valid authentication token was retrieved and
1671237fead6SMichael Halcrow  * processed; negative value for file not encrypted or for error
1672237fead6SMichael Halcrow  * conditions.
1673237fead6SMichael Halcrow  */
1674237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1675237fead6SMichael Halcrow 			      unsigned char *src,
1676237fead6SMichael Halcrow 			      struct dentry *ecryptfs_dentry)
1677237fead6SMichael Halcrow {
1678237fead6SMichael Halcrow 	size_t i = 0;
1679f4aad16aSMichael Halcrow 	size_t found_auth_tok;
1680237fead6SMichael Halcrow 	size_t next_packet_is_auth_tok_packet;
1681237fead6SMichael Halcrow 	struct list_head auth_tok_list;
1682dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *matching_auth_tok;
1683dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *candidate_auth_tok;
1684f4aad16aSMichael Halcrow 	char *candidate_auth_tok_sig;
1685237fead6SMichael Halcrow 	size_t packet_size;
1686237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *new_auth_tok;
1687237fead6SMichael Halcrow 	unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
1688f4aad16aSMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1689237fead6SMichael Halcrow 	size_t tag_11_contents_size;
1690237fead6SMichael Halcrow 	size_t tag_11_packet_size;
1691dddfa461SMichael Halcrow 	int rc = 0;
1692237fead6SMichael Halcrow 
1693237fead6SMichael Halcrow 	INIT_LIST_HEAD(&auth_tok_list);
1694f4aad16aSMichael Halcrow 	/* Parse the header to find as many packets as we can; these will be
1695237fead6SMichael Halcrow 	 * added the our &auth_tok_list */
1696237fead6SMichael Halcrow 	next_packet_is_auth_tok_packet = 1;
1697237fead6SMichael Halcrow 	while (next_packet_is_auth_tok_packet) {
1698237fead6SMichael Halcrow 		size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1699237fead6SMichael Halcrow 
1700237fead6SMichael Halcrow 		switch (src[i]) {
1701237fead6SMichael Halcrow 		case ECRYPTFS_TAG_3_PACKET_TYPE:
1702237fead6SMichael Halcrow 			rc = parse_tag_3_packet(crypt_stat,
1703237fead6SMichael Halcrow 						(unsigned char *)&src[i],
1704237fead6SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1705237fead6SMichael Halcrow 						&packet_size, max_packet_size);
1706237fead6SMichael Halcrow 			if (rc) {
1707237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1708237fead6SMichael Halcrow 						"tag 3 packet\n");
1709237fead6SMichael Halcrow 				rc = -EIO;
1710237fead6SMichael Halcrow 				goto out_wipe_list;
1711237fead6SMichael Halcrow 			}
1712237fead6SMichael Halcrow 			i += packet_size;
1713237fead6SMichael Halcrow 			rc = parse_tag_11_packet((unsigned char *)&src[i],
1714237fead6SMichael Halcrow 						 sig_tmp_space,
1715237fead6SMichael Halcrow 						 ECRYPTFS_SIG_SIZE,
1716237fead6SMichael Halcrow 						 &tag_11_contents_size,
1717237fead6SMichael Halcrow 						 &tag_11_packet_size,
1718237fead6SMichael Halcrow 						 max_packet_size);
1719237fead6SMichael Halcrow 			if (rc) {
1720237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "No valid "
1721237fead6SMichael Halcrow 						"(ecryptfs-specific) literal "
1722237fead6SMichael Halcrow 						"packet containing "
1723237fead6SMichael Halcrow 						"authentication token "
1724237fead6SMichael Halcrow 						"signature found after "
1725237fead6SMichael Halcrow 						"tag 3 packet\n");
1726237fead6SMichael Halcrow 				rc = -EIO;
1727237fead6SMichael Halcrow 				goto out_wipe_list;
1728237fead6SMichael Halcrow 			}
1729237fead6SMichael Halcrow 			i += tag_11_packet_size;
1730237fead6SMichael Halcrow 			if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1731237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Expected "
1732237fead6SMichael Halcrow 						"signature of size [%d]; "
1733237fead6SMichael Halcrow 						"read size [%d]\n",
1734237fead6SMichael Halcrow 						ECRYPTFS_SIG_SIZE,
1735237fead6SMichael Halcrow 						tag_11_contents_size);
1736237fead6SMichael Halcrow 				rc = -EIO;
1737237fead6SMichael Halcrow 				goto out_wipe_list;
1738237fead6SMichael Halcrow 			}
1739237fead6SMichael Halcrow 			ecryptfs_to_hex(new_auth_tok->token.password.signature,
1740237fead6SMichael Halcrow 					sig_tmp_space, tag_11_contents_size);
1741237fead6SMichael Halcrow 			new_auth_tok->token.password.signature[
1742237fead6SMichael Halcrow 				ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
1743e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1744237fead6SMichael Halcrow 			break;
1745dddfa461SMichael Halcrow 		case ECRYPTFS_TAG_1_PACKET_TYPE:
1746dddfa461SMichael Halcrow 			rc = parse_tag_1_packet(crypt_stat,
1747dddfa461SMichael Halcrow 						(unsigned char *)&src[i],
1748dddfa461SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1749dddfa461SMichael Halcrow 						&packet_size, max_packet_size);
1750dddfa461SMichael Halcrow 			if (rc) {
1751dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1752dddfa461SMichael Halcrow 						"tag 1 packet\n");
1753dddfa461SMichael Halcrow 				rc = -EIO;
1754dddfa461SMichael Halcrow 				goto out_wipe_list;
1755dddfa461SMichael Halcrow 			}
1756dddfa461SMichael Halcrow 			i += packet_size;
1757e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1758dddfa461SMichael Halcrow 			break;
1759237fead6SMichael Halcrow 		case ECRYPTFS_TAG_11_PACKET_TYPE:
1760237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1761237fead6SMichael Halcrow 					"(Tag 11 not allowed by itself)\n");
1762237fead6SMichael Halcrow 			rc = -EIO;
1763237fead6SMichael Halcrow 			goto out_wipe_list;
1764237fead6SMichael Halcrow 			break;
1765237fead6SMichael Halcrow 		default:
1766237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1767237fead6SMichael Halcrow 					"[%d] of the file header; hex value of "
1768237fead6SMichael Halcrow 					"character is [0x%.2x]\n", i, src[i]);
1769237fead6SMichael Halcrow 			next_packet_is_auth_tok_packet = 0;
1770237fead6SMichael Halcrow 		}
1771237fead6SMichael Halcrow 	}
1772237fead6SMichael Halcrow 	if (list_empty(&auth_tok_list)) {
1773f4aad16aSMichael Halcrow 		printk(KERN_ERR "The lower file appears to be a non-encrypted "
1774f4aad16aSMichael Halcrow 		       "eCryptfs file; this is not supported in this version "
1775f4aad16aSMichael Halcrow 		       "of the eCryptfs kernel module\n");
1776f4aad16aSMichael Halcrow 		rc = -EINVAL;
1777237fead6SMichael Halcrow 		goto out;
1778237fead6SMichael Halcrow 	}
1779f4aad16aSMichael Halcrow 	/* auth_tok_list contains the set of authentication tokens
1780f4aad16aSMichael Halcrow 	 * parsed from the metadata. We need to find a matching
1781f4aad16aSMichael Halcrow 	 * authentication token that has the secret component(s)
1782f4aad16aSMichael Halcrow 	 * necessary to decrypt the EFEK in the auth_tok parsed from
1783f4aad16aSMichael Halcrow 	 * the metadata. There may be several potential matches, but
1784f4aad16aSMichael Halcrow 	 * just one will be sufficient to decrypt to get the FEK. */
1785f4aad16aSMichael Halcrow find_next_matching_auth_tok:
1786f4aad16aSMichael Halcrow 	found_auth_tok = 0;
1787f4aad16aSMichael Halcrow 	list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
1788237fead6SMichael Halcrow 		candidate_auth_tok = &auth_tok_list_item->auth_tok;
1789237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
1790237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
1791237fead6SMichael Halcrow 					"Considering cadidate auth tok:\n");
1792237fead6SMichael Halcrow 			ecryptfs_dump_auth_tok(candidate_auth_tok);
1793237fead6SMichael Halcrow 		}
17945dda6992SMichael Halcrow 		rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
17955dda6992SMichael Halcrow 					       candidate_auth_tok);
17965dda6992SMichael Halcrow 		if (rc) {
1797f4aad16aSMichael Halcrow 			printk(KERN_ERR
1798f4aad16aSMichael Halcrow 			       "Unrecognized candidate auth tok type: [%d]\n",
1799f4aad16aSMichael Halcrow 			       candidate_auth_tok->token_type);
1800f4aad16aSMichael Halcrow 			rc = -EINVAL;
1801f4aad16aSMichael Halcrow 			goto out_wipe_list;
1802f4aad16aSMichael Halcrow 		}
18039c79f34fSMichael Halcrow 		ecryptfs_find_auth_tok_for_sig(&matching_auth_tok,
18049c79f34fSMichael Halcrow 					       crypt_stat->mount_crypt_stat,
18055dda6992SMichael Halcrow 					       candidate_auth_tok_sig);
1806f4aad16aSMichael Halcrow 		if (matching_auth_tok) {
1807237fead6SMichael Halcrow 			found_auth_tok = 1;
1808f4aad16aSMichael Halcrow 			goto found_matching_auth_tok;
1809237fead6SMichael Halcrow 		}
1810237fead6SMichael Halcrow 	}
1811237fead6SMichael Halcrow 	if (!found_auth_tok) {
1812f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Could not find a usable "
1813f4aad16aSMichael Halcrow 				"authentication token\n");
1814237fead6SMichael Halcrow 		rc = -EIO;
1815237fead6SMichael Halcrow 		goto out_wipe_list;
1816dddfa461SMichael Halcrow 	}
1817f4aad16aSMichael Halcrow found_matching_auth_tok:
1818e2bd99ecSMichael Halcrow 	if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1819dddfa461SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.private_key),
1820f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.private_key),
1821dddfa461SMichael Halcrow 		       sizeof(struct ecryptfs_private_key));
1822f4aad16aSMichael Halcrow 		rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
1823dddfa461SMichael Halcrow 						       crypt_stat);
1824dddfa461SMichael Halcrow 	} else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
1825237fead6SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.password),
1826f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.password),
1827237fead6SMichael Halcrow 		       sizeof(struct ecryptfs_password));
1828f4aad16aSMichael Halcrow 		rc = decrypt_passphrase_encrypted_session_key(
1829f4aad16aSMichael Halcrow 			candidate_auth_tok, crypt_stat);
1830dddfa461SMichael Halcrow 	}
1831237fead6SMichael Halcrow 	if (rc) {
1832f4aad16aSMichael Halcrow 		struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1833f4aad16aSMichael Halcrow 
1834f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1835f4aad16aSMichael Halcrow 				"session key for authentication token with sig "
1836f4aad16aSMichael Halcrow 				"[%.*s]; rc = [%d]. Removing auth tok "
1837f4aad16aSMichael Halcrow 				"candidate from the list and searching for "
1838f4aad16aSMichael Halcrow 				"the next match.\n", candidate_auth_tok_sig,
1839f4aad16aSMichael Halcrow 				ECRYPTFS_SIG_SIZE_HEX, rc);
1840f4aad16aSMichael Halcrow 		list_for_each_entry_safe(auth_tok_list_item,
1841f4aad16aSMichael Halcrow 					 auth_tok_list_item_tmp,
1842f4aad16aSMichael Halcrow 					 &auth_tok_list, list) {
1843f4aad16aSMichael Halcrow 			if (candidate_auth_tok
1844f4aad16aSMichael Halcrow 			    == &auth_tok_list_item->auth_tok) {
1845f4aad16aSMichael Halcrow 				list_del(&auth_tok_list_item->list);
1846f4aad16aSMichael Halcrow 				kmem_cache_free(
1847f4aad16aSMichael Halcrow 					ecryptfs_auth_tok_list_item_cache,
1848f4aad16aSMichael Halcrow 					auth_tok_list_item);
1849f4aad16aSMichael Halcrow 				goto find_next_matching_auth_tok;
1850f4aad16aSMichael Halcrow 			}
1851f4aad16aSMichael Halcrow 		}
1852f4aad16aSMichael Halcrow 		BUG();
1853237fead6SMichael Halcrow 	}
1854237fead6SMichael Halcrow 	rc = ecryptfs_compute_root_iv(crypt_stat);
1855237fead6SMichael Halcrow 	if (rc) {
1856237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error computing "
1857237fead6SMichael Halcrow 				"the root IV\n");
1858237fead6SMichael Halcrow 		goto out_wipe_list;
1859237fead6SMichael Halcrow 	}
1860237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1861237fead6SMichael Halcrow 	if (rc) {
1862237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1863237fead6SMichael Halcrow 				"context for cipher [%s]; rc = [%d]\n",
1864237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1865237fead6SMichael Halcrow 	}
1866237fead6SMichael Halcrow out_wipe_list:
1867237fead6SMichael Halcrow 	wipe_auth_tok_list(&auth_tok_list);
1868237fead6SMichael Halcrow out:
1869237fead6SMichael Halcrow 	return rc;
1870237fead6SMichael Halcrow }
1871f4aad16aSMichael Halcrow 
1872dddfa461SMichael Halcrow static int
1873dddfa461SMichael Halcrow pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1874dddfa461SMichael Halcrow 			struct ecryptfs_crypt_stat *crypt_stat,
1875dddfa461SMichael Halcrow 			struct ecryptfs_key_record *key_rec)
1876dddfa461SMichael Halcrow {
1877dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx = NULL;
1878624ae528STyler Hicks 	char *payload = NULL;
1879624ae528STyler Hicks 	size_t payload_len;
1880dddfa461SMichael Halcrow 	struct ecryptfs_message *msg;
1881dddfa461SMichael Halcrow 	int rc;
1882dddfa461SMichael Halcrow 
1883dddfa461SMichael Halcrow 	rc = write_tag_66_packet(auth_tok->token.private_key.signature,
18849c79f34fSMichael Halcrow 				 ecryptfs_code_for_cipher_string(
18859c79f34fSMichael Halcrow 					 crypt_stat->cipher,
18869c79f34fSMichael Halcrow 					 crypt_stat->key_size),
1887624ae528STyler Hicks 				 crypt_stat, &payload, &payload_len);
1888dddfa461SMichael Halcrow 	if (rc) {
1889dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1890dddfa461SMichael Halcrow 		goto out;
1891dddfa461SMichael Halcrow 	}
1892624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1893dddfa461SMichael Halcrow 	if (rc) {
1894624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
1895624ae528STyler Hicks 				"ecryptfsd\n");
1896dddfa461SMichael Halcrow 		goto out;
1897dddfa461SMichael Halcrow 	}
1898dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1899dddfa461SMichael Halcrow 	if (rc) {
1900dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1901dddfa461SMichael Halcrow 				"from the user space daemon\n");
1902dddfa461SMichael Halcrow 		rc = -EIO;
1903dddfa461SMichael Halcrow 		goto out;
1904dddfa461SMichael Halcrow 	}
1905dddfa461SMichael Halcrow 	rc = parse_tag_67_packet(key_rec, msg);
1906dddfa461SMichael Halcrow 	if (rc)
1907dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1908dddfa461SMichael Halcrow 	kfree(msg);
1909dddfa461SMichael Halcrow out:
1910624ae528STyler Hicks 	kfree(payload);
1911dddfa461SMichael Halcrow 	return rc;
1912dddfa461SMichael Halcrow }
1913dddfa461SMichael Halcrow /**
1914dddfa461SMichael Halcrow  * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1915dddfa461SMichael Halcrow  * @dest: Buffer into which to write the packet
191622e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be writtn
191722e78fafSMichael Halcrow  * @auth_tok: The authentication token used for generating the tag 1 packet
191822e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
191922e78fafSMichael Halcrow  * @key_rec: The key record struct for the tag 1 packet
1920dddfa461SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
1921dddfa461SMichael Halcrow  *               up constituting the packet; set to zero on error
1922dddfa461SMichael Halcrow  *
1923dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
1924dddfa461SMichael Halcrow  */
1925dddfa461SMichael Halcrow static int
1926f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes,
1927f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
1928dddfa461SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
1929dddfa461SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
1930dddfa461SMichael Halcrow {
1931dddfa461SMichael Halcrow 	size_t i;
1932dddfa461SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
1933dddfa461SMichael Halcrow 	size_t packet_size_length;
1934f4aad16aSMichael Halcrow 	size_t max_packet_size;
1935dddfa461SMichael Halcrow 	int rc = 0;
1936dddfa461SMichael Halcrow 
1937dddfa461SMichael Halcrow 	(*packet_size) = 0;
1938dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1939dddfa461SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
1940dddfa461SMichael Halcrow 	encrypted_session_key_valid = 0;
1941dddfa461SMichael Halcrow 	for (i = 0; i < crypt_stat->key_size; i++)
1942dddfa461SMichael Halcrow 		encrypted_session_key_valid |=
1943dddfa461SMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
1944dddfa461SMichael Halcrow 	if (encrypted_session_key_valid) {
1945dddfa461SMichael Halcrow 		memcpy(key_rec->enc_key,
1946dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key,
1947dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key_size);
1948dddfa461SMichael Halcrow 		goto encrypted_session_key_set;
1949dddfa461SMichael Halcrow 	}
1950dddfa461SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
1951dddfa461SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
1952dddfa461SMichael Halcrow 			auth_tok->token.private_key.key_size;
1953dddfa461SMichael Halcrow 	rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1954dddfa461SMichael Halcrow 	if (rc) {
1955f66e883eSMichael Halcrow 		printk(KERN_ERR "Failed to encrypt session key via a key "
1956f66e883eSMichael Halcrow 		       "module; rc = [%d]\n", rc);
1957dddfa461SMichael Halcrow 		goto out;
1958dddfa461SMichael Halcrow 	}
1959dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1960dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1961dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1962dddfa461SMichael Halcrow 	}
1963dddfa461SMichael Halcrow encrypted_session_key_set:
1964f4aad16aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1965f4aad16aSMichael Halcrow 	 * packet tag 1 */
1966f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 1 identifier */
1967f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 1 packet size */
1968f4aad16aSMichael Halcrow 			   + 1                       /* Version */
1969f4aad16aSMichael Halcrow 			   + ECRYPTFS_SIG_SIZE       /* Key identifier */
1970f4aad16aSMichael Halcrow 			   + 1                       /* Cipher identifier */
1971f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
1972f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
1973f4aad16aSMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
197481acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
1975f4aad16aSMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
1976dddfa461SMichael Halcrow 		rc = -EINVAL;
1977dddfa461SMichael Halcrow 		goto out;
1978dddfa461SMichael Halcrow 	}
1979dddfa461SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
1980f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1981f66e883eSMichael Halcrow 					  (max_packet_size - 4),
1982dddfa461SMichael Halcrow 					  &packet_size_length);
1983dddfa461SMichael Halcrow 	if (rc) {
1984dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1985dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
1986dddfa461SMichael Halcrow 		goto out;
1987dddfa461SMichael Halcrow 	}
1988dddfa461SMichael Halcrow 	(*packet_size) += packet_size_length;
1989dddfa461SMichael Halcrow 	dest[(*packet_size)++] = 0x03; /* version 3 */
1990dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1991dddfa461SMichael Halcrow 	(*packet_size) += ECRYPTFS_SIG_SIZE;
1992dddfa461SMichael Halcrow 	dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1993dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
1994dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
1995dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
1996dddfa461SMichael Halcrow out:
1997dddfa461SMichael Halcrow 	if (rc)
1998dddfa461SMichael Halcrow 		(*packet_size) = 0;
1999f4aad16aSMichael Halcrow 	else
2000f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
2001dddfa461SMichael Halcrow 	return rc;
2002dddfa461SMichael Halcrow }
2003237fead6SMichael Halcrow 
2004237fead6SMichael Halcrow /**
2005237fead6SMichael Halcrow  * write_tag_11_packet
2006237fead6SMichael Halcrow  * @dest: Target into which Tag 11 packet is to be written
200722e78fafSMichael Halcrow  * @remaining_bytes: Maximum packet length
2008237fead6SMichael Halcrow  * @contents: Byte array of contents to copy in
2009237fead6SMichael Halcrow  * @contents_length: Number of bytes in contents
2010237fead6SMichael Halcrow  * @packet_length: Length of the Tag 11 packet written; zero on error
2011237fead6SMichael Halcrow  *
2012237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2013237fead6SMichael Halcrow  */
2014237fead6SMichael Halcrow static int
201581acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
2016146a4606SMichael Halcrow 		    size_t contents_length, size_t *packet_length)
2017237fead6SMichael Halcrow {
2018237fead6SMichael Halcrow 	size_t packet_size_length;
2019146a4606SMichael Halcrow 	size_t max_packet_size;
2020dddfa461SMichael Halcrow 	int rc = 0;
2021237fead6SMichael Halcrow 
2022237fead6SMichael Halcrow 	(*packet_length) = 0;
2023146a4606SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
2024146a4606SMichael Halcrow 	 * packet tag 11 */
2025146a4606SMichael Halcrow 	max_packet_size = (1                   /* Tag 11 identifier */
2026146a4606SMichael Halcrow 			   + 3                 /* Max Tag 11 packet size */
2027146a4606SMichael Halcrow 			   + 1                 /* Binary format specifier */
2028146a4606SMichael Halcrow 			   + 1                 /* Filename length */
2029146a4606SMichael Halcrow 			   + 8                 /* Filename ("_CONSOLE") */
2030146a4606SMichael Halcrow 			   + 4                 /* Modification date */
2031146a4606SMichael Halcrow 			   + contents_length); /* Literal data */
2032146a4606SMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
2033146a4606SMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
203481acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
2035146a4606SMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
2036237fead6SMichael Halcrow 		rc = -EINVAL;
2037237fead6SMichael Halcrow 		goto out;
2038237fead6SMichael Halcrow 	}
2039237fead6SMichael Halcrow 	dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
2040f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
2041f66e883eSMichael Halcrow 					  (max_packet_size - 4),
2042f66e883eSMichael Halcrow 					  &packet_size_length);
2043237fead6SMichael Halcrow 	if (rc) {
2044146a4606SMichael Halcrow 		printk(KERN_ERR "Error generating tag 11 packet header; cannot "
2045146a4606SMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
2046237fead6SMichael Halcrow 		goto out;
2047237fead6SMichael Halcrow 	}
2048237fead6SMichael Halcrow 	(*packet_length) += packet_size_length;
2049146a4606SMichael Halcrow 	dest[(*packet_length)++] = 0x62; /* binary data format specifier */
2050237fead6SMichael Halcrow 	dest[(*packet_length)++] = 8;
2051237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
2052237fead6SMichael Halcrow 	(*packet_length) += 8;
2053237fead6SMichael Halcrow 	memset(&dest[(*packet_length)], 0x00, 4);
2054237fead6SMichael Halcrow 	(*packet_length) += 4;
2055237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], contents, contents_length);
2056237fead6SMichael Halcrow 	(*packet_length) += contents_length;
2057237fead6SMichael Halcrow  out:
2058237fead6SMichael Halcrow 	if (rc)
2059237fead6SMichael Halcrow 		(*packet_length) = 0;
2060146a4606SMichael Halcrow 	else
2061146a4606SMichael Halcrow 		(*remaining_bytes) -= (*packet_length);
2062237fead6SMichael Halcrow 	return rc;
2063237fead6SMichael Halcrow }
2064237fead6SMichael Halcrow 
2065237fead6SMichael Halcrow /**
2066237fead6SMichael Halcrow  * write_tag_3_packet
2067237fead6SMichael Halcrow  * @dest: Buffer into which to write the packet
206822e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be written
2069237fead6SMichael Halcrow  * @auth_tok: Authentication token
2070237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context
2071237fead6SMichael Halcrow  * @key_rec: encrypted key
2072237fead6SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
2073237fead6SMichael Halcrow  *               up constituting the packet; set to zero on error
2074237fead6SMichael Halcrow  *
2075237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2076237fead6SMichael Halcrow  */
2077237fead6SMichael Halcrow static int
2078f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes,
2079f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
2080237fead6SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
2081237fead6SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
2082237fead6SMichael Halcrow {
2083237fead6SMichael Halcrow 	size_t i;
2084237fead6SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
2085237fead6SMichael Halcrow 	char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
2086ac97b9f9SMichael Halcrow 	struct scatterlist dst_sg[2];
2087ac97b9f9SMichael Halcrow 	struct scatterlist src_sg[2];
2088237fead6SMichael Halcrow 	struct mutex *tfm_mutex = NULL;
208919e66a67STrevor Highland 	u8 cipher_code;
2090f4aad16aSMichael Halcrow 	size_t packet_size_length;
2091f4aad16aSMichael Halcrow 	size_t max_packet_size;
2092f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2093f4aad16aSMichael Halcrow 		crypt_stat->mount_crypt_stat;
20948bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
20958bba066fSMichael Halcrow 		.tfm = NULL,
20968bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
20978bba066fSMichael Halcrow 	};
20988bba066fSMichael Halcrow 	int rc = 0;
2099237fead6SMichael Halcrow 
2100237fead6SMichael Halcrow 	(*packet_size) = 0;
2101dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
2102237fead6SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
2103f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2104f4aad16aSMichael Halcrow 							crypt_stat->cipher);
2105f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
2106f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
2107f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
2108f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
2109f4aad16aSMichael Halcrow 		goto out;
2110237fead6SMichael Halcrow 	}
2111f4aad16aSMichael Halcrow 	if (mount_crypt_stat->global_default_cipher_key_size == 0) {
2112f4aad16aSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
2113f4aad16aSMichael Halcrow 
2114f4aad16aSMichael Halcrow 		printk(KERN_WARNING "No key size specified at mount; "
2115f4aad16aSMichael Halcrow 		       "defaulting to [%d]\n", alg->max_keysize);
2116f4aad16aSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size =
2117f4aad16aSMichael Halcrow 			alg->max_keysize;
2118f4aad16aSMichael Halcrow 	}
2119f4aad16aSMichael Halcrow 	if (crypt_stat->key_size == 0)
2120f4aad16aSMichael Halcrow 		crypt_stat->key_size =
2121f4aad16aSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
2122237fead6SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
2123237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
2124237fead6SMichael Halcrow 			crypt_stat->key_size;
2125237fead6SMichael Halcrow 	if (crypt_stat->key_size == 24
2126237fead6SMichael Halcrow 	    && strcmp("aes", crypt_stat->cipher) == 0) {
2127237fead6SMichael Halcrow 		memset((crypt_stat->key + 24), 0, 8);
2128237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size = 32;
2129f4aad16aSMichael Halcrow 	} else
2130f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
2131dddfa461SMichael Halcrow 	key_rec->enc_key_size =
2132237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
2133f4aad16aSMichael Halcrow 	encrypted_session_key_valid = 0;
2134f4aad16aSMichael Halcrow 	for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
2135f4aad16aSMichael Halcrow 		encrypted_session_key_valid |=
2136f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
2137f4aad16aSMichael Halcrow 	if (encrypted_session_key_valid) {
2138f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
2139f4aad16aSMichael Halcrow 				"using auth_tok->session_key.encrypted_key, "
2140f4aad16aSMichael Halcrow 				"where key_rec->enc_key_size = [%d]\n",
2141f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2142f4aad16aSMichael Halcrow 		memcpy(key_rec->enc_key,
2143f4aad16aSMichael Halcrow 		       auth_tok->session_key.encrypted_key,
2144f4aad16aSMichael Halcrow 		       key_rec->enc_key_size);
2145f4aad16aSMichael Halcrow 		goto encrypted_session_key_set;
2146f4aad16aSMichael Halcrow 	}
2147dddfa461SMichael Halcrow 	if (auth_tok->token.password.flags &
2148dddfa461SMichael Halcrow 	    ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
2149237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Using previously generated "
2150237fead6SMichael Halcrow 				"session key encryption key of size [%d]\n",
2151237fead6SMichael Halcrow 				auth_tok->token.password.
2152237fead6SMichael Halcrow 				session_key_encryption_key_bytes);
2153237fead6SMichael Halcrow 		memcpy(session_key_encryption_key,
2154237fead6SMichael Halcrow 		       auth_tok->token.password.session_key_encryption_key,
2155237fead6SMichael Halcrow 		       crypt_stat->key_size);
2156237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
2157237fead6SMichael Halcrow 				"Cached session key " "encryption key: \n");
2158237fead6SMichael Halcrow 		if (ecryptfs_verbosity > 0)
2159237fead6SMichael Halcrow 			ecryptfs_dump_hex(session_key_encryption_key, 16);
2160237fead6SMichael Halcrow 	}
2161237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
2162237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
2163237fead6SMichael Halcrow 		ecryptfs_dump_hex(session_key_encryption_key, 16);
2164237fead6SMichael Halcrow 	}
21655dda6992SMichael Halcrow 	rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
2166ac97b9f9SMichael Halcrow 				 src_sg, 2);
2167ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
2168237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2169f4aad16aSMichael Halcrow 				"for crypt_stat session key; expected rc = 1; "
2170f4aad16aSMichael Halcrow 				"got rc = [%d]. key_rec->enc_key_size = [%d]\n",
2171f4aad16aSMichael Halcrow 				rc, key_rec->enc_key_size);
2172237fead6SMichael Halcrow 		rc = -ENOMEM;
2173237fead6SMichael Halcrow 		goto out;
2174237fead6SMichael Halcrow 	}
21755dda6992SMichael Halcrow 	rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
2176ac97b9f9SMichael Halcrow 				 dst_sg, 2);
2177ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
2178237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2179f4aad16aSMichael Halcrow 				"for crypt_stat encrypted session key; "
2180f4aad16aSMichael Halcrow 				"expected rc = 1; got rc = [%d]. "
2181f4aad16aSMichael Halcrow 				"key_rec->enc_key_size = [%d]\n", rc,
2182f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2183237fead6SMichael Halcrow 		rc = -ENOMEM;
2184237fead6SMichael Halcrow 		goto out;
2185237fead6SMichael Halcrow 	}
2186237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
21878bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
2188237fead6SMichael Halcrow 				     crypt_stat->key_size);
2189237fead6SMichael Halcrow 	if (rc < 0) {
2190237fead6SMichael Halcrow 		mutex_unlock(tfm_mutex);
2191237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
21928bba066fSMichael Halcrow 				"context; rc = [%d]\n", rc);
2193237fead6SMichael Halcrow 		goto out;
2194237fead6SMichael Halcrow 	}
2195237fead6SMichael Halcrow 	rc = 0;
2196237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
2197237fead6SMichael Halcrow 			crypt_stat->key_size);
2198ac97b9f9SMichael Halcrow 	rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
2199237fead6SMichael Halcrow 				      (*key_rec).enc_key_size);
2200f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
22018bba066fSMichael Halcrow 	if (rc) {
22028bba066fSMichael Halcrow 		printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
22038bba066fSMichael Halcrow 		goto out;
22048bba066fSMichael Halcrow 	}
2205237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
2206f4aad16aSMichael Halcrow 	if (ecryptfs_verbosity > 0) {
2207f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
2208f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2209dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key,
2210dddfa461SMichael Halcrow 				  key_rec->enc_key_size);
2211f4aad16aSMichael Halcrow 	}
2212237fead6SMichael Halcrow encrypted_session_key_set:
2213237fead6SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
2214237fead6SMichael Halcrow 	 * packet tag 3 */
2215f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 3 identifier */
2216f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 3 packet size */
2217f4aad16aSMichael Halcrow 			   + 1                       /* Version */
2218f4aad16aSMichael Halcrow 			   + 1                       /* Cipher code */
2219f4aad16aSMichael Halcrow 			   + 1                       /* S2K specifier */
2220f4aad16aSMichael Halcrow 			   + 1                       /* Hash identifier */
2221f4aad16aSMichael Halcrow 			   + ECRYPTFS_SALT_SIZE      /* Salt */
2222f4aad16aSMichael Halcrow 			   + 1                       /* Hash iterations */
2223f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
2224f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
222581acbcd6SAndrew Morton 		printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
222681acbcd6SAndrew Morton 		       "there are only [%td] available\n", max_packet_size,
2227f4aad16aSMichael Halcrow 		       (*remaining_bytes));
2228f4aad16aSMichael Halcrow 		rc = -EINVAL;
2229f4aad16aSMichael Halcrow 		goto out;
2230f4aad16aSMichael Halcrow 	}
2231237fead6SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
2232f4aad16aSMichael Halcrow 	/* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
2233f4aad16aSMichael Halcrow 	 * to get the number of octets in the actual Tag 3 packet */
2234f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2235f66e883eSMichael Halcrow 					  (max_packet_size - 4),
2236237fead6SMichael Halcrow 					  &packet_size_length);
2237237fead6SMichael Halcrow 	if (rc) {
2238f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error generating tag 3 packet header; cannot "
2239f4aad16aSMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
2240237fead6SMichael Halcrow 		goto out;
2241237fead6SMichael Halcrow 	}
2242237fead6SMichael Halcrow 	(*packet_size) += packet_size_length;
2243237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x04; /* version 4 */
2244f4aad16aSMichael Halcrow 	/* TODO: Break from RFC2440 so that arbitrary ciphers can be
2245f4aad16aSMichael Halcrow 	 * specified with strings */
22469c79f34fSMichael Halcrow 	cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
22479c79f34fSMichael Halcrow 						      crypt_stat->key_size);
2248237fead6SMichael Halcrow 	if (cipher_code == 0) {
2249237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
2250237fead6SMichael Halcrow 				"cipher [%s]\n", crypt_stat->cipher);
2251237fead6SMichael Halcrow 		rc = -EINVAL;
2252237fead6SMichael Halcrow 		goto out;
2253237fead6SMichael Halcrow 	}
2254237fead6SMichael Halcrow 	dest[(*packet_size)++] = cipher_code;
2255237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x03;	/* S2K */
2256237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x01;	/* MD5 (TODO: parameterize) */
2257237fead6SMichael Halcrow 	memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
2258237fead6SMichael Halcrow 	       ECRYPTFS_SALT_SIZE);
2259237fead6SMichael Halcrow 	(*packet_size) += ECRYPTFS_SALT_SIZE;	/* salt */
2260237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x60;	/* hash iterations (65536) */
2261dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
2262dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
2263dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
2264237fead6SMichael Halcrow out:
2265237fead6SMichael Halcrow 	if (rc)
2266237fead6SMichael Halcrow 		(*packet_size) = 0;
2267f4aad16aSMichael Halcrow 	else
2268f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
2269237fead6SMichael Halcrow 	return rc;
2270237fead6SMichael Halcrow }
2271237fead6SMichael Halcrow 
2272eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache;
2273eb95e7ffSMichael Halcrow 
2274237fead6SMichael Halcrow /**
2275237fead6SMichael Halcrow  * ecryptfs_generate_key_packet_set
227622e78fafSMichael Halcrow  * @dest_base: Virtual address from which to write the key record set
2277237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context from which the
2278237fead6SMichael Halcrow  *              authentication tokens will be retrieved
2279237fead6SMichael Halcrow  * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
2280237fead6SMichael Halcrow  *                   for the global parameters
2281237fead6SMichael Halcrow  * @len: The amount written
2282237fead6SMichael Halcrow  * @max: The maximum amount of data allowed to be written
2283237fead6SMichael Halcrow  *
2284237fead6SMichael Halcrow  * Generates a key packet set and writes it to the virtual address
2285237fead6SMichael Halcrow  * passed in.
2286237fead6SMichael Halcrow  *
2287237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2288237fead6SMichael Halcrow  */
2289237fead6SMichael Halcrow int
2290237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base,
2291237fead6SMichael Halcrow 				 struct ecryptfs_crypt_stat *crypt_stat,
2292237fead6SMichael Halcrow 				 struct dentry *ecryptfs_dentry, size_t *len,
2293237fead6SMichael Halcrow 				 size_t max)
2294237fead6SMichael Halcrow {
2295237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
2296f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
2297237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2298237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(
2299237fead6SMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
2300237fead6SMichael Halcrow 	size_t written;
2301eb95e7ffSMichael Halcrow 	struct ecryptfs_key_record *key_rec;
2302f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig;
2303dddfa461SMichael Halcrow 	int rc = 0;
2304237fead6SMichael Halcrow 
2305237fead6SMichael Halcrow 	(*len) = 0;
2306f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
2307eb95e7ffSMichael Halcrow 	key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
2308eb95e7ffSMichael Halcrow 	if (!key_rec) {
2309eb95e7ffSMichael Halcrow 		rc = -ENOMEM;
2310eb95e7ffSMichael Halcrow 		goto out;
2311eb95e7ffSMichael Halcrow 	}
2312f4aad16aSMichael Halcrow 	list_for_each_entry(key_sig, &crypt_stat->keysig_list,
2313f4aad16aSMichael Halcrow 			    crypt_stat_list) {
2314f4aad16aSMichael Halcrow 		memset(key_rec, 0, sizeof(*key_rec));
2315f4aad16aSMichael Halcrow 		rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
2316f4aad16aSMichael Halcrow 							   mount_crypt_stat,
2317f4aad16aSMichael Halcrow 							   key_sig->keysig);
2318f4aad16aSMichael Halcrow 		if (rc) {
2319f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error attempting to get the global "
2320f4aad16aSMichael Halcrow 			       "auth_tok; rc = [%d]\n", rc);
2321f4aad16aSMichael Halcrow 			goto out_free;
2322f4aad16aSMichael Halcrow 		}
2323f4aad16aSMichael Halcrow 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
2324f4aad16aSMichael Halcrow 			printk(KERN_WARNING
2325f4aad16aSMichael Halcrow 			       "Skipping invalid auth tok with sig = [%s]\n",
2326f4aad16aSMichael Halcrow 			       global_auth_tok->sig);
2327f4aad16aSMichael Halcrow 			continue;
2328f4aad16aSMichael Halcrow 		}
2329f4aad16aSMichael Halcrow 		auth_tok = global_auth_tok->global_auth_tok;
2330237fead6SMichael Halcrow 		if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
2331237fead6SMichael Halcrow 			rc = write_tag_3_packet((dest_base + (*len)),
2332f4aad16aSMichael Halcrow 						&max, auth_tok,
2333eb95e7ffSMichael Halcrow 						crypt_stat, key_rec,
2334237fead6SMichael Halcrow 						&written);
2335237fead6SMichael Halcrow 			if (rc) {
2336237fead6SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
2337237fead6SMichael Halcrow 						"writing tag 3 packet\n");
2338eb95e7ffSMichael Halcrow 				goto out_free;
2339237fead6SMichael Halcrow 			}
2340237fead6SMichael Halcrow 			(*len) += written;
2341237fead6SMichael Halcrow 			/* Write auth tok signature packet */
2342f4aad16aSMichael Halcrow 			rc = write_tag_11_packet((dest_base + (*len)), &max,
2343f4aad16aSMichael Halcrow 						 key_rec->sig,
2344f4aad16aSMichael Halcrow 						 ECRYPTFS_SIG_SIZE, &written);
2345237fead6SMichael Halcrow 			if (rc) {
2346237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error writing "
2347237fead6SMichael Halcrow 						"auth tok signature packet\n");
2348eb95e7ffSMichael Halcrow 				goto out_free;
2349237fead6SMichael Halcrow 			}
2350237fead6SMichael Halcrow 			(*len) += written;
2351dddfa461SMichael Halcrow 		} else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
2352dddfa461SMichael Halcrow 			rc = write_tag_1_packet(dest_base + (*len),
2353f4aad16aSMichael Halcrow 						&max, auth_tok,
2354f4aad16aSMichael Halcrow 						crypt_stat, key_rec, &written);
2355dddfa461SMichael Halcrow 			if (rc) {
2356dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
2357dddfa461SMichael Halcrow 						"writing tag 1 packet\n");
2358eb95e7ffSMichael Halcrow 				goto out_free;
2359dddfa461SMichael Halcrow 			}
2360dddfa461SMichael Halcrow 			(*len) += written;
2361237fead6SMichael Halcrow 		} else {
2362237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Unsupported "
2363237fead6SMichael Halcrow 					"authentication token type\n");
2364237fead6SMichael Halcrow 			rc = -EINVAL;
2365eb95e7ffSMichael Halcrow 			goto out_free;
2366237fead6SMichael Halcrow 		}
2367f4aad16aSMichael Halcrow 	}
2368f4aad16aSMichael Halcrow 	if (likely(max > 0)) {
2369237fead6SMichael Halcrow 		dest_base[(*len)] = 0x00;
2370237fead6SMichael Halcrow 	} else {
2371237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
2372237fead6SMichael Halcrow 		rc = -EIO;
2373237fead6SMichael Halcrow 	}
2374eb95e7ffSMichael Halcrow out_free:
2375eb95e7ffSMichael Halcrow 	kmem_cache_free(ecryptfs_key_record_cache, key_rec);
2376237fead6SMichael Halcrow out:
2377237fead6SMichael Halcrow 	if (rc)
2378237fead6SMichael Halcrow 		(*len) = 0;
2379f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
2380237fead6SMichael Halcrow 	return rc;
2381237fead6SMichael Halcrow }
2382f4aad16aSMichael Halcrow 
2383f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache;
2384f4aad16aSMichael Halcrow 
2385f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
2386f4aad16aSMichael Halcrow {
2387f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *new_key_sig;
2388f4aad16aSMichael Halcrow 
2389f4aad16aSMichael Halcrow 	new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
2390f4aad16aSMichael Halcrow 	if (!new_key_sig) {
2391f4aad16aSMichael Halcrow 		printk(KERN_ERR
2392f4aad16aSMichael Halcrow 		       "Error allocating from ecryptfs_key_sig_cache\n");
2393aa06117fSRoland Dreier 		return -ENOMEM;
2394f4aad16aSMichael Halcrow 	}
2395f4aad16aSMichael Halcrow 	memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
2396aa06117fSRoland Dreier 	/* Caller must hold keysig_list_mutex */
2397f4aad16aSMichael Halcrow 	list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
2398aa06117fSRoland Dreier 
2399aa06117fSRoland Dreier 	return 0;
2400f4aad16aSMichael Halcrow }
2401f4aad16aSMichael Halcrow 
2402f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache;
2403f4aad16aSMichael Halcrow 
2404f4aad16aSMichael Halcrow int
2405f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
240684814d64STyler Hicks 			     char *sig, u32 global_auth_tok_flags)
2407f4aad16aSMichael Halcrow {
2408f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *new_auth_tok;
2409f4aad16aSMichael Halcrow 	int rc = 0;
2410f4aad16aSMichael Halcrow 
2411459e2164SEric Sandeen 	new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
2412f4aad16aSMichael Halcrow 					GFP_KERNEL);
2413f4aad16aSMichael Halcrow 	if (!new_auth_tok) {
2414f4aad16aSMichael Halcrow 		rc = -ENOMEM;
2415f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error allocating from "
2416f4aad16aSMichael Halcrow 		       "ecryptfs_global_auth_tok_cache\n");
2417f4aad16aSMichael Halcrow 		goto out;
2418f4aad16aSMichael Halcrow 	}
2419f4aad16aSMichael Halcrow 	memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
242084814d64STyler Hicks 	new_auth_tok->flags = global_auth_tok_flags;
2421f4aad16aSMichael Halcrow 	new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
2422f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
2423f4aad16aSMichael Halcrow 	list_add(&new_auth_tok->mount_crypt_stat_list,
2424f4aad16aSMichael Halcrow 		 &mount_crypt_stat->global_auth_tok_list);
2425f4aad16aSMichael Halcrow 	mount_crypt_stat->num_global_auth_toks++;
2426f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2427f4aad16aSMichael Halcrow out:
2428f4aad16aSMichael Halcrow 	return rc;
2429f4aad16aSMichael Halcrow }
2430f4aad16aSMichael Halcrow 
2431