Commit | Line | Data |
---|---|---|
0adda907 JK |
1 | /* |
2 | * linux/fs/f2fs/crypto_key.c | |
3 | * | |
4 | * Copied from linux/fs/f2fs/crypto_key.c | |
5 | * | |
6 | * Copyright (C) 2015, Google, Inc. | |
7 | * | |
8 | * This contains encryption key functions for f2fs | |
9 | * | |
10 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. | |
11 | */ | |
12 | #include <keys/encrypted-type.h> | |
13 | #include <keys/user-type.h> | |
14 | #include <linux/random.h> | |
15 | #include <linux/scatterlist.h> | |
16 | #include <uapi/linux/keyctl.h> | |
2731a944 | 17 | #include <crypto/skcipher.h> |
0adda907 JK |
18 | #include <linux/f2fs_fs.h> |
19 | ||
20 | #include "f2fs.h" | |
21 | #include "xattr.h" | |
22 | ||
23 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) | |
24 | { | |
25 | struct f2fs_completion_result *ecr = req->data; | |
26 | ||
27 | if (rc == -EINPROGRESS) | |
28 | return; | |
29 | ||
30 | ecr->res = rc; | |
31 | complete(&ecr->completion); | |
32 | } | |
33 | ||
34 | /** | |
35 | * f2fs_derive_key_aes() - Derive a key using AES-128-ECB | |
36 | * @deriving_key: Encryption key used for derivatio. | |
37 | * @source_key: Source key to which to apply derivation. | |
38 | * @derived_key: Derived key. | |
39 | * | |
40 | * Return: Zero on success; non-zero otherwise. | |
41 | */ | |
42 | static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE], | |
43 | char source_key[F2FS_AES_256_XTS_KEY_SIZE], | |
44 | char derived_key[F2FS_AES_256_XTS_KEY_SIZE]) | |
45 | { | |
46 | int res = 0; | |
2731a944 | 47 | struct skcipher_request *req = NULL; |
0adda907 JK |
48 | DECLARE_F2FS_COMPLETION_RESULT(ecr); |
49 | struct scatterlist src_sg, dst_sg; | |
2731a944 | 50 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); |
0adda907 JK |
51 | |
52 | if (IS_ERR(tfm)) { | |
53 | res = PTR_ERR(tfm); | |
54 | tfm = NULL; | |
55 | goto out; | |
56 | } | |
2731a944 HX |
57 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
58 | req = skcipher_request_alloc(tfm, GFP_NOFS); | |
0adda907 JK |
59 | if (!req) { |
60 | res = -ENOMEM; | |
61 | goto out; | |
62 | } | |
2731a944 | 63 | skcipher_request_set_callback(req, |
0adda907 JK |
64 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
65 | derive_crypt_complete, &ecr); | |
2731a944 | 66 | res = crypto_skcipher_setkey(tfm, deriving_key, |
0adda907 JK |
67 | F2FS_AES_128_ECB_KEY_SIZE); |
68 | if (res < 0) | |
69 | goto out; | |
70 | ||
71 | sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE); | |
72 | sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE); | |
2731a944 | 73 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, |
0adda907 | 74 | F2FS_AES_256_XTS_KEY_SIZE, NULL); |
2731a944 | 75 | res = crypto_skcipher_encrypt(req); |
0adda907 JK |
76 | if (res == -EINPROGRESS || res == -EBUSY) { |
77 | BUG_ON(req->base.data != &ecr); | |
78 | wait_for_completion(&ecr.completion); | |
79 | res = ecr.res; | |
80 | } | |
81 | out: | |
2731a944 HX |
82 | skcipher_request_free(req); |
83 | crypto_free_skcipher(tfm); | |
0adda907 JK |
84 | return res; |
85 | } | |
86 | ||
26bf3dc7 | 87 | static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci) |
0adda907 | 88 | { |
0adda907 JK |
89 | if (!ci) |
90 | return; | |
91 | ||
92859a5e | 92 | key_put(ci->ci_keyring_key); |
2731a944 | 93 | crypto_free_skcipher(ci->ci_ctfm); |
8bacf6de | 94 | kmem_cache_free(f2fs_crypt_info_cachep, ci); |
26bf3dc7 JK |
95 | } |
96 | ||
97 | void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci) | |
98 | { | |
99 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
100 | struct f2fs_crypt_info *prev; | |
101 | ||
102 | if (ci == NULL) | |
103 | ci = ACCESS_ONCE(fi->i_crypt_info); | |
104 | if (ci == NULL) | |
105 | return; | |
106 | prev = cmpxchg(&fi->i_crypt_info, ci, NULL); | |
107 | if (prev != ci) | |
108 | return; | |
109 | ||
110 | f2fs_free_crypt_info(ci); | |
0adda907 JK |
111 | } |
112 | ||
113 | int _f2fs_get_encryption_info(struct inode *inode) | |
114 | { | |
115 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
116 | struct f2fs_crypt_info *crypt_info; | |
117 | char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE + | |
118 | (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1]; | |
119 | struct key *keyring_key = NULL; | |
120 | struct f2fs_encryption_key *master_key; | |
121 | struct f2fs_encryption_context ctx; | |
146aa8b1 | 122 | const struct user_key_payload *ukp; |
2731a944 | 123 | struct crypto_skcipher *ctfm; |
26bf3dc7 JK |
124 | const char *cipher_str; |
125 | char raw_key[F2FS_MAX_KEY_SIZE]; | |
126 | char mode; | |
0adda907 JK |
127 | int res; |
128 | ||
cfc4d971 JK |
129 | res = f2fs_crypto_initialize(); |
130 | if (res) | |
131 | return res; | |
26bf3dc7 JK |
132 | retry: |
133 | crypt_info = ACCESS_ONCE(fi->i_crypt_info); | |
134 | if (crypt_info) { | |
135 | if (!crypt_info->ci_keyring_key || | |
136 | key_validate(crypt_info->ci_keyring_key) == 0) | |
0adda907 | 137 | return 0; |
26bf3dc7 JK |
138 | f2fs_free_encryption_info(inode, crypt_info); |
139 | goto retry; | |
0adda907 JK |
140 | } |
141 | ||
142 | res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, | |
143 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, | |
144 | &ctx, sizeof(ctx), NULL); | |
145 | if (res < 0) | |
146 | return res; | |
147 | else if (res != sizeof(ctx)) | |
148 | return -EINVAL; | |
149 | res = 0; | |
150 | ||
8bacf6de | 151 | crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS); |
0adda907 JK |
152 | if (!crypt_info) |
153 | return -ENOMEM; | |
154 | ||
155 | crypt_info->ci_flags = ctx.flags; | |
156 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; | |
157 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; | |
158 | crypt_info->ci_ctfm = NULL; | |
26bf3dc7 | 159 | crypt_info->ci_keyring_key = NULL; |
0adda907 JK |
160 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
161 | sizeof(crypt_info->ci_master_key)); | |
162 | if (S_ISREG(inode->i_mode)) | |
26bf3dc7 | 163 | mode = crypt_info->ci_data_mode; |
0adda907 | 164 | else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
26bf3dc7 | 165 | mode = crypt_info->ci_filename_mode; |
640778fb | 166 | else |
0adda907 | 167 | BUG(); |
640778fb | 168 | |
26bf3dc7 JK |
169 | switch (mode) { |
170 | case F2FS_ENCRYPTION_MODE_AES_256_XTS: | |
171 | cipher_str = "xts(aes)"; | |
172 | break; | |
173 | case F2FS_ENCRYPTION_MODE_AES_256_CTS: | |
174 | cipher_str = "cts(cbc(aes))"; | |
175 | break; | |
176 | default: | |
177 | printk_once(KERN_WARNING | |
178 | "f2fs: unsupported key mode %d (ino %u)\n", | |
179 | mode, (unsigned) inode->i_ino); | |
180 | res = -ENOKEY; | |
181 | goto out; | |
182 | } | |
0adda907 JK |
183 | |
184 | memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX, | |
185 | F2FS_KEY_DESC_PREFIX_SIZE); | |
186 | sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE, | |
187 | "%*phN", F2FS_KEY_DESCRIPTOR_SIZE, | |
188 | ctx.master_key_descriptor); | |
189 | full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE + | |
190 | (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0'; | |
191 | keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL); | |
192 | if (IS_ERR(keyring_key)) { | |
193 | res = PTR_ERR(keyring_key); | |
194 | keyring_key = NULL; | |
195 | goto out; | |
196 | } | |
26bf3dc7 | 197 | crypt_info->ci_keyring_key = keyring_key; |
0adda907 | 198 | BUG_ON(keyring_key->type != &key_type_logon); |
146aa8b1 | 199 | ukp = user_key_payload(keyring_key); |
0adda907 JK |
200 | if (ukp->datalen != sizeof(struct f2fs_encryption_key)) { |
201 | res = -EINVAL; | |
202 | goto out; | |
203 | } | |
204 | master_key = (struct f2fs_encryption_key *)ukp->data; | |
205 | BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE != | |
206 | F2FS_KEY_DERIVATION_NONCE_SIZE); | |
207 | BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE); | |
208 | res = f2fs_derive_key_aes(ctx.nonce, master_key->raw, | |
26bf3dc7 JK |
209 | raw_key); |
210 | if (res) | |
211 | goto out; | |
212 | ||
2731a944 | 213 | ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); |
26bf3dc7 JK |
214 | if (!ctfm || IS_ERR(ctfm)) { |
215 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; | |
216 | printk(KERN_DEBUG | |
217 | "%s: error %d (inode %u) allocating crypto tfm\n", | |
218 | __func__, res, (unsigned) inode->i_ino); | |
219 | goto out; | |
0adda907 | 220 | } |
26bf3dc7 | 221 | crypt_info->ci_ctfm = ctfm; |
2731a944 HX |
222 | crypto_skcipher_clear_flags(ctfm, ~0); |
223 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); | |
224 | res = crypto_skcipher_setkey(ctfm, raw_key, | |
225 | f2fs_encryption_key_size(mode)); | |
26bf3dc7 JK |
226 | if (res) |
227 | goto out; | |
228 | ||
229 | memzero_explicit(raw_key, sizeof(raw_key)); | |
230 | if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) { | |
231 | f2fs_free_crypt_info(crypt_info); | |
232 | goto retry; | |
233 | } | |
234 | return 0; | |
235 | ||
236 | out: | |
237 | if (res == -ENOKEY && !S_ISREG(inode->i_mode)) | |
238 | res = 0; | |
239 | ||
240 | f2fs_free_crypt_info(crypt_info); | |
241 | memzero_explicit(raw_key, sizeof(raw_key)); | |
0adda907 JK |
242 | return res; |
243 | } | |
244 | ||
245 | int f2fs_has_encryption_key(struct inode *inode) | |
246 | { | |
247 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
248 | ||
249 | return (fi->i_crypt_info != NULL); | |
250 | } |