1 // SPDX-License-Identifier: GPL-2.0
3 * Key setup for v1 encryption policies
5 * Copyright 2015, 2019 Google LLC
8 * Linux commit 219d54332a09
13 * This file implements compatibility functions for the original encryption
14 * policy version ("v1"), including:
16 * - Deriving per-file keys using the AES-128-ECB based KDF
17 * (rather than the new method of using HKDF-SHA512)
19 * - Retrieving llcrypt master keys from process-subscribed keyrings
20 * (rather than the new method of using a filesystem-level keyring)
22 * - Handling policies with the DIRECT_KEY flag set using a master key table
23 * (rather than the new method of implementing DIRECT_KEY with per-mode keys
24 * managed alongside the master keys in the filesystem-level keyring)
27 #include <crypto/algapi.h>
28 #include <crypto/skcipher.h>
29 #include <keys/user-type.h>
30 #include <linux/hashtable.h>
31 #include <linux/scatterlist.h>
33 #include "llcrypt_private.h"
35 /* Table of keys referenced by DIRECT_KEY policies */
36 static DEFINE_HASHTABLE(llcrypt_direct_keys, 6); /* 6 bits = 64 buckets */
37 static DEFINE_SPINLOCK(llcrypt_direct_keys_lock);
40 * v1 key derivation function. This generates the derived key by encrypting the
41 * master key with AES-128-ECB using the nonce as the AES key. This provides a
42 * unique derived key with sufficient entropy for each inode. However, it's
43 * nonstandard, non-extensible, doesn't evenly distribute the entropy from the
44 * master key, and is trivially reversible: an attacker who compromises a
45 * derived key can "decrypt" it to get back to the master key, then derive any
46 * other key. For all new code, use HKDF instead.
48 * The master key must be at least as long as the derived key. If the master
49 * key is longer, then only the first 'derived_keysize' bytes are used.
51 static int derive_key_aes(const u8 *master_key,
52 const u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE],
53 u8 *derived_key, unsigned int derived_keysize)
56 struct skcipher_request *req = NULL;
57 DECLARE_CRYPTO_WAIT(wait);
58 struct scatterlist src_sg, dst_sg;
59 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
66 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
67 req = skcipher_request_alloc(tfm, GFP_NOFS);
72 skcipher_request_set_callback(req,
73 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
74 crypto_req_done, &wait);
75 res = crypto_skcipher_setkey(tfm, nonce, FS_KEY_DERIVATION_NONCE_SIZE);
79 sg_init_one(&src_sg, master_key, derived_keysize);
80 sg_init_one(&dst_sg, derived_key, derived_keysize);
81 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
83 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
85 skcipher_request_free(req);
86 crypto_free_skcipher(tfm);
91 * Search the current task's subscribed keyrings for a "logon" key with
92 * description prefix:descriptor, and if found acquire a read lock on it and
93 * return a pointer to its validated payload in *payload_ret.
96 find_and_lock_process_key(const char *prefix,
97 const u8 descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE],
98 unsigned int min_keysize,
99 const struct llcrypt_key **payload_ret)
103 const struct user_key_payload *ukp;
104 const struct llcrypt_key *payload;
106 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
107 LLCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
109 return ERR_PTR(-ENOMEM);
111 key = request_key(&key_type_logon, description, NULL);
116 down_read(&key->sem);
117 ukp = user_key_payload_locked(key);
119 if (!ukp) /* was the key revoked before we acquired its semaphore? */
122 payload = (const struct llcrypt_key *)ukp->data;
124 if (ukp->datalen != sizeof(struct llcrypt_key) ||
125 payload->size < 1 || payload->size > LLCRYPT_MAX_KEY_SIZE) {
127 "key with description '%s' has invalid payload",
132 if (payload->size < min_keysize) {
134 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
135 key->description, payload->size, min_keysize);
139 *payload_ret = payload;
145 return ERR_PTR(-ENOKEY);
148 /* Master key referenced by DIRECT_KEY policy */
149 struct llcrypt_direct_key {
150 struct hlist_node dk_node;
151 refcount_t dk_refcount;
152 const struct llcrypt_mode *dk_mode;
153 struct crypto_skcipher *dk_ctfm;
154 u8 dk_descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE];
155 u8 dk_raw[LLCRYPT_MAX_KEY_SIZE];
158 static void free_direct_key(struct llcrypt_direct_key *dk)
161 crypto_free_skcipher(dk->dk_ctfm);
166 void llcrypt_put_direct_key(struct llcrypt_direct_key *dk)
168 if (!refcount_dec_and_lock(&dk->dk_refcount, &llcrypt_direct_keys_lock))
170 hash_del(&dk->dk_node);
171 spin_unlock(&llcrypt_direct_keys_lock);
177 * Find/insert the given key into the llcrypt_direct_keys table. If found, it
178 * is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If
179 * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
182 static struct llcrypt_direct_key *
183 find_or_insert_direct_key(struct llcrypt_direct_key *to_insert,
184 const u8 *raw_key, const struct llcrypt_info *ci)
186 unsigned long hash_key;
187 struct llcrypt_direct_key *dk;
190 * Careful: to avoid potentially leaking secret key bytes via timing
191 * information, we must key the hash table by descriptor rather than by
192 * raw key, and use crypto_memneq() when comparing raw keys.
195 BUILD_BUG_ON(sizeof(hash_key) > LLCRYPT_KEY_DESCRIPTOR_SIZE);
196 memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
199 spin_lock(&llcrypt_direct_keys_lock);
200 hash_for_each_possible(llcrypt_direct_keys, dk, dk_node, hash_key) {
201 if (memcmp(ci->ci_policy.v1.master_key_descriptor,
202 dk->dk_descriptor, LLCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
204 if (ci->ci_mode != dk->dk_mode)
206 if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
208 /* using existing tfm with same (descriptor, mode, raw_key) */
209 refcount_inc(&dk->dk_refcount);
210 spin_unlock(&llcrypt_direct_keys_lock);
211 free_direct_key(to_insert);
215 hash_add(llcrypt_direct_keys, &to_insert->dk_node, hash_key);
216 spin_unlock(&llcrypt_direct_keys_lock);
220 /* Prepare to encrypt directly using the master key in the given mode */
221 static struct llcrypt_direct_key *
222 llcrypt_get_direct_key(const struct llcrypt_info *ci, const u8 *raw_key)
224 struct llcrypt_direct_key *dk;
227 /* Is there already a tfm for this key? */
228 dk = find_or_insert_direct_key(NULL, raw_key, ci);
232 /* Nope, allocate one. */
233 dk = kzalloc(sizeof(*dk), GFP_NOFS);
235 return ERR_PTR(-ENOMEM);
236 refcount_set(&dk->dk_refcount, 1);
237 dk->dk_mode = ci->ci_mode;
238 dk->dk_ctfm = llcrypt_allocate_skcipher(ci->ci_mode, raw_key,
240 if (IS_ERR(dk->dk_ctfm)) {
241 err = PTR_ERR(dk->dk_ctfm);
245 memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
246 LLCRYPT_KEY_DESCRIPTOR_SIZE);
247 memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
249 return find_or_insert_direct_key(dk, raw_key, ci);
256 /* v1 policy, DIRECT_KEY: use the master key directly */
257 static int setup_v1_file_key_direct(struct llcrypt_info *ci,
258 const u8 *raw_master_key)
260 const struct llcrypt_mode *mode = ci->ci_mode;
261 struct llcrypt_direct_key *dk;
263 if (!llcrypt_mode_supports_direct_key(mode)) {
264 llcrypt_warn(ci->ci_inode,
265 "Direct key mode not allowed with %s",
266 mode->friendly_name);
270 if (ci->ci_policy.v1.contents_encryption_mode !=
271 ci->ci_policy.v1.filenames_encryption_mode) {
272 llcrypt_warn(ci->ci_inode,
273 "Direct key mode not allowed with different contents and filenames modes");
277 /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */
278 if (WARN_ON(mode->needs_essiv))
281 dk = llcrypt_get_direct_key(ci, raw_master_key);
284 ci->ci_direct_key = dk;
285 ci->ci_ctfm = dk->dk_ctfm;
289 /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
290 static int setup_v1_file_key_derived(struct llcrypt_info *ci,
291 const u8 *raw_master_key)
297 * This cannot be a stack buffer because it will be passed to the
298 * scatterlist crypto API during derive_key_aes().
300 derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
304 err = derive_key_aes(raw_master_key, ci->ci_nonce,
305 derived_key, ci->ci_mode->keysize);
309 err = llcrypt_set_derived_key(ci, derived_key);
315 int llcrypt_setup_v1_file_key(struct llcrypt_info *ci, const u8 *raw_master_key)
317 if (ci->ci_policy.v1.flags & LLCRYPT_POLICY_FLAG_DIRECT_KEY)
318 return setup_v1_file_key_direct(ci, raw_master_key);
320 return setup_v1_file_key_derived(ci, raw_master_key);
323 int llcrypt_setup_v1_file_key_via_subscribed_keyrings(struct llcrypt_info *ci)
326 const struct llcrypt_key *payload;
329 key = find_and_lock_process_key(LLCRYPT_KEY_DESC_PREFIX,
330 ci->ci_policy.v1.master_key_descriptor,
331 ci->ci_mode->keysize, &payload);
332 if (key == ERR_PTR(-ENOKEY)) {
333 struct lustre_sb_info *lsi = s2lsi(ci->ci_inode->i_sb);
335 if (lsi && lsi->lsi_cop->key_prefix) {
337 find_and_lock_process_key(lsi->lsi_cop->key_prefix,
338 ci->ci_policy.v1.master_key_descriptor,
339 ci->ci_mode->keysize,
346 err = llcrypt_setup_v1_file_key(ci, payload->raw);