blob: 9f77de2ef317e538ced76d5472c62df4de29fcaf [file] [log] [blame]
Jaegeuk Kim0adda902015-04-21 16:23:47 -07001/*
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>
17#include <crypto/hash.h>
18#include <linux/f2fs_fs.h>
19
20#include "f2fs.h"
21#include "xattr.h"
22
23static 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 */
42static 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;
47 struct ablkcipher_request *req = NULL;
48 DECLARE_F2FS_COMPLETION_RESULT(ecr);
49 struct scatterlist src_sg, dst_sg;
50 struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
51 0);
52
53 if (IS_ERR(tfm)) {
54 res = PTR_ERR(tfm);
55 tfm = NULL;
56 goto out;
57 }
58 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
59 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
60 if (!req) {
61 res = -ENOMEM;
62 goto out;
63 }
64 ablkcipher_request_set_callback(req,
65 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
66 derive_crypt_complete, &ecr);
67 res = crypto_ablkcipher_setkey(tfm, deriving_key,
68 F2FS_AES_128_ECB_KEY_SIZE);
69 if (res < 0)
70 goto out;
71
72 sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE);
73 sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE);
74 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
75 F2FS_AES_256_XTS_KEY_SIZE, NULL);
76 res = crypto_ablkcipher_encrypt(req);
77 if (res == -EINPROGRESS || res == -EBUSY) {
78 BUG_ON(req->base.data != &ecr);
79 wait_for_completion(&ecr.completion);
80 res = ecr.res;
81 }
82out:
83 if (req)
84 ablkcipher_request_free(req);
85 if (tfm)
86 crypto_free_ablkcipher(tfm);
87 return res;
88}
89
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -070090static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
Jaegeuk Kim0adda902015-04-21 16:23:47 -070091{
Jaegeuk Kim0adda902015-04-21 16:23:47 -070092 if (!ci)
93 return;
94
Markus Elfring92859a52015-06-26 17:28:55 +020095 key_put(ci->ci_keyring_key);
Jaegeuk Kim0adda902015-04-21 16:23:47 -070096 crypto_free_ablkcipher(ci->ci_ctfm);
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -070097 kmem_cache_free(f2fs_crypt_info_cachep, ci);
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -070098}
99
100void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
101{
102 struct f2fs_inode_info *fi = F2FS_I(inode);
103 struct f2fs_crypt_info *prev;
104
105 if (ci == NULL)
106 ci = ACCESS_ONCE(fi->i_crypt_info);
107 if (ci == NULL)
108 return;
109 prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
110 if (prev != ci)
111 return;
112
113 f2fs_free_crypt_info(ci);
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700114}
115
116int _f2fs_get_encryption_info(struct inode *inode)
117{
118 struct f2fs_inode_info *fi = F2FS_I(inode);
119 struct f2fs_crypt_info *crypt_info;
120 char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
121 (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
122 struct key *keyring_key = NULL;
123 struct f2fs_encryption_key *master_key;
124 struct f2fs_encryption_context ctx;
125 struct user_key_payload *ukp;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700126 struct crypto_ablkcipher *ctfm;
127 const char *cipher_str;
128 char raw_key[F2FS_MAX_KEY_SIZE];
129 char mode;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700130 int res;
131
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -0700132 res = f2fs_crypto_initialize();
133 if (res)
134 return res;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700135retry:
136 crypt_info = ACCESS_ONCE(fi->i_crypt_info);
137 if (crypt_info) {
138 if (!crypt_info->ci_keyring_key ||
139 key_validate(crypt_info->ci_keyring_key) == 0)
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700140 return 0;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700141 f2fs_free_encryption_info(inode, crypt_info);
142 goto retry;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700143 }
144
145 res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
146 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
147 &ctx, sizeof(ctx), NULL);
148 if (res < 0)
149 return res;
150 else if (res != sizeof(ctx))
151 return -EINVAL;
152 res = 0;
153
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700154 crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700155 if (!crypt_info)
156 return -ENOMEM;
157
158 crypt_info->ci_flags = ctx.flags;
159 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
160 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
161 crypt_info->ci_ctfm = NULL;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700162 crypt_info->ci_keyring_key = NULL;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700163 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
164 sizeof(crypt_info->ci_master_key));
165 if (S_ISREG(inode->i_mode))
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700166 mode = crypt_info->ci_data_mode;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700167 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700168 mode = crypt_info->ci_filename_mode;
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700169 else
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700170 BUG();
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700171
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700172 switch (mode) {
173 case F2FS_ENCRYPTION_MODE_AES_256_XTS:
174 cipher_str = "xts(aes)";
175 break;
176 case F2FS_ENCRYPTION_MODE_AES_256_CTS:
177 cipher_str = "cts(cbc(aes))";
178 break;
179 default:
180 printk_once(KERN_WARNING
181 "f2fs: unsupported key mode %d (ino %u)\n",
182 mode, (unsigned) inode->i_ino);
183 res = -ENOKEY;
184 goto out;
185 }
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700186
187 memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
188 F2FS_KEY_DESC_PREFIX_SIZE);
189 sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
190 "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
191 ctx.master_key_descriptor);
192 full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
193 (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
194 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
195 if (IS_ERR(keyring_key)) {
196 res = PTR_ERR(keyring_key);
197 keyring_key = NULL;
198 goto out;
199 }
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700200 crypt_info->ci_keyring_key = keyring_key;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700201 BUG_ON(keyring_key->type != &key_type_logon);
202 ukp = ((struct user_key_payload *)keyring_key->payload.data);
203 if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
204 res = -EINVAL;
205 goto out;
206 }
207 master_key = (struct f2fs_encryption_key *)ukp->data;
208 BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
209 F2FS_KEY_DERIVATION_NONCE_SIZE);
210 BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
211 res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700212 raw_key);
213 if (res)
214 goto out;
215
216 ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
217 if (!ctfm || IS_ERR(ctfm)) {
218 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
219 printk(KERN_DEBUG
220 "%s: error %d (inode %u) allocating crypto tfm\n",
221 __func__, res, (unsigned) inode->i_ino);
222 goto out;
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700223 }
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700224 crypt_info->ci_ctfm = ctfm;
225 crypto_ablkcipher_clear_flags(ctfm, ~0);
226 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
227 CRYPTO_TFM_REQ_WEAK_KEY);
228 res = crypto_ablkcipher_setkey(ctfm, raw_key,
229 f2fs_encryption_key_size(mode));
230 if (res)
231 goto out;
232
233 memzero_explicit(raw_key, sizeof(raw_key));
234 if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
235 f2fs_free_crypt_info(crypt_info);
236 goto retry;
237 }
238 return 0;
239
240out:
241 if (res == -ENOKEY && !S_ISREG(inode->i_mode))
242 res = 0;
243
244 f2fs_free_crypt_info(crypt_info);
245 memzero_explicit(raw_key, sizeof(raw_key));
Jaegeuk Kim0adda902015-04-21 16:23:47 -0700246 return res;
247}
248
249int f2fs_has_encryption_key(struct inode *inode)
250{
251 struct f2fs_inode_info *fi = F2FS_I(inode);
252
253 return (fi->i_crypt_info != NULL);
254}