2 * linux/fs/f2fs/crypto_key.c
4 * Copied from linux/fs/f2fs/crypto_key.c
6 * Copyright (C) 2015, Google, Inc.
8 * This contains encryption key functions for f2fs
10 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
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>
23 static void derive_crypt_complete(struct crypto_async_request
*req
, int rc
)
25 struct f2fs_completion_result
*ecr
= req
->data
;
27 if (rc
== -EINPROGRESS
)
31 complete(&ecr
->completion
);
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.
40 * Return: Zero on success; non-zero otherwise.
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
])
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,
58 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
59 req
= ablkcipher_request_alloc(tfm
, GFP_NOFS
);
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
);
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
);
84 ablkcipher_request_free(req
);
86 crypto_free_ablkcipher(tfm
);
90 static void f2fs_free_crypt_info(struct f2fs_crypt_info
*ci
)
95 crypto_free_ablkcipher(ci
->ci_ctfm
);
96 kmem_cache_free(f2fs_crypt_info_cachep
, ci
);
99 void f2fs_free_encryption_info(struct inode
*inode
, struct f2fs_crypt_info
*ci
)
101 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
102 struct f2fs_crypt_info
*prev
;
105 ci
= ACCESS_ONCE(fi
->i_crypt_info
);
108 prev
= cmpxchg(&fi
->i_crypt_info
, ci
, NULL
);
112 f2fs_free_crypt_info(ci
);
115 int f2fs_get_encryption_info(struct inode
*inode
)
117 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
118 struct f2fs_crypt_info
*crypt_info
;
119 char full_key_descriptor
[F2FS_KEY_DESC_PREFIX_SIZE
+
120 (F2FS_KEY_DESCRIPTOR_SIZE
* 2) + 1];
121 struct key
*keyring_key
= NULL
;
122 struct f2fs_encryption_key
*master_key
;
123 struct f2fs_encryption_context ctx
;
124 const struct user_key_payload
*ukp
;
125 struct crypto_ablkcipher
*ctfm
;
126 const char *cipher_str
;
127 char raw_key
[F2FS_MAX_KEY_SIZE
];
131 if (fi
->i_crypt_info
)
134 res
= f2fs_crypto_initialize();
138 res
= f2fs_getxattr(inode
, F2FS_XATTR_INDEX_ENCRYPTION
,
139 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT
,
140 &ctx
, sizeof(ctx
), NULL
);
143 else if (res
!= sizeof(ctx
))
147 crypt_info
= kmem_cache_alloc(f2fs_crypt_info_cachep
, GFP_NOFS
);
151 crypt_info
->ci_flags
= ctx
.flags
;
152 crypt_info
->ci_data_mode
= ctx
.contents_encryption_mode
;
153 crypt_info
->ci_filename_mode
= ctx
.filenames_encryption_mode
;
154 crypt_info
->ci_ctfm
= NULL
;
155 memcpy(crypt_info
->ci_master_key
, ctx
.master_key_descriptor
,
156 sizeof(crypt_info
->ci_master_key
));
157 if (S_ISREG(inode
->i_mode
))
158 mode
= crypt_info
->ci_data_mode
;
159 else if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
160 mode
= crypt_info
->ci_filename_mode
;
165 case F2FS_ENCRYPTION_MODE_AES_256_XTS
:
166 cipher_str
= "xts(aes)";
168 case F2FS_ENCRYPTION_MODE_AES_256_CTS
:
169 cipher_str
= "cts(cbc(aes))";
172 printk_once(KERN_WARNING
173 "f2fs: unsupported key mode %d (ino %u)\n",
174 mode
, (unsigned) inode
->i_ino
);
179 memcpy(full_key_descriptor
, F2FS_KEY_DESC_PREFIX
,
180 F2FS_KEY_DESC_PREFIX_SIZE
);
181 sprintf(full_key_descriptor
+ F2FS_KEY_DESC_PREFIX_SIZE
,
182 "%*phN", F2FS_KEY_DESCRIPTOR_SIZE
,
183 ctx
.master_key_descriptor
);
184 full_key_descriptor
[F2FS_KEY_DESC_PREFIX_SIZE
+
185 (2 * F2FS_KEY_DESCRIPTOR_SIZE
)] = '\0';
186 keyring_key
= request_key(&key_type_logon
, full_key_descriptor
, NULL
);
187 if (IS_ERR(keyring_key
)) {
188 res
= PTR_ERR(keyring_key
);
192 BUG_ON(keyring_key
->type
!= &key_type_logon
);
193 ukp
= user_key_payload(keyring_key
);
194 if (ukp
->datalen
!= sizeof(struct f2fs_encryption_key
)) {
198 master_key
= (struct f2fs_encryption_key
*)ukp
->data
;
199 BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE
!=
200 F2FS_KEY_DERIVATION_NONCE_SIZE
);
201 BUG_ON(master_key
->size
!= F2FS_AES_256_XTS_KEY_SIZE
);
202 res
= f2fs_derive_key_aes(ctx
.nonce
, master_key
->raw
,
207 ctfm
= crypto_alloc_ablkcipher(cipher_str
, 0, 0);
208 if (!ctfm
|| IS_ERR(ctfm
)) {
209 res
= ctfm
? PTR_ERR(ctfm
) : -ENOMEM
;
211 "%s: error %d (inode %u) allocating crypto tfm\n",
212 __func__
, res
, (unsigned) inode
->i_ino
);
215 crypt_info
->ci_ctfm
= ctfm
;
216 crypto_ablkcipher_clear_flags(ctfm
, ~0);
217 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm
),
218 CRYPTO_TFM_REQ_WEAK_KEY
);
219 res
= crypto_ablkcipher_setkey(ctfm
, raw_key
,
220 f2fs_encryption_key_size(mode
));
224 if (cmpxchg(&fi
->i_crypt_info
, NULL
, crypt_info
) == NULL
)
227 if (res
== -ENOKEY
&& !S_ISREG(inode
->i_mode
))
229 key_put(keyring_key
);
230 f2fs_free_crypt_info(crypt_info
);
231 memzero_explicit(raw_key
, sizeof(raw_key
));
235 int f2fs_has_encryption_key(struct inode
*inode
)
237 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
239 return (fi
->i_crypt_info
!= NULL
);