1 ext4 crypto: add encryption key management facilities
3 From: Michael Halcrow <mhalcrow@google.com>
5 Change-Id: I0cb5711a628554d3b05cefd15740d8346115cbaa
6 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
7 Signed-off-by: Ildar Muslukhov <muslukhovi@gmail.com>
8 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
10 fs/ext4/Makefile | 2 +-
11 fs/ext4/crypto_key.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 fs/ext4/ext4.h | 13 +++++++
13 fs/ext4/ext4_crypto.h | 3 ++
14 4 files changed, 179 insertions(+), 1 deletion(-)
16 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
17 index 1b1c561..4e5af21 100644
18 --- a/fs/ext4/Makefile
19 +++ b/fs/ext4/Makefile
20 @@ -12,4 +12,4 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
22 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
23 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
24 -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o
25 +ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o crypto_key.o
26 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
28 index 0000000..572bd97
30 +++ b/fs/ext4/crypto_key.c
33 + * linux/fs/ext4/crypto_key.c
35 + * Copyright (C) 2015, Google, Inc.
37 + * This contains encryption key functions for ext4
39 + * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
42 +#include <keys/encrypted-type.h>
43 +#include <keys/user-type.h>
44 +#include <linux/random.h>
45 +#include <linux/scatterlist.h>
46 +#include <uapi/linux/keyctl.h>
51 +static void derive_crypt_complete(struct crypto_async_request *req, int rc)
53 + struct ext4_completion_result *ecr = req->data;
55 + if (rc == -EINPROGRESS)
59 + complete(&ecr->completion);
63 + * ext4_derive_key_aes() - Derive a key using AES-128-ECB
64 + * @deriving_key: Encryption key used for derivatio.
65 + * @source_key: Source key to which to apply derivation.
66 + * @derived_key: Derived key.
68 + * Return: Zero on success; non-zero otherwise.
70 +static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
71 + char source_key[EXT4_AES_256_XTS_KEY_SIZE],
72 + char derived_key[EXT4_AES_256_XTS_KEY_SIZE])
75 + struct ablkcipher_request *req = NULL;
76 + DECLARE_EXT4_COMPLETION_RESULT(ecr);
77 + struct scatterlist src_sg, dst_sg;
78 + struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
86 + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
87 + req = ablkcipher_request_alloc(tfm, GFP_NOFS);
92 + ablkcipher_request_set_callback(req,
93 + CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
94 + derive_crypt_complete, &ecr);
95 + res = crypto_ablkcipher_setkey(tfm, deriving_key,
96 + EXT4_AES_128_ECB_KEY_SIZE);
99 + sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE);
100 + sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE);
101 + ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
102 + EXT4_AES_256_XTS_KEY_SIZE, NULL);
103 + res = crypto_ablkcipher_encrypt(req);
104 + if (res == -EINPROGRESS || res == -EBUSY) {
105 + BUG_ON(req->base.data != &ecr);
106 + wait_for_completion(&ecr.completion);
112 + ablkcipher_request_free(req);
114 + crypto_free_ablkcipher(tfm);
119 + * ext4_generate_encryption_key() - generates an encryption key
120 + * @inode: The inode to generate the encryption key for.
122 +int ext4_generate_encryption_key(struct inode *inode)
124 + struct ext4_inode_info *ei = EXT4_I(inode);
125 + struct ext4_encryption_key *crypt_key = &ei->i_encryption_key;
126 + char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
127 + (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
128 + struct key *keyring_key = NULL;
129 + struct ext4_encryption_key *master_key;
130 + struct ext4_encryption_context ctx;
131 + struct user_key_payload *ukp;
132 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
133 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
134 + &ctx, sizeof(ctx));
136 + if (res != sizeof(ctx)) {
143 + memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
144 + EXT4_KEY_DESC_PREFIX_SIZE);
145 + sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
146 + "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
147 + ctx.master_key_descriptor);
148 + full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
149 + (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
150 + keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
151 + if (IS_ERR(keyring_key)) {
152 + res = PTR_ERR(keyring_key);
153 + keyring_key = NULL;
156 + BUG_ON(keyring_key->type != &key_type_logon);
157 + ukp = ((struct user_key_payload *)keyring_key->payload.data);
158 + if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
162 + master_key = (struct ext4_encryption_key *)ukp->data;
164 + if (S_ISREG(inode->i_mode))
165 + crypt_key->mode = ctx.contents_encryption_mode;
166 + else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
167 + crypt_key->mode = ctx.filenames_encryption_mode;
169 + printk(KERN_ERR "ext4 crypto: Unsupported inode type.\n");
172 + crypt_key->size = ext4_encryption_key_size(crypt_key->mode);
173 + BUG_ON(!crypt_key->size);
174 + BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
175 + EXT4_KEY_DERIVATION_NONCE_SIZE);
176 + BUG_ON(master_key->size != EXT4_AES_256_XTS_KEY_SIZE);
177 + BUG_ON(crypt_key->size < EXT4_AES_256_CBC_KEY_SIZE);
178 + res = ext4_derive_key_aes(ctx.nonce, master_key->raw, crypt_key->raw);
181 + key_put(keyring_key);
183 + crypt_key->mode = EXT4_ENCRYPTION_MODE_INVALID;
187 +int ext4_has_encryption_key(struct inode *inode)
189 + struct ext4_inode_info *ei = EXT4_I(inode);
190 + struct ext4_encryption_key *crypt_key = &ei->i_encryption_key;
192 + return (crypt_key->mode != EXT4_ENCRYPTION_MODE_INVALID);
194 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
195 index 4533e94..b5255eb 100644
198 @@ -2064,6 +2064,19 @@ static inline int ext4_sb_has_crypto(struct super_block *sb)
203 +int ext4_generate_encryption_key(struct inode *inode);
205 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
206 +int ext4_has_encryption_key(struct inode *inode);
208 +static inline int ext4_has_encryption_key(struct inode *inode)
216 extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
218 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
219 index 9d5d2e5..6a7c0c0 100644
220 --- a/fs/ext4/ext4_crypto.h
221 +++ b/fs/ext4/ext4_crypto.h
222 @@ -55,6 +55,9 @@ struct ext4_encryption_context {
223 #define EXT4_AES_256_XTS_KEY_SIZE 64
224 #define EXT4_MAX_KEY_SIZE 64
226 +#define EXT4_KEY_DESC_PREFIX "ext4:"
227 +#define EXT4_KEY_DESC_PREFIX_SIZE 5
229 struct ext4_encryption_key {
231 char raw[EXT4_MAX_KEY_SIZE];