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