keyinfo.c 7.2 KB
Newer Older
1
/*
2
 * key management facility for FS encryption support.
3 4 5
 *
 * Copyright (C) 2015, Google, Inc.
 *
6
 * This contains encryption key functions.
7 8 9
 *
 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
 */
10

11 12 13 14 15 16
#include <keys/encrypted-type.h>
#include <keys/user-type.h>
#include <linux/random.h>
#include <linux/scatterlist.h>
#include <uapi/linux/keyctl.h>
#include <crypto/hash.h>
17
#include <linux/fscrypto.h>
18 19 20

static void derive_crypt_complete(struct crypto_async_request *req, int rc)
{
21
	struct fscrypt_completion_result *ecr = req->data;
22 23 24 25 26 27 28 29 30

	if (rc == -EINPROGRESS)
		return;

	ecr->res = rc;
	complete(&ecr->completion);
}

/**
31
 * derive_key_aes() - Derive a key using AES-128-ECB
32
 * @deriving_key: Encryption key used for derivation.
33 34 35 36 37
 * @source_key:   Source key to which to apply derivation.
 * @derived_key:  Derived key.
 *
 * Return: Zero on success; non-zero otherwise.
 */
38 39 40
static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
				u8 source_key[FS_AES_256_XTS_KEY_SIZE],
				u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
41 42 43
{
	int res = 0;
	struct ablkcipher_request *req = NULL;
44
	DECLARE_FS_COMPLETION_RESULT(ecr);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	struct scatterlist src_sg, dst_sg;
	struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
								0);

	if (IS_ERR(tfm)) {
		res = PTR_ERR(tfm);
		tfm = NULL;
		goto out;
	}
	crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
	req = ablkcipher_request_alloc(tfm, GFP_NOFS);
	if (!req) {
		res = -ENOMEM;
		goto out;
	}
	ablkcipher_request_set_callback(req,
			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
			derive_crypt_complete, &ecr);
	res = crypto_ablkcipher_setkey(tfm, deriving_key,
64
					FS_AES_128_ECB_KEY_SIZE);
65 66 67
	if (res < 0)
		goto out;

68 69
	sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
	sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
70
	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
71
					FS_AES_256_XTS_KEY_SIZE, NULL);
72 73 74 75 76 77 78 79 80 81 82 83 84
	res = crypto_ablkcipher_encrypt(req);
	if (res == -EINPROGRESS || res == -EBUSY) {
		wait_for_completion(&ecr.completion);
		res = ecr.res;
	}
out:
	if (req)
		ablkcipher_request_free(req);
	if (tfm)
		crypto_free_ablkcipher(tfm);
	return res;
}

85
static void put_crypt_info(struct fscrypt_info *ci)
86 87 88 89
{
	if (!ci)
		return;

90 91
	if (ci->ci_keyring_key)
		key_put(ci->ci_keyring_key);
92
	crypto_free_ablkcipher(ci->ci_ctfm);
93
	kmem_cache_free(fscrypt_info_cachep, ci);
94 95
}

96
int get_crypt_info(struct inode *inode)
97
{
98 99 100
	struct fscrypt_info *crypt_info;
	u8 full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
				(FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
101
	struct key *keyring_key = NULL;
102 103
	struct fscrypt_key *master_key;
	struct fscrypt_context ctx;
104
	const struct user_key_payload *ukp;
105 106
	struct crypto_ablkcipher *ctfm;
	const char *cipher_str;
107 108
	u8 raw_key[FS_MAX_KEY_SIZE];
	u8 mode;
109 110
	int res;

111
	res = fscrypt_initialize();
112 113
	if (res)
		return res;
114 115 116

	if (!inode->i_sb->s_cop->get_context)
		return -EOPNOTSUPP;
117
retry:
118
	crypt_info = ACCESS_ONCE(inode->i_crypt_info);
119 120 121
	if (crypt_info) {
		if (!crypt_info->ci_keyring_key ||
				key_validate(crypt_info->ci_keyring_key) == 0)
122
			return 0;
123
		fscrypt_put_encryption_info(inode, crypt_info);
124
		goto retry;
125 126
	}

127 128 129 130 131 132 133 134
	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
	if (res < 0) {
		if (!fscrypt_dummy_context_enabled(inode))
			return res;
		ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
		ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
		ctx.flags = 0;
	} else if (res != sizeof(ctx)) {
135
		return -EINVAL;
136
	}
137 138
	res = 0;

139
	crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
140 141 142 143 144 145 146
	if (!crypt_info)
		return -ENOMEM;

	crypt_info->ci_flags = ctx.flags;
	crypt_info->ci_data_mode = ctx.contents_encryption_mode;
	crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
	crypt_info->ci_ctfm = NULL;
147
	crypt_info->ci_keyring_key = NULL;
148 149 150
	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
				sizeof(crypt_info->ci_master_key));
	if (S_ISREG(inode->i_mode))
151
		mode = crypt_info->ci_data_mode;
152
	else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
153
		mode = crypt_info->ci_filename_mode;
154
	else
155
		BUG();
156

157
	switch (mode) {
158
	case FS_ENCRYPTION_MODE_AES_256_XTS:
159 160
		cipher_str = "xts(aes)";
		break;
161
	case FS_ENCRYPTION_MODE_AES_256_CTS:
162 163 164 165
		cipher_str = "cts(cbc(aes))";
		break;
	default:
		printk_once(KERN_WARNING
166 167
			    "%s: unsupported key mode %d (ino %u)\n",
			    __func__, mode, (unsigned) inode->i_ino);
168 169 170
		res = -ENOKEY;
		goto out;
	}
171 172 173 174 175 176 177 178
	if (fscrypt_dummy_context_enabled(inode)) {
		memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
		goto got_key;
	}
	memcpy(full_key_descriptor, FS_KEY_DESC_PREFIX,
					FS_KEY_DESC_PREFIX_SIZE);
	sprintf(full_key_descriptor + FS_KEY_DESC_PREFIX_SIZE,
					"%*phN", FS_KEY_DESCRIPTOR_SIZE,
179
					ctx.master_key_descriptor);
180 181
	full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
					(2 * FS_KEY_DESCRIPTOR_SIZE)] = '\0';
