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>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <linux/string.h>
28 #include <linux/sched.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);
67 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
69 struct list_head *walker;
70 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
72 walker = auth_tok_list_head->next;
73 while (walker != auth_tok_list_head) {
75 list_entry(walker, struct ecryptfs_auth_tok_list_item,
77 walker = auth_tok_list_item->list.next;
78 memset(auth_tok_list_item, 0,
79 sizeof(struct ecryptfs_auth_tok_list_item));
80 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
85 struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
89 * @data: Pointer to memory containing length at offset
90 * @size: This function writes the decoded size to this memory
91 * address; zero on error
92 * @length_size: The number of bytes occupied by the encoded length
94 * Returns Zero on success
96 static int parse_packet_length(unsigned char *data, size_t *size,
104 /* One-byte length */
107 } else if (data[0] < 224) {
108 /* Two-byte length */
109 (*size) = ((data[0] - 192) * 256);
110 (*size) += (data[1] + 192);
112 } else if (data[0] == 255) {
113 /* Five-byte length; we're not supposed to see this */
114 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
119 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
128 * write_packet_length
129 * @dest: The byte array target into which to write the
130 * length. Must have at least 5 bytes allocated.
131 * @size: The length to write.
132 * @packet_size_length: The number of bytes used to encode the
133 * packet length is written to this address.
135 * Returns zero on success; non-zero on error.
137 static int write_packet_length(char *dest, size_t size,
138 size_t *packet_size_length)
144 (*packet_size_length) = 1;
145 } else if (size < 65536) {
146 dest[0] = (((size - 192) / 256) + 192);
147 dest[1] = ((size - 192) % 256);
148 (*packet_size_length) = 2;
151 ecryptfs_printk(KERN_WARNING,
152 "Unsupported packet size: [%d]\n", size);
159 * @crypt_stat: The cryptographic context to modify based on packet
161 * @data: The raw bytes of the packet.
162 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
163 * a new authentication token will be placed at the end
164 * of this list for this packet.
165 * @new_auth_tok: Pointer to a pointer to memory that this function
166 * allocates; sets the memory address of the pointer to
167 * NULL on error. This object is added to the
169 * @packet_size: This function writes the size of the parsed packet
170 * into this memory location; zero on error.
171 * @max_packet_size: maximum number of bytes to parse
173 * Returns zero on success; non-zero on error.
176 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
177 unsigned char *data, struct list_head *auth_tok_list,
178 struct ecryptfs_auth_tok **new_auth_tok,
179 size_t *packet_size, size_t max_packet_size)
183 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
187 (*new_auth_tok) = NULL;
190 * one byte for the Tag 3 ID flag
191 * two bytes for the body size
192 * do not exceed the maximum_packet_size
194 if (unlikely((*packet_size) + 3 > max_packet_size)) {
195 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
200 /* check for Tag 3 identifyer - one byte */
201 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
202 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
203 ECRYPTFS_TAG_3_PACKET_TYPE);
207 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
208 * at end of function upon failure */
210 kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, SLAB_KERNEL);
211 if (!auth_tok_list_item) {
212 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
216 memset(auth_tok_list_item, 0,
217 sizeof(struct ecryptfs_auth_tok_list_item));
218 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
220 /* check for body size - one to two bytes */
221 rc = parse_packet_length(&data[(*packet_size)], &body_size,
224 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
228 if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
229 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
234 (*packet_size) += length_size;
236 /* now we know the length of the remainting Tag 3 packet size:
237 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
238 * number of hash iterations
239 * ECRYPTFS_SALT_SIZE bytes for salt
240 * body_size bytes minus the stuff above is the encrypted key size
242 if (unlikely((*packet_size) + body_size > max_packet_size)) {
243 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
248 /* There are 5 characters of additional information in the
250 (*new_auth_tok)->session_key.encrypted_key_size =
251 body_size - (0x05 + ECRYPTFS_SALT_SIZE);
252 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
253 (*new_auth_tok)->session_key.encrypted_key_size);
255 /* Version 4 (from RFC2440) - one byte */
256 if (unlikely(data[(*packet_size)++] != 0x04)) {
257 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
258 "[%d]\n", data[(*packet_size) - 1]);
263 /* cipher - one byte */
264 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
265 (u16)data[(*packet_size)]);
266 /* A little extra work to differentiate among the AES key
267 * sizes; see RFC2440 */
268 switch(data[(*packet_size)++]) {
269 case RFC2440_CIPHER_AES_192:
270 crypt_stat->key_size = 24;
273 crypt_stat->key_size =
274 (*new_auth_tok)->session_key.encrypted_key_size;
276 ecryptfs_init_crypt_ctx(crypt_stat);
277 /* S2K identifier 3 (from RFC2440) */
278 if (unlikely(data[(*packet_size)++] != 0x03)) {
279 ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
285 /* TODO: finish the hash mapping */
286 /* hash algorithm - one byte */
287 switch (data[(*packet_size)++]) {
288 case 0x01: /* See RFC2440 for these numbers and their mappings */
290 /* salt - ECRYPTFS_SALT_SIZE bytes */
291 memcpy((*new_auth_tok)->token.password.salt,
292 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
293 (*packet_size) += ECRYPTFS_SALT_SIZE;
295 /* This conversion was taken straight from RFC2440 */
296 /* number of hash iterations - one byte */
297 (*new_auth_tok)->token.password.hash_iterations =
298 ((u32) 16 + (data[(*packet_size)] & 15))
299 << ((data[(*packet_size)] >> 4) + 6);
302 /* encrypted session key -
303 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
304 memcpy((*new_auth_tok)->session_key.encrypted_key,
305 &data[(*packet_size)],
306 (*new_auth_tok)->session_key.encrypted_key_size);
308 (*new_auth_tok)->session_key.encrypted_key_size;
309 (*new_auth_tok)->session_key.flags &=
310 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
311 (*new_auth_tok)->session_key.flags |=
312 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
313 (*new_auth_tok)->token.password.hash_algo = 0x01;
316 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
317 "[%d]\n", data[(*packet_size) - 1]);
321 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
322 /* TODO: Parametarize; we might actually want userspace to
323 * decrypt the session key. */
324 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
325 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
326 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
327 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
328 list_add(&auth_tok_list_item->list, auth_tok_list);
331 (*new_auth_tok) = NULL;
332 memset(auth_tok_list_item, 0,
333 sizeof(struct ecryptfs_auth_tok_list_item));
334 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
343 * parse_tag_11_packet
344 * @data: The raw bytes of the packet
345 * @contents: This function writes the data contents of the literal
346 * packet into this memory location
347 * @max_contents_bytes: The maximum number of bytes that this function
348 * is allowed to write into contents
349 * @tag_11_contents_size: This function writes the size of the parsed
350 * contents into this memory location; zero on
352 * @packet_size: This function writes the size of the parsed packet
353 * into this memory location; zero on error
354 * @max_packet_size: maximum number of bytes to parse
356 * Returns zero on success; non-zero on error.
359 parse_tag_11_packet(unsigned char *data, unsigned char *contents,
360 size_t max_contents_bytes, size_t *tag_11_contents_size,
361 size_t *packet_size, size_t max_packet_size)
368 (*tag_11_contents_size) = 0;
371 * one byte for the Tag 11 ID flag
372 * two bytes for the Tag 11 length
373 * do not exceed the maximum_packet_size
375 if (unlikely((*packet_size) + 3 > max_packet_size)) {
376 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
381 /* check for Tag 11 identifyer - one byte */
382 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
383 ecryptfs_printk(KERN_WARNING,
384 "Invalid tag 11 packet format\n");
389 /* get Tag 11 content length - one or two bytes */
390 rc = parse_packet_length(&data[(*packet_size)], &body_size,
393 ecryptfs_printk(KERN_WARNING,
394 "Invalid tag 11 packet format\n");
397 (*packet_size) += length_size;
399 if (body_size < 13) {
400 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
405 /* We have 13 bytes of surrounding packet values */
406 (*tag_11_contents_size) = (body_size - 13);
408 /* now we know the length of the remainting Tag 11 packet size:
409 * 14 fix bytes for: special flag one, special flag two,
411 * body_size bytes minus the stuff above is the Tag 11 content
413 /* FIXME why is the body size one byte smaller than the actual
415 * this seems to be an error here as well as in
416 * write_tag_11_packet() */
417 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
418 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
423 /* special flag one - one byte */
424 if (data[(*packet_size)++] != 0x62) {
425 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
430 /* special flag two - one byte */
431 if (data[(*packet_size)++] != 0x08) {
432 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
437 /* skip the next 12 bytes */
438 (*packet_size) += 12; /* We don't care about the filename or
441 /* get the Tag 11 contents - tag_11_contents_size bytes */
442 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
443 (*packet_size) += (*tag_11_contents_size);
448 (*tag_11_contents_size) = 0;
454 * decrypt_session_key - Decrypt the session key with the given auth_tok.
456 * Returns Zero on success; non-zero error otherwise.
458 static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
459 struct ecryptfs_crypt_stat *crypt_stat)
461 struct ecryptfs_password *password_s_ptr;
462 struct scatterlist src_sg[2], dst_sg[2];
463 struct mutex *tfm_mutex = NULL;
464 /* TODO: Use virt_to_scatterlist for these */
465 char *encrypted_session_key;
467 struct blkcipher_desc desc = {
468 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
472 password_s_ptr = &auth_tok->token.password;
473 if (ECRYPTFS_CHECK_FLAG(password_s_ptr->flags,
474 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))
475 ecryptfs_printk(KERN_DEBUG, "Session key encryption key "
476 "set; skipping key generation\n");
477 ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])"
479 password_s_ptr->session_key_encryption_key_bytes);
480 if (ecryptfs_verbosity > 0)
481 ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key,
483 session_key_encryption_key_bytes);
484 if (!strcmp(crypt_stat->cipher,
485 crypt_stat->mount_crypt_stat->global_default_cipher_name)
486 && crypt_stat->mount_crypt_stat->global_key_tfm) {
487 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
488 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
492 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
497 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
499 kfree(full_alg_name);
500 if (IS_ERR(desc.tfm)) {
501 rc = PTR_ERR(desc.tfm);
502 printk(KERN_ERR "Error allocating crypto context; "
506 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
509 mutex_lock(tfm_mutex);
510 rc = crypto_blkcipher_setkey(desc.tfm,
511 password_s_ptr->session_key_encryption_key,
512 crypt_stat->key_size);
514 printk(KERN_ERR "Error setting key for crypto context\n");
518 /* TODO: virt_to_scatterlist */
519 encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
520 if (!encrypted_session_key) {
521 ecryptfs_printk(KERN_ERR, "Out of memory\n");
525 session_key = (char *)__get_free_page(GFP_KERNEL);
527 kfree(encrypted_session_key);
528 ecryptfs_printk(KERN_ERR, "Out of memory\n");
532 memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
533 auth_tok->session_key.encrypted_key_size);
534 src_sg[0].page = virt_to_page(encrypted_session_key);
535 src_sg[0].offset = 0;
536 BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
537 src_sg[0].length = auth_tok->session_key.encrypted_key_size;
538 dst_sg[0].page = virt_to_page(session_key);
539 dst_sg[0].offset = 0;
540 auth_tok->session_key.decrypted_key_size =
541 auth_tok->session_key.encrypted_key_size;
542 dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
543 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
544 auth_tok->session_key.encrypted_key_size);
546 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
547 goto out_free_memory;
549 auth_tok->session_key.decrypted_key_size =
550 auth_tok->session_key.encrypted_key_size;
551 memcpy(auth_tok->session_key.decrypted_key, session_key,
552 auth_tok->session_key.decrypted_key_size);
553 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
554 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
555 auth_tok->session_key.decrypted_key_size);
556 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
557 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
558 if (ecryptfs_verbosity > 0)
559 ecryptfs_dump_hex(crypt_stat->key,
560 crypt_stat->key_size);
562 memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
563 free_page((unsigned long)encrypted_session_key);
564 memset(session_key, 0, PAGE_CACHE_SIZE);
565 free_page((unsigned long)session_key);
568 mutex_unlock(tfm_mutex);
570 crypto_free_blkcipher(desc.tfm);
576 * ecryptfs_parse_packet_set
577 * @dest: The header page in memory
578 * @version: Version of file format, to guide parsing behavior
580 * Get crypt_stat to have the file's session key if the requisite key
581 * is available to decrypt the session key.
583 * Returns Zero if a valid authentication token was retrieved and
584 * processed; negative value for file not encrypted or for error
587 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
589 struct dentry *ecryptfs_dentry)
593 size_t found_auth_tok = 0;
594 size_t next_packet_is_auth_tok_packet;
595 char sig[ECRYPTFS_SIG_SIZE_HEX];
596 struct list_head auth_tok_list;
597 struct list_head *walker;
598 struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
599 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
600 &ecryptfs_superblock_to_private(
601 ecryptfs_dentry->d_sb)->mount_crypt_stat;
602 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
604 struct ecryptfs_auth_tok *new_auth_tok;
605 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
606 size_t tag_11_contents_size;
607 size_t tag_11_packet_size;
609 INIT_LIST_HEAD(&auth_tok_list);
610 /* Parse the header to find as many packets as we can, these will be
611 * added the our &auth_tok_list */
612 next_packet_is_auth_tok_packet = 1;
613 while (next_packet_is_auth_tok_packet) {
614 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
617 case ECRYPTFS_TAG_3_PACKET_TYPE:
618 rc = parse_tag_3_packet(crypt_stat,
619 (unsigned char *)&src[i],
620 &auth_tok_list, &new_auth_tok,
621 &packet_size, max_packet_size);
623 ecryptfs_printk(KERN_ERR, "Error parsing "
629 rc = parse_tag_11_packet((unsigned char *)&src[i],
632 &tag_11_contents_size,
636 ecryptfs_printk(KERN_ERR, "No valid "
637 "(ecryptfs-specific) literal "
639 "authentication token "
640 "signature found after "
645 i += tag_11_packet_size;
646 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
647 ecryptfs_printk(KERN_ERR, "Expected "
648 "signature of size [%d]; "
651 tag_11_contents_size);
655 ecryptfs_to_hex(new_auth_tok->token.password.signature,
656 sig_tmp_space, tag_11_contents_size);
657 new_auth_tok->token.password.signature[
658 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
659 ECRYPTFS_SET_FLAG(crypt_stat->flags,
662 case ECRYPTFS_TAG_11_PACKET_TYPE:
663 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
664 "(Tag 11 not allowed by itself)\n");
669 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
670 "[%d] of the file header; hex value of "
671 "character is [0x%.2x]\n", i, src[i]);
672 next_packet_is_auth_tok_packet = 0;
675 if (list_empty(&auth_tok_list)) {
676 rc = -EINVAL; /* Do not support non-encrypted files in
680 /* If we have a global auth tok, then we should try to use
682 if (mount_crypt_stat->global_auth_tok) {
683 memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
684 ECRYPTFS_SIG_SIZE_HEX);
685 chosen_auth_tok = mount_crypt_stat->global_auth_tok;
687 BUG(); /* We should always have a global auth tok in
689 /* Scan list to see if our chosen_auth_tok works */
690 list_for_each(walker, &auth_tok_list) {
691 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
693 list_entry(walker, struct ecryptfs_auth_tok_list_item,
695 candidate_auth_tok = &auth_tok_list_item->auth_tok;
696 if (unlikely(ecryptfs_verbosity > 0)) {
697 ecryptfs_printk(KERN_DEBUG,
698 "Considering cadidate auth tok:\n");
699 ecryptfs_dump_auth_tok(candidate_auth_tok);
701 /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
702 if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
703 && !strncmp(candidate_auth_tok->token.password.signature,
704 sig, ECRYPTFS_SIG_SIZE_HEX)) {
707 /* TODO: Transfer the common salt into the
712 if (!found_auth_tok) {
713 ecryptfs_printk(KERN_ERR, "Could not find authentication "
714 "token on temporary list for sig [%.*s]\n",
715 ECRYPTFS_SIG_SIZE_HEX, sig);
719 memcpy(&(candidate_auth_tok->token.password),
720 &(chosen_auth_tok->token.password),
721 sizeof(struct ecryptfs_password));
722 rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
724 ecryptfs_printk(KERN_ERR, "Error decrypting the "
728 rc = ecryptfs_compute_root_iv(crypt_stat);
730 ecryptfs_printk(KERN_ERR, "Error computing "
735 rc = ecryptfs_init_crypt_ctx(crypt_stat);
737 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
738 "context for cipher [%s]; rc = [%d]\n",
739 crypt_stat->cipher, rc);
742 wipe_auth_tok_list(&auth_tok_list);
748 * write_tag_11_packet
749 * @dest: Target into which Tag 11 packet is to be written
750 * @max: Maximum packet length
751 * @contents: Byte array of contents to copy in
752 * @contents_length: Number of bytes in contents
753 * @packet_length: Length of the Tag 11 packet written; zero on error
755 * Returns zero on success; non-zero on error.
758 write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
759 size_t *packet_length)
762 size_t packet_size_length;
764 (*packet_length) = 0;
765 if ((13 + contents_length) > max) {
767 ecryptfs_printk(KERN_ERR, "Packet length larger than "
768 "maximum allowable\n");
771 /* General packet header */
773 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
775 rc = write_packet_length(&dest[(*packet_length)],
776 (13 + contents_length), &packet_size_length);
778 ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
779 "header; cannot generate packet length\n");
782 (*packet_length) += packet_size_length;
783 /* Tag 11 specific */
784 /* One-octet field that describes how the data is formatted */
785 dest[(*packet_length)++] = 0x62; /* binary data */
786 /* One-octet filename length followed by filename */
787 dest[(*packet_length)++] = 8;
788 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
789 (*packet_length) += 8;
790 /* Four-octet number indicating modification date */
791 memset(&dest[(*packet_length)], 0x00, 4);
792 (*packet_length) += 4;
793 /* Remainder is literal data */
794 memcpy(&dest[(*packet_length)], contents, contents_length);
795 (*packet_length) += contents_length;
798 (*packet_length) = 0;
804 * @dest: Buffer into which to write the packet
805 * @max: Maximum number of bytes that can be written
806 * @auth_tok: Authentication token
807 * @crypt_stat: The cryptographic context
808 * @key_rec: encrypted key
809 * @packet_size: This function will write the number of bytes that end
810 * up constituting the packet; set to zero on error
812 * Returns zero on success; non-zero on error.
815 write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
816 struct ecryptfs_crypt_stat *crypt_stat,
817 struct ecryptfs_key_record *key_rec, size_t *packet_size)
820 size_t signature_is_valid = 0;
821 size_t encrypted_session_key_valid = 0;
822 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
823 struct scatterlist dest_sg[2];
824 struct scatterlist src_sg[2];
825 struct mutex *tfm_mutex = NULL;
827 size_t packet_size_length;
829 struct blkcipher_desc desc = {
831 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
836 /* Check for a valid signature on the auth_tok */
837 for (i = 0; i < ECRYPTFS_SIG_SIZE_HEX; i++)
838 signature_is_valid |= auth_tok->token.password.signature[i];
839 if (!signature_is_valid)
841 ecryptfs_from_hex((*key_rec).sig, auth_tok->token.password.signature,
843 encrypted_session_key_valid = 0;
844 for (i = 0; i < crypt_stat->key_size; i++)
845 encrypted_session_key_valid |=
846 auth_tok->session_key.encrypted_key[i];
847 if (encrypted_session_key_valid) {
848 memcpy((*key_rec).enc_key,
849 auth_tok->session_key.encrypted_key,
850 auth_tok->session_key.encrypted_key_size);
851 goto encrypted_session_key_set;
853 if (auth_tok->session_key.encrypted_key_size == 0)
854 auth_tok->session_key.encrypted_key_size =
855 crypt_stat->key_size;
856 if (crypt_stat->key_size == 24
857 && strcmp("aes", crypt_stat->cipher) == 0) {
858 memset((crypt_stat->key + 24), 0, 8);
859 auth_tok->session_key.encrypted_key_size = 32;
861 (*key_rec).enc_key_size =
862 auth_tok->session_key.encrypted_key_size;
863 if (ECRYPTFS_CHECK_FLAG(auth_tok->token.password.flags,
864 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET)) {
865 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
866 "session key encryption key of size [%d]\n",
867 auth_tok->token.password.
868 session_key_encryption_key_bytes);
869 memcpy(session_key_encryption_key,
870 auth_tok->token.password.session_key_encryption_key,
871 crypt_stat->key_size);
872 ecryptfs_printk(KERN_DEBUG,
873 "Cached session key " "encryption key: \n");
874 if (ecryptfs_verbosity > 0)
875 ecryptfs_dump_hex(session_key_encryption_key, 16);
877 if (unlikely(ecryptfs_verbosity > 0)) {
878 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
879 ecryptfs_dump_hex(session_key_encryption_key, 16);
881 rc = virt_to_scatterlist(crypt_stat->key,
882 (*key_rec).enc_key_size, src_sg, 2);
884 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
885 "for crypt_stat session key\n");
889 rc = virt_to_scatterlist((*key_rec).enc_key,
890 (*key_rec).enc_key_size, dest_sg, 2);
892 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
893 "for crypt_stat encrypted session key\n");
897 if (!strcmp(crypt_stat->cipher,
898 crypt_stat->mount_crypt_stat->global_default_cipher_name)
899 && crypt_stat->mount_crypt_stat->global_key_tfm) {
900 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
901 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
905 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
910 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
912 kfree(full_alg_name);
913 if (IS_ERR(desc.tfm)) {
914 rc = PTR_ERR(desc.tfm);
915 ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
916 "context for cipher [%s]; rc = [%d]\n",
917 crypt_stat->cipher, rc);
920 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
923 mutex_lock(tfm_mutex);
924 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
925 crypt_stat->key_size);
928 mutex_unlock(tfm_mutex);
929 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
930 "context; rc = [%d]\n", rc);
934 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
935 crypt_stat->key_size);
936 rc = crypto_blkcipher_encrypt(&desc, dest_sg, src_sg,
937 (*key_rec).enc_key_size);
939 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
943 mutex_unlock(tfm_mutex);
944 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
945 if (ecryptfs_verbosity > 0)
946 ecryptfs_dump_hex((*key_rec).enc_key,
947 (*key_rec).enc_key_size);
948 encrypted_session_key_set:
949 /* Now we have a valid key_rec. Append it to the
951 key_rec_size = (sizeof(struct ecryptfs_key_record)
952 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
953 + ((*key_rec).enc_key_size));
954 /* TODO: Include a packet size limit as a parameter to this
955 * function once we have multi-packet headers (for versions
957 if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
958 ecryptfs_printk(KERN_ERR, "Keyset too large\n");
962 /* TODO: Packet size limit */
963 /* We have 5 bytes of surrounding packet data */
964 if ((0x05 + ECRYPTFS_SALT_SIZE
965 + (*key_rec).enc_key_size) >= max) {
966 ecryptfs_printk(KERN_ERR, "Authentication token is too "
971 /* This format is inspired by OpenPGP; see RFC 2440
973 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
974 /* ver+cipher+s2k+hash+salt+iter+enc_key */
975 rc = write_packet_length(&dest[(*packet_size)],
976 (0x05 + ECRYPTFS_SALT_SIZE
977 + (*key_rec).enc_key_size),
978 &packet_size_length);
980 ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
981 "header; cannot generate packet length\n");
984 (*packet_size) += packet_size_length;
985 dest[(*packet_size)++] = 0x04; /* version 4 */
986 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
987 if (cipher_code == 0) {
988 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
989 "cipher [%s]\n", crypt_stat->cipher);
993 dest[(*packet_size)++] = cipher_code;
994 dest[(*packet_size)++] = 0x03; /* S2K */
995 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
996 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
998 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
999 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
1000 memcpy(&dest[(*packet_size)], (*key_rec).enc_key,
1001 (*key_rec).enc_key_size);
1002 (*packet_size) += (*key_rec).enc_key_size;
1004 if (desc.tfm && !tfm_mutex)
1005 crypto_free_blkcipher(desc.tfm);
1012 * ecryptfs_generate_key_packet_set
1013 * @dest: Virtual address from which to write the key record set
1014 * @crypt_stat: The cryptographic context from which the
1015 * authentication tokens will be retrieved
1016 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1017 * for the global parameters
1018 * @len: The amount written
1019 * @max: The maximum amount of data allowed to be written
1021 * Generates a key packet set and writes it to the virtual address
1024 * Returns zero on success; non-zero on error.
1027 ecryptfs_generate_key_packet_set(char *dest_base,
1028 struct ecryptfs_crypt_stat *crypt_stat,
1029 struct dentry *ecryptfs_dentry, size_t *len,
1033 struct ecryptfs_auth_tok *auth_tok;
1034 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1035 &ecryptfs_superblock_to_private(
1036 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1038 struct ecryptfs_key_record key_rec;
1041 if (mount_crypt_stat->global_auth_tok) {
1042 auth_tok = mount_crypt_stat->global_auth_tok;
1043 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1044 rc = write_tag_3_packet((dest_base + (*len)),
1046 crypt_stat, &key_rec,
1049 ecryptfs_printk(KERN_WARNING, "Error "
1050 "writing tag 3 packet\n");
1054 /* Write auth tok signature packet */
1055 rc = write_tag_11_packet(
1056 (dest_base + (*len)),
1058 key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
1060 ecryptfs_printk(KERN_ERR, "Error writing "
1061 "auth tok signature packet\n");
1066 ecryptfs_printk(KERN_WARNING, "Unsupported "
1067 "authentication token type\n");
1072 ecryptfs_printk(KERN_WARNING, "Error writing "
1073 "authentication token packet with sig "
1075 mount_crypt_stat->global_auth_tok_sig);
1081 if (likely((max - (*len)) > 0)) {
1082 dest_base[(*len)] = 0x00;
1084 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");