Lines Matching +full:unlock +full:- +full:keys

1 // SPDX-License-Identifier: GPL-2.0
18 .friendly_name = "AES-256-XTS",
26 .friendly_name = "AES-256-CTS-CBC",
33 .friendly_name = "AES-128-CBC-ESSIV",
41 .friendly_name = "AES-128-CTS-CBC",
56 .friendly_name = "AES-256-HCTR2",
72 if (S_ISREG(inode->i_mode)) in select_encryption_mode()
75 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) in select_encryption_mode()
79 inode->i_ino, (inode->i_mode & S_IFMT)); in select_encryption_mode()
80 return ERR_PTR(-EINVAL); in select_encryption_mode()
91 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); in fscrypt_allocate_skcipher()
93 if (PTR_ERR(tfm) == -ENOENT) { in fscrypt_allocate_skcipher()
96 mode->friendly_name, mode->cipher_str); in fscrypt_allocate_skcipher()
97 return ERR_PTR(-ENOPKG); in fscrypt_allocate_skcipher()
100 mode->cipher_str, PTR_ERR(tfm)); in fscrypt_allocate_skcipher()
103 if (!xchg(&mode->logged_cryptoapi_impl, 1)) { in fscrypt_allocate_skcipher()
107 * performance problems by logging the ->cra_driver_name the in fscrypt_allocate_skcipher()
111 mode->friendly_name, crypto_skcipher_driver_name(tfm)); in fscrypt_allocate_skcipher()
113 if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) { in fscrypt_allocate_skcipher()
114 err = -EINVAL; in fscrypt_allocate_skcipher()
118 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); in fscrypt_allocate_skcipher()
130 * Prepare the crypto transform object or blk-crypto key in @prep_key, given the
131 * raw key, encryption mode (@ci->ci_mode), flag indicating which encryption
132 * implementation (fs-layer or blk-crypto) will be used (@ci->ci_inlinecrypt),
133 * and IV generation method (@ci->ci_policy.flags).
143 tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode); in fscrypt_prepare_key()
148 * I.e., here we publish ->tfm with a RELEASE barrier so that in fscrypt_prepare_key()
150 * possible for per-mode keys, not for per-file keys. in fscrypt_prepare_key()
152 smp_store_release(&prep_key->tfm, tfm); in fscrypt_prepare_key()
156 /* Destroy a crypto transform object and/or blk-crypto key. */
160 crypto_free_skcipher(prep_key->tfm); in fscrypt_destroy_prepared_key()
165 /* Given a per-file encryption key, set up the file's crypto transform object */
168 ci->ci_owns_key = true; in fscrypt_set_per_file_enc_key()
169 return fscrypt_prepare_key(&ci->ci_enc_key, raw_key, ci); in fscrypt_set_per_file_enc_key()
174 struct fscrypt_prepared_key *keys, in setup_per_mode_enc_key() argument
177 const struct inode *inode = ci->ci_inode; in setup_per_mode_enc_key()
178 const struct super_block *sb = inode->i_sb; in setup_per_mode_enc_key()
179 struct fscrypt_mode *mode = ci->ci_mode; in setup_per_mode_enc_key()
180 const u8 mode_num = mode - fscrypt_modes; in setup_per_mode_enc_key()
183 u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)]; in setup_per_mode_enc_key()
188 return -EINVAL; in setup_per_mode_enc_key()
190 prep_key = &keys[mode_num]; in setup_per_mode_enc_key()
192 ci->ci_enc_key = *prep_key; in setup_per_mode_enc_key()
202 BUILD_BUG_ON(sizeof(sb->s_uuid) != 16); in setup_per_mode_enc_key()
206 memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid, in setup_per_mode_enc_key()
207 sizeof(sb->s_uuid)); in setup_per_mode_enc_key()
208 hkdf_infolen += sizeof(sb->s_uuid); in setup_per_mode_enc_key()
210 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, in setup_per_mode_enc_key()
212 mode_key, mode->keysize); in setup_per_mode_enc_key()
216 memzero_explicit(mode_key, mode->keysize); in setup_per_mode_enc_key()
220 ci->ci_enc_key = *prep_key; in setup_per_mode_enc_key()
229 * application-specific information string.
232 * as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an
241 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen, in fscrypt_derive_siphash_key()
247 BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2); in fscrypt_derive_siphash_key()
248 le64_to_cpus(&key->key[0]); in fscrypt_derive_siphash_key()
249 le64_to_cpus(&key->key[1]); in fscrypt_derive_siphash_key()
259 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE, in fscrypt_derive_dirhash_key()
260 &ci->ci_dirhash_key); in fscrypt_derive_dirhash_key()
263 ci->ci_dirhash_key_initialized = true; in fscrypt_derive_dirhash_key()
270 WARN_ON(ci->ci_inode->i_ino == 0); in fscrypt_hash_inode_number()
271 WARN_ON(!mk->mk_ino_hash_key_initialized); in fscrypt_hash_inode_number()
273 ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino, in fscrypt_hash_inode_number()
274 &mk->mk_ino_hash_key); in fscrypt_hash_inode_number()
282 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys, in fscrypt_setup_iv_ino_lblk_32_key()
288 if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) { in fscrypt_setup_iv_ino_lblk_32_key()
292 if (mk->mk_ino_hash_key_initialized) in fscrypt_setup_iv_ino_lblk_32_key()
293 goto unlock; in fscrypt_setup_iv_ino_lblk_32_key()
297 NULL, 0, &mk->mk_ino_hash_key); in fscrypt_setup_iv_ino_lblk_32_key()
299 goto unlock; in fscrypt_setup_iv_ino_lblk_32_key()
301 smp_store_release(&mk->mk_ino_hash_key_initialized, true); in fscrypt_setup_iv_ino_lblk_32_key()
302 unlock: in fscrypt_setup_iv_ino_lblk_32_key()
312 if (ci->ci_inode->i_ino) in fscrypt_setup_iv_ino_lblk_32_key()
323 if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { in fscrypt_setup_v2_file_key()
325 * DIRECT_KEY: instead of deriving per-file encryption keys, the in fscrypt_setup_v2_file_key()
326 * per-file nonce will be included in all the IVs. But unlike in fscrypt_setup_v2_file_key()
328 * with the master key directly but rather derive a per-mode in fscrypt_setup_v2_file_key()
332 err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys, in fscrypt_setup_v2_file_key()
334 } else if (ci->ci_policy.v2.flags & in fscrypt_setup_v2_file_key()
337 * IV_INO_LBLK_64: encryption keys are derived from (master_key, in fscrypt_setup_v2_file_key()
342 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys, in fscrypt_setup_v2_file_key()
345 } else if (ci->ci_policy.v2.flags & in fscrypt_setup_v2_file_key()
351 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, in fscrypt_setup_v2_file_key()
353 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE, in fscrypt_setup_v2_file_key()
354 derived_key, ci->ci_mode->keysize); in fscrypt_setup_v2_file_key()
359 memzero_explicit(derived_key, ci->ci_mode->keysize); in fscrypt_setup_v2_file_key()
384 * (but in practice we only need to consider @ci->ci_mode, since any other
386 * required key size over @ci->ci_mode). This allows AES-256-XTS keys to be
387 * derived from a 256-bit master key, which is cryptographically sufficient,
388 * rather than requiring a 512-bit master key which is unnecessarily long. (We
389 * still allow 512-bit master keys if the user chooses to use them, though.)
396 if (ci->ci_policy.version == FSCRYPT_POLICY_V1) in fscrypt_valid_master_key_size()
397 min_keysize = ci->ci_mode->keysize; in fscrypt_valid_master_key_size()
399 min_keysize = ci->ci_mode->security_strength; in fscrypt_valid_master_key_size()
401 if (mk->mk_secret.size < min_keysize) { in fscrypt_valid_master_key_size()
404 master_key_spec_type(&mk->mk_spec), in fscrypt_valid_master_key_size()
405 master_key_spec_len(&mk->mk_spec), in fscrypt_valid_master_key_size()
406 (u8 *)&mk->mk_spec.u, in fscrypt_valid_master_key_size()
407 mk->mk_secret.size, min_keysize); in fscrypt_valid_master_key_size()
416 * If the master key is found in the filesystem-level keyring, then it is
417 * returned in *mk_ret with its semaphore read-locked. This is needed to ensure
418 * that only one task links the fscrypt_info into ->mk_decrypted_inodes (as
434 err = fscrypt_policy_to_key_spec(&ci->ci_policy, &mk_spec); in setup_file_encryption_key()
438 mk = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec); in setup_file_encryption_key()
440 if (ci->ci_policy.version != FSCRYPT_POLICY_V1) in setup_file_encryption_key()
441 return -ENOKEY; in setup_file_encryption_key()
446 * to before the search of ->s_master_keys, since users in setup_file_encryption_key()
447 * shouldn't be able to override filesystem-level keys. in setup_file_encryption_key()
451 down_read(&mk->mk_sem); in setup_file_encryption_key()
454 if (!is_master_key_secret_present(&mk->mk_secret)) { in setup_file_encryption_key()
455 err = -ENOKEY; in setup_file_encryption_key()
460 err = -ENOKEY; in setup_file_encryption_key()
464 switch (ci->ci_policy.version) { in setup_file_encryption_key()
466 err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw); in setup_file_encryption_key()
473 err = -EINVAL; in setup_file_encryption_key()
483 up_read(&mk->mk_sem); in setup_file_encryption_key()
495 if (ci->ci_direct_key) in put_crypt_info()
496 fscrypt_put_direct_key(ci->ci_direct_key); in put_crypt_info()
497 else if (ci->ci_owns_key) in put_crypt_info()
498 fscrypt_destroy_prepared_key(ci->ci_inode->i_sb, in put_crypt_info()
499 &ci->ci_enc_key); in put_crypt_info()
501 mk = ci->ci_master_key; in put_crypt_info()
509 spin_lock(&mk->mk_decrypted_inodes_lock); in put_crypt_info()
510 list_del(&ci->ci_master_key_link); in put_crypt_info()
511 spin_unlock(&mk->mk_decrypted_inodes_lock); in put_crypt_info()
529 res = fscrypt_initialize(inode->i_sb->s_cop->flags); in fscrypt_setup_encryption_info()
535 return -ENOMEM; in fscrypt_setup_encryption_info()
537 crypt_info->ci_inode = inode; in fscrypt_setup_encryption_info()
538 crypt_info->ci_policy = *policy; in fscrypt_setup_encryption_info()
539 memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); in fscrypt_setup_encryption_info()
541 mode = select_encryption_mode(&crypt_info->ci_policy, inode); in fscrypt_setup_encryption_info()
546 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); in fscrypt_setup_encryption_info()
547 crypt_info->ci_mode = mode; in fscrypt_setup_encryption_info()
554 * For existing inodes, multiple tasks may race to set ->i_crypt_info. in fscrypt_setup_encryption_info()
556 * fscrypt_get_info(). I.e., here we publish ->i_crypt_info with a in fscrypt_setup_encryption_info()
559 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) { in fscrypt_setup_encryption_info()
561 * We won the race and set ->i_crypt_info to our crypt_info. in fscrypt_setup_encryption_info()
565 crypt_info->ci_master_key = mk; in fscrypt_setup_encryption_info()
566 refcount_inc(&mk->mk_active_refs); in fscrypt_setup_encryption_info()
567 spin_lock(&mk->mk_decrypted_inodes_lock); in fscrypt_setup_encryption_info()
568 list_add(&crypt_info->ci_master_key_link, in fscrypt_setup_encryption_info()
569 &mk->mk_decrypted_inodes); in fscrypt_setup_encryption_info()
570 spin_unlock(&mk->mk_decrypted_inodes_lock); in fscrypt_setup_encryption_info()
577 up_read(&mk->mk_sem); in fscrypt_setup_encryption_info()
585 * fscrypt_get_encryption_info() - set up an inode's encryption key
593 * Set up ->i_crypt_info, if it hasn't already been done.
595 * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe. So
598 * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
600 * distinguish these cases.) Also can return another -errno code.
611 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); in fscrypt_get_encryption_info()
613 if (res == -ERANGE && allow_unsupported) in fscrypt_get_encryption_info()
631 return -EINVAL; in fscrypt_get_encryption_info()
637 S_ISDIR(inode->i_mode)); in fscrypt_get_encryption_info()
639 if (res == -ENOPKG && allow_unsupported) /* Algorithm unavailable? */ in fscrypt_get_encryption_info()
641 if (res == -ENOKEY) in fscrypt_get_encryption_info()
647 * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
648 * @dir: a possibly-encrypted directory
649 * @inode: the new inode. ->i_mode must be set already.
650 * ->i_ino doesn't need to be set yet.
653 * If the directory is encrypted, set up its ->i_crypt_info in preparation for
655 * encrypted, set up its ->i_crypt_info and set *encrypt_ret=true.
657 * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
658 * any filesystem transaction to create the inode. For this reason, ->i_ino
664 * Return: 0 on success, -ENOKEY if the encryption key is missing, or another
665 * -errno code
679 if (WARN_ON_ONCE(inode->i_mode == 0)) in fscrypt_prepare_new_inode()
680 return -EINVAL; in fscrypt_prepare_new_inode()
686 if (!S_ISREG(inode->i_mode) && in fscrypt_prepare_new_inode()
687 !S_ISDIR(inode->i_mode) && in fscrypt_prepare_new_inode()
688 !S_ISLNK(inode->i_mode)) in fscrypt_prepare_new_inode()
696 S_ISDIR(inode->i_mode)); in fscrypt_prepare_new_inode()
701 * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
709 put_crypt_info(inode->i_crypt_info); in fscrypt_put_encryption_info()
710 inode->i_crypt_info = NULL; in fscrypt_put_encryption_info()
715 * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay
723 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { in fscrypt_free_inode()
724 kfree(inode->i_link); in fscrypt_free_inode()
725 inode->i_link = NULL; in fscrypt_free_inode()
731 * fscrypt_drop_inode() - check whether the inode's master key has been removed
734 * Filesystems supporting fscrypt must call this from their ->drop_inode()
747 * was provided via the legacy mechanism of the process-subscribed in fscrypt_drop_inode()
750 if (!ci || !ci->ci_master_key) in fscrypt_drop_inode()
754 * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes in fscrypt_drop_inode()
759 if (inode->i_state & I_DIRTY_ALL) in fscrypt_drop_inode()
770 return !is_master_key_secret_present(&ci->ci_master_key->mk_secret); in fscrypt_drop_inode()