keyinfo.c 7.1 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
#include <keys/encrypted-type.h>
#include <keys/user-type.h>
#include <linux/random.h>
#include <linux/scatterlist.h>
#include <uapi/linux/keyctl.h>
16
#include <linux/fscrypto.h>
17 18 19

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

	if (rc == -EINPROGRESS)
		return;

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

/**
30
 * derive_key_aes() - Derive a key using AES-128-ECB
31
 * @deriving_key: Encryption key used for derivation.
32 33 34 35 36
 * @source_key:   Source key to which to apply derivation.
 * @derived_key:  Derived key.
 *
 * Return: Zero on success; non-zero otherwise.
 */
37 38 39
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])
40 41
{
	int res = 0;
42
	struct skcipher_request *req = NULL;
43
	DECLARE_FS_COMPLETION_RESULT(ecr);
44
	struct scatterlist src_sg, dst_sg;
45
	struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
46 47 48 49 50 51

	if (IS_ERR(tfm)) {
		res = PTR_ERR(tfm);
		tfm = NULL;
		goto out;
	}
52 53
	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
	req = skcipher_request_alloc(tfm, GFP_NOFS);
54 55 56 57
	if (!req) {
		res = -ENOMEM;
		goto out;
	}
58
	skcipher_request_set_callback(req,
59 60
			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
			derive_crypt_complete, &ecr);
61
	res = crypto_skcipher_setkey(tfm, deriving_key,
62
					FS_AES_128_ECB_KEY_SIZE);
63 64 65
	if (res < 0)
		goto out;

66 67
	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);
68
	skcipher_request_set_crypt(req, &src_sg, &dst_sg,
69
					FS_AES_256_XTS_KEY_SIZE, NULL);
70
	res = crypto_skcipher_encrypt(req);
71 72 73 74 75
	if (res == -EINPROGRESS || res == -EBUSY) {
		wait_for_completion(&ecr.completion);
		res = ecr.res;
	}
out:
76 77
	skcipher_request_free(req);
	crypto_free_skcipher(tfm);
78 79 80
	return res;
}

81
static void put_crypt_info(struct fscrypt_info *ci)
82 83 84 85
{
	if (!ci)
		return;

86 87
	key_put(ci->ci_keyring_key);
	crypto_free_skcipher(ci->ci_ctfm);
88
	kmem_cache_free(fscrypt_info_cachep, ci);
89 90
}

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

106
	res = fscrypt_initialize();
107 108
	if (res)
		return res;
109 110 111

	if (!inode->i_sb->s_cop->get_context)
		return -EOPNOTSUPP;
112
retry:
113
	crypt_info = ACCESS_ONCE(inode->i_crypt_info);
114 115 116
	if (crypt_info) {
		if (!crypt_info->ci_keyring_key ||
				key_validate(crypt_info->ci_keyring_key) == 0)
117
			return 0;
118
		fscrypt_put_encryption_info(inode, crypt_info);
119
		goto retry;
120 121
	}

122 123 124 125 126 127 128 129
	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)) {
130
		return -EINVAL;
131
	}
132 133
	res = 0;

134
	crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
135 136 137 138 139 140 141
	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;
142
	crypt_info->ci_keyring_key = NULL;
143 144 145
	memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
				sizeof(crypt_info->ci_master_key));
	if (S_ISREG(inode->i_mode))
146
		mode = crypt_info->ci_data_mode;
147
	else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
148
		mode = crypt_info->ci_filename_mode;
149
	else
150
		BUG();
151

152
	switch (mode) {
153
	case FS_ENCRYPTION_MODE_AES_256_XTS:
154 155
		cipher_str = "xts(aes)";
		break;
156
	case FS_ENCRYPTION_MODE_AES_256_CTS:
157 158 159 160
		cipher_str = "cts(cbc(aes))";
		break;
	default:
		printk_once(KERN_WARNING
161 162
			    "%s: unsupported key mode %d (ino %u)\n",
			    __func__, mode, (unsigned) inode->i_ino);
163 164 165
		res = -ENOKEY;
		goto out;
	}
166 167 168 169 170 171 172 173
	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,
174
					ctx.master_key_descriptor);
175 176
	full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
					(2 * FS_KEY_DESCRIPTOR_SIZE)] = '\0';
177 178 179 180 181 182
	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;
	}
183
	crypt_info->ci_keyring_key = keyring_key;
184
	if (keyring_key->type != &key_type_logon) {
185 186
		printk_once(KERN_WARNING
				"%s: key type must be logon\n", __func__);
187 188 189
		res = -ENOKEY;
		goto out;
	}
190
	down_read(&keyring_key->sem);
191
	ukp = user_key_payload(keyring_key);
192
	if (ukp->datalen != sizeof(struct fscrypt_key)) {
193
		res = -EINVAL;
194
		up_read(&keyring_key->sem);
195 196
		goto out;
	}
197 198 199 200
	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) {
201
		printk_once(KERN_WARNING
202 203
				"%s: key size incorrect: %d\n",
				__func__, master_key->size);
204
		res = -ENOKEY;
205
		up_read(&keyring_key->sem);
206 207
		goto out;
	}
208
	res = derive_key_aes(ctx.nonce, master_key->raw, raw_key);
209
	up_read(&keyring_key->sem);
210 211
	if (res)
		goto out;
212
got_key:
213
	ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
214 215 216 217 218 219
	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;
220
	}
221
	crypt_info->ci_ctfm = ctfm;
222 223 224
	crypto_skcipher_clear_flags(ctfm, ~0);
	crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
	res = crypto_skcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
225 226 227 228
	if (res)
		goto out;

	memzero_explicit(raw_key, sizeof(raw_key));
229 230
	if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
		put_crypt_info(crypt_info);
231 232 233 234 235
		goto retry;
	}
	return 0;

out:
236
	if (res == -ENOKEY)
237
		res = 0;
238
	put_crypt_info(crypt_info);
239
	memzero_explicit(raw_key, sizeof(raw_key));
240 241 242
	return res;
}

243
void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
244
{
245 246 247 248 249 250
	struct fscrypt_info *prev;

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

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	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;
271
}
272
EXPORT_SYMBOL(fscrypt_get_encryption_info);