xref: /openbmc/linux/fs/ecryptfs/keystore.c (revision f24b38874e1e37bb70291bbc4c5c3c13f5f9dac8)
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: "
62888d57bbSJoe Perches 				"[0x%.16lx]\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,
133*f24b3887STyler Hicks 				"Unsupported packet size: [%zd]\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(
449aee683b9SRoberto Sassu 	struct key **auth_tok_key,
4509c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok **auth_tok,
4519c79f34fSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
4529c79f34fSMichael Halcrow 	char *sig)
4539c79f34fSMichael Halcrow {
4549c79f34fSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
4559c79f34fSMichael Halcrow 	int rc = 0;
4569c79f34fSMichael Halcrow 
457aee683b9SRoberto Sassu 	(*auth_tok_key) = NULL;
4589c79f34fSMichael Halcrow 	(*auth_tok) = NULL;
4599c79f34fSMichael Halcrow 	if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
4609c79f34fSMichael Halcrow 						  mount_crypt_stat, sig)) {
4619c79f34fSMichael Halcrow 
462f16feb51SRoberto Sassu 		/* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the
463f16feb51SRoberto Sassu 		 * mount_crypt_stat structure, we prevent to use auth toks that
464f16feb51SRoberto Sassu 		 * are not inserted through the ecryptfs_add_global_auth_tok
465f16feb51SRoberto Sassu 		 * function.
466f16feb51SRoberto Sassu 		 */
467f16feb51SRoberto Sassu 		if (mount_crypt_stat->flags
468f16feb51SRoberto Sassu 				& ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
469f16feb51SRoberto Sassu 			return -EINVAL;
470f16feb51SRoberto Sassu 
471aee683b9SRoberto Sassu 		rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok,
4729c79f34fSMichael Halcrow 						       sig);
4739c79f34fSMichael Halcrow 	} else
4749c79f34fSMichael Halcrow 		(*auth_tok) = global_auth_tok->global_auth_tok;
4759c79f34fSMichael Halcrow 	return rc;
4769c79f34fSMichael Halcrow }
4779c79f34fSMichael Halcrow 
4789c79f34fSMichael Halcrow /**
4799c79f34fSMichael Halcrow  * write_tag_70_packet can gobble a lot of stack space. We stuff most
4809c79f34fSMichael Halcrow  * of the function's parameters in a kmalloc'd struct to help reduce
4819c79f34fSMichael Halcrow  * eCryptfs' overall stack usage.
4829c79f34fSMichael Halcrow  */
4839c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack {
4849c79f34fSMichael Halcrow 	u8 cipher_code;
4859c79f34fSMichael Halcrow 	size_t max_packet_size;
4869c79f34fSMichael Halcrow 	size_t packet_size_len;
4879c79f34fSMichael Halcrow 	size_t block_aligned_filename_size;
4889c79f34fSMichael Halcrow 	size_t block_size;
4899c79f34fSMichael Halcrow 	size_t i;
4909c79f34fSMichael Halcrow 	size_t j;
4919c79f34fSMichael Halcrow 	size_t num_rand_bytes;
4929c79f34fSMichael Halcrow 	struct mutex *tfm_mutex;
4939c79f34fSMichael Halcrow 	char *block_aligned_filename;
4949c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
4959c79f34fSMichael Halcrow 	struct scatterlist src_sg;
4969c79f34fSMichael Halcrow 	struct scatterlist dst_sg;
4979c79f34fSMichael Halcrow 	struct blkcipher_desc desc;
4989c79f34fSMichael Halcrow 	char iv[ECRYPTFS_MAX_IV_BYTES];
4999c79f34fSMichael Halcrow 	char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
5009c79f34fSMichael Halcrow 	char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
5019c79f34fSMichael Halcrow 	struct hash_desc hash_desc;
5029c79f34fSMichael Halcrow 	struct scatterlist hash_sg;
5039c79f34fSMichael Halcrow };
5049c79f34fSMichael Halcrow 
5059c79f34fSMichael Halcrow /**
5069c79f34fSMichael Halcrow  * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
5079c79f34fSMichael Halcrow  * @filename: NULL-terminated filename string
5089c79f34fSMichael Halcrow  *
5099c79f34fSMichael Halcrow  * This is the simplest mechanism for achieving filename encryption in
5109c79f34fSMichael Halcrow  * eCryptfs. It encrypts the given filename with the mount-wide
5119c79f34fSMichael Halcrow  * filename encryption key (FNEK) and stores it in a packet to @dest,
5129c79f34fSMichael Halcrow  * which the callee will encode and write directly into the dentry
5139c79f34fSMichael Halcrow  * name.
5149c79f34fSMichael Halcrow  */
5159c79f34fSMichael Halcrow int
5169c79f34fSMichael Halcrow ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
5179c79f34fSMichael Halcrow 			     size_t *packet_size,
5189c79f34fSMichael Halcrow 			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
5199c79f34fSMichael Halcrow 			     char *filename, size_t filename_size)
5209c79f34fSMichael Halcrow {
5219c79f34fSMichael Halcrow 	struct ecryptfs_write_tag_70_packet_silly_stack *s;
522aee683b9SRoberto Sassu 	struct key *auth_tok_key = NULL;
5239c79f34fSMichael Halcrow 	int rc = 0;
5249c79f34fSMichael Halcrow 
5259c79f34fSMichael Halcrow 	s = kmalloc(sizeof(*s), GFP_KERNEL);
5269c79f34fSMichael Halcrow 	if (!s) {
5279c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
528df261c52SMichael Halcrow 		       "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
529f137f150SJulia Lawall 		rc = -ENOMEM;
5309c79f34fSMichael Halcrow 		goto out;
5319c79f34fSMichael Halcrow 	}
5329c79f34fSMichael Halcrow 	s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
5339c79f34fSMichael Halcrow 	(*packet_size) = 0;
5349c79f34fSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
5359c79f34fSMichael Halcrow 		&s->desc.tfm,
5369c79f34fSMichael Halcrow 		&s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
5379c79f34fSMichael Halcrow 	if (unlikely(rc)) {
5389c79f34fSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
5399c79f34fSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
5409c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_name, rc);
5419c79f34fSMichael Halcrow 		goto out;
5429c79f34fSMichael Halcrow 	}
5439c79f34fSMichael Halcrow 	mutex_lock(s->tfm_mutex);
5449c79f34fSMichael Halcrow 	s->block_size = crypto_blkcipher_blocksize(s->desc.tfm);
5459c79f34fSMichael Halcrow 	/* Plus one for the \0 separator between the random prefix
5469c79f34fSMichael Halcrow 	 * and the plaintext filename */
5479c79f34fSMichael Halcrow 	s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
5489c79f34fSMichael Halcrow 	s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
5499c79f34fSMichael Halcrow 	if ((s->block_aligned_filename_size % s->block_size) != 0) {
5509c79f34fSMichael Halcrow 		s->num_rand_bytes += (s->block_size
5519c79f34fSMichael Halcrow 				      - (s->block_aligned_filename_size
5529c79f34fSMichael Halcrow 					 % s->block_size));
5539c79f34fSMichael Halcrow 		s->block_aligned_filename_size = (s->num_rand_bytes
5549c79f34fSMichael Halcrow 						  + filename_size);
5559c79f34fSMichael Halcrow 	}
5569c79f34fSMichael Halcrow 	/* Octet 0: Tag 70 identifier
5579c79f34fSMichael Halcrow 	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
5589c79f34fSMichael Halcrow 	 *              and block-aligned encrypted filename size)
5599c79f34fSMichael Halcrow 	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
5609c79f34fSMichael Halcrow 	 * Octet N2-N3: Cipher identifier (1 octet)
5619c79f34fSMichael Halcrow 	 * Octets N3-N4: Block-aligned encrypted filename
5629c79f34fSMichael Halcrow 	 *  - Consists of a minimum number of random characters, a \0
5639c79f34fSMichael Halcrow 	 *    separator, and then the filename */
5649c79f34fSMichael Halcrow 	s->max_packet_size = (1                   /* Tag 70 identifier */
5659c79f34fSMichael Halcrow 			      + 3                 /* Max Tag 70 packet size */
5669c79f34fSMichael Halcrow 			      + ECRYPTFS_SIG_SIZE /* FNEK sig */
5679c79f34fSMichael Halcrow 			      + 1                 /* Cipher identifier */
5689c79f34fSMichael Halcrow 			      + s->block_aligned_filename_size);
5699c79f34fSMichael Halcrow 	if (dest == NULL) {
5709c79f34fSMichael Halcrow 		(*packet_size) = s->max_packet_size;
5719c79f34fSMichael Halcrow 		goto out_unlock;
5729c79f34fSMichael Halcrow 	}
5739c79f34fSMichael Halcrow 	if (s->max_packet_size > (*remaining_bytes)) {
574a8f12864SMichael Halcrow 		printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
575a8f12864SMichael Halcrow 		       "[%zd] available\n", __func__, s->max_packet_size,
5769c79f34fSMichael Halcrow 		       (*remaining_bytes));
5779c79f34fSMichael Halcrow 		rc = -EINVAL;
5789c79f34fSMichael Halcrow 		goto out_unlock;
5799c79f34fSMichael Halcrow 	}
5809c79f34fSMichael Halcrow 	s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
5819c79f34fSMichael Halcrow 					    GFP_KERNEL);
5829c79f34fSMichael Halcrow 	if (!s->block_aligned_filename) {
5839c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
584df261c52SMichael Halcrow 		       "kzalloc [%zd] bytes\n", __func__,
5859c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
5869c79f34fSMichael Halcrow 		rc = -ENOMEM;
5879c79f34fSMichael Halcrow 		goto out_unlock;
5889c79f34fSMichael Halcrow 	}
5899c79f34fSMichael Halcrow 	s->i = 0;
5909c79f34fSMichael Halcrow 	dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
5919c79f34fSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[s->i],
5929c79f34fSMichael Halcrow 					  (ECRYPTFS_SIG_SIZE
5939c79f34fSMichael Halcrow 					   + 1 /* Cipher code */
5949c79f34fSMichael Halcrow 					   + s->block_aligned_filename_size),
5959c79f34fSMichael Halcrow 					  &s->packet_size_len);
5969c79f34fSMichael Halcrow 	if (rc) {
5979c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error generating tag 70 packet "
5989c79f34fSMichael Halcrow 		       "header; cannot generate packet length; rc = [%d]\n",
5999c79f34fSMichael Halcrow 		       __func__, rc);
6009c79f34fSMichael Halcrow 		goto out_free_unlock;
6019c79f34fSMichael Halcrow 	}
6029c79f34fSMichael Halcrow 	s->i += s->packet_size_len;
6039c79f34fSMichael Halcrow 	ecryptfs_from_hex(&dest[s->i],
6049c79f34fSMichael Halcrow 			  mount_crypt_stat->global_default_fnek_sig,
6059c79f34fSMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
6069c79f34fSMichael Halcrow 	s->i += ECRYPTFS_SIG_SIZE;
6079c79f34fSMichael Halcrow 	s->cipher_code = ecryptfs_code_for_cipher_string(
6089c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_name,
6099c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
6109c79f34fSMichael Halcrow 	if (s->cipher_code == 0) {
6119c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Unable to generate code for "
612a8f12864SMichael Halcrow 		       "cipher [%s] with key bytes [%zd]\n", __func__,
6139c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_name,
6149c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
6159c79f34fSMichael Halcrow 		rc = -EINVAL;
6169c79f34fSMichael Halcrow 		goto out_free_unlock;
6179c79f34fSMichael Halcrow 	}
6189c79f34fSMichael Halcrow 	dest[s->i++] = s->cipher_code;
6199c79f34fSMichael Halcrow 	rc = ecryptfs_find_auth_tok_for_sig(
620aee683b9SRoberto Sassu 		&auth_tok_key,
6219c79f34fSMichael Halcrow 		&s->auth_tok, mount_crypt_stat,
6229c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fnek_sig);
6239c79f34fSMichael Halcrow 	if (rc) {
6249c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to find auth tok for "
6259c79f34fSMichael Halcrow 		       "fnek sig [%s]; rc = [%d]\n", __func__,
6269c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fnek_sig, rc);
6279c79f34fSMichael Halcrow 		goto out_free_unlock;
6289c79f34fSMichael Halcrow 	}
6299c79f34fSMichael Halcrow 	/* TODO: Support other key modules than passphrase for
6309c79f34fSMichael Halcrow 	 * filename encryption */
631df6ad33bSTyler Hicks 	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
632df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
633df6ad33bSTyler Hicks 		printk(KERN_INFO "%s: Filename encryption only supports "
634df6ad33bSTyler Hicks 		       "password tokens\n", __func__);
635df6ad33bSTyler Hicks 		goto out_free_unlock;
636df6ad33bSTyler Hicks 	}
6379c79f34fSMichael Halcrow 	sg_init_one(
6389c79f34fSMichael Halcrow 		&s->hash_sg,
6399c79f34fSMichael Halcrow 		(u8 *)s->auth_tok->token.password.session_key_encryption_key,
6409c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key_bytes);
6419c79f34fSMichael Halcrow 	s->hash_desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
6429c79f34fSMichael Halcrow 	s->hash_desc.tfm = crypto_alloc_hash(ECRYPTFS_TAG_70_DIGEST, 0,
6439c79f34fSMichael Halcrow 					     CRYPTO_ALG_ASYNC);
6449c79f34fSMichael Halcrow 	if (IS_ERR(s->hash_desc.tfm)) {
6459c79f34fSMichael Halcrow 			rc = PTR_ERR(s->hash_desc.tfm);
6469c79f34fSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to "
6479c79f34fSMichael Halcrow 			       "allocate hash crypto context; rc = [%d]\n",
6489c79f34fSMichael Halcrow 			       __func__, rc);
6499c79f34fSMichael Halcrow 			goto out_free_unlock;
6509c79f34fSMichael Halcrow 	}
6519c79f34fSMichael Halcrow 	rc = crypto_hash_init(&s->hash_desc);
6529c79f34fSMichael Halcrow 	if (rc) {
6539c79f34fSMichael Halcrow 		printk(KERN_ERR
6549c79f34fSMichael Halcrow 		       "%s: Error initializing crypto hash; rc = [%d]\n",
6559c79f34fSMichael Halcrow 		       __func__, rc);
6569c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6579c79f34fSMichael Halcrow 	}
6589c79f34fSMichael Halcrow 	rc = crypto_hash_update(
6599c79f34fSMichael Halcrow 		&s->hash_desc, &s->hash_sg,
6609c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key_bytes);
6619c79f34fSMichael Halcrow 	if (rc) {
6629c79f34fSMichael Halcrow 		printk(KERN_ERR
6639c79f34fSMichael Halcrow 		       "%s: Error updating crypto hash; rc = [%d]\n",
6649c79f34fSMichael Halcrow 		       __func__, rc);
6659c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6669c79f34fSMichael Halcrow 	}
6679c79f34fSMichael Halcrow 	rc = crypto_hash_final(&s->hash_desc, s->hash);
6689c79f34fSMichael Halcrow 	if (rc) {
6699c79f34fSMichael Halcrow 		printk(KERN_ERR
6709c79f34fSMichael Halcrow 		       "%s: Error finalizing crypto hash; rc = [%d]\n",
6719c79f34fSMichael Halcrow 		       __func__, rc);
6729c79f34fSMichael Halcrow 		goto out_release_free_unlock;
6739c79f34fSMichael Halcrow 	}
6749c79f34fSMichael Halcrow 	for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
6759c79f34fSMichael Halcrow 		s->block_aligned_filename[s->j] =
6769c79f34fSMichael Halcrow 			s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
6779c79f34fSMichael Halcrow 		if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
6789c79f34fSMichael Halcrow 		    == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
6799c79f34fSMichael Halcrow 			sg_init_one(&s->hash_sg, (u8 *)s->hash,
6809c79f34fSMichael Halcrow 				    ECRYPTFS_TAG_70_DIGEST_SIZE);
6819c79f34fSMichael Halcrow 			rc = crypto_hash_init(&s->hash_desc);
6829c79f34fSMichael Halcrow 			if (rc) {
6839c79f34fSMichael Halcrow 				printk(KERN_ERR
6849c79f34fSMichael Halcrow 				       "%s: Error initializing crypto hash; "
6859c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
6869c79f34fSMichael Halcrow 				goto out_release_free_unlock;
6879c79f34fSMichael Halcrow 			}
6889c79f34fSMichael Halcrow 			rc = crypto_hash_update(&s->hash_desc, &s->hash_sg,
6899c79f34fSMichael Halcrow 						ECRYPTFS_TAG_70_DIGEST_SIZE);
6909c79f34fSMichael Halcrow 			if (rc) {
6919c79f34fSMichael Halcrow 				printk(KERN_ERR
6929c79f34fSMichael Halcrow 				       "%s: Error updating crypto hash; "
6939c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
6949c79f34fSMichael Halcrow 				goto out_release_free_unlock;
6959c79f34fSMichael Halcrow 			}
6969c79f34fSMichael Halcrow 			rc = crypto_hash_final(&s->hash_desc, s->tmp_hash);
6979c79f34fSMichael Halcrow 			if (rc) {
6989c79f34fSMichael Halcrow 				printk(KERN_ERR
6999c79f34fSMichael Halcrow 				       "%s: Error finalizing crypto hash; "
7009c79f34fSMichael Halcrow 				       "rc = [%d]\n", __func__, rc);
7019c79f34fSMichael Halcrow 				goto out_release_free_unlock;
7029c79f34fSMichael Halcrow 			}
7039c79f34fSMichael Halcrow 			memcpy(s->hash, s->tmp_hash,
7049c79f34fSMichael Halcrow 			       ECRYPTFS_TAG_70_DIGEST_SIZE);
7059c79f34fSMichael Halcrow 		}
7069c79f34fSMichael Halcrow 		if (s->block_aligned_filename[s->j] == '\0')
7079c79f34fSMichael Halcrow 			s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
7089c79f34fSMichael Halcrow 	}
7099c79f34fSMichael Halcrow 	memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
7109c79f34fSMichael Halcrow 	       filename_size);
7119c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(s->block_aligned_filename,
7129c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->src_sg, 1);
7139c79f34fSMichael Halcrow 	if (rc != 1) {
7149c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
7159c79f34fSMichael Halcrow 		       "convert filename memory to scatterlist; "
7169c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
717a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
7189c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
7199c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7209c79f34fSMichael Halcrow 	}
7219c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
7229c79f34fSMichael Halcrow 				 &s->dst_sg, 1);
7239c79f34fSMichael Halcrow 	if (rc != 1) {
7249c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
7259c79f34fSMichael Halcrow 		       "convert encrypted filename memory to scatterlist; "
7269c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
727a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
7289c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
7299c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7309c79f34fSMichael Halcrow 	}
7319c79f34fSMichael Halcrow 	/* The characters in the first block effectively do the job
7329c79f34fSMichael Halcrow 	 * of the IV here, so we just use 0's for the IV. Note the
7339c79f34fSMichael Halcrow 	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
7349c79f34fSMichael Halcrow 	 * >= ECRYPTFS_MAX_IV_BYTES. */
7359c79f34fSMichael Halcrow 	memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
7369c79f34fSMichael Halcrow 	s->desc.info = s->iv;
7379c79f34fSMichael Halcrow 	rc = crypto_blkcipher_setkey(
7389c79f34fSMichael Halcrow 		s->desc.tfm,
7399c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key,
7409c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
7419c79f34fSMichael Halcrow 	if (rc < 0) {
7429c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error setting key for crypto context; "
7439c79f34fSMichael Halcrow 		       "rc = [%d]. s->auth_tok->token.password.session_key_"
7449c79f34fSMichael Halcrow 		       "encryption_key = [0x%p]; mount_crypt_stat->"
745df261c52SMichael Halcrow 		       "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
7469c79f34fSMichael Halcrow 		       rc,
7479c79f34fSMichael Halcrow 		       s->auth_tok->token.password.session_key_encryption_key,
7489c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
7499c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7509c79f34fSMichael Halcrow 	}
7519c79f34fSMichael Halcrow 	rc = crypto_blkcipher_encrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
7529c79f34fSMichael Halcrow 					 s->block_aligned_filename_size);
7539c79f34fSMichael Halcrow 	if (rc) {
7549c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to encrypt filename; "
7559c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
7569c79f34fSMichael Halcrow 		goto out_release_free_unlock;
7579c79f34fSMichael Halcrow 	}
7589c79f34fSMichael Halcrow 	s->i += s->block_aligned_filename_size;
7599c79f34fSMichael Halcrow 	(*packet_size) = s->i;
7609c79f34fSMichael Halcrow 	(*remaining_bytes) -= (*packet_size);
7619c79f34fSMichael Halcrow out_release_free_unlock:
7629c79f34fSMichael Halcrow 	crypto_free_hash(s->hash_desc.tfm);
7639c79f34fSMichael Halcrow out_free_unlock:
76400fcf2cbSJohannes Weiner 	kzfree(s->block_aligned_filename);
7659c79f34fSMichael Halcrow out_unlock:
7669c79f34fSMichael Halcrow 	mutex_unlock(s->tfm_mutex);
7679c79f34fSMichael Halcrow out:
768aee683b9SRoberto Sassu 	if (auth_tok_key)
769aee683b9SRoberto Sassu 		key_put(auth_tok_key);
7709c79f34fSMichael Halcrow 	kfree(s);
7719c79f34fSMichael Halcrow 	return rc;
7729c79f34fSMichael Halcrow }
7739c79f34fSMichael Halcrow 
7749c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack {
7759c79f34fSMichael Halcrow 	u8 cipher_code;
7769c79f34fSMichael Halcrow 	size_t max_packet_size;
7779c79f34fSMichael Halcrow 	size_t packet_size_len;
7789c79f34fSMichael Halcrow 	size_t parsed_tag_70_packet_size;
7799c79f34fSMichael Halcrow 	size_t block_aligned_filename_size;
7809c79f34fSMichael Halcrow 	size_t block_size;
7819c79f34fSMichael Halcrow 	size_t i;
7829c79f34fSMichael Halcrow 	struct mutex *tfm_mutex;
7839c79f34fSMichael Halcrow 	char *decrypted_filename;
7849c79f34fSMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
7859c79f34fSMichael Halcrow 	struct scatterlist src_sg;
7869c79f34fSMichael Halcrow 	struct scatterlist dst_sg;
7879c79f34fSMichael Halcrow 	struct blkcipher_desc desc;
7889c79f34fSMichael Halcrow 	char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
7899c79f34fSMichael Halcrow 	char iv[ECRYPTFS_MAX_IV_BYTES];
7909c79f34fSMichael Halcrow 	char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
7919c79f34fSMichael Halcrow };
7929c79f34fSMichael Halcrow 
7939c79f34fSMichael Halcrow /**
7949c79f34fSMichael Halcrow  * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
7959c79f34fSMichael Halcrow  * @filename: This function kmalloc's the memory for the filename
7967d8bc2beSMichael Halcrow  * @filename_size: This function sets this to the amount of memory
7977d8bc2beSMichael Halcrow  *                 kmalloc'd for the filename
7987d8bc2beSMichael Halcrow  * @packet_size: This function sets this to the the number of octets
7997d8bc2beSMichael Halcrow  *               in the packet parsed
8007d8bc2beSMichael Halcrow  * @mount_crypt_stat: The mount-wide cryptographic context
8017d8bc2beSMichael Halcrow  * @data: The memory location containing the start of the tag 70
8027d8bc2beSMichael Halcrow  *        packet
8037d8bc2beSMichael Halcrow  * @max_packet_size: The maximum legal size of the packet to be parsed
8047d8bc2beSMichael Halcrow  *                   from @data
8057d8bc2beSMichael Halcrow  *
8067d8bc2beSMichael Halcrow  * Returns zero on success; non-zero otherwise
8079c79f34fSMichael Halcrow  */
8089c79f34fSMichael Halcrow int
8099c79f34fSMichael Halcrow ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
8109c79f34fSMichael Halcrow 			     size_t *packet_size,
8119c79f34fSMichael Halcrow 			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
8129c79f34fSMichael Halcrow 			     char *data, size_t max_packet_size)
8139c79f34fSMichael Halcrow {
8149c79f34fSMichael Halcrow 	struct ecryptfs_parse_tag_70_packet_silly_stack *s;
815aee683b9SRoberto Sassu 	struct key *auth_tok_key = NULL;
8169c79f34fSMichael Halcrow 	int rc = 0;
8179c79f34fSMichael Halcrow 
8189c79f34fSMichael Halcrow 	(*packet_size) = 0;
8199c79f34fSMichael Halcrow 	(*filename_size) = 0;
8209c79f34fSMichael Halcrow 	(*filename) = NULL;
8219c79f34fSMichael Halcrow 	s = kmalloc(sizeof(*s), GFP_KERNEL);
8229c79f34fSMichael Halcrow 	if (!s) {
8239c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
824a8f12864SMichael Halcrow 		       "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
825f137f150SJulia Lawall 		rc = -ENOMEM;
8269c79f34fSMichael Halcrow 		goto out;
8279c79f34fSMichael Halcrow 	}
8289c79f34fSMichael Halcrow 	s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
8299c79f34fSMichael Halcrow 	if (max_packet_size < (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)) {
830df261c52SMichael Halcrow 		printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
8319c79f34fSMichael Halcrow 		       "at least [%d]\n", __func__, max_packet_size,
8329c79f34fSMichael Halcrow 			(1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1));
8339c79f34fSMichael Halcrow 		rc = -EINVAL;
8349c79f34fSMichael Halcrow 		goto out;
8359c79f34fSMichael Halcrow 	}
8369c79f34fSMichael Halcrow 	/* Octet 0: Tag 70 identifier
8379c79f34fSMichael Halcrow 	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
8389c79f34fSMichael Halcrow 	 *              and block-aligned encrypted filename size)
8399c79f34fSMichael Halcrow 	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
8409c79f34fSMichael Halcrow 	 * Octet N2-N3: Cipher identifier (1 octet)
8419c79f34fSMichael Halcrow 	 * Octets N3-N4: Block-aligned encrypted filename
8429c79f34fSMichael Halcrow 	 *  - Consists of a minimum number of random numbers, a \0
8439c79f34fSMichael Halcrow 	 *    separator, and then the filename */
8449c79f34fSMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
8459c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
8469c79f34fSMichael Halcrow 		       "tag [0x%.2x]\n", __func__,
8479c79f34fSMichael Halcrow 		       data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
8489c79f34fSMichael Halcrow 		rc = -EINVAL;
8499c79f34fSMichael Halcrow 		goto out;
8509c79f34fSMichael Halcrow 	}
8519c79f34fSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
8529c79f34fSMichael Halcrow 					  &s->parsed_tag_70_packet_size,
8539c79f34fSMichael Halcrow 					  &s->packet_size_len);
8549c79f34fSMichael Halcrow 	if (rc) {
8559c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Error parsing packet length; "
8569c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
8579c79f34fSMichael Halcrow 		goto out;
8589c79f34fSMichael Halcrow 	}
8599c79f34fSMichael Halcrow 	s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
8609c79f34fSMichael Halcrow 					  - ECRYPTFS_SIG_SIZE - 1);
8619c79f34fSMichael Halcrow 	if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
8629c79f34fSMichael Halcrow 	    > max_packet_size) {
863a8f12864SMichael Halcrow 		printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
864a8f12864SMichael Halcrow 		       "size is [%zd]\n", __func__, max_packet_size,
8659c79f34fSMichael Halcrow 		       (1 + s->packet_size_len + 1
8669c79f34fSMichael Halcrow 			+ s->block_aligned_filename_size));
8679c79f34fSMichael Halcrow 		rc = -EINVAL;
8689c79f34fSMichael Halcrow 		goto out;
8699c79f34fSMichael Halcrow 	}
8709c79f34fSMichael Halcrow 	(*packet_size) += s->packet_size_len;
8719c79f34fSMichael Halcrow 	ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
8729c79f34fSMichael Halcrow 			ECRYPTFS_SIG_SIZE);
8739c79f34fSMichael Halcrow 	s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
8749c79f34fSMichael Halcrow 	(*packet_size) += ECRYPTFS_SIG_SIZE;
8759c79f34fSMichael Halcrow 	s->cipher_code = data[(*packet_size)++];
8769c79f34fSMichael Halcrow 	rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
8779c79f34fSMichael Halcrow 	if (rc) {
8789c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
8799c79f34fSMichael Halcrow 		       __func__, s->cipher_code);
8809c79f34fSMichael Halcrow 		goto out;
8819c79f34fSMichael Halcrow 	}
8829c79f34fSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
8839c79f34fSMichael Halcrow 							&s->tfm_mutex,
8849c79f34fSMichael Halcrow 							s->cipher_string);
8859c79f34fSMichael Halcrow 	if (unlikely(rc)) {
8869c79f34fSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
8879c79f34fSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
8889c79f34fSMichael Halcrow 		       s->cipher_string, rc);
8899c79f34fSMichael Halcrow 		goto out;
8909c79f34fSMichael Halcrow 	}
8919c79f34fSMichael Halcrow 	mutex_lock(s->tfm_mutex);
8929c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(&data[(*packet_size)],
8939c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->src_sg, 1);
8949c79f34fSMichael Halcrow 	if (rc != 1) {
8959c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
8969c79f34fSMichael Halcrow 		       "convert encrypted filename memory to scatterlist; "
8979c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
898a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
8999c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
9009c79f34fSMichael Halcrow 		goto out_unlock;
9019c79f34fSMichael Halcrow 	}
9029c79f34fSMichael Halcrow 	(*packet_size) += s->block_aligned_filename_size;
9039c79f34fSMichael Halcrow 	s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
9049c79f34fSMichael Halcrow 					GFP_KERNEL);
9059c79f34fSMichael Halcrow 	if (!s->decrypted_filename) {
9069c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst attempting to "
907a8f12864SMichael Halcrow 		       "kmalloc [%zd] bytes\n", __func__,
9089c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
9099c79f34fSMichael Halcrow 		rc = -ENOMEM;
9109c79f34fSMichael Halcrow 		goto out_unlock;
9119c79f34fSMichael Halcrow 	}
9129c79f34fSMichael Halcrow 	rc = virt_to_scatterlist(s->decrypted_filename,
9139c79f34fSMichael Halcrow 				 s->block_aligned_filename_size, &s->dst_sg, 1);
9149c79f34fSMichael Halcrow 	if (rc != 1) {
9159c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Internal error whilst attempting to "
9169c79f34fSMichael Halcrow 		       "convert decrypted filename memory to scatterlist; "
9179c79f34fSMichael Halcrow 		       "expected rc = 1; got rc = [%d]. "
918a8f12864SMichael Halcrow 		       "block_aligned_filename_size = [%zd]\n", __func__, rc,
9199c79f34fSMichael Halcrow 		       s->block_aligned_filename_size);
9209c79f34fSMichael Halcrow 		goto out_free_unlock;
9219c79f34fSMichael Halcrow 	}
9229c79f34fSMichael Halcrow 	/* The characters in the first block effectively do the job of
9239c79f34fSMichael Halcrow 	 * the IV here, so we just use 0's for the IV. Note the
9249c79f34fSMichael Halcrow 	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
9259c79f34fSMichael Halcrow 	 * >= ECRYPTFS_MAX_IV_BYTES. */
9269c79f34fSMichael Halcrow 	memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
9279c79f34fSMichael Halcrow 	s->desc.info = s->iv;
928aee683b9SRoberto Sassu 	rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
929aee683b9SRoberto Sassu 					    &s->auth_tok, mount_crypt_stat,
9309c79f34fSMichael Halcrow 					    s->fnek_sig_hex);
9319c79f34fSMichael Halcrow 	if (rc) {
9329c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to find auth tok for "
9339c79f34fSMichael Halcrow 		       "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
9349c79f34fSMichael Halcrow 		       rc);
9359c79f34fSMichael Halcrow 		goto out_free_unlock;
9369c79f34fSMichael Halcrow 	}
9379c79f34fSMichael Halcrow 	/* TODO: Support other key modules than passphrase for
9389c79f34fSMichael Halcrow 	 * filename encryption */
939df6ad33bSTyler Hicks 	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
940df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
941df6ad33bSTyler Hicks 		printk(KERN_INFO "%s: Filename encryption only supports "
942df6ad33bSTyler Hicks 		       "password tokens\n", __func__);
943df6ad33bSTyler Hicks 		goto out_free_unlock;
944df6ad33bSTyler Hicks 	}
9459c79f34fSMichael Halcrow 	rc = crypto_blkcipher_setkey(
9469c79f34fSMichael Halcrow 		s->desc.tfm,
9479c79f34fSMichael Halcrow 		s->auth_tok->token.password.session_key_encryption_key,
9489c79f34fSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes);
9499c79f34fSMichael Halcrow 	if (rc < 0) {
9509c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error setting key for crypto context; "
9519c79f34fSMichael Halcrow 		       "rc = [%d]. s->auth_tok->token.password.session_key_"
9529c79f34fSMichael Halcrow 		       "encryption_key = [0x%p]; mount_crypt_stat->"
953df261c52SMichael Halcrow 		       "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
9549c79f34fSMichael Halcrow 		       rc,
9559c79f34fSMichael Halcrow 		       s->auth_tok->token.password.session_key_encryption_key,
9569c79f34fSMichael Halcrow 		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
9579c79f34fSMichael Halcrow 		goto out_free_unlock;
9589c79f34fSMichael Halcrow 	}
9599c79f34fSMichael Halcrow 	rc = crypto_blkcipher_decrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
9609c79f34fSMichael Halcrow 					 s->block_aligned_filename_size);
9619c79f34fSMichael Halcrow 	if (rc) {
9629c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decrypt filename; "
9639c79f34fSMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
9649c79f34fSMichael Halcrow 		goto out_free_unlock;
9659c79f34fSMichael Halcrow 	}
9669c79f34fSMichael Halcrow 	s->i = 0;
9679c79f34fSMichael Halcrow 	while (s->decrypted_filename[s->i] != '\0'
9689c79f34fSMichael Halcrow 	       && s->i < s->block_aligned_filename_size)
9699c79f34fSMichael Halcrow 		s->i++;
9709c79f34fSMichael Halcrow 	if (s->i == s->block_aligned_filename_size) {
9719c79f34fSMichael Halcrow 		printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
9729c79f34fSMichael Halcrow 		       "find valid separator between random characters and "
9739c79f34fSMichael Halcrow 		       "the filename\n", __func__);
9749c79f34fSMichael Halcrow 		rc = -EINVAL;
9759c79f34fSMichael Halcrow 		goto out_free_unlock;
9769c79f34fSMichael Halcrow 	}
9779c79f34fSMichael Halcrow 	s->i++;
9789c79f34fSMichael Halcrow 	(*filename_size) = (s->block_aligned_filename_size - s->i);
9799c79f34fSMichael Halcrow 	if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
980df261c52SMichael Halcrow 		printk(KERN_WARNING "%s: Filename size is [%zd], which is "
9819c79f34fSMichael Halcrow 		       "invalid\n", __func__, (*filename_size));
9829c79f34fSMichael Halcrow 		rc = -EINVAL;
9839c79f34fSMichael Halcrow 		goto out_free_unlock;
9849c79f34fSMichael Halcrow 	}
9859c79f34fSMichael Halcrow 	(*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
9869c79f34fSMichael Halcrow 	if (!(*filename)) {
9879c79f34fSMichael Halcrow 		printk(KERN_ERR "%s: Out of memory whilst attempting to "
988a8f12864SMichael Halcrow 		       "kmalloc [%zd] bytes\n", __func__,
9899c79f34fSMichael Halcrow 		       ((*filename_size) + 1));
9909c79f34fSMichael Halcrow 		rc = -ENOMEM;
9919c79f34fSMichael Halcrow 		goto out_free_unlock;
9929c79f34fSMichael Halcrow 	}
9939c79f34fSMichael Halcrow 	memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
9949c79f34fSMichael Halcrow 	(*filename)[(*filename_size)] = '\0';
9959c79f34fSMichael Halcrow out_free_unlock:
9969c79f34fSMichael Halcrow 	kfree(s->decrypted_filename);
9979c79f34fSMichael Halcrow out_unlock:
9989c79f34fSMichael Halcrow 	mutex_unlock(s->tfm_mutex);
9999c79f34fSMichael Halcrow out:
10009c79f34fSMichael Halcrow 	if (rc) {
10019c79f34fSMichael Halcrow 		(*packet_size) = 0;
10029c79f34fSMichael Halcrow 		(*filename_size) = 0;
10039c79f34fSMichael Halcrow 		(*filename) = NULL;
10049c79f34fSMichael Halcrow 	}
1005aee683b9SRoberto Sassu 	if (auth_tok_key)
1006aee683b9SRoberto Sassu 		key_put(auth_tok_key);
10079c79f34fSMichael Halcrow 	kfree(s);
10089c79f34fSMichael Halcrow 	return rc;
10099c79f34fSMichael Halcrow }
10109c79f34fSMichael Halcrow 
10119c79f34fSMichael Halcrow static int
1012cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1013cd9d67dfSMichael Halcrow {
1014cd9d67dfSMichael Halcrow 	int rc = 0;
1015cd9d67dfSMichael Halcrow 
1016cd9d67dfSMichael Halcrow 	(*sig) = NULL;
1017cd9d67dfSMichael Halcrow 	switch (auth_tok->token_type) {
1018cd9d67dfSMichael Halcrow 	case ECRYPTFS_PASSWORD:
1019cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.password.signature;
1020cd9d67dfSMichael Halcrow 		break;
1021cd9d67dfSMichael Halcrow 	case ECRYPTFS_PRIVATE_KEY:
1022cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.private_key.signature;
1023cd9d67dfSMichael Halcrow 		break;
1024cd9d67dfSMichael Halcrow 	default:
1025cd9d67dfSMichael Halcrow 		printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1026cd9d67dfSMichael Halcrow 		       auth_tok->token_type);
1027cd9d67dfSMichael Halcrow 		rc = -EINVAL;
1028cd9d67dfSMichael Halcrow 	}
1029cd9d67dfSMichael Halcrow 	return rc;
1030cd9d67dfSMichael Halcrow }
1031cd9d67dfSMichael Halcrow 
1032dddfa461SMichael Halcrow /**
103322e78fafSMichael Halcrow  * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
103422e78fafSMichael Halcrow  * @auth_tok: The key authentication token used to decrypt the session key
103522e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1036dddfa461SMichael Halcrow  *
103722e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise.
1038dddfa461SMichael Halcrow  */
1039f4aad16aSMichael Halcrow static int
1040f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1041dddfa461SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat)
1042dddfa461SMichael Halcrow {
104319e66a67STrevor Highland 	u8 cipher_code = 0;
1044dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx;
1045dddfa461SMichael Halcrow 	struct ecryptfs_message *msg = NULL;
1046f4aad16aSMichael Halcrow 	char *auth_tok_sig;
1047624ae528STyler Hicks 	char *payload;
1048624ae528STyler Hicks 	size_t payload_len;
1049dddfa461SMichael Halcrow 	int rc;
1050dddfa461SMichael Halcrow 
10515dda6992SMichael Halcrow 	rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
10525dda6992SMichael Halcrow 	if (rc) {
1053f4aad16aSMichael Halcrow 		printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
1054f4aad16aSMichael Halcrow 		       auth_tok->token_type);
1055f4aad16aSMichael Halcrow 		goto out;
1056f4aad16aSMichael Halcrow 	}
1057f4aad16aSMichael Halcrow 	rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
1058624ae528STyler Hicks 				 &payload, &payload_len);
1059dddfa461SMichael Halcrow 	if (rc) {
1060f66e883eSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
1061dddfa461SMichael Halcrow 		goto out;
1062dddfa461SMichael Halcrow 	}
1063624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1064dddfa461SMichael Halcrow 	if (rc) {
1065624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
1066624ae528STyler Hicks 				"ecryptfsd\n");
1067dddfa461SMichael Halcrow 		goto out;
1068dddfa461SMichael Halcrow 	}
1069dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1070dddfa461SMichael Halcrow 	if (rc) {
1071dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
1072dddfa461SMichael Halcrow 				"from the user space daemon\n");
1073dddfa461SMichael Halcrow 		rc = -EIO;
1074dddfa461SMichael Halcrow 		goto out;
1075dddfa461SMichael Halcrow 	}
1076dddfa461SMichael Halcrow 	rc = parse_tag_65_packet(&(auth_tok->session_key),
1077dddfa461SMichael Halcrow 				 &cipher_code, msg);
1078dddfa461SMichael Halcrow 	if (rc) {
1079dddfa461SMichael Halcrow 		printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
1080dddfa461SMichael Halcrow 		       rc);
1081dddfa461SMichael Halcrow 		goto out;
1082dddfa461SMichael Halcrow 	}
1083dddfa461SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1084dddfa461SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1085dddfa461SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
1086dddfa461SMichael Halcrow 	crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
1087dddfa461SMichael Halcrow 	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
1088dddfa461SMichael Halcrow 	if (rc) {
1089dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
1090dddfa461SMichael Halcrow 				cipher_code)
1091dddfa461SMichael Halcrow 		goto out;
1092dddfa461SMichael Halcrow 	}
1093dddfa461SMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1094dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1095dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
1096dddfa461SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
1097dddfa461SMichael Halcrow 				  crypt_stat->key_size);
1098dddfa461SMichael Halcrow 	}
1099dddfa461SMichael Halcrow out:
1100dddfa461SMichael Halcrow 	if (msg)
1101dddfa461SMichael Halcrow 		kfree(msg);
1102dddfa461SMichael Halcrow 	return rc;
1103dddfa461SMichael Halcrow }
1104dddfa461SMichael Halcrow 
1105dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
1106dddfa461SMichael Halcrow {
1107dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1108e0869cc1SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1109dddfa461SMichael Halcrow 
1110e0869cc1SMichael Halcrow 	list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
1111e0869cc1SMichael Halcrow 				 auth_tok_list_head, list) {
1112e0869cc1SMichael Halcrow 		list_del(&auth_tok_list_item->list);
1113dddfa461SMichael Halcrow 		kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1114dddfa461SMichael Halcrow 				auth_tok_list_item);
1115dddfa461SMichael Halcrow 	}
1116dddfa461SMichael Halcrow }
1117dddfa461SMichael Halcrow 
1118dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
1119dddfa461SMichael Halcrow 
1120dddfa461SMichael Halcrow /**
1121dddfa461SMichael Halcrow  * parse_tag_1_packet
112222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet contents
1123dddfa461SMichael Halcrow  * @data: The raw bytes of the packet.
1124dddfa461SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
112522e78fafSMichael Halcrow  *                 a new authentication token will be placed at the
112622e78fafSMichael Halcrow  *                 end of this list for this packet.
1127dddfa461SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
1128dddfa461SMichael Halcrow  *                allocates; sets the memory address of the pointer to
1129dddfa461SMichael Halcrow  *                NULL on error. This object is added to the
1130dddfa461SMichael Halcrow  *                auth_tok_list.
1131dddfa461SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1132dddfa461SMichael Halcrow  *               into this memory location; zero on error.
113322e78fafSMichael Halcrow  * @max_packet_size: The maximum allowable packet size
1134dddfa461SMichael Halcrow  *
1135dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
1136dddfa461SMichael Halcrow  */
1137dddfa461SMichael Halcrow static int
1138dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
1139dddfa461SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
1140dddfa461SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
1141dddfa461SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
1142dddfa461SMichael Halcrow {
1143dddfa461SMichael Halcrow 	size_t body_size;
1144dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1145dddfa461SMichael Halcrow 	size_t length_size;
1146dddfa461SMichael Halcrow 	int rc = 0;
1147dddfa461SMichael Halcrow 
1148dddfa461SMichael Halcrow 	(*packet_size) = 0;
1149dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
115013218179SMichael Halcrow 	/**
115113218179SMichael Halcrow 	 * This format is inspired by OpenPGP; see RFC 2440
115213218179SMichael Halcrow 	 * packet tag 1
115313218179SMichael Halcrow 	 *
115413218179SMichael Halcrow 	 * Tag 1 identifier (1 byte)
115513218179SMichael Halcrow 	 * Max Tag 1 packet size (max 3 bytes)
115613218179SMichael Halcrow 	 * Version (1 byte)
115713218179SMichael Halcrow 	 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
115813218179SMichael Halcrow 	 * Cipher identifier (1 byte)
115913218179SMichael Halcrow 	 * Encrypted key size (arbitrary)
116013218179SMichael Halcrow 	 *
116113218179SMichael Halcrow 	 * 12 bytes minimum packet size
1162dddfa461SMichael Halcrow 	 */
116313218179SMichael Halcrow 	if (unlikely(max_packet_size < 12)) {
116413218179SMichael Halcrow 		printk(KERN_ERR "Invalid max packet size; must be >=12\n");
1165dddfa461SMichael Halcrow 		rc = -EINVAL;
1166dddfa461SMichael Halcrow 		goto out;
1167dddfa461SMichael Halcrow 	}
1168dddfa461SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
116913218179SMichael Halcrow 		printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
1170dddfa461SMichael Halcrow 		       ECRYPTFS_TAG_1_PACKET_TYPE);
1171dddfa461SMichael Halcrow 		rc = -EINVAL;
1172dddfa461SMichael Halcrow 		goto out;
1173dddfa461SMichael Halcrow 	}
1174dddfa461SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1175dddfa461SMichael Halcrow 	 * at end of function upon failure */
1176dddfa461SMichael Halcrow 	auth_tok_list_item =
117713218179SMichael Halcrow 		kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
1178dddfa461SMichael Halcrow 				  GFP_KERNEL);
1179dddfa461SMichael Halcrow 	if (!auth_tok_list_item) {
118013218179SMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
1181dddfa461SMichael Halcrow 		rc = -ENOMEM;
1182dddfa461SMichael Halcrow 		goto out;
1183dddfa461SMichael Halcrow 	}
1184dddfa461SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
1185f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
11865dda6992SMichael Halcrow 					  &length_size);
11875dda6992SMichael Halcrow 	if (rc) {
118813218179SMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; "
1189dddfa461SMichael Halcrow 		       "rc = [%d]\n", rc);
1190dddfa461SMichael Halcrow 		goto out_free;
1191dddfa461SMichael Halcrow 	}
119213218179SMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
119381acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1194dddfa461SMichael Halcrow 		rc = -EINVAL;
1195dddfa461SMichael Halcrow 		goto out_free;
1196dddfa461SMichael Halcrow 	}
1197dddfa461SMichael Halcrow 	(*packet_size) += length_size;
1198dddfa461SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
119913218179SMichael Halcrow 		printk(KERN_WARNING "Packet size exceeds max\n");
1200dddfa461SMichael Halcrow 		rc = -EINVAL;
1201dddfa461SMichael Halcrow 		goto out_free;
1202dddfa461SMichael Halcrow 	}
1203dddfa461SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
120413218179SMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
120513218179SMichael Halcrow 		       data[(*packet_size) - 1]);
1206dddfa461SMichael Halcrow 		rc = -EINVAL;
1207dddfa461SMichael Halcrow 		goto out_free;
1208dddfa461SMichael Halcrow 	}
1209dddfa461SMichael Halcrow 	ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
1210dddfa461SMichael Halcrow 			&data[(*packet_size)], ECRYPTFS_SIG_SIZE);
1211dddfa461SMichael Halcrow 	*packet_size += ECRYPTFS_SIG_SIZE;
1212dddfa461SMichael Halcrow 	/* This byte is skipped because the kernel does not need to
1213dddfa461SMichael Halcrow 	 * know which public key encryption algorithm was used */
1214dddfa461SMichael Halcrow 	(*packet_size)++;
1215dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
121613218179SMichael Halcrow 		body_size - (ECRYPTFS_SIG_SIZE + 2);
1217dddfa461SMichael Halcrow 	if ((*new_auth_tok)->session_key.encrypted_key_size
1218dddfa461SMichael Halcrow 	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
121913218179SMichael Halcrow 		printk(KERN_WARNING "Tag 1 packet contains key larger "
1220dddfa461SMichael Halcrow 		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
1221dddfa461SMichael Halcrow 		rc = -EINVAL;
1222dddfa461SMichael Halcrow 		goto out;
1223dddfa461SMichael Halcrow 	}
1224dddfa461SMichael Halcrow 	memcpy((*new_auth_tok)->session_key.encrypted_key,
122513218179SMichael Halcrow 	       &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
1226dddfa461SMichael Halcrow 	(*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
1227dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1228dddfa461SMichael Halcrow 		~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1229dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags |=
1230dddfa461SMichael Halcrow 		ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1231dddfa461SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
123213218179SMichael Halcrow 	(*new_auth_tok)->flags = 0;
1233e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1234e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1235e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1236e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1237dddfa461SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
1238dddfa461SMichael Halcrow 	goto out;
1239dddfa461SMichael Halcrow out_free:
1240dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
1241dddfa461SMichael Halcrow 	memset(auth_tok_list_item, 0,
1242dddfa461SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
1243dddfa461SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1244dddfa461SMichael Halcrow 			auth_tok_list_item);
1245dddfa461SMichael Halcrow out:
1246dddfa461SMichael Halcrow 	if (rc)
1247dddfa461SMichael Halcrow 		(*packet_size) = 0;
1248dddfa461SMichael Halcrow 	return rc;
1249dddfa461SMichael Halcrow }
1250dddfa461SMichael Halcrow 
1251237fead6SMichael Halcrow /**
1252237fead6SMichael Halcrow  * parse_tag_3_packet
1253237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet
1254237fead6SMichael Halcrow  *              contents.
1255237fead6SMichael Halcrow  * @data: The raw bytes of the packet.
1256237fead6SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
1257237fead6SMichael Halcrow  *                 a new authentication token will be placed at the end
1258237fead6SMichael Halcrow  *                 of this list for this packet.
1259237fead6SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
1260237fead6SMichael Halcrow  *                allocates; sets the memory address of the pointer to
1261237fead6SMichael Halcrow  *                NULL on error. This object is added to the
1262237fead6SMichael Halcrow  *                auth_tok_list.
1263237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1264237fead6SMichael Halcrow  *               into this memory location; zero on error.
1265237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
1266237fead6SMichael Halcrow  *
1267237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1268237fead6SMichael Halcrow  */
1269237fead6SMichael Halcrow static int
1270237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
1271237fead6SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
1272237fead6SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
1273237fead6SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
1274237fead6SMichael Halcrow {
1275237fead6SMichael Halcrow 	size_t body_size;
1276237fead6SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1277237fead6SMichael Halcrow 	size_t length_size;
1278dddfa461SMichael Halcrow 	int rc = 0;
1279237fead6SMichael Halcrow 
1280237fead6SMichael Halcrow 	(*packet_size) = 0;
1281237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
1282c59becfcSMichael Halcrow 	/**
1283c59becfcSMichael Halcrow 	 *This format is inspired by OpenPGP; see RFC 2440
1284c59becfcSMichael Halcrow 	 * packet tag 3
1285c59becfcSMichael Halcrow 	 *
1286c59becfcSMichael Halcrow 	 * Tag 3 identifier (1 byte)
1287c59becfcSMichael Halcrow 	 * Max Tag 3 packet size (max 3 bytes)
1288c59becfcSMichael Halcrow 	 * Version (1 byte)
1289c59becfcSMichael Halcrow 	 * Cipher code (1 byte)
1290c59becfcSMichael Halcrow 	 * S2K specifier (1 byte)
1291c59becfcSMichael Halcrow 	 * Hash identifier (1 byte)
1292c59becfcSMichael Halcrow 	 * Salt (ECRYPTFS_SALT_SIZE)
1293c59becfcSMichael Halcrow 	 * Hash iterations (1 byte)
1294c59becfcSMichael Halcrow 	 * Encrypted key (arbitrary)
1295c59becfcSMichael Halcrow 	 *
1296c59becfcSMichael Halcrow 	 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
1297237fead6SMichael Halcrow 	 */
1298c59becfcSMichael Halcrow 	if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
1299c59becfcSMichael Halcrow 		printk(KERN_ERR "Max packet size too large\n");
1300237fead6SMichael Halcrow 		rc = -EINVAL;
1301237fead6SMichael Halcrow 		goto out;
1302237fead6SMichael Halcrow 	}
1303237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
1304c59becfcSMichael Halcrow 		printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
1305237fead6SMichael Halcrow 		       ECRYPTFS_TAG_3_PACKET_TYPE);
1306237fead6SMichael Halcrow 		rc = -EINVAL;
1307237fead6SMichael Halcrow 		goto out;
1308237fead6SMichael Halcrow 	}
1309237fead6SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1310237fead6SMichael Halcrow 	 * at end of function upon failure */
1311237fead6SMichael Halcrow 	auth_tok_list_item =
1312c3762229SRobert P. J. Day 	    kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
1313237fead6SMichael Halcrow 	if (!auth_tok_list_item) {
1314c59becfcSMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
1315237fead6SMichael Halcrow 		rc = -ENOMEM;
1316237fead6SMichael Halcrow 		goto out;
1317237fead6SMichael Halcrow 	}
1318237fead6SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
1319f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
13205dda6992SMichael Halcrow 					  &length_size);
13215dda6992SMichael Halcrow 	if (rc) {
1322c59becfcSMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
1323c59becfcSMichael Halcrow 		       rc);
1324237fead6SMichael Halcrow 		goto out_free;
1325237fead6SMichael Halcrow 	}
1326c59becfcSMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
132781acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1328237fead6SMichael Halcrow 		rc = -EINVAL;
1329237fead6SMichael Halcrow 		goto out_free;
1330237fead6SMichael Halcrow 	}
1331237fead6SMichael Halcrow 	(*packet_size) += length_size;
1332237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
1333c59becfcSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
1334237fead6SMichael Halcrow 		rc = -EINVAL;
1335237fead6SMichael Halcrow 		goto out_free;
1336237fead6SMichael Halcrow 	}
1337237fead6SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
1338c59becfcSMichael Halcrow 		(body_size - (ECRYPTFS_SALT_SIZE + 5));
1339f151cd2cSRamon de Carvalho Valle 	if ((*new_auth_tok)->session_key.encrypted_key_size
1340f151cd2cSRamon de Carvalho Valle 	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
1341f151cd2cSRamon de Carvalho Valle 		printk(KERN_WARNING "Tag 3 packet contains key larger "
1342f151cd2cSRamon de Carvalho Valle 		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
1343f151cd2cSRamon de Carvalho Valle 		rc = -EINVAL;
1344f151cd2cSRamon de Carvalho Valle 		goto out_free;
1345f151cd2cSRamon de Carvalho Valle 	}
1346237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x04)) {
1347c59becfcSMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
1348c59becfcSMichael Halcrow 		       data[(*packet_size) - 1]);
1349237fead6SMichael Halcrow 		rc = -EINVAL;
1350237fead6SMichael Halcrow 		goto out_free;
1351237fead6SMichael Halcrow 	}
1352b0105eaeSTyler Hicks 	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1353237fead6SMichael Halcrow 					    (u16)data[(*packet_size)]);
1354b0105eaeSTyler Hicks 	if (rc)
1355b0105eaeSTyler Hicks 		goto out_free;
1356237fead6SMichael Halcrow 	/* A little extra work to differentiate among the AES key
1357237fead6SMichael Halcrow 	 * sizes; see RFC2440 */
1358237fead6SMichael Halcrow 	switch(data[(*packet_size)++]) {
1359237fead6SMichael Halcrow 	case RFC2440_CIPHER_AES_192:
1360237fead6SMichael Halcrow 		crypt_stat->key_size = 24;
1361237fead6SMichael Halcrow 		break;
1362237fead6SMichael Halcrow 	default:
1363237fead6SMichael Halcrow 		crypt_stat->key_size =
1364237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
1365237fead6SMichael Halcrow 	}
1366b0105eaeSTyler Hicks 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1367b0105eaeSTyler Hicks 	if (rc)
1368b0105eaeSTyler Hicks 		goto out_free;
1369237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
1370c59becfcSMichael Halcrow 		printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
1371237fead6SMichael Halcrow 		rc = -ENOSYS;
1372237fead6SMichael Halcrow 		goto out_free;
1373237fead6SMichael Halcrow 	}
1374237fead6SMichael Halcrow 	/* TODO: finish the hash mapping */
1375237fead6SMichael Halcrow 	switch (data[(*packet_size)++]) {
1376237fead6SMichael Halcrow 	case 0x01: /* See RFC2440 for these numbers and their mappings */
1377237fead6SMichael Halcrow 		/* Choose MD5 */
1378237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->token.password.salt,
1379237fead6SMichael Halcrow 		       &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
1380237fead6SMichael Halcrow 		(*packet_size) += ECRYPTFS_SALT_SIZE;
1381237fead6SMichael Halcrow 		/* This conversion was taken straight from RFC2440 */
1382237fead6SMichael Halcrow 		(*new_auth_tok)->token.password.hash_iterations =
1383237fead6SMichael Halcrow 			((u32) 16 + (data[(*packet_size)] & 15))
1384237fead6SMichael Halcrow 				<< ((data[(*packet_size)] >> 4) + 6);
1385237fead6SMichael Halcrow 		(*packet_size)++;
1386c59becfcSMichael Halcrow 		/* Friendly reminder:
1387c59becfcSMichael Halcrow 		 * (*new_auth_tok)->session_key.encrypted_key_size =
1388c59becfcSMichael Halcrow 		 *         (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
1389237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->session_key.encrypted_key,
1390237fead6SMichael Halcrow 		       &data[(*packet_size)],
1391237fead6SMichael Halcrow 		       (*new_auth_tok)->session_key.encrypted_key_size);
1392237fead6SMichael Halcrow 		(*packet_size) +=
1393237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
1394237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags &=
1395237fead6SMichael Halcrow 			~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1396237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags |=
1397237fead6SMichael Halcrow 			ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1398c59becfcSMichael Halcrow 		(*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
1399237fead6SMichael Halcrow 		break;
1400237fead6SMichael Halcrow 	default:
1401237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
1402237fead6SMichael Halcrow 				"[%d]\n", data[(*packet_size) - 1]);
1403237fead6SMichael Halcrow 		rc = -ENOSYS;
1404237fead6SMichael Halcrow 		goto out_free;
1405237fead6SMichael Halcrow 	}
1406237fead6SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
1407237fead6SMichael Halcrow 	/* TODO: Parametarize; we might actually want userspace to
1408237fead6SMichael Halcrow 	 * decrypt the session key. */
1409e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1410e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1411e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
1412e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1413237fead6SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
1414237fead6SMichael Halcrow 	goto out;
1415237fead6SMichael Halcrow out_free:
1416237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
1417237fead6SMichael Halcrow 	memset(auth_tok_list_item, 0,
1418237fead6SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
1419237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1420237fead6SMichael Halcrow 			auth_tok_list_item);
1421237fead6SMichael Halcrow out:
1422237fead6SMichael Halcrow 	if (rc)
1423237fead6SMichael Halcrow 		(*packet_size) = 0;
1424237fead6SMichael Halcrow 	return rc;
1425237fead6SMichael Halcrow }
1426237fead6SMichael Halcrow 
1427237fead6SMichael Halcrow /**
1428237fead6SMichael Halcrow  * parse_tag_11_packet
1429237fead6SMichael Halcrow  * @data: The raw bytes of the packet
1430237fead6SMichael Halcrow  * @contents: This function writes the data contents of the literal
1431237fead6SMichael Halcrow  *            packet into this memory location
1432237fead6SMichael Halcrow  * @max_contents_bytes: The maximum number of bytes that this function
1433237fead6SMichael Halcrow  *                      is allowed to write into contents
1434237fead6SMichael Halcrow  * @tag_11_contents_size: This function writes the size of the parsed
1435237fead6SMichael Halcrow  *                        contents into this memory location; zero on
1436237fead6SMichael Halcrow  *                        error
1437237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
1438237fead6SMichael Halcrow  *               into this memory location; zero on error
1439237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
1440237fead6SMichael Halcrow  *
1441237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1442237fead6SMichael Halcrow  */
1443237fead6SMichael Halcrow static int
1444237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents,
1445237fead6SMichael Halcrow 		    size_t max_contents_bytes, size_t *tag_11_contents_size,
1446237fead6SMichael Halcrow 		    size_t *packet_size, size_t max_packet_size)
1447237fead6SMichael Halcrow {
1448237fead6SMichael Halcrow 	size_t body_size;
1449237fead6SMichael Halcrow 	size_t length_size;
1450dddfa461SMichael Halcrow 	int rc = 0;
1451237fead6SMichael Halcrow 
1452237fead6SMichael Halcrow 	(*packet_size) = 0;
1453237fead6SMichael Halcrow 	(*tag_11_contents_size) = 0;
1454f648104aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1455f648104aSMichael Halcrow 	 * packet tag 11
1456f648104aSMichael Halcrow 	 *
1457f648104aSMichael Halcrow 	 * Tag 11 identifier (1 byte)
1458f648104aSMichael Halcrow 	 * Max Tag 11 packet size (max 3 bytes)
1459f648104aSMichael Halcrow 	 * Binary format specifier (1 byte)
1460f648104aSMichael Halcrow 	 * Filename length (1 byte)
1461f648104aSMichael Halcrow 	 * Filename ("_CONSOLE") (8 bytes)
1462f648104aSMichael Halcrow 	 * Modification date (4 bytes)
1463f648104aSMichael Halcrow 	 * Literal data (arbitrary)
1464f648104aSMichael Halcrow 	 *
1465f648104aSMichael Halcrow 	 * We need at least 16 bytes of data for the packet to even be
1466f648104aSMichael Halcrow 	 * valid.
1467237fead6SMichael Halcrow 	 */
1468f648104aSMichael Halcrow 	if (max_packet_size < 16) {
1469f648104aSMichael Halcrow 		printk(KERN_ERR "Maximum packet size too small\n");
1470237fead6SMichael Halcrow 		rc = -EINVAL;
1471237fead6SMichael Halcrow 		goto out;
1472237fead6SMichael Halcrow 	}
1473237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
1474f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
1475237fead6SMichael Halcrow 		rc = -EINVAL;
1476237fead6SMichael Halcrow 		goto out;
1477237fead6SMichael Halcrow 	}
1478f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
14795dda6992SMichael Halcrow 					  &length_size);
14805dda6992SMichael Halcrow 	if (rc) {
1481f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
1482f648104aSMichael Halcrow 		goto out;
1483f648104aSMichael Halcrow 	}
1484f648104aSMichael Halcrow 	if (body_size < 14) {
148581acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1486f648104aSMichael Halcrow 		rc = -EINVAL;
1487237fead6SMichael Halcrow 		goto out;
1488237fead6SMichael Halcrow 	}
1489237fead6SMichael Halcrow 	(*packet_size) += length_size;
1490f648104aSMichael Halcrow 	(*tag_11_contents_size) = (body_size - 14);
1491237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
1492f648104aSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
1493237fead6SMichael Halcrow 		rc = -EINVAL;
1494237fead6SMichael Halcrow 		goto out;
1495237fead6SMichael Halcrow 	}
14966352a293STyler Hicks 	if (unlikely((*tag_11_contents_size) > max_contents_bytes)) {
14976352a293STyler Hicks 		printk(KERN_ERR "Literal data section in tag 11 packet exceeds "
14986352a293STyler Hicks 		       "expected size\n");
14996352a293STyler Hicks 		rc = -EINVAL;
15006352a293STyler Hicks 		goto out;
15016352a293STyler Hicks 	}
1502237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x62) {
1503f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
1504237fead6SMichael Halcrow 		rc = -EINVAL;
1505237fead6SMichael Halcrow 		goto out;
1506237fead6SMichael Halcrow 	}
1507237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x08) {
1508f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
1509237fead6SMichael Halcrow 		rc = -EINVAL;
1510237fead6SMichael Halcrow 		goto out;
1511237fead6SMichael Halcrow 	}
1512f648104aSMichael Halcrow 	(*packet_size) += 12; /* Ignore filename and modification date */
1513237fead6SMichael Halcrow 	memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
1514237fead6SMichael Halcrow 	(*packet_size) += (*tag_11_contents_size);
1515237fead6SMichael Halcrow out:
1516237fead6SMichael Halcrow 	if (rc) {
1517237fead6SMichael Halcrow 		(*packet_size) = 0;
1518237fead6SMichael Halcrow 		(*tag_11_contents_size) = 0;
1519237fead6SMichael Halcrow 	}
1520237fead6SMichael Halcrow 	return rc;
1521237fead6SMichael Halcrow }
1522237fead6SMichael Halcrow 
1523237fead6SMichael Halcrow /**
1524f4aad16aSMichael Halcrow  * ecryptfs_verify_version
1525f4aad16aSMichael Halcrow  * @version: The version number to confirm
1526f4aad16aSMichael Halcrow  *
1527f4aad16aSMichael Halcrow  * Returns zero on good version; non-zero otherwise
1528f4aad16aSMichael Halcrow  */
1529f4aad16aSMichael Halcrow static int ecryptfs_verify_version(u16 version)
1530f4aad16aSMichael Halcrow {
1531f4aad16aSMichael Halcrow 	int rc = 0;
1532f4aad16aSMichael Halcrow 	unsigned char major;
1533f4aad16aSMichael Halcrow 	unsigned char minor;
1534f4aad16aSMichael Halcrow 
1535f4aad16aSMichael Halcrow 	major = ((version >> 8) & 0xFF);
1536f4aad16aSMichael Halcrow 	minor = (version & 0xFF);
1537f4aad16aSMichael Halcrow 	if (major != ECRYPTFS_VERSION_MAJOR) {
1538f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
1539f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
1540f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MAJOR, major);
1541f4aad16aSMichael Halcrow 		rc = -EINVAL;
1542f4aad16aSMichael Halcrow 		goto out;
1543f4aad16aSMichael Halcrow 	}
1544f4aad16aSMichael Halcrow 	if (minor != ECRYPTFS_VERSION_MINOR) {
1545f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
1546f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
1547f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MINOR, minor);
1548f4aad16aSMichael Halcrow 		rc = -EINVAL;
1549f4aad16aSMichael Halcrow 		goto out;
1550f4aad16aSMichael Halcrow 	}
1551f4aad16aSMichael Halcrow out:
1552f4aad16aSMichael Halcrow 	return rc;
1553f4aad16aSMichael Halcrow }
1554f4aad16aSMichael Halcrow 
1555f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
1556f4aad16aSMichael Halcrow 				      struct ecryptfs_auth_tok **auth_tok,
1557f4aad16aSMichael Halcrow 				      char *sig)
1558f4aad16aSMichael Halcrow {
1559f4aad16aSMichael Halcrow 	int rc = 0;
1560f4aad16aSMichael Halcrow 
1561f4aad16aSMichael Halcrow 	(*auth_tok_key) = request_key(&key_type_user, sig, NULL);
1562f4aad16aSMichael Halcrow 	if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1563f4aad16aSMichael Halcrow 		printk(KERN_ERR "Could not find key with description: [%s]\n",
1564f4aad16aSMichael Halcrow 		       sig);
1565982363c9SEric Sandeen 		rc = process_request_key_err(PTR_ERR(*auth_tok_key));
1566f4aad16aSMichael Halcrow 		goto out;
1567f4aad16aSMichael Halcrow 	}
1568f4aad16aSMichael Halcrow 	(*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
1569f4aad16aSMichael Halcrow 	if (ecryptfs_verify_version((*auth_tok)->version)) {
1570f4aad16aSMichael Halcrow 		printk(KERN_ERR
1571f4aad16aSMichael Halcrow 		       "Data structure version mismatch. "
1572f4aad16aSMichael Halcrow 		       "Userspace tools must match eCryptfs "
1573f4aad16aSMichael Halcrow 		       "kernel module with major version [%d] "
1574f4aad16aSMichael Halcrow 		       "and minor version [%d]\n",
1575f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MAJOR,
1576f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MINOR);
1577f4aad16aSMichael Halcrow 		rc = -EINVAL;
1578aee683b9SRoberto Sassu 		goto out_release_key;
1579f4aad16aSMichael Halcrow 	}
1580f4aad16aSMichael Halcrow 	if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
1581f4aad16aSMichael Halcrow 	    && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
1582f4aad16aSMichael Halcrow 		printk(KERN_ERR "Invalid auth_tok structure "
1583f4aad16aSMichael Halcrow 		       "returned from key query\n");
1584f4aad16aSMichael Halcrow 		rc = -EINVAL;
1585aee683b9SRoberto Sassu 		goto out_release_key;
1586aee683b9SRoberto Sassu 	}
1587aee683b9SRoberto Sassu out_release_key:
1588aee683b9SRoberto Sassu 	if (rc) {
1589aee683b9SRoberto Sassu 		key_put(*auth_tok_key);
1590aee683b9SRoberto Sassu 		(*auth_tok_key) = NULL;
1591f4aad16aSMichael Halcrow 	}
1592f4aad16aSMichael Halcrow out:
1593f4aad16aSMichael Halcrow 	return rc;
1594f4aad16aSMichael Halcrow }
1595f4aad16aSMichael Halcrow 
1596f4aad16aSMichael Halcrow /**
159722e78fafSMichael Halcrow  * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
159822e78fafSMichael Halcrow  * @auth_tok: The passphrase authentication token to use to encrypt the FEK
159922e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1600237fead6SMichael Halcrow  *
160122e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise
1602237fead6SMichael Halcrow  */
1603f4aad16aSMichael Halcrow static int
1604f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1605237fead6SMichael Halcrow 					 struct ecryptfs_crypt_stat *crypt_stat)
1606237fead6SMichael Halcrow {
1607ac97b9f9SMichael Halcrow 	struct scatterlist dst_sg[2];
1608ac97b9f9SMichael Halcrow 	struct scatterlist src_sg[2];
1609dd8e2902SMichael Halcrow 	struct mutex *tfm_mutex;
16108bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
16118bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
16128bba066fSMichael Halcrow 	};
16138bba066fSMichael Halcrow 	int rc = 0;
1614237fead6SMichael Halcrow 
1615f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1616f4aad16aSMichael Halcrow 		ecryptfs_printk(
1617f4aad16aSMichael Halcrow 			KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1618f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1619f4aad16aSMichael Halcrow 		ecryptfs_dump_hex(
1620f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key,
1621f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1622f4aad16aSMichael Halcrow 	}
1623f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1624f4aad16aSMichael Halcrow 							crypt_stat->cipher);
1625f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
1626f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
1627f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1628f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
1629237fead6SMichael Halcrow 		goto out;
1630237fead6SMichael Halcrow 	}
16315dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1632f4aad16aSMichael Halcrow 				 auth_tok->session_key.encrypted_key_size,
1633ac97b9f9SMichael Halcrow 				 src_sg, 2);
1634ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
1635f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1636f4aad16aSMichael Halcrow 			"auth_tok->session_key.encrypted_key to scatterlist; "
1637f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]. "
1638f4aad16aSMichael Halcrow 		       "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1639f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key_size);
1640f4aad16aSMichael Halcrow 		goto out;
1641237fead6SMichael Halcrow 	}
1642f4aad16aSMichael Halcrow 	auth_tok->session_key.decrypted_key_size =
1643f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
16445dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1645f4aad16aSMichael Halcrow 				 auth_tok->session_key.decrypted_key_size,
1646ac97b9f9SMichael Halcrow 				 dst_sg, 2);
1647ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
1648f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1649f4aad16aSMichael Halcrow 			"auth_tok->session_key.decrypted_key to scatterlist; "
1650f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]\n", rc);
1651f4aad16aSMichael Halcrow 		goto out;
1652f4aad16aSMichael Halcrow 	}
1653237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
1654f4aad16aSMichael Halcrow 	rc = crypto_blkcipher_setkey(
1655f4aad16aSMichael Halcrow 		desc.tfm, auth_tok->token.password.session_key_encryption_key,
1656237fead6SMichael Halcrow 		crypt_stat->key_size);
1657f4aad16aSMichael Halcrow 	if (unlikely(rc < 0)) {
1658f4aad16aSMichael Halcrow 		mutex_unlock(tfm_mutex);
1659e5d9cbdeSMichael Halcrow 		printk(KERN_ERR "Error setting key for crypto context\n");
1660e5d9cbdeSMichael Halcrow 		rc = -EINVAL;
1661f4aad16aSMichael Halcrow 		goto out;
1662e5d9cbdeSMichael Halcrow 	}
1663ac97b9f9SMichael Halcrow 	rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
1664237fead6SMichael Halcrow 				      auth_tok->session_key.encrypted_key_size);
1665f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
1666f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
16678bba066fSMichael Halcrow 		printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
1668f4aad16aSMichael Halcrow 		goto out;
16698bba066fSMichael Halcrow 	}
1670237fead6SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1671237fead6SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1672237fead6SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
1673e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1674f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1675*f24b3887STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n",
1676f4aad16aSMichael Halcrow 				crypt_stat->key_size);
1677237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
1678237fead6SMichael Halcrow 				  crypt_stat->key_size);
1679f4aad16aSMichael Halcrow 	}
1680237fead6SMichael Halcrow out:
1681237fead6SMichael Halcrow 	return rc;
1682237fead6SMichael Halcrow }
1683237fead6SMichael Halcrow 
1684237fead6SMichael Halcrow /**
1685237fead6SMichael Halcrow  * ecryptfs_parse_packet_set
168622e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
168722e78fafSMichael Halcrow  * @src: Virtual address of region of memory containing the packets
168822e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
1689237fead6SMichael Halcrow  *
1690237fead6SMichael Halcrow  * Get crypt_stat to have the file's session key if the requisite key
1691237fead6SMichael Halcrow  * is available to decrypt the session key.
1692237fead6SMichael Halcrow  *
1693237fead6SMichael Halcrow  * Returns Zero if a valid authentication token was retrieved and
1694237fead6SMichael Halcrow  * processed; negative value for file not encrypted or for error
1695237fead6SMichael Halcrow  * conditions.
1696237fead6SMichael Halcrow  */
1697237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1698237fead6SMichael Halcrow 			      unsigned char *src,
1699237fead6SMichael Halcrow 			      struct dentry *ecryptfs_dentry)
1700237fead6SMichael Halcrow {
1701237fead6SMichael Halcrow 	size_t i = 0;
1702f4aad16aSMichael Halcrow 	size_t found_auth_tok;
1703237fead6SMichael Halcrow 	size_t next_packet_is_auth_tok_packet;
1704237fead6SMichael Halcrow 	struct list_head auth_tok_list;
1705dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *matching_auth_tok;
1706dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *candidate_auth_tok;
1707f4aad16aSMichael Halcrow 	char *candidate_auth_tok_sig;
1708237fead6SMichael Halcrow 	size_t packet_size;
1709237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *new_auth_tok;
1710237fead6SMichael Halcrow 	unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
1711f4aad16aSMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1712237fead6SMichael Halcrow 	size_t tag_11_contents_size;
1713237fead6SMichael Halcrow 	size_t tag_11_packet_size;
1714aee683b9SRoberto Sassu 	struct key *auth_tok_key = NULL;
1715dddfa461SMichael Halcrow 	int rc = 0;
1716237fead6SMichael Halcrow 
1717237fead6SMichael Halcrow 	INIT_LIST_HEAD(&auth_tok_list);
1718f4aad16aSMichael Halcrow 	/* Parse the header to find as many packets as we can; these will be
1719237fead6SMichael Halcrow 	 * added the our &auth_tok_list */
1720237fead6SMichael Halcrow 	next_packet_is_auth_tok_packet = 1;
1721237fead6SMichael Halcrow 	while (next_packet_is_auth_tok_packet) {
1722237fead6SMichael Halcrow 		size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1723237fead6SMichael Halcrow 
1724237fead6SMichael Halcrow 		switch (src[i]) {
1725237fead6SMichael Halcrow 		case ECRYPTFS_TAG_3_PACKET_TYPE:
1726237fead6SMichael Halcrow 			rc = parse_tag_3_packet(crypt_stat,
1727237fead6SMichael Halcrow 						(unsigned char *)&src[i],
1728237fead6SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1729237fead6SMichael Halcrow 						&packet_size, max_packet_size);
1730237fead6SMichael Halcrow 			if (rc) {
1731237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1732237fead6SMichael Halcrow 						"tag 3 packet\n");
1733237fead6SMichael Halcrow 				rc = -EIO;
1734237fead6SMichael Halcrow 				goto out_wipe_list;
1735237fead6SMichael Halcrow 			}
1736237fead6SMichael Halcrow 			i += packet_size;
1737237fead6SMichael Halcrow 			rc = parse_tag_11_packet((unsigned char *)&src[i],
1738237fead6SMichael Halcrow 						 sig_tmp_space,
1739237fead6SMichael Halcrow 						 ECRYPTFS_SIG_SIZE,
1740237fead6SMichael Halcrow 						 &tag_11_contents_size,
1741237fead6SMichael Halcrow 						 &tag_11_packet_size,
1742237fead6SMichael Halcrow 						 max_packet_size);
1743237fead6SMichael Halcrow 			if (rc) {
1744237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "No valid "
1745237fead6SMichael Halcrow 						"(ecryptfs-specific) literal "
1746237fead6SMichael Halcrow 						"packet containing "
1747237fead6SMichael Halcrow 						"authentication token "
1748237fead6SMichael Halcrow 						"signature found after "
1749237fead6SMichael Halcrow 						"tag 3 packet\n");
1750237fead6SMichael Halcrow 				rc = -EIO;
1751237fead6SMichael Halcrow 				goto out_wipe_list;
1752237fead6SMichael Halcrow 			}
1753237fead6SMichael Halcrow 			i += tag_11_packet_size;
1754237fead6SMichael Halcrow 			if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1755237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Expected "
1756237fead6SMichael Halcrow 						"signature of size [%d]; "
1757*f24b3887STyler Hicks 						"read size [%zd]\n",
1758237fead6SMichael Halcrow 						ECRYPTFS_SIG_SIZE,
1759237fead6SMichael Halcrow 						tag_11_contents_size);
1760237fead6SMichael Halcrow 				rc = -EIO;
1761237fead6SMichael Halcrow 				goto out_wipe_list;
1762237fead6SMichael Halcrow 			}
1763237fead6SMichael Halcrow 			ecryptfs_to_hex(new_auth_tok->token.password.signature,
1764237fead6SMichael Halcrow 					sig_tmp_space, tag_11_contents_size);
1765237fead6SMichael Halcrow 			new_auth_tok->token.password.signature[
1766237fead6SMichael Halcrow 				ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
1767e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1768237fead6SMichael Halcrow 			break;
1769dddfa461SMichael Halcrow 		case ECRYPTFS_TAG_1_PACKET_TYPE:
1770dddfa461SMichael Halcrow 			rc = parse_tag_1_packet(crypt_stat,
1771dddfa461SMichael Halcrow 						(unsigned char *)&src[i],
1772dddfa461SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1773dddfa461SMichael Halcrow 						&packet_size, max_packet_size);
1774dddfa461SMichael Halcrow 			if (rc) {
1775dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1776dddfa461SMichael Halcrow 						"tag 1 packet\n");
1777dddfa461SMichael Halcrow 				rc = -EIO;
1778dddfa461SMichael Halcrow 				goto out_wipe_list;
1779dddfa461SMichael Halcrow 			}
1780dddfa461SMichael Halcrow 			i += packet_size;
1781e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1782dddfa461SMichael Halcrow 			break;
1783237fead6SMichael Halcrow 		case ECRYPTFS_TAG_11_PACKET_TYPE:
1784237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1785237fead6SMichael Halcrow 					"(Tag 11 not allowed by itself)\n");
1786237fead6SMichael Halcrow 			rc = -EIO;
1787237fead6SMichael Halcrow 			goto out_wipe_list;
1788237fead6SMichael Halcrow 			break;
1789237fead6SMichael Halcrow 		default:
1790*f24b3887STyler Hicks 			ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] "
1791*f24b3887STyler Hicks 					"of the file header; hex value of "
1792237fead6SMichael Halcrow 					"character is [0x%.2x]\n", i, src[i]);
1793237fead6SMichael Halcrow 			next_packet_is_auth_tok_packet = 0;
1794237fead6SMichael Halcrow 		}
1795237fead6SMichael Halcrow 	}
1796237fead6SMichael Halcrow 	if (list_empty(&auth_tok_list)) {
1797f4aad16aSMichael Halcrow 		printk(KERN_ERR "The lower file appears to be a non-encrypted "
1798f4aad16aSMichael Halcrow 		       "eCryptfs file; this is not supported in this version "
1799f4aad16aSMichael Halcrow 		       "of the eCryptfs kernel module\n");
1800f4aad16aSMichael Halcrow 		rc = -EINVAL;
1801237fead6SMichael Halcrow 		goto out;
1802237fead6SMichael Halcrow 	}
1803f4aad16aSMichael Halcrow 	/* auth_tok_list contains the set of authentication tokens
1804f4aad16aSMichael Halcrow 	 * parsed from the metadata. We need to find a matching
1805f4aad16aSMichael Halcrow 	 * authentication token that has the secret component(s)
1806f4aad16aSMichael Halcrow 	 * necessary to decrypt the EFEK in the auth_tok parsed from
1807f4aad16aSMichael Halcrow 	 * the metadata. There may be several potential matches, but
1808f4aad16aSMichael Halcrow 	 * just one will be sufficient to decrypt to get the FEK. */
1809f4aad16aSMichael Halcrow find_next_matching_auth_tok:
1810f4aad16aSMichael Halcrow 	found_auth_tok = 0;
1811aee683b9SRoberto Sassu 	if (auth_tok_key) {
1812aee683b9SRoberto Sassu 		key_put(auth_tok_key);
1813aee683b9SRoberto Sassu 		auth_tok_key = NULL;
1814aee683b9SRoberto Sassu 	}
1815f4aad16aSMichael Halcrow 	list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
1816237fead6SMichael Halcrow 		candidate_auth_tok = &auth_tok_list_item->auth_tok;
1817237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
1818237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
1819237fead6SMichael Halcrow 					"Considering cadidate auth tok:\n");
1820237fead6SMichael Halcrow 			ecryptfs_dump_auth_tok(candidate_auth_tok);
1821237fead6SMichael Halcrow 		}
18225dda6992SMichael Halcrow 		rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
18235dda6992SMichael Halcrow 					       candidate_auth_tok);
18245dda6992SMichael Halcrow 		if (rc) {
1825f4aad16aSMichael Halcrow 			printk(KERN_ERR
1826f4aad16aSMichael Halcrow 			       "Unrecognized candidate auth tok type: [%d]\n",
1827f4aad16aSMichael Halcrow 			       candidate_auth_tok->token_type);
1828f4aad16aSMichael Halcrow 			rc = -EINVAL;
1829f4aad16aSMichael Halcrow 			goto out_wipe_list;
1830f4aad16aSMichael Halcrow 		}
183139fac853SRoberto Sassu 		rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
1832aee683b9SRoberto Sassu 					       &matching_auth_tok,
18339c79f34fSMichael Halcrow 					       crypt_stat->mount_crypt_stat,
18345dda6992SMichael Halcrow 					       candidate_auth_tok_sig);
183539fac853SRoberto Sassu 		if (!rc) {
1836237fead6SMichael Halcrow 			found_auth_tok = 1;
1837f4aad16aSMichael Halcrow 			goto found_matching_auth_tok;
1838237fead6SMichael Halcrow 		}
1839237fead6SMichael Halcrow 	}
1840237fead6SMichael Halcrow 	if (!found_auth_tok) {
1841f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Could not find a usable "
1842f4aad16aSMichael Halcrow 				"authentication token\n");
1843237fead6SMichael Halcrow 		rc = -EIO;
1844237fead6SMichael Halcrow 		goto out_wipe_list;
1845dddfa461SMichael Halcrow 	}
1846f4aad16aSMichael Halcrow found_matching_auth_tok:
1847e2bd99ecSMichael Halcrow 	if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1848dddfa461SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.private_key),
1849f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.private_key),
1850dddfa461SMichael Halcrow 		       sizeof(struct ecryptfs_private_key));
1851f4aad16aSMichael Halcrow 		rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
1852dddfa461SMichael Halcrow 						       crypt_stat);
1853dddfa461SMichael Halcrow 	} else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
1854237fead6SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.password),
1855f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.password),
1856237fead6SMichael Halcrow 		       sizeof(struct ecryptfs_password));
1857f4aad16aSMichael Halcrow 		rc = decrypt_passphrase_encrypted_session_key(
1858f4aad16aSMichael Halcrow 			candidate_auth_tok, crypt_stat);
1859dddfa461SMichael Halcrow 	}
1860237fead6SMichael Halcrow 	if (rc) {
1861f4aad16aSMichael Halcrow 		struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1862f4aad16aSMichael Halcrow 
1863f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1864f4aad16aSMichael Halcrow 				"session key for authentication token with sig "
1865f4aad16aSMichael Halcrow 				"[%.*s]; rc = [%d]. Removing auth tok "
1866f4aad16aSMichael Halcrow 				"candidate from the list and searching for "
1867888d57bbSJoe Perches 				"the next match.\n", ECRYPTFS_SIG_SIZE_HEX,
1868888d57bbSJoe Perches 				candidate_auth_tok_sig,	rc);
1869f4aad16aSMichael Halcrow 		list_for_each_entry_safe(auth_tok_list_item,
1870f4aad16aSMichael Halcrow 					 auth_tok_list_item_tmp,
1871f4aad16aSMichael Halcrow 					 &auth_tok_list, list) {
1872f4aad16aSMichael Halcrow 			if (candidate_auth_tok
1873f4aad16aSMichael Halcrow 			    == &auth_tok_list_item->auth_tok) {
1874f4aad16aSMichael Halcrow 				list_del(&auth_tok_list_item->list);
1875f4aad16aSMichael Halcrow 				kmem_cache_free(
1876f4aad16aSMichael Halcrow 					ecryptfs_auth_tok_list_item_cache,
1877f4aad16aSMichael Halcrow 					auth_tok_list_item);
1878f4aad16aSMichael Halcrow 				goto find_next_matching_auth_tok;
1879f4aad16aSMichael Halcrow 			}
1880f4aad16aSMichael Halcrow 		}
1881f4aad16aSMichael Halcrow 		BUG();
1882237fead6SMichael Halcrow 	}
1883237fead6SMichael Halcrow 	rc = ecryptfs_compute_root_iv(crypt_stat);
1884237fead6SMichael Halcrow 	if (rc) {
1885237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error computing "
1886237fead6SMichael Halcrow 				"the root IV\n");
1887237fead6SMichael Halcrow 		goto out_wipe_list;
1888237fead6SMichael Halcrow 	}
1889237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1890237fead6SMichael Halcrow 	if (rc) {
1891237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1892237fead6SMichael Halcrow 				"context for cipher [%s]; rc = [%d]\n",
1893237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1894237fead6SMichael Halcrow 	}
1895237fead6SMichael Halcrow out_wipe_list:
1896237fead6SMichael Halcrow 	wipe_auth_tok_list(&auth_tok_list);
1897237fead6SMichael Halcrow out:
1898aee683b9SRoberto Sassu 	if (auth_tok_key)
1899aee683b9SRoberto Sassu 		key_put(auth_tok_key);
1900237fead6SMichael Halcrow 	return rc;
1901237fead6SMichael Halcrow }
1902f4aad16aSMichael Halcrow 
1903dddfa461SMichael Halcrow static int
1904dddfa461SMichael Halcrow pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1905dddfa461SMichael Halcrow 			struct ecryptfs_crypt_stat *crypt_stat,
1906dddfa461SMichael Halcrow 			struct ecryptfs_key_record *key_rec)
1907dddfa461SMichael Halcrow {
1908dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx = NULL;
1909624ae528STyler Hicks 	char *payload = NULL;
1910624ae528STyler Hicks 	size_t payload_len;
1911dddfa461SMichael Halcrow 	struct ecryptfs_message *msg;
1912dddfa461SMichael Halcrow 	int rc;
1913dddfa461SMichael Halcrow 
1914dddfa461SMichael Halcrow 	rc = write_tag_66_packet(auth_tok->token.private_key.signature,
19159c79f34fSMichael Halcrow 				 ecryptfs_code_for_cipher_string(
19169c79f34fSMichael Halcrow 					 crypt_stat->cipher,
19179c79f34fSMichael Halcrow 					 crypt_stat->key_size),
1918624ae528STyler Hicks 				 crypt_stat, &payload, &payload_len);
1919dddfa461SMichael Halcrow 	if (rc) {
1920dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1921dddfa461SMichael Halcrow 		goto out;
1922dddfa461SMichael Halcrow 	}
1923624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1924dddfa461SMichael Halcrow 	if (rc) {
1925624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
1926624ae528STyler Hicks 				"ecryptfsd\n");
1927dddfa461SMichael Halcrow 		goto out;
1928dddfa461SMichael Halcrow 	}
1929dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1930dddfa461SMichael Halcrow 	if (rc) {
1931dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1932dddfa461SMichael Halcrow 				"from the user space daemon\n");
1933dddfa461SMichael Halcrow 		rc = -EIO;
1934dddfa461SMichael Halcrow 		goto out;
1935dddfa461SMichael Halcrow 	}
1936dddfa461SMichael Halcrow 	rc = parse_tag_67_packet(key_rec, msg);
1937dddfa461SMichael Halcrow 	if (rc)
1938dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1939dddfa461SMichael Halcrow 	kfree(msg);
1940dddfa461SMichael Halcrow out:
1941624ae528STyler Hicks 	kfree(payload);
1942dddfa461SMichael Halcrow 	return rc;
1943dddfa461SMichael Halcrow }
1944dddfa461SMichael Halcrow /**
1945dddfa461SMichael Halcrow  * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1946dddfa461SMichael Halcrow  * @dest: Buffer into which to write the packet
194722e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be writtn
194822e78fafSMichael Halcrow  * @auth_tok: The authentication token used for generating the tag 1 packet
194922e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
195022e78fafSMichael Halcrow  * @key_rec: The key record struct for the tag 1 packet
1951dddfa461SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
1952dddfa461SMichael Halcrow  *               up constituting the packet; set to zero on error
1953dddfa461SMichael Halcrow  *
1954dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
1955dddfa461SMichael Halcrow  */
1956dddfa461SMichael Halcrow static int
1957f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes,
1958f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
1959dddfa461SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
1960dddfa461SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
1961dddfa461SMichael Halcrow {
1962dddfa461SMichael Halcrow 	size_t i;
1963dddfa461SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
1964dddfa461SMichael Halcrow 	size_t packet_size_length;
1965f4aad16aSMichael Halcrow 	size_t max_packet_size;
1966dddfa461SMichael Halcrow 	int rc = 0;
1967dddfa461SMichael Halcrow 
1968dddfa461SMichael Halcrow 	(*packet_size) = 0;
1969dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1970dddfa461SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
1971dddfa461SMichael Halcrow 	encrypted_session_key_valid = 0;
1972dddfa461SMichael Halcrow 	for (i = 0; i < crypt_stat->key_size; i++)
1973dddfa461SMichael Halcrow 		encrypted_session_key_valid |=
1974dddfa461SMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
1975dddfa461SMichael Halcrow 	if (encrypted_session_key_valid) {
1976dddfa461SMichael Halcrow 		memcpy(key_rec->enc_key,
1977dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key,
1978dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key_size);
1979dddfa461SMichael Halcrow 		goto encrypted_session_key_set;
1980dddfa461SMichael Halcrow 	}
1981dddfa461SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
1982dddfa461SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
1983dddfa461SMichael Halcrow 			auth_tok->token.private_key.key_size;
1984dddfa461SMichael Halcrow 	rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1985dddfa461SMichael Halcrow 	if (rc) {
1986f66e883eSMichael Halcrow 		printk(KERN_ERR "Failed to encrypt session key via a key "
1987f66e883eSMichael Halcrow 		       "module; rc = [%d]\n", rc);
1988dddfa461SMichael Halcrow 		goto out;
1989dddfa461SMichael Halcrow 	}
1990dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1991dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1992dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1993dddfa461SMichael Halcrow 	}
1994dddfa461SMichael Halcrow encrypted_session_key_set:
1995f4aad16aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1996f4aad16aSMichael Halcrow 	 * packet tag 1 */
1997f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 1 identifier */
1998f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 1 packet size */
1999f4aad16aSMichael Halcrow 			   + 1                       /* Version */
2000f4aad16aSMichael Halcrow 			   + ECRYPTFS_SIG_SIZE       /* Key identifier */
2001f4aad16aSMichael Halcrow 			   + 1                       /* Cipher identifier */
2002f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
2003f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
2004f4aad16aSMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
200581acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
2006f4aad16aSMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
2007dddfa461SMichael Halcrow 		rc = -EINVAL;
2008dddfa461SMichael Halcrow 		goto out;
2009dddfa461SMichael Halcrow 	}
2010dddfa461SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
2011f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2012f66e883eSMichael Halcrow 					  (max_packet_size - 4),
2013dddfa461SMichael Halcrow 					  &packet_size_length);
2014dddfa461SMichael Halcrow 	if (rc) {
2015dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
2016dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
2017dddfa461SMichael Halcrow 		goto out;
2018dddfa461SMichael Halcrow 	}
2019dddfa461SMichael Halcrow 	(*packet_size) += packet_size_length;
2020dddfa461SMichael Halcrow 	dest[(*packet_size)++] = 0x03; /* version 3 */
2021dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
2022dddfa461SMichael Halcrow 	(*packet_size) += ECRYPTFS_SIG_SIZE;
2023dddfa461SMichael Halcrow 	dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
2024dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
2025dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
2026dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
2027dddfa461SMichael Halcrow out:
2028dddfa461SMichael Halcrow 	if (rc)
2029dddfa461SMichael Halcrow 		(*packet_size) = 0;
2030f4aad16aSMichael Halcrow 	else
2031f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
2032dddfa461SMichael Halcrow 	return rc;
2033dddfa461SMichael Halcrow }
2034237fead6SMichael Halcrow 
2035237fead6SMichael Halcrow /**
2036237fead6SMichael Halcrow  * write_tag_11_packet
2037237fead6SMichael Halcrow  * @dest: Target into which Tag 11 packet is to be written
203822e78fafSMichael Halcrow  * @remaining_bytes: Maximum packet length
2039237fead6SMichael Halcrow  * @contents: Byte array of contents to copy in
2040237fead6SMichael Halcrow  * @contents_length: Number of bytes in contents
2041237fead6SMichael Halcrow  * @packet_length: Length of the Tag 11 packet written; zero on error
2042237fead6SMichael Halcrow  *
2043237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2044237fead6SMichael Halcrow  */
2045237fead6SMichael Halcrow static int
204681acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
2047146a4606SMichael Halcrow 		    size_t contents_length, size_t *packet_length)
2048237fead6SMichael Halcrow {
2049237fead6SMichael Halcrow 	size_t packet_size_length;
2050146a4606SMichael Halcrow 	size_t max_packet_size;
2051dddfa461SMichael Halcrow 	int rc = 0;
2052237fead6SMichael Halcrow 
2053237fead6SMichael Halcrow 	(*packet_length) = 0;
2054146a4606SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
2055146a4606SMichael Halcrow 	 * packet tag 11 */
2056146a4606SMichael Halcrow 	max_packet_size = (1                   /* Tag 11 identifier */
2057146a4606SMichael Halcrow 			   + 3                 /* Max Tag 11 packet size */
2058146a4606SMichael Halcrow 			   + 1                 /* Binary format specifier */
2059146a4606SMichael Halcrow 			   + 1                 /* Filename length */
2060146a4606SMichael Halcrow 			   + 8                 /* Filename ("_CONSOLE") */
2061146a4606SMichael Halcrow 			   + 4                 /* Modification date */
2062146a4606SMichael Halcrow 			   + contents_length); /* Literal data */
2063146a4606SMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
2064146a4606SMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
206581acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
2066146a4606SMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
2067237fead6SMichael Halcrow 		rc = -EINVAL;
2068237fead6SMichael Halcrow 		goto out;
2069237fead6SMichael Halcrow 	}
2070237fead6SMichael Halcrow 	dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
2071f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
2072f66e883eSMichael Halcrow 					  (max_packet_size - 4),
2073f66e883eSMichael Halcrow 					  &packet_size_length);
2074237fead6SMichael Halcrow 	if (rc) {
2075146a4606SMichael Halcrow 		printk(KERN_ERR "Error generating tag 11 packet header; cannot "
2076146a4606SMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
2077237fead6SMichael Halcrow 		goto out;
2078237fead6SMichael Halcrow 	}
2079237fead6SMichael Halcrow 	(*packet_length) += packet_size_length;
2080146a4606SMichael Halcrow 	dest[(*packet_length)++] = 0x62; /* binary data format specifier */
2081237fead6SMichael Halcrow 	dest[(*packet_length)++] = 8;
2082237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
2083237fead6SMichael Halcrow 	(*packet_length) += 8;
2084237fead6SMichael Halcrow 	memset(&dest[(*packet_length)], 0x00, 4);
2085237fead6SMichael Halcrow 	(*packet_length) += 4;
2086237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], contents, contents_length);
2087237fead6SMichael Halcrow 	(*packet_length) += contents_length;
2088237fead6SMichael Halcrow  out:
2089237fead6SMichael Halcrow 	if (rc)
2090237fead6SMichael Halcrow 		(*packet_length) = 0;
2091146a4606SMichael Halcrow 	else
2092146a4606SMichael Halcrow 		(*remaining_bytes) -= (*packet_length);
2093237fead6SMichael Halcrow 	return rc;
2094237fead6SMichael Halcrow }
2095237fead6SMichael Halcrow 
2096237fead6SMichael Halcrow /**
2097237fead6SMichael Halcrow  * write_tag_3_packet
2098237fead6SMichael Halcrow  * @dest: Buffer into which to write the packet
209922e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be written
2100237fead6SMichael Halcrow  * @auth_tok: Authentication token
2101237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context
2102237fead6SMichael Halcrow  * @key_rec: encrypted key
2103237fead6SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
2104237fead6SMichael Halcrow  *               up constituting the packet; set to zero on error
2105237fead6SMichael Halcrow  *
2106237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2107237fead6SMichael Halcrow  */
2108237fead6SMichael Halcrow static int
2109f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes,
2110f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
2111237fead6SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
2112237fead6SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
2113237fead6SMichael Halcrow {
2114237fead6SMichael Halcrow 	size_t i;
2115237fead6SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
2116237fead6SMichael Halcrow 	char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
2117ac97b9f9SMichael Halcrow 	struct scatterlist dst_sg[2];
2118ac97b9f9SMichael Halcrow 	struct scatterlist src_sg[2];
2119237fead6SMichael Halcrow 	struct mutex *tfm_mutex = NULL;
212019e66a67STrevor Highland 	u8 cipher_code;
2121f4aad16aSMichael Halcrow 	size_t packet_size_length;
2122f4aad16aSMichael Halcrow 	size_t max_packet_size;
2123f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2124f4aad16aSMichael Halcrow 		crypt_stat->mount_crypt_stat;
21258bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
21268bba066fSMichael Halcrow 		.tfm = NULL,
21278bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
21288bba066fSMichael Halcrow 	};
21298bba066fSMichael Halcrow 	int rc = 0;
2130237fead6SMichael Halcrow 
2131237fead6SMichael Halcrow 	(*packet_size) = 0;
2132dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
2133237fead6SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
2134f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2135f4aad16aSMichael Halcrow 							crypt_stat->cipher);
2136f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
2137f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
2138f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
2139f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
2140f4aad16aSMichael Halcrow 		goto out;
2141237fead6SMichael Halcrow 	}
2142f4aad16aSMichael Halcrow 	if (mount_crypt_stat->global_default_cipher_key_size == 0) {
2143f4aad16aSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
2144f4aad16aSMichael Halcrow 
2145f4aad16aSMichael Halcrow 		printk(KERN_WARNING "No key size specified at mount; "
2146f4aad16aSMichael Halcrow 		       "defaulting to [%d]\n", alg->max_keysize);
2147f4aad16aSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size =
2148f4aad16aSMichael Halcrow 			alg->max_keysize;
2149f4aad16aSMichael Halcrow 	}
2150f4aad16aSMichael Halcrow 	if (crypt_stat->key_size == 0)
2151f4aad16aSMichael Halcrow 		crypt_stat->key_size =
2152f4aad16aSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
2153237fead6SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
2154237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
2155237fead6SMichael Halcrow 			crypt_stat->key_size;
2156237fead6SMichael Halcrow 	if (crypt_stat->key_size == 24
2157237fead6SMichael Halcrow 	    && strcmp("aes", crypt_stat->cipher) == 0) {
2158237fead6SMichael Halcrow 		memset((crypt_stat->key + 24), 0, 8);
2159237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size = 32;
2160f4aad16aSMichael Halcrow 	} else
2161f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
2162dddfa461SMichael Halcrow 	key_rec->enc_key_size =
2163237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
2164f4aad16aSMichael Halcrow 	encrypted_session_key_valid = 0;
2165f4aad16aSMichael Halcrow 	for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
2166f4aad16aSMichael Halcrow 		encrypted_session_key_valid |=
2167f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
2168f4aad16aSMichael Halcrow 	if (encrypted_session_key_valid) {
2169f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
2170f4aad16aSMichael Halcrow 				"using auth_tok->session_key.encrypted_key, "
2171*f24b3887STyler Hicks 				"where key_rec->enc_key_size = [%zd]\n",
2172f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2173f4aad16aSMichael Halcrow 		memcpy(key_rec->enc_key,
2174f4aad16aSMichael Halcrow 		       auth_tok->session_key.encrypted_key,
2175f4aad16aSMichael Halcrow 		       key_rec->enc_key_size);
2176f4aad16aSMichael Halcrow 		goto encrypted_session_key_set;
2177f4aad16aSMichael Halcrow 	}
2178dddfa461SMichael Halcrow 	if (auth_tok->token.password.flags &
2179dddfa461SMichael Halcrow 	    ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
2180237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Using previously generated "
2181237fead6SMichael Halcrow 				"session key encryption key of size [%d]\n",
2182237fead6SMichael Halcrow 				auth_tok->token.password.
2183237fead6SMichael Halcrow 				session_key_encryption_key_bytes);
2184237fead6SMichael Halcrow 		memcpy(session_key_encryption_key,
2185237fead6SMichael Halcrow 		       auth_tok->token.password.session_key_encryption_key,
2186237fead6SMichael Halcrow 		       crypt_stat->key_size);
2187237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
2188237fead6SMichael Halcrow 				"Cached session key " "encryption key: \n");
2189237fead6SMichael Halcrow 		if (ecryptfs_verbosity > 0)
2190237fead6SMichael Halcrow 			ecryptfs_dump_hex(session_key_encryption_key, 16);
2191237fead6SMichael Halcrow 	}
2192237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
2193237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
2194237fead6SMichael Halcrow 		ecryptfs_dump_hex(session_key_encryption_key, 16);
2195237fead6SMichael Halcrow 	}
21965dda6992SMichael Halcrow 	rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
2197ac97b9f9SMichael Halcrow 				 src_sg, 2);
2198ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
2199237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2200f4aad16aSMichael Halcrow 				"for crypt_stat session key; expected rc = 1; "
2201*f24b3887STyler Hicks 				"got rc = [%d]. key_rec->enc_key_size = [%zd]\n",
2202f4aad16aSMichael Halcrow 				rc, key_rec->enc_key_size);
2203237fead6SMichael Halcrow 		rc = -ENOMEM;
2204237fead6SMichael Halcrow 		goto out;
2205237fead6SMichael Halcrow 	}
22065dda6992SMichael Halcrow 	rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
2207ac97b9f9SMichael Halcrow 				 dst_sg, 2);
2208ac97b9f9SMichael Halcrow 	if (rc < 1 || rc > 2) {
2209237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2210f4aad16aSMichael Halcrow 				"for crypt_stat encrypted session key; "
2211f4aad16aSMichael Halcrow 				"expected rc = 1; got rc = [%d]. "
2212*f24b3887STyler Hicks 				"key_rec->enc_key_size = [%zd]\n", rc,
2213f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2214237fead6SMichael Halcrow 		rc = -ENOMEM;
2215237fead6SMichael Halcrow 		goto out;
2216237fead6SMichael Halcrow 	}
2217237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
22188bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
2219237fead6SMichael Halcrow 				     crypt_stat->key_size);
2220237fead6SMichael Halcrow 	if (rc < 0) {
2221237fead6SMichael Halcrow 		mutex_unlock(tfm_mutex);
2222237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
22238bba066fSMichael Halcrow 				"context; rc = [%d]\n", rc);
2224237fead6SMichael Halcrow 		goto out;
2225237fead6SMichael Halcrow 	}
2226237fead6SMichael Halcrow 	rc = 0;
2227*f24b3887STyler Hicks 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n",
2228237fead6SMichael Halcrow 			crypt_stat->key_size);
2229ac97b9f9SMichael Halcrow 	rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
2230237fead6SMichael Halcrow 				      (*key_rec).enc_key_size);
2231f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
22328bba066fSMichael Halcrow 	if (rc) {
22338bba066fSMichael Halcrow 		printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
22348bba066fSMichael Halcrow 		goto out;
22358bba066fSMichael Halcrow 	}
2236237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
2237f4aad16aSMichael Halcrow 	if (ecryptfs_verbosity > 0) {
2238*f24b3887STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n",
2239f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
2240dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key,
2241dddfa461SMichael Halcrow 				  key_rec->enc_key_size);
2242f4aad16aSMichael Halcrow 	}
2243237fead6SMichael Halcrow encrypted_session_key_set:
2244237fead6SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
2245237fead6SMichael Halcrow 	 * packet tag 3 */
2246f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 3 identifier */
2247f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 3 packet size */
2248f4aad16aSMichael Halcrow 			   + 1                       /* Version */
2249f4aad16aSMichael Halcrow 			   + 1                       /* Cipher code */
2250f4aad16aSMichael Halcrow 			   + 1                       /* S2K specifier */
2251f4aad16aSMichael Halcrow 			   + 1                       /* Hash identifier */
2252f4aad16aSMichael Halcrow 			   + ECRYPTFS_SALT_SIZE      /* Salt */
2253f4aad16aSMichael Halcrow 			   + 1                       /* Hash iterations */
2254f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
2255f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
225681acbcd6SAndrew Morton 		printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
225781acbcd6SAndrew Morton 		       "there are only [%td] available\n", max_packet_size,
2258f4aad16aSMichael Halcrow 		       (*remaining_bytes));
2259f4aad16aSMichael Halcrow 		rc = -EINVAL;
2260f4aad16aSMichael Halcrow 		goto out;
2261f4aad16aSMichael Halcrow 	}
2262237fead6SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
2263f4aad16aSMichael Halcrow 	/* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
2264f4aad16aSMichael Halcrow 	 * to get the number of octets in the actual Tag 3 packet */
2265f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2266f66e883eSMichael Halcrow 					  (max_packet_size - 4),
2267237fead6SMichael Halcrow 					  &packet_size_length);
2268237fead6SMichael Halcrow 	if (rc) {
2269f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error generating tag 3 packet header; cannot "
2270f4aad16aSMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
2271237fead6SMichael Halcrow 		goto out;
2272237fead6SMichael Halcrow 	}
2273237fead6SMichael Halcrow 	(*packet_size) += packet_size_length;
2274237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x04; /* version 4 */
2275f4aad16aSMichael Halcrow 	/* TODO: Break from RFC2440 so that arbitrary ciphers can be
2276f4aad16aSMichael Halcrow 	 * specified with strings */
22779c79f34fSMichael Halcrow 	cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
22789c79f34fSMichael Halcrow 						      crypt_stat->key_size);
2279237fead6SMichael Halcrow 	if (cipher_code == 0) {
2280237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
2281237fead6SMichael Halcrow 				"cipher [%s]\n", crypt_stat->cipher);
2282237fead6SMichael Halcrow 		rc = -EINVAL;
2283237fead6SMichael Halcrow 		goto out;
2284237fead6SMichael Halcrow 	}
2285237fead6SMichael Halcrow 	dest[(*packet_size)++] = cipher_code;
2286237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x03;	/* S2K */
2287237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x01;	/* MD5 (TODO: parameterize) */
2288237fead6SMichael Halcrow 	memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
2289237fead6SMichael Halcrow 	       ECRYPTFS_SALT_SIZE);
2290237fead6SMichael Halcrow 	(*packet_size) += ECRYPTFS_SALT_SIZE;	/* salt */
2291237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x60;	/* hash iterations (65536) */
2292dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
2293dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
2294dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
2295237fead6SMichael Halcrow out:
2296237fead6SMichael Halcrow 	if (rc)
2297237fead6SMichael Halcrow 		(*packet_size) = 0;
2298f4aad16aSMichael Halcrow 	else
2299f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
2300237fead6SMichael Halcrow 	return rc;
2301237fead6SMichael Halcrow }
2302237fead6SMichael Halcrow 
2303eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache;
2304eb95e7ffSMichael Halcrow 
2305237fead6SMichael Halcrow /**
2306237fead6SMichael Halcrow  * ecryptfs_generate_key_packet_set
230722e78fafSMichael Halcrow  * @dest_base: Virtual address from which to write the key record set
2308237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context from which the
2309237fead6SMichael Halcrow  *              authentication tokens will be retrieved
2310237fead6SMichael Halcrow  * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
2311237fead6SMichael Halcrow  *                   for the global parameters
2312237fead6SMichael Halcrow  * @len: The amount written
2313237fead6SMichael Halcrow  * @max: The maximum amount of data allowed to be written
2314237fead6SMichael Halcrow  *
2315237fead6SMichael Halcrow  * Generates a key packet set and writes it to the virtual address
2316237fead6SMichael Halcrow  * passed in.
2317237fead6SMichael Halcrow  *
2318237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
2319237fead6SMichael Halcrow  */
2320237fead6SMichael Halcrow int
2321237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base,
2322237fead6SMichael Halcrow 				 struct ecryptfs_crypt_stat *crypt_stat,
2323237fead6SMichael Halcrow 				 struct dentry *ecryptfs_dentry, size_t *len,
2324237fead6SMichael Halcrow 				 size_t max)
2325237fead6SMichael Halcrow {
2326237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
2327f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
2328237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2329237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(
2330237fead6SMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
2331237fead6SMichael Halcrow 	size_t written;
2332eb95e7ffSMichael Halcrow 	struct ecryptfs_key_record *key_rec;
2333f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig;
2334dddfa461SMichael Halcrow 	int rc = 0;
2335237fead6SMichael Halcrow 
2336237fead6SMichael Halcrow 	(*len) = 0;
2337f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
2338eb95e7ffSMichael Halcrow 	key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
2339eb95e7ffSMichael Halcrow 	if (!key_rec) {
2340eb95e7ffSMichael Halcrow 		rc = -ENOMEM;
2341eb95e7ffSMichael Halcrow 		goto out;
2342eb95e7ffSMichael Halcrow 	}
2343f4aad16aSMichael Halcrow 	list_for_each_entry(key_sig, &crypt_stat->keysig_list,
2344f4aad16aSMichael Halcrow 			    crypt_stat_list) {
2345f4aad16aSMichael Halcrow 		memset(key_rec, 0, sizeof(*key_rec));
2346f4aad16aSMichael Halcrow 		rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
2347f4aad16aSMichael Halcrow 							   mount_crypt_stat,
2348f4aad16aSMichael Halcrow 							   key_sig->keysig);
2349f4aad16aSMichael Halcrow 		if (rc) {
2350f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error attempting to get the global "
2351f4aad16aSMichael Halcrow 			       "auth_tok; rc = [%d]\n", rc);
2352f4aad16aSMichael Halcrow 			goto out_free;
2353f4aad16aSMichael Halcrow 		}
2354f4aad16aSMichael Halcrow 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
2355f4aad16aSMichael Halcrow 			printk(KERN_WARNING
2356f4aad16aSMichael Halcrow 			       "Skipping invalid auth tok with sig = [%s]\n",
2357f4aad16aSMichael Halcrow 			       global_auth_tok->sig);
2358f4aad16aSMichael Halcrow 			continue;
2359f4aad16aSMichael Halcrow 		}
2360f4aad16aSMichael Halcrow 		auth_tok = global_auth_tok->global_auth_tok;
2361237fead6SMichael Halcrow 		if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
2362237fead6SMichael Halcrow 			rc = write_tag_3_packet((dest_base + (*len)),
2363f4aad16aSMichael Halcrow 						&max, auth_tok,
2364eb95e7ffSMichael Halcrow 						crypt_stat, key_rec,
2365237fead6SMichael Halcrow 						&written);
2366237fead6SMichael Halcrow 			if (rc) {
2367237fead6SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
2368237fead6SMichael Halcrow 						"writing tag 3 packet\n");
2369eb95e7ffSMichael Halcrow 				goto out_free;
2370237fead6SMichael Halcrow 			}
2371237fead6SMichael Halcrow 			(*len) += written;
2372237fead6SMichael Halcrow 			/* Write auth tok signature packet */
2373f4aad16aSMichael Halcrow 			rc = write_tag_11_packet((dest_base + (*len)), &max,
2374f4aad16aSMichael Halcrow 						 key_rec->sig,
2375f4aad16aSMichael Halcrow 						 ECRYPTFS_SIG_SIZE, &written);
2376237fead6SMichael Halcrow 			if (rc) {
2377237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error writing "
2378237fead6SMichael Halcrow 						"auth tok signature packet\n");
2379eb95e7ffSMichael Halcrow 				goto out_free;
2380237fead6SMichael Halcrow 			}
2381237fead6SMichael Halcrow 			(*len) += written;
2382dddfa461SMichael Halcrow 		} else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
2383dddfa461SMichael Halcrow 			rc = write_tag_1_packet(dest_base + (*len),
2384f4aad16aSMichael Halcrow 						&max, auth_tok,
2385f4aad16aSMichael Halcrow 						crypt_stat, key_rec, &written);
2386dddfa461SMichael Halcrow 			if (rc) {
2387dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
2388dddfa461SMichael Halcrow 						"writing tag 1 packet\n");
2389eb95e7ffSMichael Halcrow 				goto out_free;
2390dddfa461SMichael Halcrow 			}
2391dddfa461SMichael Halcrow 			(*len) += written;
2392237fead6SMichael Halcrow 		} else {
2393237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Unsupported "
2394237fead6SMichael Halcrow 					"authentication token type\n");
2395237fead6SMichael Halcrow 			rc = -EINVAL;
2396eb95e7ffSMichael Halcrow 			goto out_free;
2397237fead6SMichael Halcrow 		}
2398f4aad16aSMichael Halcrow 	}
2399f4aad16aSMichael Halcrow 	if (likely(max > 0)) {
2400237fead6SMichael Halcrow 		dest_base[(*len)] = 0x00;
2401237fead6SMichael Halcrow 	} else {
2402237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
2403237fead6SMichael Halcrow 		rc = -EIO;
2404237fead6SMichael Halcrow 	}
2405eb95e7ffSMichael Halcrow out_free:
2406eb95e7ffSMichael Halcrow 	kmem_cache_free(ecryptfs_key_record_cache, key_rec);
2407237fead6SMichael Halcrow out:
2408237fead6SMichael Halcrow 	if (rc)
2409237fead6SMichael Halcrow 		(*len) = 0;
2410f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
2411237fead6SMichael Halcrow 	return rc;
2412237fead6SMichael Halcrow }
2413f4aad16aSMichael Halcrow 
2414f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache;
2415f4aad16aSMichael Halcrow 
2416f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
2417f4aad16aSMichael Halcrow {
2418f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *new_key_sig;
2419f4aad16aSMichael Halcrow 
2420f4aad16aSMichael Halcrow 	new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
2421f4aad16aSMichael Halcrow 	if (!new_key_sig) {
2422f4aad16aSMichael Halcrow 		printk(KERN_ERR
2423f4aad16aSMichael Halcrow 		       "Error allocating from ecryptfs_key_sig_cache\n");
2424aa06117fSRoland Dreier 		return -ENOMEM;
2425f4aad16aSMichael Halcrow 	}
2426f4aad16aSMichael Halcrow 	memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
2427aa06117fSRoland Dreier 	/* Caller must hold keysig_list_mutex */
2428f4aad16aSMichael Halcrow 	list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
2429aa06117fSRoland Dreier 
2430aa06117fSRoland Dreier 	return 0;
2431f4aad16aSMichael Halcrow }
2432f4aad16aSMichael Halcrow 
2433f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache;
2434f4aad16aSMichael Halcrow 
2435f4aad16aSMichael Halcrow int
2436f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
243784814d64STyler Hicks 			     char *sig, u32 global_auth_tok_flags)
2438f4aad16aSMichael Halcrow {
2439f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *new_auth_tok;
2440f4aad16aSMichael Halcrow 	int rc = 0;
2441f4aad16aSMichael Halcrow 
2442459e2164SEric Sandeen 	new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
2443f4aad16aSMichael Halcrow 					GFP_KERNEL);
2444f4aad16aSMichael Halcrow 	if (!new_auth_tok) {
2445f4aad16aSMichael Halcrow 		rc = -ENOMEM;
2446f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error allocating from "
2447f4aad16aSMichael Halcrow 		       "ecryptfs_global_auth_tok_cache\n");
2448f4aad16aSMichael Halcrow 		goto out;
2449f4aad16aSMichael Halcrow 	}
2450f4aad16aSMichael Halcrow 	memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
245184814d64STyler Hicks 	new_auth_tok->flags = global_auth_tok_flags;
2452f4aad16aSMichael Halcrow 	new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
2453f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
2454f4aad16aSMichael Halcrow 	list_add(&new_auth_tok->mount_crypt_stat_list,
2455f4aad16aSMichael Halcrow 		 &mount_crypt_stat->global_auth_tok_list);
2456f4aad16aSMichael Halcrow 	mount_crypt_stat->num_global_auth_toks++;
2457f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2458f4aad16aSMichael Halcrow out:
2459f4aad16aSMichael Halcrow 	return rc;
2460f4aad16aSMichael Halcrow }
2461f4aad16aSMichael Halcrow 
2462