2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
10 * Trevor S. Highland <trevor.highland@gmail.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <linux/string.h>
29 #include <linux/sched.h>
30 #include <linux/syscalls.h>
31 #include <linux/pagemap.h>
32 #include <linux/key.h>
33 #include <linux/random.h>
34 #include <linux/crypto.h>
35 #include <linux/scatterlist.h>
36 #include "ecryptfs_kernel.h"
39 * request_key returned an error instead of a valid key address;
40 * determine the type of error, make appropriate log entries, and
41 * return an error code.
43 int process_request_key_err(long err_code)
49 ecryptfs_printk(KERN_WARNING, "No key\n");
53 ecryptfs_printk(KERN_WARNING, "Key expired\n");
57 ecryptfs_printk(KERN_WARNING, "Key revoked\n");
61 ecryptfs_printk(KERN_WARNING, "Unknown error code: "
62 "[0x%.16x]\n", err_code);
70 * @data: Pointer to memory containing length at offset
71 * @size: This function writes the decoded size to this memory
72 * address; zero on error
73 * @length_size: The number of bytes occupied by the encoded length
75 * Returns Zero on success
77 static int parse_packet_length(unsigned char *data, size_t *size,
86 (*size) = (unsigned char)data[0];
88 } else if (data[0] < 224) {
90 (*size) = (((unsigned char)(data[0]) - 192) * 256);
91 (*size) += ((unsigned char)(data[1]) + 192);
93 } else if (data[0] == 255) {
94 /* Five-byte length; we're not supposed to see this */
95 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
100 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
109 * write_packet_length
110 * @dest: The byte array target into which to write the
111 * length. Must have at least 5 bytes allocated.
112 * @size: The length to write.
113 * @packet_size_length: The number of bytes used to encode the
114 * packet length is written to this address.
116 * Returns zero on success; non-zero on error.
118 static int write_packet_length(char *dest, size_t size,
119 size_t *packet_size_length)
125 (*packet_size_length) = 1;
126 } else if (size < 65536) {
127 dest[0] = (((size - 192) / 256) + 192);
128 dest[1] = ((size - 192) % 256);
129 (*packet_size_length) = 2;
132 ecryptfs_printk(KERN_WARNING,
133 "Unsupported packet size: [%d]\n", size);
139 write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
140 char **packet, size_t *packet_len)
144 size_t packet_size_len;
149 * ***** TAG 64 Packet Format *****
150 * | Content Type | 1 byte |
151 * | Key Identifier Size | 1 or 2 bytes |
152 * | Key Identifier | arbitrary |
153 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
154 * | Encrypted File Encryption Key | arbitrary |
156 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
157 + session_key->encrypted_key_size);
158 *packet = kmalloc(data_len, GFP_KERNEL);
161 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
165 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
166 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
169 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
170 "header; cannot generate packet length\n");
173 i += packet_size_len;
174 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
175 i += ECRYPTFS_SIG_SIZE_HEX;
176 rc = write_packet_length(&message[i], session_key->encrypted_key_size,
179 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
180 "header; cannot generate packet length\n");
183 i += packet_size_len;
184 memcpy(&message[i], session_key->encrypted_key,
185 session_key->encrypted_key_size);
186 i += session_key->encrypted_key_size;
193 parse_tag_65_packet(struct ecryptfs_session_key *session_key, u16 *cipher_code,
194 struct ecryptfs_message *msg)
202 u16 expected_checksum = 0;
206 * ***** TAG 65 Packet Format *****
207 * | Content Type | 1 byte |
208 * | Status Indicator | 1 byte |
209 * | File Encryption Key Size | 1 or 2 bytes |
210 * | File Encryption Key | arbitrary |
212 message_len = msg->data_len;
214 if (message_len < 4) {
218 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
219 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
224 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
225 "[%d]\n", data[i-1]);
229 rc = parse_packet_length(&data[i], &m_size, &data_len);
231 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
236 if (message_len < (i + m_size)) {
237 ecryptfs_printk(KERN_ERR, "The received netlink message is "
238 "shorter than expected\n");
243 ecryptfs_printk(KERN_ERR,
244 "The decrypted key is not long enough to "
245 "include a cipher code and checksum\n");
249 *cipher_code = data[i++];
250 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
251 session_key->decrypted_key_size = m_size - 3;
252 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
253 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
254 "the maximum key size [%d]\n",
255 session_key->decrypted_key_size,
256 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
260 memcpy(session_key->decrypted_key, &data[i],
261 session_key->decrypted_key_size);
262 i += session_key->decrypted_key_size;
263 expected_checksum += (unsigned char)(data[i++]) << 8;
264 expected_checksum += (unsigned char)(data[i++]);
265 for (i = 0; i < session_key->decrypted_key_size; i++)
266 checksum += session_key->decrypted_key[i];
267 if (expected_checksum != checksum) {
268 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
269 "encryption key; expected [%x]; calculated "
270 "[%x]\n", expected_checksum, checksum);
279 write_tag_66_packet(char *signature, size_t cipher_code,
280 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
287 size_t packet_size_len;
292 * ***** TAG 66 Packet Format *****
293 * | Content Type | 1 byte |
294 * | Key Identifier Size | 1 or 2 bytes |
295 * | Key Identifier | arbitrary |
296 * | File Encryption Key Size | 1 or 2 bytes |
297 * | File Encryption Key | arbitrary |
299 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
300 *packet = kmalloc(data_len, GFP_KERNEL);
303 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
307 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
308 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
311 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
312 "header; cannot generate packet length\n");
315 i += packet_size_len;
316 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
317 i += ECRYPTFS_SIG_SIZE_HEX;
318 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
319 rc = write_packet_length(&message[i], crypt_stat->key_size + 3,
322 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
323 "header; cannot generate packet length\n");
326 i += packet_size_len;
327 message[i++] = cipher_code;
328 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
329 i += crypt_stat->key_size;
330 for (j = 0; j < crypt_stat->key_size; j++)
331 checksum += crypt_stat->key[j];
332 message[i++] = (checksum / 256) % 256;
333 message[i++] = (checksum % 256);
340 parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
341 struct ecryptfs_message *msg)
350 * ***** TAG 65 Packet Format *****
351 * | Content Type | 1 byte |
352 * | Status Indicator | 1 byte |
353 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
354 * | Encrypted File Encryption Key | arbitrary |
356 message_len = msg->data_len;
358 /* verify that everything through the encrypted FEK size is present */
359 if (message_len < 4) {
363 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
364 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_67\n");
369 ecryptfs_printk(KERN_ERR, "Status indicator has non zero value"
370 " [%d]\n", data[i-1]);
374 rc = parse_packet_length(&data[i], &key_rec->enc_key_size, &data_len);
376 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
381 if (message_len < (i + key_rec->enc_key_size)) {
382 ecryptfs_printk(KERN_ERR, "message_len [%d]; max len is [%d]\n",
383 message_len, (i + key_rec->enc_key_size));
387 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
388 ecryptfs_printk(KERN_ERR, "Encrypted key_size [%d] larger than "
389 "the maximum key size [%d]\n",
390 key_rec->enc_key_size,
391 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
395 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
401 * decrypt_pki_encrypted_session_key - Decrypt the session key with
402 * the given auth_tok.
404 * Returns Zero on success; non-zero error otherwise.
406 static int decrypt_pki_encrypted_session_key(
407 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
408 struct ecryptfs_auth_tok *auth_tok,
409 struct ecryptfs_crypt_stat *crypt_stat)
412 struct ecryptfs_msg_ctx *msg_ctx;
413 struct ecryptfs_message *msg = NULL;
414 char *netlink_message;
415 size_t netlink_message_length;
418 rc = write_tag_64_packet(mount_crypt_stat->global_auth_tok_sig,
419 &(auth_tok->session_key),
420 &netlink_message, &netlink_message_length);
422 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
425 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
426 netlink_message_length, &msg_ctx);
428 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
431 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
433 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
434 "from the user space daemon\n");
438 rc = parse_tag_65_packet(&(auth_tok->session_key),
441 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
445 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
446 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
447 auth_tok->session_key.decrypted_key_size);
448 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
449 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
451 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
455 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
456 if (ecryptfs_verbosity > 0) {
457 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
458 ecryptfs_dump_hex(crypt_stat->key,
459 crypt_stat->key_size);
467 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
469 struct list_head *walker;
470 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
472 walker = auth_tok_list_head->next;
473 while (walker != auth_tok_list_head) {
475 list_entry(walker, struct ecryptfs_auth_tok_list_item,
477 walker = auth_tok_list_item->list.next;
478 memset(auth_tok_list_item, 0,
479 sizeof(struct ecryptfs_auth_tok_list_item));
480 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
483 auth_tok_list_head->next = NULL;
486 struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
491 * @crypt_stat: The cryptographic context to modify based on packet
493 * @data: The raw bytes of the packet.
494 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
495 * a new authentication token will be placed at the end
496 * of this list for this packet.
497 * @new_auth_tok: Pointer to a pointer to memory that this function
498 * allocates; sets the memory address of the pointer to
499 * NULL on error. This object is added to the
501 * @packet_size: This function writes the size of the parsed packet
502 * into this memory location; zero on error.
504 * Returns zero on success; non-zero on error.
507 parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
508 unsigned char *data, struct list_head *auth_tok_list,
509 struct ecryptfs_auth_tok **new_auth_tok,
510 size_t *packet_size, size_t max_packet_size)
513 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
518 (*new_auth_tok) = NULL;
521 * one byte for the Tag 1 ID flag
522 * two bytes for the body size
523 * do not exceed the maximum_packet_size
525 if (unlikely((*packet_size) + 3 > max_packet_size)) {
526 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
530 /* check for Tag 1 identifier - one byte */
531 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
532 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
533 ECRYPTFS_TAG_1_PACKET_TYPE);
537 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
538 * at end of function upon failure */
540 kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache,
542 if (!auth_tok_list_item) {
543 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
547 memset(auth_tok_list_item, 0,
548 sizeof(struct ecryptfs_auth_tok_list_item));
549 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
550 /* check for body size - one to two bytes
552 * ***** TAG 1 Packet Format *****
553 * | version number | 1 byte |
554 * | key ID | 8 bytes |
555 * | public key algorithm | 1 byte |
556 * | encrypted session key | arbitrary |
558 rc = parse_packet_length(&data[(*packet_size)], &body_size,
561 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
565 if (unlikely(body_size < (0x02 + ECRYPTFS_SIG_SIZE))) {
566 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
571 (*packet_size) += length_size;
572 if (unlikely((*packet_size) + body_size > max_packet_size)) {
573 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
577 /* Version 3 (from RFC2440) - one byte */
578 if (unlikely(data[(*packet_size)++] != 0x03)) {
579 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
580 "[%d]\n", data[(*packet_size) - 1]);
585 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
586 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
587 *packet_size += ECRYPTFS_SIG_SIZE;
588 /* This byte is skipped because the kernel does not need to
589 * know which public key encryption algorithm was used */
591 (*new_auth_tok)->session_key.encrypted_key_size =
592 body_size - (0x02 + ECRYPTFS_SIG_SIZE);
593 if ((*new_auth_tok)->session_key.encrypted_key_size
594 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
595 ecryptfs_printk(KERN_ERR, "Tag 1 packet contains key larger "
596 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
600 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
601 (*new_auth_tok)->session_key.encrypted_key_size);
602 memcpy((*new_auth_tok)->session_key.encrypted_key,
603 &data[(*packet_size)], (body_size - 0x02 - ECRYPTFS_SIG_SIZE));
604 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
605 (*new_auth_tok)->session_key.flags &=
606 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
607 (*new_auth_tok)->session_key.flags |=
608 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
609 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
610 ECRYPTFS_SET_FLAG((*new_auth_tok)->flags, ECRYPTFS_PRIVATE_KEY);
611 /* TODO: Why are we setting this flag here? Don't we want the
612 * userspace to decrypt the session key? */
613 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
614 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
615 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
616 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
617 list_add(&auth_tok_list_item->list, auth_tok_list);
620 (*new_auth_tok) = NULL;
621 memset(auth_tok_list_item, 0,
622 sizeof(struct ecryptfs_auth_tok_list_item));
623 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
633 * @crypt_stat: The cryptographic context to modify based on packet
635 * @data: The raw bytes of the packet.
636 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
637 * a new authentication token will be placed at the end
638 * of this list for this packet.
639 * @new_auth_tok: Pointer to a pointer to memory that this function
640 * allocates; sets the memory address of the pointer to
641 * NULL on error. This object is added to the
643 * @packet_size: This function writes the size of the parsed packet
644 * into this memory location; zero on error.
645 * @max_packet_size: maximum number of bytes to parse
647 * Returns zero on success; non-zero on error.
650 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
651 unsigned char *data, struct list_head *auth_tok_list,
652 struct ecryptfs_auth_tok **new_auth_tok,
653 size_t *packet_size, size_t max_packet_size)
656 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
661 (*new_auth_tok) = NULL;
664 * one byte for the Tag 3 ID flag
665 * two bytes for the body size
666 * do not exceed the maximum_packet_size
668 if (unlikely((*packet_size) + 3 > max_packet_size)) {
669 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
674 /* check for Tag 3 identifyer - one byte */
675 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
676 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
677 ECRYPTFS_TAG_3_PACKET_TYPE);
681 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
682 * at end of function upon failure */
684 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
685 if (!auth_tok_list_item) {
686 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
690 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
692 /* check for body size - one to two bytes */
693 rc = parse_packet_length(&data[(*packet_size)], &body_size,
696 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
700 if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
701 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
706 (*packet_size) += length_size;
708 /* now we know the length of the remainting Tag 3 packet size:
709 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
710 * number of hash iterations
711 * ECRYPTFS_SALT_SIZE bytes for salt
712 * body_size bytes minus the stuff above is the encrypted key size
714 if (unlikely((*packet_size) + body_size > max_packet_size)) {
715 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
720 /* There are 5 characters of additional information in the
722 (*new_auth_tok)->session_key.encrypted_key_size =
723 body_size - (0x05 + ECRYPTFS_SALT_SIZE);
724 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
725 (*new_auth_tok)->session_key.encrypted_key_size);
727 /* Version 4 (from RFC2440) - one byte */
728 if (unlikely(data[(*packet_size)++] != 0x04)) {
729 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
730 "[%d]\n", data[(*packet_size) - 1]);
735 /* cipher - one byte */
736 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
737 (u16)data[(*packet_size)]);
738 /* A little extra work to differentiate among the AES key
739 * sizes; see RFC2440 */
740 switch(data[(*packet_size)++]) {
741 case RFC2440_CIPHER_AES_192:
742 crypt_stat->key_size = 24;
745 crypt_stat->key_size =
746 (*new_auth_tok)->session_key.encrypted_key_size;
748 ecryptfs_init_crypt_ctx(crypt_stat);
749 /* S2K identifier 3 (from RFC2440) */
750 if (unlikely(data[(*packet_size)++] != 0x03)) {
751 ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
757 /* TODO: finish the hash mapping */
758 /* hash algorithm - one byte */
759 switch (data[(*packet_size)++]) {
760 case 0x01: /* See RFC2440 for these numbers and their mappings */
762 /* salt - ECRYPTFS_SALT_SIZE bytes */
763 memcpy((*new_auth_tok)->token.password.salt,
764 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
765 (*packet_size) += ECRYPTFS_SALT_SIZE;
767 /* This conversion was taken straight from RFC2440 */
768 /* number of hash iterations - one byte */
769 (*new_auth_tok)->token.password.hash_iterations =
770 ((u32) 16 + (data[(*packet_size)] & 15))
771 << ((data[(*packet_size)] >> 4) + 6);
774 /* encrypted session key -
775 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
776 memcpy((*new_auth_tok)->session_key.encrypted_key,
777 &data[(*packet_size)],
778 (*new_auth_tok)->session_key.encrypted_key_size);
780 (*new_auth_tok)->session_key.encrypted_key_size;
781 (*new_auth_tok)->session_key.flags &=
782 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
783 (*new_auth_tok)->session_key.flags |=
784 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
785 (*new_auth_tok)->token.password.hash_algo = 0x01;
788 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
789 "[%d]\n", data[(*packet_size) - 1]);
793 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
794 /* TODO: Parametarize; we might actually want userspace to
795 * decrypt the session key. */
796 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
797 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
798 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
799 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
800 list_add(&auth_tok_list_item->list, auth_tok_list);
803 (*new_auth_tok) = NULL;
804 memset(auth_tok_list_item, 0,
805 sizeof(struct ecryptfs_auth_tok_list_item));
806 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
815 * parse_tag_11_packet
816 * @data: The raw bytes of the packet
817 * @contents: This function writes the data contents of the literal
818 * packet into this memory location
819 * @max_contents_bytes: The maximum number of bytes that this function
820 * is allowed to write into contents
821 * @tag_11_contents_size: This function writes the size of the parsed
822 * contents into this memory location; zero on
824 * @packet_size: This function writes the size of the parsed packet
825 * into this memory location; zero on error
826 * @max_packet_size: maximum number of bytes to parse
828 * Returns zero on success; non-zero on error.
831 parse_tag_11_packet(unsigned char *data, unsigned char *contents,
832 size_t max_contents_bytes, size_t *tag_11_contents_size,
833 size_t *packet_size, size_t max_packet_size)
840 (*tag_11_contents_size) = 0;
843 * one byte for the Tag 11 ID flag
844 * two bytes for the Tag 11 length
845 * do not exceed the maximum_packet_size
847 if (unlikely((*packet_size) + 3 > max_packet_size)) {
848 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
853 /* check for Tag 11 identifyer - one byte */
854 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
855 ecryptfs_printk(KERN_WARNING,
856 "Invalid tag 11 packet format\n");
861 /* get Tag 11 content length - one or two bytes */
862 rc = parse_packet_length(&data[(*packet_size)], &body_size,
865 ecryptfs_printk(KERN_WARNING,
866 "Invalid tag 11 packet format\n");
869 (*packet_size) += length_size;
871 if (body_size < 13) {
872 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
877 /* We have 13 bytes of surrounding packet values */
878 (*tag_11_contents_size) = (body_size - 13);
880 /* now we know the length of the remainting Tag 11 packet size:
881 * 14 fix bytes for: special flag one, special flag two,
883 * body_size bytes minus the stuff above is the Tag 11 content
885 /* FIXME why is the body size one byte smaller than the actual
887 * this seems to be an error here as well as in
888 * write_tag_11_packet() */
889 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
890 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
895 /* special flag one - one byte */
896 if (data[(*packet_size)++] != 0x62) {
897 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
902 /* special flag two - one byte */
903 if (data[(*packet_size)++] != 0x08) {
904 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
909 /* skip the next 12 bytes */
910 (*packet_size) += 12; /* We don't care about the filename or
913 /* get the Tag 11 contents - tag_11_contents_size bytes */
914 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
915 (*packet_size) += (*tag_11_contents_size);
920 (*tag_11_contents_size) = 0;
926 * decrypt_session_key - Decrypt the session key with the given auth_tok.
928 * Returns Zero on success; non-zero error otherwise.
930 static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
931 struct ecryptfs_crypt_stat *crypt_stat)
933 struct ecryptfs_password *password_s_ptr;
934 struct scatterlist src_sg[2], dst_sg[2];
935 struct mutex *tfm_mutex = NULL;
936 char *encrypted_session_key;
938 struct blkcipher_desc desc = {
939 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
943 password_s_ptr = &auth_tok->token.password;
944 if (ECRYPTFS_CHECK_FLAG(password_s_ptr->flags,
945 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))
946 ecryptfs_printk(KERN_DEBUG, "Session key encryption key "
947 "set; skipping key generation\n");
948 ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])"
950 password_s_ptr->session_key_encryption_key_bytes);
951 if (ecryptfs_verbosity > 0)
952 ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key,
954 session_key_encryption_key_bytes);
955 if (!strcmp(crypt_stat->cipher,
956 crypt_stat->mount_crypt_stat->global_default_cipher_name)
957 && crypt_stat->mount_crypt_stat->global_key_tfm) {
958 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
959 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
963 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
968 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
970 kfree(full_alg_name);
971 if (IS_ERR(desc.tfm)) {
972 rc = PTR_ERR(desc.tfm);
973 printk(KERN_ERR "Error allocating crypto context; "
977 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
980 mutex_lock(tfm_mutex);
981 rc = crypto_blkcipher_setkey(desc.tfm,
982 password_s_ptr->session_key_encryption_key,
983 crypt_stat->key_size);
985 printk(KERN_ERR "Error setting key for crypto context\n");
989 /* TODO: virt_to_scatterlist */
990 encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
991 if (!encrypted_session_key) {
992 ecryptfs_printk(KERN_ERR, "Out of memory\n");
996 session_key = (char *)__get_free_page(GFP_KERNEL);
998 kfree(encrypted_session_key);
999 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1003 memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
1004 auth_tok->session_key.encrypted_key_size);
1005 src_sg[0].page = virt_to_page(encrypted_session_key);
1006 src_sg[0].offset = 0;
1007 BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
1008 src_sg[0].length = auth_tok->session_key.encrypted_key_size;
1009 dst_sg[0].page = virt_to_page(session_key);
1010 dst_sg[0].offset = 0;
1011 auth_tok->session_key.decrypted_key_size =
1012 auth_tok->session_key.encrypted_key_size;
1013 dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
1014 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
1015 auth_tok->session_key.encrypted_key_size);
1017 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
1018 goto out_free_memory;
1020 auth_tok->session_key.decrypted_key_size =
1021 auth_tok->session_key.encrypted_key_size;
1022 memcpy(auth_tok->session_key.decrypted_key, session_key,
1023 auth_tok->session_key.decrypted_key_size);
1024 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1025 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1026 auth_tok->session_key.decrypted_key_size);
1027 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
1028 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
1029 if (ecryptfs_verbosity > 0)
1030 ecryptfs_dump_hex(crypt_stat->key,
1031 crypt_stat->key_size);
1033 memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
1034 free_page((unsigned long)encrypted_session_key);
1035 memset(session_key, 0, PAGE_CACHE_SIZE);
1036 free_page((unsigned long)session_key);
1039 mutex_unlock(tfm_mutex);
1041 crypto_free_blkcipher(desc.tfm);
1047 * ecryptfs_parse_packet_set
1048 * @dest: The header page in memory
1049 * @version: Version of file format, to guide parsing behavior
1051 * Get crypt_stat to have the file's session key if the requisite key
1052 * is available to decrypt the session key.
1054 * Returns Zero if a valid authentication token was retrieved and
1055 * processed; negative value for file not encrypted or for error
1058 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1060 struct dentry *ecryptfs_dentry)
1063 size_t found_auth_tok = 0;
1064 size_t next_packet_is_auth_tok_packet;
1065 char sig[ECRYPTFS_SIG_SIZE_HEX];
1066 struct list_head auth_tok_list;
1067 struct list_head *walker;
1068 struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
1069 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1070 &ecryptfs_superblock_to_private(
1071 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1072 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
1074 struct ecryptfs_auth_tok *new_auth_tok;
1075 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
1076 size_t tag_11_contents_size;
1077 size_t tag_11_packet_size;
1080 INIT_LIST_HEAD(&auth_tok_list);
1081 /* Parse the header to find as many packets as we can, these will be
1082 * added the our &auth_tok_list */
1083 next_packet_is_auth_tok_packet = 1;
1084 while (next_packet_is_auth_tok_packet) {
1085 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1088 case ECRYPTFS_TAG_3_PACKET_TYPE:
1089 rc = parse_tag_3_packet(crypt_stat,
1090 (unsigned char *)&src[i],
1091 &auth_tok_list, &new_auth_tok,
1092 &packet_size, max_packet_size);
1094 ecryptfs_printk(KERN_ERR, "Error parsing "
1100 rc = parse_tag_11_packet((unsigned char *)&src[i],
1103 &tag_11_contents_size,
1104 &tag_11_packet_size,
1107 ecryptfs_printk(KERN_ERR, "No valid "
1108 "(ecryptfs-specific) literal "
1109 "packet containing "
1110 "authentication token "
1111 "signature found after "
1116 i += tag_11_packet_size;
1117 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1118 ecryptfs_printk(KERN_ERR, "Expected "
1119 "signature of size [%d]; "
1122 tag_11_contents_size);
1126 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1127 sig_tmp_space, tag_11_contents_size);
1128 new_auth_tok->token.password.signature[
1129 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
1130 ECRYPTFS_SET_FLAG(crypt_stat->flags,
1131 ECRYPTFS_ENCRYPTED);
1133 case ECRYPTFS_TAG_1_PACKET_TYPE:
1134 rc = parse_tag_1_packet(crypt_stat,
1135 (unsigned char *)&src[i],
1136 &auth_tok_list, &new_auth_tok,
1137 &packet_size, max_packet_size);
1139 ecryptfs_printk(KERN_ERR, "Error parsing "
1145 ECRYPTFS_SET_FLAG(crypt_stat->flags,
1146 ECRYPTFS_ENCRYPTED);
1148 case ECRYPTFS_TAG_11_PACKET_TYPE:
1149 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1150 "(Tag 11 not allowed by itself)\n");
1155 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1156 "[%d] of the file header; hex value of "
1157 "character is [0x%.2x]\n", i, src[i]);
1158 next_packet_is_auth_tok_packet = 0;
1161 if (list_empty(&auth_tok_list)) {
1162 rc = -EINVAL; /* Do not support non-encrypted files in
1163 * the 0.1 release */
1166 /* If we have a global auth tok, then we should try to use
1168 if (mount_crypt_stat->global_auth_tok) {
1169 memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
1170 ECRYPTFS_SIG_SIZE_HEX);
1171 chosen_auth_tok = mount_crypt_stat->global_auth_tok;
1173 BUG(); /* We should always have a global auth tok in
1174 * the 0.1 release */
1175 /* Scan list to see if our chosen_auth_tok works */
1176 list_for_each(walker, &auth_tok_list) {
1177 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1178 auth_tok_list_item =
1179 list_entry(walker, struct ecryptfs_auth_tok_list_item,
1181 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1182 if (unlikely(ecryptfs_verbosity > 0)) {
1183 ecryptfs_printk(KERN_DEBUG,
1184 "Considering cadidate auth tok:\n");
1185 ecryptfs_dump_auth_tok(candidate_auth_tok);
1187 /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
1188 if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
1189 && !strncmp(candidate_auth_tok->token.password.signature,
1190 sig, ECRYPTFS_SIG_SIZE_HEX)) {
1193 /* TODO: Transfer the common salt into the
1194 * crypt_stat salt */
1195 } else if ((candidate_auth_tok->token_type
1196 == ECRYPTFS_PRIVATE_KEY)
1197 && !strncmp(candidate_auth_tok->token.private_key.signature,
1198 sig, ECRYPTFS_SIG_SIZE_HEX)) {
1203 if (!found_auth_tok) {
1204 ecryptfs_printk(KERN_ERR, "Could not find authentication "
1205 "token on temporary list for sig [%.*s]\n",
1206 ECRYPTFS_SIG_SIZE_HEX, sig);
1212 if ((ECRYPTFS_CHECK_FLAG(candidate_auth_tok->flags,
1213 ECRYPTFS_PRIVATE_KEY))) {
1214 memcpy(&(candidate_auth_tok->token.private_key),
1215 &(chosen_auth_tok->token.private_key),
1216 sizeof(struct ecryptfs_private_key));
1217 rc = decrypt_pki_encrypted_session_key(mount_crypt_stat,
1220 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
1221 memcpy(&(candidate_auth_tok->token.password),
1222 &(chosen_auth_tok->token.password),
1223 sizeof(struct ecryptfs_password));
1224 rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
1227 ecryptfs_printk(KERN_ERR, "Error decrypting the "
1228 "session key; rc = [%d]\n", rc);
1231 rc = ecryptfs_compute_root_iv(crypt_stat);
1233 ecryptfs_printk(KERN_ERR, "Error computing "
1237 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1239 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1240 "context for cipher [%s]; rc = [%d]\n",
1241 crypt_stat->cipher, rc);
1244 wipe_auth_tok_list(&auth_tok_list);
1249 pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1250 struct ecryptfs_crypt_stat *crypt_stat,
1251 struct ecryptfs_key_record *key_rec)
1253 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1254 char *netlink_payload;
1255 size_t netlink_payload_length;
1256 struct ecryptfs_message *msg;
1259 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1260 ecryptfs_code_for_cipher_string(crypt_stat),
1261 crypt_stat, &netlink_payload,
1262 &netlink_payload_length);
1264 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1267 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1268 netlink_payload_length, &msg_ctx);
1270 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1273 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1275 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1276 "from the user space daemon\n");
1280 rc = parse_tag_67_packet(key_rec, msg);
1282 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1285 if (netlink_payload)
1286 kfree(netlink_payload);
1290 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1291 * @dest: Buffer into which to write the packet
1292 * @max: Maximum number of bytes that can be writtn
1293 * @packet_size: This function will write the number of bytes that end
1294 * up constituting the packet; set to zero on error
1296 * Returns zero on success; non-zero on error.
1299 write_tag_1_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
1300 struct ecryptfs_crypt_stat *crypt_stat,
1301 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1302 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1305 size_t encrypted_session_key_valid = 0;
1306 size_t key_rec_size;
1307 size_t packet_size_length;
1311 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1313 encrypted_session_key_valid = 0;
1314 for (i = 0; i < crypt_stat->key_size; i++)
1315 encrypted_session_key_valid |=
1316 auth_tok->session_key.encrypted_key[i];
1317 if (encrypted_session_key_valid) {
1318 memcpy(key_rec->enc_key,
1319 auth_tok->session_key.encrypted_key,
1320 auth_tok->session_key.encrypted_key_size);
1321 goto encrypted_session_key_set;
1323 if (auth_tok->session_key.encrypted_key_size == 0)
1324 auth_tok->session_key.encrypted_key_size =
1325 auth_tok->token.private_key.key_size;
1326 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1328 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1332 if (ecryptfs_verbosity > 0) {
1333 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1334 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1336 encrypted_session_key_set:
1337 /* Now we have a valid key_rec. Append it to the
1339 key_rec_size = (sizeof(struct ecryptfs_key_record)
1340 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
1341 + (key_rec->enc_key_size));
1342 /* TODO: Include a packet size limit as a parameter to this
1343 * function once we have multi-packet headers (for versions
1345 if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
1346 ecryptfs_printk(KERN_ERR, "Keyset too large\n");
1350 /* ***** TAG 1 Packet Format *****
1351 * | version number | 1 byte |
1352 * | key ID | 8 bytes |
1353 * | public key algorithm | 1 byte |
1354 * | encrypted session key | arbitrary |
1356 if ((0x02 + ECRYPTFS_SIG_SIZE + key_rec->enc_key_size) >= max) {
1357 ecryptfs_printk(KERN_ERR,
1358 "Authentication token is too large\n");
1362 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
1363 /* This format is inspired by OpenPGP; see RFC 2440
1365 rc = write_packet_length(&dest[(*packet_size)],
1366 (0x02 + ECRYPTFS_SIG_SIZE +
1367 key_rec->enc_key_size),
1368 &packet_size_length);
1370 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1371 "header; cannot generate packet length\n");
1374 (*packet_size) += packet_size_length;
1375 dest[(*packet_size)++] = 0x03; /* version 3 */
1376 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1377 (*packet_size) += ECRYPTFS_SIG_SIZE;
1378 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1379 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1380 key_rec->enc_key_size);
1381 (*packet_size) += key_rec->enc_key_size;
1389 * write_tag_11_packet
1390 * @dest: Target into which Tag 11 packet is to be written
1391 * @max: Maximum packet length
1392 * @contents: Byte array of contents to copy in
1393 * @contents_length: Number of bytes in contents
1394 * @packet_length: Length of the Tag 11 packet written; zero on error
1396 * Returns zero on success; non-zero on error.
1399 write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
1400 size_t *packet_length)
1402 size_t packet_size_length;
1405 (*packet_length) = 0;
1406 if ((13 + contents_length) > max) {
1408 ecryptfs_printk(KERN_ERR, "Packet length larger than "
1409 "maximum allowable\n");
1412 /* General packet header */
1414 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
1416 rc = write_packet_length(&dest[(*packet_length)],
1417 (13 + contents_length), &packet_size_length);
1419 ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
1420 "header; cannot generate packet length\n");
1423 (*packet_length) += packet_size_length;
1424 /* Tag 11 specific */
1425 /* One-octet field that describes how the data is formatted */
1426 dest[(*packet_length)++] = 0x62; /* binary data */
1427 /* One-octet filename length followed by filename */
1428 dest[(*packet_length)++] = 8;
1429 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1430 (*packet_length) += 8;
1431 /* Four-octet number indicating modification date */
1432 memset(&dest[(*packet_length)], 0x00, 4);
1433 (*packet_length) += 4;
1434 /* Remainder is literal data */
1435 memcpy(&dest[(*packet_length)], contents, contents_length);
1436 (*packet_length) += contents_length;
1439 (*packet_length) = 0;
1444 * write_tag_3_packet
1445 * @dest: Buffer into which to write the packet
1446 * @max: Maximum number of bytes that can be written
1447 * @auth_tok: Authentication token
1448 * @crypt_stat: The cryptographic context
1449 * @key_rec: encrypted key
1450 * @packet_size: This function will write the number of bytes that end
1451 * up constituting the packet; set to zero on error
1453 * Returns zero on success; non-zero on error.
1456 write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
1457 struct ecryptfs_crypt_stat *crypt_stat,
1458 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1461 size_t encrypted_session_key_valid = 0;
1462 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
1463 struct scatterlist dest_sg[2];
1464 struct scatterlist src_sg[2];
1465 struct mutex *tfm_mutex = NULL;
1466 size_t key_rec_size;
1467 size_t packet_size_length;
1469 struct blkcipher_desc desc = {
1471 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1476 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
1478 encrypted_session_key_valid = 0;
1479 for (i = 0; i < crypt_stat->key_size; i++)
1480 encrypted_session_key_valid |=
1481 auth_tok->session_key.encrypted_key[i];
1482 if (encrypted_session_key_valid) {
1483 memcpy(key_rec->enc_key,
1484 auth_tok->session_key.encrypted_key,
1485 auth_tok->session_key.encrypted_key_size);
1486 goto encrypted_session_key_set;
1488 if (auth_tok->session_key.encrypted_key_size == 0)
1489 auth_tok->session_key.encrypted_key_size =
1490 crypt_stat->key_size;
1491 if (crypt_stat->key_size == 24
1492 && strcmp("aes", crypt_stat->cipher) == 0) {
1493 memset((crypt_stat->key + 24), 0, 8);
1494 auth_tok->session_key.encrypted_key_size = 32;
1496 key_rec->enc_key_size =
1497 auth_tok->session_key.encrypted_key_size;
1498 if (auth_tok->token.password.flags &
1499 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
1500 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1501 "session key encryption key of size [%d]\n",
1502 auth_tok->token.password.
1503 session_key_encryption_key_bytes);
1504 memcpy(session_key_encryption_key,
1505 auth_tok->token.password.session_key_encryption_key,
1506 crypt_stat->key_size);
1507 ecryptfs_printk(KERN_DEBUG,
1508 "Cached session key " "encryption key: \n");
1509 if (ecryptfs_verbosity > 0)
1510 ecryptfs_dump_hex(session_key_encryption_key, 16);
1512 if (unlikely(ecryptfs_verbosity > 0)) {
1513 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1514 ecryptfs_dump_hex(session_key_encryption_key, 16);
1516 rc = virt_to_scatterlist(crypt_stat->key,
1517 key_rec->enc_key_size, src_sg, 2);
1519 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
1520 "for crypt_stat session key\n");
1524 rc = virt_to_scatterlist(key_rec->enc_key,
1525 key_rec->enc_key_size, dest_sg, 2);
1527 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
1528 "for crypt_stat encrypted session key\n");
1532 if (!strcmp(crypt_stat->cipher,
1533 crypt_stat->mount_crypt_stat->global_default_cipher_name)
1534 && crypt_stat->mount_crypt_stat->global_key_tfm) {
1535 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
1536 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
1538 char *full_alg_name;
1540 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
1545 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
1547 kfree(full_alg_name);
1548 if (IS_ERR(desc.tfm)) {
1549 rc = PTR_ERR(desc.tfm);
1550 ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
1551 "context for cipher [%s]; rc = [%d]\n",
1552 crypt_stat->cipher, rc);
1555 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1558 mutex_lock(tfm_mutex);
1559 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1560 crypt_stat->key_size);
1563 mutex_unlock(tfm_mutex);
1564 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
1565 "context; rc = [%d]\n", rc);
1569 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1570 crypt_stat->key_size);
1571 rc = crypto_blkcipher_encrypt(&desc, dest_sg, src_sg,
1572 (*key_rec).enc_key_size);
1574 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1578 mutex_unlock(tfm_mutex);
1579 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
1580 if (ecryptfs_verbosity > 0)
1581 ecryptfs_dump_hex(key_rec->enc_key,
1582 key_rec->enc_key_size);
1583 encrypted_session_key_set:
1584 /* Now we have a valid key_rec. Append it to the
1586 key_rec_size = (sizeof(struct ecryptfs_key_record)
1587 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
1588 + (key_rec->enc_key_size));
1589 /* TODO: Include a packet size limit as a parameter to this
1590 * function once we have multi-packet headers (for versions
1592 if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
1593 ecryptfs_printk(KERN_ERR, "Keyset too large\n");
1597 /* TODO: Packet size limit */
1598 /* We have 5 bytes of surrounding packet data */
1599 if ((0x05 + ECRYPTFS_SALT_SIZE
1600 + key_rec->enc_key_size) >= max) {
1601 ecryptfs_printk(KERN_ERR, "Authentication token is too "
1606 /* This format is inspired by OpenPGP; see RFC 2440
1608 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
1609 /* ver+cipher+s2k+hash+salt+iter+enc_key */
1610 rc = write_packet_length(&dest[(*packet_size)],
1611 (0x05 + ECRYPTFS_SALT_SIZE
1612 + key_rec->enc_key_size),
1613 &packet_size_length);
1615 ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
1616 "header; cannot generate packet length\n");
1619 (*packet_size) += packet_size_length;
1620 dest[(*packet_size)++] = 0x04; /* version 4 */
1621 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1622 if (cipher_code == 0) {
1623 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1624 "cipher [%s]\n", crypt_stat->cipher);
1628 dest[(*packet_size)++] = cipher_code;
1629 dest[(*packet_size)++] = 0x03; /* S2K */
1630 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1631 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1632 ECRYPTFS_SALT_SIZE);
1633 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1634 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
1635 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1636 key_rec->enc_key_size);
1637 (*packet_size) += key_rec->enc_key_size;
1639 if (desc.tfm && !tfm_mutex)
1640 crypto_free_blkcipher(desc.tfm);
1647 * ecryptfs_generate_key_packet_set
1648 * @dest: Virtual address from which to write the key record set
1649 * @crypt_stat: The cryptographic context from which the
1650 * authentication tokens will be retrieved
1651 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1652 * for the global parameters
1653 * @len: The amount written
1654 * @max: The maximum amount of data allowed to be written
1656 * Generates a key packet set and writes it to the virtual address
1659 * Returns zero on success; non-zero on error.
1662 ecryptfs_generate_key_packet_set(char *dest_base,
1663 struct ecryptfs_crypt_stat *crypt_stat,
1664 struct dentry *ecryptfs_dentry, size_t *len,
1667 struct ecryptfs_auth_tok *auth_tok;
1668 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1669 &ecryptfs_superblock_to_private(
1670 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1672 struct ecryptfs_key_record key_rec;
1676 if (mount_crypt_stat->global_auth_tok) {
1677 auth_tok = mount_crypt_stat->global_auth_tok;
1678 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1679 rc = write_tag_3_packet((dest_base + (*len)),
1681 crypt_stat, &key_rec,
1684 ecryptfs_printk(KERN_WARNING, "Error "
1685 "writing tag 3 packet\n");
1689 /* Write auth tok signature packet */
1690 rc = write_tag_11_packet(
1691 (dest_base + (*len)),
1693 key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
1695 ecryptfs_printk(KERN_ERR, "Error writing "
1696 "auth tok signature packet\n");
1700 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1701 rc = write_tag_1_packet(dest_base + (*len),
1703 crypt_stat,mount_crypt_stat,
1704 &key_rec, &written);
1706 ecryptfs_printk(KERN_WARNING, "Error "
1707 "writing tag 1 packet\n");
1712 ecryptfs_printk(KERN_WARNING, "Unsupported "
1713 "authentication token type\n");
1719 if (likely((max - (*len)) > 0)) {
1720 dest_base[(*len)] = 0x00;
1722 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");