182 183 184 185 186 187
	keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
	if (IS_ERR(keyring_key)) {
		res = PTR_ERR(keyring_key);
		keyring_key = NULL;
		goto out;
	}
188
	crypt_info->ci_keyring_key = keyring_key;
189
	if (keyring_key->type != &key_type_logon) {
190 191
		printk_once(KERN_WARNING
				"%s: key type must be logon\n", __func__);
192 193 194
		res = -ENOKEY;
		goto out;
	}
195
	down_read(&keyring_key->sem);
196
	ukp = user_key_payload(keyring_key);
197
	if (ukp->datalen != sizeof(struct fscrypt_key)) {
198
		res = -EINVAL;
199
		up_read(&keyring_key->sem);
200 201
		goto out;
	}
202 203 204 205
	master_key = (struct fscrypt_key *)ukp->data;
	BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);

	if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
206
		printk_once(KERN_WARNING
207 208
				"%s: key size incorrect: %d\n",
				__func__, master_key->size);
209
		res = -ENOKEY;
210
		up_read(&keyring_key->sem);
211 212
		goto out;
	}
213
	res = derive_key_aes(ctx.nonce, master_key->raw, raw_key);
214
	up_read(&keyring_key->sem);
215 216
	if (res)
		goto out;
217
got_key:
218 219 220 221 222 223 224
	ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
	if (!ctfm || IS_ERR(ctfm)) {
		res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
		printk(KERN_DEBUG
		       "%s: error %d (inode %u) allocating crypto tfm\n",
		       __func__, res, (unsigned) inode->i_ino);
		goto out;
225
	}
226 227 228
	crypt_info->ci_ctfm = ctfm;
	crypto_ablkcipher_clear_flags(ctfm, ~0);
	crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
229 230
					CRYPTO_TFM_REQ_WEAK_KEY);
	res = crypto_ablkcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
231 232 233 234
	if (res)
		goto out;

	memzero_explicit(raw_key, sizeof(raw_key));
235 236
	if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
		put_crypt_info(crypt_info);
237 238 239 240 241
		goto retry;
	}
	return 0;

out:
242
	if (res == -ENOKEY)
243
		res = 0;
244
	put_crypt_info(crypt_info);
245
	memzero_explicit(raw_key, sizeof(raw_key));
246 247 248
	return res;
}

249
void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
250
{
251 252 253 254 255 256
	struct fscrypt_info *prev;

	if (ci == NULL)
		ci = ACCESS_ONCE(inode->i_crypt_info);
	if (ci == NULL)
		return;
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
	if (prev != ci)
		return;

	put_crypt_info(ci);
}
EXPORT_SYMBOL(fscrypt_put_encryption_info);

int fscrypt_get_encryption_info(struct inode *inode)
{
	struct fscrypt_info *ci = inode->i_crypt_info;

	if (!ci ||
		(ci->ci_keyring_key &&
		 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
					       (1 << KEY_FLAG_REVOKED) |
					       (1 << KEY_FLAG_DEAD)))))
		return get_crypt_info(inode);
	return 0;
277
}
278
EXPORT_SYMBOL(fscrypt_get_encryption_info);