xref: /linux/security/keys/encrypted-keys/ecryptfs_format.c (revision 61cf45d0199041df1a8ba334b6bf4a3a13b7f904)
1*61cf45d0SMimi Zohar /*
2*61cf45d0SMimi Zohar  * ecryptfs_format.c: helper functions for the encrypted key type
3*61cf45d0SMimi Zohar  *
4*61cf45d0SMimi Zohar  * Copyright (C) 2006 International Business Machines Corp.
5*61cf45d0SMimi Zohar  * Copyright (C) 2010 Politecnico di Torino, Italy
6*61cf45d0SMimi Zohar  *                    TORSEC group -- http://security.polito.it
7*61cf45d0SMimi Zohar  *
8*61cf45d0SMimi Zohar  * Authors:
9*61cf45d0SMimi Zohar  * Michael A. Halcrow <mahalcro@us.ibm.com>
10*61cf45d0SMimi Zohar  * Tyler Hicks <tyhicks@ou.edu>
11*61cf45d0SMimi Zohar  * Roberto Sassu <roberto.sassu@polito.it>
12*61cf45d0SMimi Zohar  *
13*61cf45d0SMimi Zohar  * This program is free software; you can redistribute it and/or modify
14*61cf45d0SMimi Zohar  * it under the terms of the GNU General Public License as published by
15*61cf45d0SMimi Zohar  * the Free Software Foundation, version 2 of the License.
16*61cf45d0SMimi Zohar  */
17*61cf45d0SMimi Zohar 
18*61cf45d0SMimi Zohar #include <linux/module.h>
19*61cf45d0SMimi Zohar #include "ecryptfs_format.h"
20*61cf45d0SMimi Zohar 
21*61cf45d0SMimi Zohar u8 *ecryptfs_get_auth_tok_key(struct ecryptfs_auth_tok *auth_tok)
22*61cf45d0SMimi Zohar {
23*61cf45d0SMimi Zohar 	return auth_tok->token.password.session_key_encryption_key;
24*61cf45d0SMimi Zohar }
25*61cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_get_auth_tok_key);
26*61cf45d0SMimi Zohar 
27*61cf45d0SMimi Zohar /*
28*61cf45d0SMimi Zohar  * ecryptfs_get_versions()
29*61cf45d0SMimi Zohar  *
30*61cf45d0SMimi Zohar  * Source code taken from the software 'ecryptfs-utils' version 83.
31*61cf45d0SMimi Zohar  *
32*61cf45d0SMimi Zohar  */
33*61cf45d0SMimi Zohar void ecryptfs_get_versions(int *major, int *minor, int *file_version)
34*61cf45d0SMimi Zohar {
35*61cf45d0SMimi Zohar 	*major = ECRYPTFS_VERSION_MAJOR;
36*61cf45d0SMimi Zohar 	*minor = ECRYPTFS_VERSION_MINOR;
37*61cf45d0SMimi Zohar 	if (file_version)
38*61cf45d0SMimi Zohar 		*file_version = ECRYPTFS_SUPPORTED_FILE_VERSION;
39*61cf45d0SMimi Zohar }
40*61cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_get_versions);
41*61cf45d0SMimi Zohar 
42*61cf45d0SMimi Zohar /*
43*61cf45d0SMimi Zohar  * ecryptfs_fill_auth_tok - fill the ecryptfs_auth_tok structure
44*61cf45d0SMimi Zohar  *
45*61cf45d0SMimi Zohar  * Fill the ecryptfs_auth_tok structure with required ecryptfs data.
46*61cf45d0SMimi Zohar  * The source code is inspired to the original function generate_payload()
47*61cf45d0SMimi Zohar  * shipped with the software 'ecryptfs-utils' version 83.
48*61cf45d0SMimi Zohar  *
49*61cf45d0SMimi Zohar  */
50*61cf45d0SMimi Zohar int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok,
51*61cf45d0SMimi Zohar 			   const char *key_desc)
52*61cf45d0SMimi Zohar {
53*61cf45d0SMimi Zohar 	int major, minor;
54*61cf45d0SMimi Zohar 
55*61cf45d0SMimi Zohar 	ecryptfs_get_versions(&major, &minor, NULL);
56*61cf45d0SMimi Zohar 	auth_tok->version = (((uint16_t)(major << 8) & 0xFF00)
57*61cf45d0SMimi Zohar 			     | ((uint16_t)minor & 0x00FF));
58*61cf45d0SMimi Zohar 	auth_tok->token_type = ECRYPTFS_PASSWORD;
59*61cf45d0SMimi Zohar 	strncpy((char *)auth_tok->token.password.signature, key_desc,
60*61cf45d0SMimi Zohar 		ECRYPTFS_PASSWORD_SIG_SIZE);
61*61cf45d0SMimi Zohar 	auth_tok->token.password.session_key_encryption_key_bytes =
62*61cf45d0SMimi Zohar 		ECRYPTFS_MAX_KEY_BYTES;
63*61cf45d0SMimi Zohar 	/*
64*61cf45d0SMimi Zohar 	 * Removed auth_tok->token.password.salt and
65*61cf45d0SMimi Zohar 	 * auth_tok->token.password.session_key_encryption_key
66*61cf45d0SMimi Zohar 	 * initialization from the original code
67*61cf45d0SMimi Zohar 	 */
68*61cf45d0SMimi Zohar 	/* TODO: Make the hash parameterizable via policy */
69*61cf45d0SMimi Zohar 	auth_tok->token.password.flags |=
70*61cf45d0SMimi Zohar 		ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET;
71*61cf45d0SMimi Zohar 	/* The kernel code will encrypt the session key. */
72*61cf45d0SMimi Zohar 	auth_tok->session_key.encrypted_key[0] = 0;
73*61cf45d0SMimi Zohar 	auth_tok->session_key.encrypted_key_size = 0;
74*61cf45d0SMimi Zohar 	/* Default; subject to change by kernel eCryptfs */
75*61cf45d0SMimi Zohar 	auth_tok->token.password.hash_algo = PGP_DIGEST_ALGO_SHA512;
76*61cf45d0SMimi Zohar 	auth_tok->token.password.flags &= ~(ECRYPTFS_PERSISTENT_PASSWORD);
77*61cf45d0SMimi Zohar 	return 0;
78*61cf45d0SMimi Zohar }
79*61cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_fill_auth_tok);
80*61cf45d0SMimi Zohar 
81*61cf45d0SMimi Zohar MODULE_LICENSE("GPL");
82