Clean up whitespace and fixed a bug in the context inheritance error path
[ext4-patch-queue.git] / add-encryption-key-management-facilities
blob2cd5b9acb3feebd76d3f0ec31e374a8f0fb8edce
1 ext4 crypto: add encryption key management facilities
3 From: Michael Halcrow <mhalcrow@google.com>
5 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
6 Signed-off-by: Ildar Muslukhov <muslukhovi@gmail.com>
7 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 ---
9  fs/ext4/Makefile      |   2 +-
10  fs/ext4/crypto_key.c  | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11  fs/ext4/ext4.h        |  13 +++++++
12  fs/ext4/ext4_crypto.h |   3 ++
13  4 files changed, 179 insertions(+), 1 deletion(-)
15 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
16 index 1b1c561..4e5af21 100644
17 --- a/fs/ext4/Makefile
18 +++ b/fs/ext4/Makefile
19 @@ -12,4 +12,4 @@ ext4-y        := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
21  ext4-$(CONFIG_EXT4_FS_POSIX_ACL)       += acl.o
22  ext4-$(CONFIG_EXT4_FS_SECURITY)                += xattr_security.o
23 -ext4-$(CONFIG_EXT4_FS_ENCRYPTION)      += crypto_policy.o crypto.o
24 +ext4-$(CONFIG_EXT4_FS_ENCRYPTION)      += crypto_policy.o crypto.o crypto_key.o
25 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
26 new file mode 100644
27 index 0000000..572bd97
28 --- /dev/null
29 +++ b/fs/ext4/crypto_key.c
30 @@ -0,0 +1,162 @@
31 +/*
32 + * linux/fs/ext4/crypto_key.c
33 + *
34 + * Copyright (C) 2015, Google, Inc.
35 + *
36 + * This contains encryption key functions for ext4
37 + *
38 + * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
39 + */
41 +#include <keys/encrypted-type.h>
42 +#include <keys/user-type.h>
43 +#include <linux/random.h>
44 +#include <linux/scatterlist.h>
45 +#include <uapi/linux/keyctl.h>
47 +#include "ext4.h"
48 +#include "xattr.h"
50 +static void derive_crypt_complete(struct crypto_async_request *req, int rc)
52 +       struct ext4_completion_result *ecr = req->data;
54 +       if (rc == -EINPROGRESS)
55 +               return;
57 +       ecr->res = rc;
58 +       complete(&ecr->completion);
61 +/**
62 + * ext4_derive_key_aes() - Derive a key using AES-128-ECB
63 + * @deriving_key: Encryption key used for derivatio.
64 + * @source_key:   Source key to which to apply derivation.
65 + * @derived_key:  Derived key.
66 + *
67 + * Return: Zero on success; non-zero otherwise.
68 + */
69 +static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
70 +                              char source_key[EXT4_AES_256_XTS_KEY_SIZE],
71 +                              char derived_key[EXT4_AES_256_XTS_KEY_SIZE])
73 +       int res = 0;
74 +       struct ablkcipher_request *req = NULL;
75 +       DECLARE_EXT4_COMPLETION_RESULT(ecr);
76 +       struct scatterlist src_sg, dst_sg;
77 +       struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
78 +                                                               0);
80 +       if (IS_ERR(tfm)) {
81 +               res = PTR_ERR(tfm);
82 +               tfm = NULL;
83 +               goto out;
84 +       }
85 +       crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
86 +       req = ablkcipher_request_alloc(tfm, GFP_NOFS);
87 +       if (!req) {
88 +               res = -ENOMEM;
89 +               goto out;
90 +       }
91 +       ablkcipher_request_set_callback(req,
92 +                       CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
93 +                       derive_crypt_complete, &ecr);
94 +       res = crypto_ablkcipher_setkey(tfm, deriving_key,
95 +                                      EXT4_AES_128_ECB_KEY_SIZE);
96 +       if (res < 0)
97 +               goto out;
98 +       sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE);
99 +       sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE);
100 +       ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
101 +                                    EXT4_AES_256_XTS_KEY_SIZE, NULL);
102 +       res = crypto_ablkcipher_encrypt(req);
103 +       if (res == -EINPROGRESS || res == -EBUSY) {
104 +               BUG_ON(req->base.data != &ecr);
105 +               wait_for_completion(&ecr.completion);
106 +               res = ecr.res;
107 +       }
109 +out:
110 +       if (req)
111 +               ablkcipher_request_free(req);
112 +       if (tfm)
113 +               crypto_free_ablkcipher(tfm);
114 +       return res;
117 +/**
118 + * ext4_generate_encryption_key() - generates an encryption key
119 + * @inode: The inode to generate the encryption key for.
120 + */
121 +int ext4_generate_encryption_key(struct inode *inode)
123 +       struct ext4_inode_info *ei = EXT4_I(inode);
124 +       struct ext4_encryption_key *crypt_key = &ei->i_encryption_key;
125 +       char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
126 +                                (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
127 +       struct key *keyring_key = NULL;
128 +       struct ext4_encryption_key *master_key;
129 +       struct ext4_encryption_context ctx;
130 +       struct user_key_payload *ukp;
131 +       int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
132 +                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
133 +                                &ctx, sizeof(ctx));
135 +       if (res != sizeof(ctx)) {
136 +               if (res > 0)
137 +                       res = -EINVAL;
138 +               goto out;
139 +       }
140 +       res = 0;
142 +       memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
143 +              EXT4_KEY_DESC_PREFIX_SIZE);
144 +       sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
145 +               "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
146 +               ctx.master_key_descriptor);
147 +       full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
148 +                           (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
149 +       keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
150 +       if (IS_ERR(keyring_key)) {
151 +               res = PTR_ERR(keyring_key);
152 +               keyring_key = NULL;
153 +               goto out;
154 +       }
155 +       BUG_ON(keyring_key->type != &key_type_logon);
156 +       ukp = ((struct user_key_payload *)keyring_key->payload.data);
157 +       if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
158 +               res = -EINVAL;
159 +               goto out;
160 +       }
161 +       master_key = (struct ext4_encryption_key *)ukp->data;
163 +       if (S_ISREG(inode->i_mode))
164 +               crypt_key->mode = ctx.contents_encryption_mode;
165 +       else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
166 +               crypt_key->mode = ctx.filenames_encryption_mode;
167 +       else {
168 +               printk(KERN_ERR "ext4 crypto: Unsupported inode type.\n");
169 +               BUG();
170 +       }
171 +       crypt_key->size = ext4_encryption_key_size(crypt_key->mode);
172 +       BUG_ON(!crypt_key->size);
173 +       BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
174 +                    EXT4_KEY_DERIVATION_NONCE_SIZE);
175 +       BUG_ON(master_key->size != EXT4_AES_256_XTS_KEY_SIZE);
176 +       BUG_ON(crypt_key->size < EXT4_AES_256_CBC_KEY_SIZE);
177 +       res = ext4_derive_key_aes(ctx.nonce, master_key->raw, crypt_key->raw);
178 +out:
179 +       if (keyring_key)
180 +               key_put(keyring_key);
181 +       if (res < 0)
182 +               crypt_key->mode = EXT4_ENCRYPTION_MODE_INVALID;
183 +       return res;
186 +int ext4_has_encryption_key(struct inode *inode)
188 +       struct ext4_inode_info *ei = EXT4_I(inode);
189 +       struct ext4_encryption_key *crypt_key = &ei->i_encryption_key;
191 +       return (crypt_key->mode != EXT4_ENCRYPTION_MODE_INVALID);
193 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
194 index 4533e94..b5255eb 100644
195 --- a/fs/ext4/ext4.h
196 +++ b/fs/ext4/ext4.h
197 @@ -2064,6 +2064,19 @@ static inline int ext4_sb_has_crypto(struct super_block *sb)
199  #endif
201 +/* crypto_key.c */
202 +int ext4_generate_encryption_key(struct inode *inode);
204 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
205 +int ext4_has_encryption_key(struct inode *inode);
206 +#else
207 +static inline int ext4_has_encryption_key(struct inode *inode)
209 +       return 0;
211 +#endif
214  /* dir.c */
215  extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
216                                   struct file *,
217 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
218 index 9d5d2e5..6a7c0c0 100644
219 --- a/fs/ext4/ext4_crypto.h
220 +++ b/fs/ext4/ext4_crypto.h
221 @@ -55,6 +55,9 @@ struct ext4_encryption_context {
222  #define EXT4_AES_256_XTS_KEY_SIZE 64
223  #define EXT4_MAX_KEY_SIZE 64
225 +#define EXT4_KEY_DESC_PREFIX "ext4:"
226 +#define EXT4_KEY_DESC_PREFIX_SIZE 5
228  struct ext4_encryption_key {
229         uint32_t mode;
230         char raw[EXT4_MAX_KEY_SIZE];