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/syscalls.h>
30 #include <linux/pagemap.h>
31 #include <linux/key.h>
32 #include <linux/random.h>
33 #include <linux/crypto.h>
34 #include <linux/scatterlist.h>
35 #include "ecryptfs_kernel.h"
38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and
40 * return an error code.
42 int process_request_key_err(long err_code
)
48 ecryptfs_printk(KERN_WARNING
, "No key\n");
52 ecryptfs_printk(KERN_WARNING
, "Key expired\n");
56 ecryptfs_printk(KERN_WARNING
, "Key revoked\n");
60 ecryptfs_printk(KERN_WARNING
, "Unknown error code: "
61 "[0x%.16x]\n", err_code
);
69 * @data: Pointer to memory containing length at offset
70 * @size: This function writes the decoded size to this memory
71 * address; zero on error
72 * @length_size: The number of bytes occupied by the encoded length
74 * Returns Zero on success
76 static int parse_packet_length(unsigned char *data
, size_t *size
,
85 (*size
) = (unsigned char)data
[0];
87 } else if (data
[0] < 224) {
89 (*size
) = (((unsigned char)(data
[0]) - 192) * 256);
90 (*size
) += ((unsigned char)(data
[1]) + 192);
92 } else if (data
[0] == 255) {
93 /* Five-byte length; we're not supposed to see this */
94 ecryptfs_printk(KERN_ERR
, "Five-byte packet length not "
99 ecryptfs_printk(KERN_ERR
, "Error parsing packet length\n");
108 * write_packet_length
109 * @dest: The byte array target into which to write the
110 * length. Must have at least 5 bytes allocated.
111 * @size: The length to write.
112 * @packet_size_length: The number of bytes used to encode the
113 * packet length is written to this address.
115 * Returns zero on success; non-zero on error.
117 static int write_packet_length(char *dest
, size_t size
,
118 size_t *packet_size_length
)
124 (*packet_size_length
) = 1;
125 } else if (size
< 65536) {
126 dest
[0] = (((size
- 192) / 256) + 192);
127 dest
[1] = ((size
- 192) % 256);
128 (*packet_size_length
) = 2;
131 ecryptfs_printk(KERN_WARNING
,
132 "Unsupported packet size: [%d]\n", size
);
138 write_tag_64_packet(char *signature
, struct ecryptfs_session_key
*session_key
,
139 char **packet
, size_t *packet_len
)
143 size_t packet_size_len
;
148 * ***** TAG 64 Packet Format *****
149 * | Content Type | 1 byte |
150 * | Key Identifier Size | 1 or 2 bytes |
151 * | Key Identifier | arbitrary |
152 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
153 * | Encrypted File Encryption Key | arbitrary |
155 data_len
= (5 + ECRYPTFS_SIG_SIZE_HEX
156 + session_key
->encrypted_key_size
);
157 *packet
= kmalloc(data_len
, GFP_KERNEL
);
160 ecryptfs_printk(KERN_ERR
, "Unable to allocate memory\n");
164 message
[i
++] = ECRYPTFS_TAG_64_PACKET_TYPE
;
165 rc
= write_packet_length(&message
[i
], ECRYPTFS_SIG_SIZE_HEX
,
168 ecryptfs_printk(KERN_ERR
, "Error generating tag 64 packet "
169 "header; cannot generate packet length\n");
172 i
+= packet_size_len
;
173 memcpy(&message
[i
], signature
, ECRYPTFS_SIG_SIZE_HEX
);
174 i
+= ECRYPTFS_SIG_SIZE_HEX
;
175 rc
= write_packet_length(&message
[i
], session_key
->encrypted_key_size
,
178 ecryptfs_printk(KERN_ERR
, "Error generating tag 64 packet "
179 "header; cannot generate packet length\n");
182 i
+= packet_size_len
;
183 memcpy(&message
[i
], session_key
->encrypted_key
,
184 session_key
->encrypted_key_size
);
185 i
+= session_key
->encrypted_key_size
;
192 parse_tag_65_packet(struct ecryptfs_session_key
*session_key
, u16
*cipher_code
,
193 struct ecryptfs_message
*msg
)
201 u16 expected_checksum
= 0;
205 * ***** TAG 65 Packet Format *****
206 * | Content Type | 1 byte |
207 * | Status Indicator | 1 byte |
208 * | File Encryption Key Size | 1 or 2 bytes |
209 * | File Encryption Key | arbitrary |
211 message_len
= msg
->data_len
;
213 if (message_len
< 4) {
217 if (data
[i
++] != ECRYPTFS_TAG_65_PACKET_TYPE
) {
218 ecryptfs_printk(KERN_ERR
, "Type should be ECRYPTFS_TAG_65\n");
223 ecryptfs_printk(KERN_ERR
, "Status indicator has non-zero value "
224 "[%d]\n", data
[i
-1]);
228 rc
= parse_packet_length(&data
[i
], &m_size
, &data_len
);
230 ecryptfs_printk(KERN_WARNING
, "Error parsing packet length; "
235 if (message_len
< (i
+ m_size
)) {
236 ecryptfs_printk(KERN_ERR
, "The received netlink message is "
237 "shorter than expected\n");
242 ecryptfs_printk(KERN_ERR
,
243 "The decrypted key is not long enough to "
244 "include a cipher code and checksum\n");
248 *cipher_code
= data
[i
++];
249 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
250 session_key
->decrypted_key_size
= m_size
- 3;
251 if (session_key
->decrypted_key_size
> ECRYPTFS_MAX_KEY_BYTES
) {
252 ecryptfs_printk(KERN_ERR
, "key_size [%d] larger than "
253 "the maximum key size [%d]\n",
254 session_key
->decrypted_key_size
,
255 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
);
259 memcpy(session_key
->decrypted_key
, &data
[i
],
260 session_key
->decrypted_key_size
);
261 i
+= session_key
->decrypted_key_size
;
262 expected_checksum
+= (unsigned char)(data
[i
++]) << 8;
263 expected_checksum
+= (unsigned char)(data
[i
++]);
264 for (i
= 0; i
< session_key
->decrypted_key_size
; i
++)
265 checksum
+= session_key
->decrypted_key
[i
];
266 if (expected_checksum
!= checksum
) {
267 ecryptfs_printk(KERN_ERR
, "Invalid checksum for file "
268 "encryption key; expected [%x]; calculated "
269 "[%x]\n", expected_checksum
, checksum
);
278 write_tag_66_packet(char *signature
, size_t cipher_code
,
279 struct ecryptfs_crypt_stat
*crypt_stat
, char **packet
,
286 size_t packet_size_len
;
291 * ***** TAG 66 Packet Format *****
292 * | Content Type | 1 byte |
293 * | Key Identifier Size | 1 or 2 bytes |
294 * | Key Identifier | arbitrary |
295 * | File Encryption Key Size | 1 or 2 bytes |
296 * | File Encryption Key | arbitrary |
298 data_len
= (5 + ECRYPTFS_SIG_SIZE_HEX
+ crypt_stat
->key_size
);
299 *packet
= kmalloc(data_len
, GFP_KERNEL
);
302 ecryptfs_printk(KERN_ERR
, "Unable to allocate memory\n");
306 message
[i
++] = ECRYPTFS_TAG_66_PACKET_TYPE
;
307 rc
= write_packet_length(&message
[i
], ECRYPTFS_SIG_SIZE_HEX
,
310 ecryptfs_printk(KERN_ERR
, "Error generating tag 66 packet "
311 "header; cannot generate packet length\n");
314 i
+= packet_size_len
;
315 memcpy(&message
[i
], signature
, ECRYPTFS_SIG_SIZE_HEX
);
316 i
+= ECRYPTFS_SIG_SIZE_HEX
;
317 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
318 rc
= write_packet_length(&message
[i
], crypt_stat
->key_size
+ 3,
321 ecryptfs_printk(KERN_ERR
, "Error generating tag 66 packet "
322 "header; cannot generate packet length\n");
325 i
+= packet_size_len
;
326 message
[i
++] = cipher_code
;
327 memcpy(&message
[i
], crypt_stat
->key
, crypt_stat
->key_size
);
328 i
+= crypt_stat
->key_size
;
329 for (j
= 0; j
< crypt_stat
->key_size
; j
++)
330 checksum
+= crypt_stat
->key
[j
];
331 message
[i
++] = (checksum
/ 256) % 256;
332 message
[i
++] = (checksum
% 256);
339 parse_tag_67_packet(struct ecryptfs_key_record
*key_rec
,
340 struct ecryptfs_message
*msg
)
349 * ***** TAG 65 Packet Format *****
350 * | Content Type | 1 byte |
351 * | Status Indicator | 1 byte |
352 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
353 * | Encrypted File Encryption Key | arbitrary |
355 message_len
= msg
->data_len
;
357 /* verify that everything through the encrypted FEK size is present */
358 if (message_len
< 4) {
362 if (data
[i
++] != ECRYPTFS_TAG_67_PACKET_TYPE
) {
363 ecryptfs_printk(KERN_ERR
, "Type should be ECRYPTFS_TAG_67\n");
368 ecryptfs_printk(KERN_ERR
, "Status indicator has non zero value"
369 " [%d]\n", data
[i
-1]);
373 rc
= parse_packet_length(&data
[i
], &key_rec
->enc_key_size
, &data_len
);
375 ecryptfs_printk(KERN_WARNING
, "Error parsing packet length; "
380 if (message_len
< (i
+ key_rec
->enc_key_size
)) {
381 ecryptfs_printk(KERN_ERR
, "message_len [%d]; max len is [%d]\n",
382 message_len
, (i
+ key_rec
->enc_key_size
));
386 if (key_rec
->enc_key_size
> ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
) {
387 ecryptfs_printk(KERN_ERR
, "Encrypted key_size [%d] larger than "
388 "the maximum key size [%d]\n",
389 key_rec
->enc_key_size
,
390 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
);
394 memcpy(key_rec
->enc_key
, &data
[i
], key_rec
->enc_key_size
);
400 * decrypt_pki_encrypted_session_key - Decrypt the session key with
401 * the given auth_tok.
403 * Returns Zero on success; non-zero error otherwise.
405 static int decrypt_pki_encrypted_session_key(
406 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
,
407 struct ecryptfs_auth_tok
*auth_tok
,
408 struct ecryptfs_crypt_stat
*crypt_stat
)
411 struct ecryptfs_msg_ctx
*msg_ctx
;
412 struct ecryptfs_message
*msg
= NULL
;
413 char *netlink_message
;
414 size_t netlink_message_length
;
417 rc
= write_tag_64_packet(mount_crypt_stat
->global_auth_tok_sig
,
418 &(auth_tok
->session_key
),
419 &netlink_message
, &netlink_message_length
);
421 ecryptfs_printk(KERN_ERR
, "Failed to write tag 64 packet");
424 rc
= ecryptfs_send_message(ecryptfs_transport
, netlink_message
,
425 netlink_message_length
, &msg_ctx
);
427 ecryptfs_printk(KERN_ERR
, "Error sending netlink message\n");
430 rc
= ecryptfs_wait_for_response(msg_ctx
, &msg
);
432 ecryptfs_printk(KERN_ERR
, "Failed to receive tag 65 packet "
433 "from the user space daemon\n");
437 rc
= parse_tag_65_packet(&(auth_tok
->session_key
),
440 printk(KERN_ERR
"Failed to parse tag 65 packet; rc = [%d]\n",
444 auth_tok
->session_key
.flags
|= ECRYPTFS_CONTAINS_DECRYPTED_KEY
;
445 memcpy(crypt_stat
->key
, auth_tok
->session_key
.decrypted_key
,
446 auth_tok
->session_key
.decrypted_key_size
);
447 crypt_stat
->key_size
= auth_tok
->session_key
.decrypted_key_size
;
448 rc
= ecryptfs_cipher_code_to_string(crypt_stat
->cipher
, cipher_code
);
450 ecryptfs_printk(KERN_ERR
, "Cipher code [%d] is invalid\n",
454 crypt_stat
->flags
|= ECRYPTFS_KEY_VALID
;
455 if (ecryptfs_verbosity
> 0) {
456 ecryptfs_printk(KERN_DEBUG
, "Decrypted session key:\n");
457 ecryptfs_dump_hex(crypt_stat
->key
,
458 crypt_stat
->key_size
);
466 static void wipe_auth_tok_list(struct list_head
*auth_tok_list_head
)
468 struct list_head
*walker
;
469 struct ecryptfs_auth_tok_list_item
*auth_tok_list_item
;
471 walker
= auth_tok_list_head
->next
;
472 while (walker
!= auth_tok_list_head
) {
474 list_entry(walker
, struct ecryptfs_auth_tok_list_item
,
476 walker
= auth_tok_list_item
->list
.next
;
477 memset(auth_tok_list_item
, 0,
478 sizeof(struct ecryptfs_auth_tok_list_item
));
479 kmem_cache_free(ecryptfs_auth_tok_list_item_cache
,
482 auth_tok_list_head
->next
= NULL
;
485 struct kmem_cache
*ecryptfs_auth_tok_list_item_cache
;
490 * @crypt_stat: The cryptographic context to modify based on packet
492 * @data: The raw bytes of the packet.
493 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
494 * a new authentication token will be placed at the end
495 * of this list for this packet.
496 * @new_auth_tok: Pointer to a pointer to memory that this function
497 * allocates; sets the memory address of the pointer to
498 * NULL on error. This object is added to the
500 * @packet_size: This function writes the size of the parsed packet
501 * into this memory location; zero on error.
503 * Returns zero on success; non-zero on error.
506 parse_tag_1_packet(struct ecryptfs_crypt_stat
*crypt_stat
,
507 unsigned char *data
, struct list_head
*auth_tok_list
,
508 struct ecryptfs_auth_tok
**new_auth_tok
,
509 size_t *packet_size
, size_t max_packet_size
)
512 struct ecryptfs_auth_tok_list_item
*auth_tok_list_item
;
517 (*new_auth_tok
) = NULL
;
520 * one byte for the Tag 1 ID flag
521 * two bytes for the body size
522 * do not exceed the maximum_packet_size
524 if (unlikely((*packet_size
) + 3 > max_packet_size
)) {
525 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
529 /* check for Tag 1 identifier - one byte */
530 if (data
[(*packet_size
)++] != ECRYPTFS_TAG_1_PACKET_TYPE
) {
531 ecryptfs_printk(KERN_ERR
, "Enter w/ first byte != 0x%.2x\n",
532 ECRYPTFS_TAG_1_PACKET_TYPE
);
536 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
537 * at end of function upon failure */
539 kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache
,
541 if (!auth_tok_list_item
) {
542 ecryptfs_printk(KERN_ERR
, "Unable to allocate memory\n");
546 memset(auth_tok_list_item
, 0,
547 sizeof(struct ecryptfs_auth_tok_list_item
));
548 (*new_auth_tok
) = &auth_tok_list_item
->auth_tok
;
549 /* check for body size - one to two bytes
551 * ***** TAG 1 Packet Format *****
552 * | version number | 1 byte |
553 * | key ID | 8 bytes |
554 * | public key algorithm | 1 byte |
555 * | encrypted session key | arbitrary |
557 rc
= parse_packet_length(&data
[(*packet_size
)], &body_size
,
560 ecryptfs_printk(KERN_WARNING
, "Error parsing packet length; "
564 if (unlikely(body_size
< (0x02 + ECRYPTFS_SIG_SIZE
))) {
565 ecryptfs_printk(KERN_WARNING
, "Invalid body size ([%d])\n",
570 (*packet_size
) += length_size
;
571 if (unlikely((*packet_size
) + body_size
> max_packet_size
)) {
572 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
576 /* Version 3 (from RFC2440) - one byte */
577 if (unlikely(data
[(*packet_size
)++] != 0x03)) {
578 ecryptfs_printk(KERN_DEBUG
, "Unknown version number "
579 "[%d]\n", data
[(*packet_size
) - 1]);
584 ecryptfs_to_hex((*new_auth_tok
)->token
.private_key
.signature
,
585 &data
[(*packet_size
)], ECRYPTFS_SIG_SIZE
);
586 *packet_size
+= ECRYPTFS_SIG_SIZE
;
587 /* This byte is skipped because the kernel does not need to
588 * know which public key encryption algorithm was used */
590 (*new_auth_tok
)->session_key
.encrypted_key_size
=
591 body_size
- (0x02 + ECRYPTFS_SIG_SIZE
);
592 if ((*new_auth_tok
)->session_key
.encrypted_key_size
593 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
) {
594 ecryptfs_printk(KERN_ERR
, "Tag 1 packet contains key larger "
595 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
599 ecryptfs_printk(KERN_DEBUG
, "Encrypted key size = [%d]\n",
600 (*new_auth_tok
)->session_key
.encrypted_key_size
);
601 memcpy((*new_auth_tok
)->session_key
.encrypted_key
,
602 &data
[(*packet_size
)], (body_size
- 0x02 - ECRYPTFS_SIG_SIZE
));
603 (*packet_size
) += (*new_auth_tok
)->session_key
.encrypted_key_size
;
604 (*new_auth_tok
)->session_key
.flags
&=
605 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY
;
606 (*new_auth_tok
)->session_key
.flags
|=
607 ECRYPTFS_CONTAINS_ENCRYPTED_KEY
;
608 (*new_auth_tok
)->token_type
= ECRYPTFS_PRIVATE_KEY
;
609 (*new_auth_tok
)->flags
|= ECRYPTFS_PRIVATE_KEY
;
610 /* TODO: Why are we setting this flag here? Don't we want the
611 * userspace to decrypt the session key? */
612 (*new_auth_tok
)->session_key
.flags
&=
613 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT
);
614 (*new_auth_tok
)->session_key
.flags
&=
615 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT
);
616 list_add(&auth_tok_list_item
->list
, auth_tok_list
);
619 (*new_auth_tok
) = NULL
;
620 memset(auth_tok_list_item
, 0,
621 sizeof(struct ecryptfs_auth_tok_list_item
));
622 kmem_cache_free(ecryptfs_auth_tok_list_item_cache
,
632 * @crypt_stat: The cryptographic context to modify based on packet
634 * @data: The raw bytes of the packet.
635 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
636 * a new authentication token will be placed at the end
637 * of this list for this packet.
638 * @new_auth_tok: Pointer to a pointer to memory that this function
639 * allocates; sets the memory address of the pointer to
640 * NULL on error. This object is added to the
642 * @packet_size: This function writes the size of the parsed packet
643 * into this memory location; zero on error.
644 * @max_packet_size: maximum number of bytes to parse
646 * Returns zero on success; non-zero on error.
649 parse_tag_3_packet(struct ecryptfs_crypt_stat
*crypt_stat
,
650 unsigned char *data
, struct list_head
*auth_tok_list
,
651 struct ecryptfs_auth_tok
**new_auth_tok
,
652 size_t *packet_size
, size_t max_packet_size
)
655 struct ecryptfs_auth_tok_list_item
*auth_tok_list_item
;
660 (*new_auth_tok
) = NULL
;
663 * one byte for the Tag 3 ID flag
664 * two bytes for the body size
665 * do not exceed the maximum_packet_size
667 if (unlikely((*packet_size
) + 3 > max_packet_size
)) {
668 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
673 /* check for Tag 3 identifyer - one byte */
674 if (data
[(*packet_size
)++] != ECRYPTFS_TAG_3_PACKET_TYPE
) {
675 ecryptfs_printk(KERN_ERR
, "Enter w/ first byte != 0x%.2x\n",
676 ECRYPTFS_TAG_3_PACKET_TYPE
);
680 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
681 * at end of function upon failure */
683 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache
, GFP_KERNEL
);
684 if (!auth_tok_list_item
) {
685 ecryptfs_printk(KERN_ERR
, "Unable to allocate memory\n");
689 (*new_auth_tok
) = &auth_tok_list_item
->auth_tok
;
691 /* check for body size - one to two bytes */
692 rc
= parse_packet_length(&data
[(*packet_size
)], &body_size
,
695 ecryptfs_printk(KERN_WARNING
, "Error parsing packet length; "
699 if (unlikely(body_size
< (0x05 + ECRYPTFS_SALT_SIZE
))) {
700 ecryptfs_printk(KERN_WARNING
, "Invalid body size ([%d])\n",
705 (*packet_size
) += length_size
;
707 /* now we know the length of the remainting Tag 3 packet size:
708 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
709 * number of hash iterations
710 * ECRYPTFS_SALT_SIZE bytes for salt
711 * body_size bytes minus the stuff above is the encrypted key size
713 if (unlikely((*packet_size
) + body_size
> max_packet_size
)) {
714 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
719 /* There are 5 characters of additional information in the
721 (*new_auth_tok
)->session_key
.encrypted_key_size
=
722 body_size
- (0x05 + ECRYPTFS_SALT_SIZE
);
723 ecryptfs_printk(KERN_DEBUG
, "Encrypted key size = [%d]\n",
724 (*new_auth_tok
)->session_key
.encrypted_key_size
);
726 /* Version 4 (from RFC2440) - one byte */
727 if (unlikely(data
[(*packet_size
)++] != 0x04)) {
728 ecryptfs_printk(KERN_DEBUG
, "Unknown version number "
729 "[%d]\n", data
[(*packet_size
) - 1]);
734 /* cipher - one byte */
735 ecryptfs_cipher_code_to_string(crypt_stat
->cipher
,
736 (u16
)data
[(*packet_size
)]);
737 /* A little extra work to differentiate among the AES key
738 * sizes; see RFC2440 */
739 switch(data
[(*packet_size
)++]) {
740 case RFC2440_CIPHER_AES_192
:
741 crypt_stat
->key_size
= 24;
744 crypt_stat
->key_size
=
745 (*new_auth_tok
)->session_key
.encrypted_key_size
;
747 ecryptfs_init_crypt_ctx(crypt_stat
);
748 /* S2K identifier 3 (from RFC2440) */
749 if (unlikely(data
[(*packet_size
)++] != 0x03)) {
750 ecryptfs_printk(KERN_ERR
, "Only S2K ID 3 is currently "
756 /* TODO: finish the hash mapping */
757 /* hash algorithm - one byte */
758 switch (data
[(*packet_size
)++]) {
759 case 0x01: /* See RFC2440 for these numbers and their mappings */
761 /* salt - ECRYPTFS_SALT_SIZE bytes */
762 memcpy((*new_auth_tok
)->token
.password
.salt
,
763 &data
[(*packet_size
)], ECRYPTFS_SALT_SIZE
);
764 (*packet_size
) += ECRYPTFS_SALT_SIZE
;
766 /* This conversion was taken straight from RFC2440 */
767 /* number of hash iterations - one byte */
768 (*new_auth_tok
)->token
.password
.hash_iterations
=
769 ((u32
) 16 + (data
[(*packet_size
)] & 15))
770 << ((data
[(*packet_size
)] >> 4) + 6);
773 /* encrypted session key -
774 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
775 memcpy((*new_auth_tok
)->session_key
.encrypted_key
,
776 &data
[(*packet_size
)],
777 (*new_auth_tok
)->session_key
.encrypted_key_size
);
779 (*new_auth_tok
)->session_key
.encrypted_key_size
;
780 (*new_auth_tok
)->session_key
.flags
&=
781 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY
;
782 (*new_auth_tok
)->session_key
.flags
|=
783 ECRYPTFS_CONTAINS_ENCRYPTED_KEY
;
784 (*new_auth_tok
)->token
.password
.hash_algo
= 0x01;
787 ecryptfs_printk(KERN_ERR
, "Unsupported hash algorithm: "
788 "[%d]\n", data
[(*packet_size
) - 1]);
792 (*new_auth_tok
)->token_type
= ECRYPTFS_PASSWORD
;
793 /* TODO: Parametarize; we might actually want userspace to
794 * decrypt the session key. */
795 (*new_auth_tok
)->session_key
.flags
&=
796 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT
);
797 (*new_auth_tok
)->session_key
.flags
&=
798 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT
);
799 list_add(&auth_tok_list_item
->list
, auth_tok_list
);
802 (*new_auth_tok
) = NULL
;
803 memset(auth_tok_list_item
, 0,
804 sizeof(struct ecryptfs_auth_tok_list_item
));
805 kmem_cache_free(ecryptfs_auth_tok_list_item_cache
,
814 * parse_tag_11_packet
815 * @data: The raw bytes of the packet
816 * @contents: This function writes the data contents of the literal
817 * packet into this memory location
818 * @max_contents_bytes: The maximum number of bytes that this function
819 * is allowed to write into contents
820 * @tag_11_contents_size: This function writes the size of the parsed
821 * contents into this memory location; zero on
823 * @packet_size: This function writes the size of the parsed packet
824 * into this memory location; zero on error
825 * @max_packet_size: maximum number of bytes to parse
827 * Returns zero on success; non-zero on error.
830 parse_tag_11_packet(unsigned char *data
, unsigned char *contents
,
831 size_t max_contents_bytes
, size_t *tag_11_contents_size
,
832 size_t *packet_size
, size_t max_packet_size
)
839 (*tag_11_contents_size
) = 0;
842 * one byte for the Tag 11 ID flag
843 * two bytes for the Tag 11 length
844 * do not exceed the maximum_packet_size
846 if (unlikely((*packet_size
) + 3 > max_packet_size
)) {
847 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
852 /* check for Tag 11 identifyer - one byte */
853 if (data
[(*packet_size
)++] != ECRYPTFS_TAG_11_PACKET_TYPE
) {
854 ecryptfs_printk(KERN_WARNING
,
855 "Invalid tag 11 packet format\n");
860 /* get Tag 11 content length - one or two bytes */
861 rc
= parse_packet_length(&data
[(*packet_size
)], &body_size
,
864 ecryptfs_printk(KERN_WARNING
,
865 "Invalid tag 11 packet format\n");
868 (*packet_size
) += length_size
;
870 if (body_size
< 13) {
871 ecryptfs_printk(KERN_WARNING
, "Invalid body size ([%d])\n",
876 /* We have 13 bytes of surrounding packet values */
877 (*tag_11_contents_size
) = (body_size
- 13);
879 /* now we know the length of the remainting Tag 11 packet size:
880 * 14 fix bytes for: special flag one, special flag two,
882 * body_size bytes minus the stuff above is the Tag 11 content
884 /* FIXME why is the body size one byte smaller than the actual
886 * this seems to be an error here as well as in
887 * write_tag_11_packet() */
888 if (unlikely((*packet_size
) + body_size
+ 1 > max_packet_size
)) {
889 ecryptfs_printk(KERN_ERR
, "Packet size exceeds max\n");
894 /* special flag one - one byte */
895 if (data
[(*packet_size
)++] != 0x62) {
896 ecryptfs_printk(KERN_WARNING
, "Unrecognizable packet\n");
901 /* special flag two - one byte */
902 if (data
[(*packet_size
)++] != 0x08) {
903 ecryptfs_printk(KERN_WARNING
, "Unrecognizable packet\n");
908 /* skip the next 12 bytes */
909 (*packet_size
) += 12; /* We don't care about the filename or
912 /* get the Tag 11 contents - tag_11_contents_size bytes */
913 memcpy(contents
, &data
[(*packet_size
)], (*tag_11_contents_size
));
914 (*packet_size
) += (*tag_11_contents_size
);
919 (*tag_11_contents_size
) = 0;
925 * decrypt_session_key - Decrypt the session key with the given auth_tok.
927 * Returns Zero on success; non-zero error otherwise.
929 static int decrypt_session_key(struct ecryptfs_auth_tok
*auth_tok
,
930 struct ecryptfs_crypt_stat
*crypt_stat
)
932 struct ecryptfs_password
*password_s_ptr
;
933 struct scatterlist src_sg
[2], dst_sg
[2];
934 struct mutex
*tfm_mutex
= NULL
;
935 char *encrypted_session_key
;
937 struct blkcipher_desc desc
= {
938 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
942 password_s_ptr
= &auth_tok
->token
.password
;
943 if (password_s_ptr
->flags
& ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET
)
944 ecryptfs_printk(KERN_DEBUG
, "Session key encryption key "
945 "set; skipping key generation\n");
946 ecryptfs_printk(KERN_DEBUG
, "Session key encryption key (size [%d])"
948 password_s_ptr
->session_key_encryption_key_bytes
);
949 if (ecryptfs_verbosity
> 0)
950 ecryptfs_dump_hex(password_s_ptr
->session_key_encryption_key
,
952 session_key_encryption_key_bytes
);
953 if (!strcmp(crypt_stat
->cipher
,
954 crypt_stat
->mount_crypt_stat
->global_default_cipher_name
)
955 && crypt_stat
->mount_crypt_stat
->global_key_tfm
) {
956 desc
.tfm
= crypt_stat
->mount_crypt_stat
->global_key_tfm
;
957 tfm_mutex
= &crypt_stat
->mount_crypt_stat
->global_key_tfm_mutex
;
961 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
,
966 desc
.tfm
= crypto_alloc_blkcipher(full_alg_name
, 0,
968 kfree(full_alg_name
);
969 if (IS_ERR(desc
.tfm
)) {
970 rc
= PTR_ERR(desc
.tfm
);
971 printk(KERN_ERR
"Error allocating crypto context; "
975 crypto_blkcipher_set_flags(desc
.tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
978 mutex_lock(tfm_mutex
);
979 rc
= crypto_blkcipher_setkey(desc
.tfm
,
980 password_s_ptr
->session_key_encryption_key
,
981 crypt_stat
->key_size
);
983 printk(KERN_ERR
"Error setting key for crypto context\n");
987 /* TODO: virt_to_scatterlist */
988 encrypted_session_key
= (char *)__get_free_page(GFP_KERNEL
);
989 if (!encrypted_session_key
) {
990 ecryptfs_printk(KERN_ERR
, "Out of memory\n");
994 session_key
= (char *)__get_free_page(GFP_KERNEL
);
996 kfree(encrypted_session_key
);
997 ecryptfs_printk(KERN_ERR
, "Out of memory\n");
1001 memcpy(encrypted_session_key
, auth_tok
->session_key
.encrypted_key
,
1002 auth_tok
->session_key
.encrypted_key_size
);
1003 src_sg
[0].page
= virt_to_page(encrypted_session_key
);
1004 src_sg
[0].offset
= 0;
1005 BUG_ON(auth_tok
->session_key
.encrypted_key_size
> PAGE_CACHE_SIZE
);
1006 src_sg
[0].length
= auth_tok
->session_key
.encrypted_key_size
;
1007 dst_sg
[0].page
= virt_to_page(session_key
);
1008 dst_sg
[0].offset
= 0;
1009 auth_tok
->session_key
.decrypted_key_size
=
1010 auth_tok
->session_key
.encrypted_key_size
;
1011 dst_sg
[0].length
= auth_tok
->session_key
.encrypted_key_size
;
1012 rc
= crypto_blkcipher_decrypt(&desc
, dst_sg
, src_sg
,
1013 auth_tok
->session_key
.encrypted_key_size
);
1015 printk(KERN_ERR
"Error decrypting; rc = [%d]\n", rc
);
1016 goto out_free_memory
;
1018 auth_tok
->session_key
.decrypted_key_size
=
1019 auth_tok
->session_key
.encrypted_key_size
;
1020 memcpy(auth_tok
->session_key
.decrypted_key
, session_key
,
1021 auth_tok
->session_key
.decrypted_key_size
);
1022 auth_tok
->session_key
.flags
|= ECRYPTFS_CONTAINS_DECRYPTED_KEY
;
1023 memcpy(crypt_stat
->key
, auth_tok
->session_key
.decrypted_key
,
1024 auth_tok
->session_key
.decrypted_key_size
);
1025 crypt_stat
->flags
|= ECRYPTFS_KEY_VALID
;
1026 ecryptfs_printk(KERN_DEBUG
, "Decrypted session key:\n");
1027 if (ecryptfs_verbosity
> 0)
1028 ecryptfs_dump_hex(crypt_stat
->key
,
1029 crypt_stat
->key_size
);
1031 memset(encrypted_session_key
, 0, PAGE_CACHE_SIZE
);
1032 free_page((unsigned long)encrypted_session_key
);
1033 memset(session_key
, 0, PAGE_CACHE_SIZE
);
1034 free_page((unsigned long)session_key
);
1037 mutex_unlock(tfm_mutex
);
1039 crypto_free_blkcipher(desc
.tfm
);
1045 * ecryptfs_parse_packet_set
1046 * @dest: The header page in memory
1047 * @version: Version of file format, to guide parsing behavior
1049 * Get crypt_stat to have the file's session key if the requisite key
1050 * is available to decrypt the session key.
1052 * Returns Zero if a valid authentication token was retrieved and
1053 * processed; negative value for file not encrypted or for error
1056 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat
*crypt_stat
,
1058 struct dentry
*ecryptfs_dentry
)
1061 size_t found_auth_tok
= 0;
1062 size_t next_packet_is_auth_tok_packet
;
1063 char sig
[ECRYPTFS_SIG_SIZE_HEX
];
1064 struct list_head auth_tok_list
;
1065 struct list_head
*walker
;
1066 struct ecryptfs_auth_tok
*chosen_auth_tok
= NULL
;
1067 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
1068 &ecryptfs_superblock_to_private(
1069 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
1070 struct ecryptfs_auth_tok
*candidate_auth_tok
= NULL
;
1072 struct ecryptfs_auth_tok
*new_auth_tok
;
1073 unsigned char sig_tmp_space
[ECRYPTFS_SIG_SIZE
];
1074 size_t tag_11_contents_size
;
1075 size_t tag_11_packet_size
;
1078 INIT_LIST_HEAD(&auth_tok_list
);
1079 /* Parse the header to find as many packets as we can, these will be
1080 * added the our &auth_tok_list */
1081 next_packet_is_auth_tok_packet
= 1;
1082 while (next_packet_is_auth_tok_packet
) {
1083 size_t max_packet_size
= ((PAGE_CACHE_SIZE
- 8) - i
);
1086 case ECRYPTFS_TAG_3_PACKET_TYPE
:
1087 rc
= parse_tag_3_packet(crypt_stat
,
1088 (unsigned char *)&src
[i
],
1089 &auth_tok_list
, &new_auth_tok
,
1090 &packet_size
, max_packet_size
);
1092 ecryptfs_printk(KERN_ERR
, "Error parsing "
1098 rc
= parse_tag_11_packet((unsigned char *)&src
[i
],
1101 &tag_11_contents_size
,
1102 &tag_11_packet_size
,
1105 ecryptfs_printk(KERN_ERR
, "No valid "
1106 "(ecryptfs-specific) literal "
1107 "packet containing "
1108 "authentication token "
1109 "signature found after "
1114 i
+= tag_11_packet_size
;
1115 if (ECRYPTFS_SIG_SIZE
!= tag_11_contents_size
) {
1116 ecryptfs_printk(KERN_ERR
, "Expected "
1117 "signature of size [%d]; "
1120 tag_11_contents_size
);
1124 ecryptfs_to_hex(new_auth_tok
->token
.password
.signature
,
1125 sig_tmp_space
, tag_11_contents_size
);
1126 new_auth_tok
->token
.password
.signature
[
1127 ECRYPTFS_PASSWORD_SIG_SIZE
] = '\0';
1128 crypt_stat
->flags
|= ECRYPTFS_ENCRYPTED
;
1130 case ECRYPTFS_TAG_1_PACKET_TYPE
:
1131 rc
= parse_tag_1_packet(crypt_stat
,
1132 (unsigned char *)&src
[i
],
1133 &auth_tok_list
, &new_auth_tok
,
1134 &packet_size
, max_packet_size
);
1136 ecryptfs_printk(KERN_ERR
, "Error parsing "
1142 crypt_stat
->flags
|= ECRYPTFS_ENCRYPTED
;
1144 case ECRYPTFS_TAG_11_PACKET_TYPE
:
1145 ecryptfs_printk(KERN_WARNING
, "Invalid packet set "
1146 "(Tag 11 not allowed by itself)\n");
1151 ecryptfs_printk(KERN_DEBUG
, "No packet at offset "
1152 "[%d] of the file header; hex value of "
1153 "character is [0x%.2x]\n", i
, src
[i
]);
1154 next_packet_is_auth_tok_packet
= 0;
1157 if (list_empty(&auth_tok_list
)) {
1158 rc
= -EINVAL
; /* Do not support non-encrypted files in
1159 * the 0.1 release */
1162 /* If we have a global auth tok, then we should try to use
1164 if (mount_crypt_stat
->global_auth_tok
) {
1165 memcpy(sig
, mount_crypt_stat
->global_auth_tok_sig
,
1166 ECRYPTFS_SIG_SIZE_HEX
);
1167 chosen_auth_tok
= mount_crypt_stat
->global_auth_tok
;
1169 BUG(); /* We should always have a global auth tok in
1170 * the 0.1 release */
1171 /* Scan list to see if our chosen_auth_tok works */
1172 list_for_each(walker
, &auth_tok_list
) {
1173 struct ecryptfs_auth_tok_list_item
*auth_tok_list_item
;
1174 auth_tok_list_item
=
1175 list_entry(walker
, struct ecryptfs_auth_tok_list_item
,
1177 candidate_auth_tok
= &auth_tok_list_item
->auth_tok
;
1178 if (unlikely(ecryptfs_verbosity
> 0)) {
1179 ecryptfs_printk(KERN_DEBUG
,
1180 "Considering cadidate auth tok:\n");
1181 ecryptfs_dump_auth_tok(candidate_auth_tok
);
1183 /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
1184 if (candidate_auth_tok
->token_type
== ECRYPTFS_PASSWORD
1185 && !strncmp(candidate_auth_tok
->token
.password
.signature
,
1186 sig
, ECRYPTFS_SIG_SIZE_HEX
)) {
1189 /* TODO: Transfer the common salt into the
1190 * crypt_stat salt */
1191 } else if ((candidate_auth_tok
->token_type
1192 == ECRYPTFS_PRIVATE_KEY
)
1193 && !strncmp(candidate_auth_tok
->token
.private_key
.signature
,
1194 sig
, ECRYPTFS_SIG_SIZE_HEX
)) {
1199 if (!found_auth_tok
) {
1200 ecryptfs_printk(KERN_ERR
, "Could not find authentication "
1201 "token on temporary list for sig [%.*s]\n",
1202 ECRYPTFS_SIG_SIZE_HEX
, sig
);
1208 if (candidate_auth_tok
->token_type
== ECRYPTFS_PRIVATE_KEY
) {
1209 memcpy(&(candidate_auth_tok
->token
.private_key
),
1210 &(chosen_auth_tok
->token
.private_key
),
1211 sizeof(struct ecryptfs_private_key
));
1212 rc
= decrypt_pki_encrypted_session_key(mount_crypt_stat
,
1215 } else if (candidate_auth_tok
->token_type
== ECRYPTFS_PASSWORD
) {
1216 memcpy(&(candidate_auth_tok
->token
.password
),
1217 &(chosen_auth_tok
->token
.password
),
1218 sizeof(struct ecryptfs_password
));
1219 rc
= decrypt_session_key(candidate_auth_tok
, crypt_stat
);
1222 ecryptfs_printk(KERN_ERR
, "Error decrypting the "
1223 "session key; rc = [%d]\n", rc
);
1226 rc
= ecryptfs_compute_root_iv(crypt_stat
);
1228 ecryptfs_printk(KERN_ERR
, "Error computing "
1232 rc
= ecryptfs_init_crypt_ctx(crypt_stat
);
1234 ecryptfs_printk(KERN_ERR
, "Error initializing crypto "
1235 "context for cipher [%s]; rc = [%d]\n",
1236 crypt_stat
->cipher
, rc
);
1239 wipe_auth_tok_list(&auth_tok_list
);
1244 pki_encrypt_session_key(struct ecryptfs_auth_tok
*auth_tok
,
1245 struct ecryptfs_crypt_stat
*crypt_stat
,
1246 struct ecryptfs_key_record
*key_rec
)
1248 struct ecryptfs_msg_ctx
*msg_ctx
= NULL
;
1249 char *netlink_payload
;
1250 size_t netlink_payload_length
;
1251 struct ecryptfs_message
*msg
;
1254 rc
= write_tag_66_packet(auth_tok
->token
.private_key
.signature
,
1255 ecryptfs_code_for_cipher_string(crypt_stat
),
1256 crypt_stat
, &netlink_payload
,
1257 &netlink_payload_length
);
1259 ecryptfs_printk(KERN_ERR
, "Error generating tag 66 packet\n");
1262 rc
= ecryptfs_send_message(ecryptfs_transport
, netlink_payload
,
1263 netlink_payload_length
, &msg_ctx
);
1265 ecryptfs_printk(KERN_ERR
, "Error sending netlink message\n");
1268 rc
= ecryptfs_wait_for_response(msg_ctx
, &msg
);
1270 ecryptfs_printk(KERN_ERR
, "Failed to receive tag 67 packet "
1271 "from the user space daemon\n");
1275 rc
= parse_tag_67_packet(key_rec
, msg
);
1277 ecryptfs_printk(KERN_ERR
, "Error parsing tag 67 packet\n");
1280 if (netlink_payload
)
1281 kfree(netlink_payload
);
1285 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1286 * @dest: Buffer into which to write the packet
1287 * @max: Maximum number of bytes that can be writtn
1288 * @packet_size: This function will write the number of bytes that end
1289 * up constituting the packet; set to zero on error
1291 * Returns zero on success; non-zero on error.
1294 write_tag_1_packet(char *dest
, size_t max
, struct ecryptfs_auth_tok
*auth_tok
,
1295 struct ecryptfs_crypt_stat
*crypt_stat
,
1296 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
,
1297 struct ecryptfs_key_record
*key_rec
, size_t *packet_size
)
1300 size_t encrypted_session_key_valid
= 0;
1301 size_t key_rec_size
;
1302 size_t packet_size_length
;
1306 ecryptfs_from_hex(key_rec
->sig
, auth_tok
->token
.private_key
.signature
,
1308 encrypted_session_key_valid
= 0;
1309 for (i
= 0; i
< crypt_stat
->key_size
; i
++)
1310 encrypted_session_key_valid
|=
1311 auth_tok
->session_key
.encrypted_key
[i
];
1312 if (encrypted_session_key_valid
) {
1313 memcpy(key_rec
->enc_key
,
1314 auth_tok
->session_key
.encrypted_key
,
1315 auth_tok
->session_key
.encrypted_key_size
);
1316 goto encrypted_session_key_set
;
1318 if (auth_tok
->session_key
.encrypted_key_size
== 0)
1319 auth_tok
->session_key
.encrypted_key_size
=
1320 auth_tok
->token
.private_key
.key_size
;
1321 rc
= pki_encrypt_session_key(auth_tok
, crypt_stat
, key_rec
);
1323 ecryptfs_printk(KERN_ERR
, "Failed to encrypt session key "
1327 if (ecryptfs_verbosity
> 0) {
1328 ecryptfs_printk(KERN_DEBUG
, "Encrypted key:\n");
1329 ecryptfs_dump_hex(key_rec
->enc_key
, key_rec
->enc_key_size
);
1331 encrypted_session_key_set
:
1332 /* Now we have a valid key_rec. Append it to the
1334 key_rec_size
= (sizeof(struct ecryptfs_key_record
)
1335 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
1336 + (key_rec
->enc_key_size
));
1337 /* TODO: Include a packet size limit as a parameter to this
1338 * function once we have multi-packet headers (for versions
1340 if (key_rec_size
>= ECRYPTFS_MAX_KEYSET_SIZE
) {
1341 ecryptfs_printk(KERN_ERR
, "Keyset too large\n");
1345 /* ***** TAG 1 Packet Format *****
1346 * | version number | 1 byte |
1347 * | key ID | 8 bytes |
1348 * | public key algorithm | 1 byte |
1349 * | encrypted session key | arbitrary |
1351 if ((0x02 + ECRYPTFS_SIG_SIZE
+ key_rec
->enc_key_size
) >= max
) {
1352 ecryptfs_printk(KERN_ERR
,
1353 "Authentication token is too large\n");
1357 dest
[(*packet_size
)++] = ECRYPTFS_TAG_1_PACKET_TYPE
;
1358 /* This format is inspired by OpenPGP; see RFC 2440
1360 rc
= write_packet_length(&dest
[(*packet_size
)],
1361 (0x02 + ECRYPTFS_SIG_SIZE
+
1362 key_rec
->enc_key_size
),
1363 &packet_size_length
);
1365 ecryptfs_printk(KERN_ERR
, "Error generating tag 1 packet "
1366 "header; cannot generate packet length\n");
1369 (*packet_size
) += packet_size_length
;
1370 dest
[(*packet_size
)++] = 0x03; /* version 3 */
1371 memcpy(&dest
[(*packet_size
)], key_rec
->sig
, ECRYPTFS_SIG_SIZE
);
1372 (*packet_size
) += ECRYPTFS_SIG_SIZE
;
1373 dest
[(*packet_size
)++] = RFC2440_CIPHER_RSA
;
1374 memcpy(&dest
[(*packet_size
)], key_rec
->enc_key
,
1375 key_rec
->enc_key_size
);
1376 (*packet_size
) += key_rec
->enc_key_size
;
1384 * write_tag_11_packet
1385 * @dest: Target into which Tag 11 packet is to be written
1386 * @max: Maximum packet length
1387 * @contents: Byte array of contents to copy in
1388 * @contents_length: Number of bytes in contents
1389 * @packet_length: Length of the Tag 11 packet written; zero on error
1391 * Returns zero on success; non-zero on error.
1394 write_tag_11_packet(char *dest
, int max
, char *contents
, size_t contents_length
,
1395 size_t *packet_length
)
1397 size_t packet_size_length
;
1400 (*packet_length
) = 0;
1401 if ((13 + contents_length
) > max
) {
1403 ecryptfs_printk(KERN_ERR
, "Packet length larger than "
1404 "maximum allowable\n");
1407 /* General packet header */
1409 dest
[(*packet_length
)++] = ECRYPTFS_TAG_11_PACKET_TYPE
;
1411 rc
= write_packet_length(&dest
[(*packet_length
)],
1412 (13 + contents_length
), &packet_size_length
);
1414 ecryptfs_printk(KERN_ERR
, "Error generating tag 11 packet "
1415 "header; cannot generate packet length\n");
1418 (*packet_length
) += packet_size_length
;
1419 /* Tag 11 specific */
1420 /* One-octet field that describes how the data is formatted */
1421 dest
[(*packet_length
)++] = 0x62; /* binary data */
1422 /* One-octet filename length followed by filename */
1423 dest
[(*packet_length
)++] = 8;
1424 memcpy(&dest
[(*packet_length
)], "_CONSOLE", 8);
1425 (*packet_length
) += 8;
1426 /* Four-octet number indicating modification date */
1427 memset(&dest
[(*packet_length
)], 0x00, 4);
1428 (*packet_length
) += 4;
1429 /* Remainder is literal data */
1430 memcpy(&dest
[(*packet_length
)], contents
, contents_length
);
1431 (*packet_length
) += contents_length
;
1434 (*packet_length
) = 0;
1439 * write_tag_3_packet
1440 * @dest: Buffer into which to write the packet
1441 * @max: Maximum number of bytes that can be written
1442 * @auth_tok: Authentication token
1443 * @crypt_stat: The cryptographic context
1444 * @key_rec: encrypted key
1445 * @packet_size: This function will write the number of bytes that end
1446 * up constituting the packet; set to zero on error
1448 * Returns zero on success; non-zero on error.
1451 write_tag_3_packet(char *dest
, size_t max
, struct ecryptfs_auth_tok
*auth_tok
,
1452 struct ecryptfs_crypt_stat
*crypt_stat
,
1453 struct ecryptfs_key_record
*key_rec
, size_t *packet_size
)
1456 size_t encrypted_session_key_valid
= 0;
1457 char session_key_encryption_key
[ECRYPTFS_MAX_KEY_BYTES
];
1458 struct scatterlist dest_sg
[2];
1459 struct scatterlist src_sg
[2];
1460 struct mutex
*tfm_mutex
= NULL
;
1461 size_t key_rec_size
;
1462 size_t packet_size_length
;
1464 struct blkcipher_desc desc
= {
1466 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
1471 ecryptfs_from_hex(key_rec
->sig
, auth_tok
->token
.password
.signature
,
1473 encrypted_session_key_valid
= 0;
1474 for (i
= 0; i
< crypt_stat
->key_size
; i
++)
1475 encrypted_session_key_valid
|=
1476 auth_tok
->session_key
.encrypted_key
[i
];
1477 if (encrypted_session_key_valid
) {
1478 memcpy(key_rec
->enc_key
,
1479 auth_tok
->session_key
.encrypted_key
,
1480 auth_tok
->session_key
.encrypted_key_size
);
1481 goto encrypted_session_key_set
;
1483 if (auth_tok
->session_key
.encrypted_key_size
== 0)
1484 auth_tok
->session_key
.encrypted_key_size
=
1485 crypt_stat
->key_size
;
1486 if (crypt_stat
->key_size
== 24
1487 && strcmp("aes", crypt_stat
->cipher
) == 0) {
1488 memset((crypt_stat
->key
+ 24), 0, 8);
1489 auth_tok
->session_key
.encrypted_key_size
= 32;
1491 key_rec
->enc_key_size
=
1492 auth_tok
->session_key
.encrypted_key_size
;
1493 if (auth_tok
->token
.password
.flags
&
1494 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET
) {
1495 ecryptfs_printk(KERN_DEBUG
, "Using previously generated "
1496 "session key encryption key of size [%d]\n",
1497 auth_tok
->token
.password
.
1498 session_key_encryption_key_bytes
);
1499 memcpy(session_key_encryption_key
,
1500 auth_tok
->token
.password
.session_key_encryption_key
,
1501 crypt_stat
->key_size
);
1502 ecryptfs_printk(KERN_DEBUG
,
1503 "Cached session key " "encryption key: \n");
1504 if (ecryptfs_verbosity
> 0)
1505 ecryptfs_dump_hex(session_key_encryption_key
, 16);
1507 if (unlikely(ecryptfs_verbosity
> 0)) {
1508 ecryptfs_printk(KERN_DEBUG
, "Session key encryption key:\n");
1509 ecryptfs_dump_hex(session_key_encryption_key
, 16);
1511 rc
= virt_to_scatterlist(crypt_stat
->key
,
1512 key_rec
->enc_key_size
, src_sg
, 2);
1514 ecryptfs_printk(KERN_ERR
, "Error generating scatterlist "
1515 "for crypt_stat session key\n");
1519 rc
= virt_to_scatterlist(key_rec
->enc_key
,
1520 key_rec
->enc_key_size
, dest_sg
, 2);
1522 ecryptfs_printk(KERN_ERR
, "Error generating scatterlist "
1523 "for crypt_stat encrypted session key\n");
1527 if (!strcmp(crypt_stat
->cipher
,
1528 crypt_stat
->mount_crypt_stat
->global_default_cipher_name
)
1529 && crypt_stat
->mount_crypt_stat
->global_key_tfm
) {
1530 desc
.tfm
= crypt_stat
->mount_crypt_stat
->global_key_tfm
;
1531 tfm_mutex
= &crypt_stat
->mount_crypt_stat
->global_key_tfm_mutex
;
1533 char *full_alg_name
;
1535 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
,
1540 desc
.tfm
= crypto_alloc_blkcipher(full_alg_name
, 0,
1542 kfree(full_alg_name
);
1543 if (IS_ERR(desc
.tfm
)) {
1544 rc
= PTR_ERR(desc
.tfm
);
1545 ecryptfs_printk(KERN_ERR
, "Could not initialize crypto "
1546 "context for cipher [%s]; rc = [%d]\n",
1547 crypt_stat
->cipher
, rc
);
1550 crypto_blkcipher_set_flags(desc
.tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
1553 mutex_lock(tfm_mutex
);
1554 rc
= crypto_blkcipher_setkey(desc
.tfm
, session_key_encryption_key
,
1555 crypt_stat
->key_size
);
1558 mutex_unlock(tfm_mutex
);
1559 ecryptfs_printk(KERN_ERR
, "Error setting key for crypto "
1560 "context; rc = [%d]\n", rc
);
1564 ecryptfs_printk(KERN_DEBUG
, "Encrypting [%d] bytes of the key\n",
1565 crypt_stat
->key_size
);
1566 rc
= crypto_blkcipher_encrypt(&desc
, dest_sg
, src_sg
,
1567 (*key_rec
).enc_key_size
);
1569 printk(KERN_ERR
"Error encrypting; rc = [%d]\n", rc
);
1573 mutex_unlock(tfm_mutex
);
1574 ecryptfs_printk(KERN_DEBUG
, "This should be the encrypted key:\n");
1575 if (ecryptfs_verbosity
> 0)
1576 ecryptfs_dump_hex(key_rec
->enc_key
,
1577 key_rec
->enc_key_size
);
1578 encrypted_session_key_set
:
1579 /* Now we have a valid key_rec. Append it to the
1581 key_rec_size
= (sizeof(struct ecryptfs_key_record
)
1582 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
1583 + (key_rec
->enc_key_size
));
1584 /* TODO: Include a packet size limit as a parameter to this
1585 * function once we have multi-packet headers (for versions
1587 if (key_rec_size
>= ECRYPTFS_MAX_KEYSET_SIZE
) {
1588 ecryptfs_printk(KERN_ERR
, "Keyset too large\n");
1592 /* TODO: Packet size limit */
1593 /* We have 5 bytes of surrounding packet data */
1594 if ((0x05 + ECRYPTFS_SALT_SIZE
1595 + key_rec
->enc_key_size
) >= max
) {
1596 ecryptfs_printk(KERN_ERR
, "Authentication token is too "
1601 /* This format is inspired by OpenPGP; see RFC 2440
1603 dest
[(*packet_size
)++] = ECRYPTFS_TAG_3_PACKET_TYPE
;
1604 /* ver+cipher+s2k+hash+salt+iter+enc_key */
1605 rc
= write_packet_length(&dest
[(*packet_size
)],
1606 (0x05 + ECRYPTFS_SALT_SIZE
1607 + key_rec
->enc_key_size
),
1608 &packet_size_length
);
1610 ecryptfs_printk(KERN_ERR
, "Error generating tag 3 packet "
1611 "header; cannot generate packet length\n");
1614 (*packet_size
) += packet_size_length
;
1615 dest
[(*packet_size
)++] = 0x04; /* version 4 */
1616 cipher_code
= ecryptfs_code_for_cipher_string(crypt_stat
);
1617 if (cipher_code
== 0) {
1618 ecryptfs_printk(KERN_WARNING
, "Unable to generate code for "
1619 "cipher [%s]\n", crypt_stat
->cipher
);
1623 dest
[(*packet_size
)++] = cipher_code
;
1624 dest
[(*packet_size
)++] = 0x03; /* S2K */
1625 dest
[(*packet_size
)++] = 0x01; /* MD5 (TODO: parameterize) */
1626 memcpy(&dest
[(*packet_size
)], auth_tok
->token
.password
.salt
,
1627 ECRYPTFS_SALT_SIZE
);
1628 (*packet_size
) += ECRYPTFS_SALT_SIZE
; /* salt */
1629 dest
[(*packet_size
)++] = 0x60; /* hash iterations (65536) */
1630 memcpy(&dest
[(*packet_size
)], key_rec
->enc_key
,
1631 key_rec
->enc_key_size
);
1632 (*packet_size
) += key_rec
->enc_key_size
;
1634 if (desc
.tfm
&& !tfm_mutex
)
1635 crypto_free_blkcipher(desc
.tfm
);
1641 struct kmem_cache
*ecryptfs_key_record_cache
;
1644 * ecryptfs_generate_key_packet_set
1645 * @dest: Virtual address from which to write the key record set
1646 * @crypt_stat: The cryptographic context from which the
1647 * authentication tokens will be retrieved
1648 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1649 * for the global parameters
1650 * @len: The amount written
1651 * @max: The maximum amount of data allowed to be written
1653 * Generates a key packet set and writes it to the virtual address
1656 * Returns zero on success; non-zero on error.
1659 ecryptfs_generate_key_packet_set(char *dest_base
,
1660 struct ecryptfs_crypt_stat
*crypt_stat
,
1661 struct dentry
*ecryptfs_dentry
, size_t *len
,
1664 struct ecryptfs_auth_tok
*auth_tok
;
1665 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
1666 &ecryptfs_superblock_to_private(
1667 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
1669 struct ecryptfs_key_record
*key_rec
;
1673 key_rec
= kmem_cache_alloc(ecryptfs_key_record_cache
, GFP_KERNEL
);
1678 if (mount_crypt_stat
->global_auth_tok
) {
1679 auth_tok
= mount_crypt_stat
->global_auth_tok
;
1680 if (auth_tok
->token_type
== ECRYPTFS_PASSWORD
) {
1681 rc
= write_tag_3_packet((dest_base
+ (*len
)),
1683 crypt_stat
, key_rec
,
1686 ecryptfs_printk(KERN_WARNING
, "Error "
1687 "writing tag 3 packet\n");
1691 /* Write auth tok signature packet */
1692 rc
= write_tag_11_packet(
1693 (dest_base
+ (*len
)),
1695 key_rec
->sig
, ECRYPTFS_SIG_SIZE
, &written
);
1697 ecryptfs_printk(KERN_ERR
, "Error writing "
1698 "auth tok signature packet\n");
1702 } else if (auth_tok
->token_type
== ECRYPTFS_PRIVATE_KEY
) {
1703 rc
= write_tag_1_packet(dest_base
+ (*len
),
1705 crypt_stat
,mount_crypt_stat
,
1708 ecryptfs_printk(KERN_WARNING
, "Error "
1709 "writing tag 1 packet\n");
1714 ecryptfs_printk(KERN_WARNING
, "Unsupported "
1715 "authentication token type\n");
1721 if (likely((max
- (*len
)) > 0)) {
1722 dest_base
[(*len
)] = 0x00;
1724 ecryptfs_printk(KERN_ERR
, "Error writing boundary byte\n");
1729 kmem_cache_free(ecryptfs_key_record_cache
, key_rec
);