xref: /linux/security/keys/encrypted-keys/ecryptfs_format.c (revision a79860800e0b500902baf745c259625e48a403db)
161cf45d0SMimi Zohar /*
261cf45d0SMimi Zohar  * ecryptfs_format.c: helper functions for the encrypted key type
361cf45d0SMimi Zohar  *
461cf45d0SMimi Zohar  * Copyright (C) 2006 International Business Machines Corp.
561cf45d0SMimi Zohar  * Copyright (C) 2010 Politecnico di Torino, Italy
661cf45d0SMimi Zohar  *                    TORSEC group -- http://security.polito.it
761cf45d0SMimi Zohar  *
861cf45d0SMimi Zohar  * Authors:
961cf45d0SMimi Zohar  * Michael A. Halcrow <mahalcro@us.ibm.com>
1061cf45d0SMimi Zohar  * Tyler Hicks <tyhicks@ou.edu>
1161cf45d0SMimi Zohar  * Roberto Sassu <roberto.sassu@polito.it>
1261cf45d0SMimi Zohar  *
1361cf45d0SMimi Zohar  * This program is free software; you can redistribute it and/or modify
1461cf45d0SMimi Zohar  * it under the terms of the GNU General Public License as published by
1561cf45d0SMimi Zohar  * the Free Software Foundation, version 2 of the License.
1661cf45d0SMimi Zohar  */
1761cf45d0SMimi Zohar 
18*a7986080SPaul Gortmaker #include <linux/export.h>
19*a7986080SPaul Gortmaker #include <linux/string.h>
2061cf45d0SMimi Zohar #include "ecryptfs_format.h"
2161cf45d0SMimi Zohar 
2261cf45d0SMimi Zohar u8 *ecryptfs_get_auth_tok_key(struct ecryptfs_auth_tok *auth_tok)
2361cf45d0SMimi Zohar {
2461cf45d0SMimi Zohar 	return auth_tok->token.password.session_key_encryption_key;
2561cf45d0SMimi Zohar }
2661cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_get_auth_tok_key);
2761cf45d0SMimi Zohar 
2861cf45d0SMimi Zohar /*
2961cf45d0SMimi Zohar  * ecryptfs_get_versions()
3061cf45d0SMimi Zohar  *
3161cf45d0SMimi Zohar  * Source code taken from the software 'ecryptfs-utils' version 83.
3261cf45d0SMimi Zohar  *
3361cf45d0SMimi Zohar  */
3461cf45d0SMimi Zohar void ecryptfs_get_versions(int *major, int *minor, int *file_version)
3561cf45d0SMimi Zohar {
3661cf45d0SMimi Zohar 	*major = ECRYPTFS_VERSION_MAJOR;
3761cf45d0SMimi Zohar 	*minor = ECRYPTFS_VERSION_MINOR;
3861cf45d0SMimi Zohar 	if (file_version)
3961cf45d0SMimi Zohar 		*file_version = ECRYPTFS_SUPPORTED_FILE_VERSION;
4061cf45d0SMimi Zohar }
4161cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_get_versions);
4261cf45d0SMimi Zohar 
4361cf45d0SMimi Zohar /*
4461cf45d0SMimi Zohar  * ecryptfs_fill_auth_tok - fill the ecryptfs_auth_tok structure
4561cf45d0SMimi Zohar  *
4661cf45d0SMimi Zohar  * Fill the ecryptfs_auth_tok structure with required ecryptfs data.
4761cf45d0SMimi Zohar  * The source code is inspired to the original function generate_payload()
4861cf45d0SMimi Zohar  * shipped with the software 'ecryptfs-utils' version 83.
4961cf45d0SMimi Zohar  *
5061cf45d0SMimi Zohar  */
5161cf45d0SMimi Zohar int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok,
5261cf45d0SMimi Zohar 			   const char *key_desc)
5361cf45d0SMimi Zohar {
5461cf45d0SMimi Zohar 	int major, minor;
5561cf45d0SMimi Zohar 
5661cf45d0SMimi Zohar 	ecryptfs_get_versions(&major, &minor, NULL);
5761cf45d0SMimi Zohar 	auth_tok->version = (((uint16_t)(major << 8) & 0xFF00)
5861cf45d0SMimi Zohar 			     | ((uint16_t)minor & 0x00FF));
5961cf45d0SMimi Zohar 	auth_tok->token_type = ECRYPTFS_PASSWORD;
6061cf45d0SMimi Zohar 	strncpy((char *)auth_tok->token.password.signature, key_desc,
6161cf45d0SMimi Zohar 		ECRYPTFS_PASSWORD_SIG_SIZE);
6261cf45d0SMimi Zohar 	auth_tok->token.password.session_key_encryption_key_bytes =
6361cf45d0SMimi Zohar 		ECRYPTFS_MAX_KEY_BYTES;
6461cf45d0SMimi Zohar 	/*
6561cf45d0SMimi Zohar 	 * Removed auth_tok->token.password.salt and
6661cf45d0SMimi Zohar 	 * auth_tok->token.password.session_key_encryption_key
6761cf45d0SMimi Zohar 	 * initialization from the original code
6861cf45d0SMimi Zohar 	 */
6961cf45d0SMimi Zohar 	/* TODO: Make the hash parameterizable via policy */
7061cf45d0SMimi Zohar 	auth_tok->token.password.flags |=
7161cf45d0SMimi Zohar 		ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET;
7261cf45d0SMimi Zohar 	/* The kernel code will encrypt the session key. */
7361cf45d0SMimi Zohar 	auth_tok->session_key.encrypted_key[0] = 0;
7461cf45d0SMimi Zohar 	auth_tok->session_key.encrypted_key_size = 0;
7561cf45d0SMimi Zohar 	/* Default; subject to change by kernel eCryptfs */
7661cf45d0SMimi Zohar 	auth_tok->token.password.hash_algo = PGP_DIGEST_ALGO_SHA512;
7761cf45d0SMimi Zohar 	auth_tok->token.password.flags &= ~(ECRYPTFS_PERSISTENT_PASSWORD);
7861cf45d0SMimi Zohar 	return 0;
7961cf45d0SMimi Zohar }
8061cf45d0SMimi Zohar EXPORT_SYMBOL(ecryptfs_fill_auth_tok);
81