Skip to content

Commit 8129bac

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fscrypt updates from Eric Biggers: "This release adds SM4 encryption support, contributed by Tianjia Zhang. SM4 is a Chinese block cipher that is an alternative to AES. I recommend against using SM4, but (according to Tianjia) some people are being required to use it. Since SM4 has been turning up in many other places (crypto API, wireless, TLS, OpenSSL, ARMv8 CPUs, etc.), it hasn't been very controversial, and some people have to use it, I don't think it would be fair for me to reject this optional feature. Besides the above, there are a couple cleanups" * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: fscrypt: add additional documentation for SM4 support fscrypt: remove unused Speck definitions fscrypt: Add SM4 XTS/CTS symmetric algorithm support blk-crypto: Add support for SM4-XTS blk crypto mode fscrypt: add comment for fscrypt_valid_enc_modes_v1() fscrypt: pass super_block to fscrypt_put_master_key_activeref()
2 parents deb9acc + 4195255 commit 8129bac

File tree

8 files changed

+54
-20
lines changed

8 files changed

+54
-20
lines changed

Documentation/filesystems/fscrypt.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Currently, the following pairs of encryption modes are supported:
338338
- AES-128-CBC for contents and AES-128-CTS-CBC for filenames
339339
- Adiantum for both contents and filenames
340340
- AES-256-XTS for contents and AES-256-HCTR2 for filenames (v2 policies only)
341+
- SM4-XTS for contents and SM4-CTS-CBC for filenames (v2 policies only)
341342

342343
If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
343344

@@ -369,6 +370,12 @@ CONFIG_CRYPTO_HCTR2 must be enabled. Also, fast implementations of XCTR and
369370
POLYVAL should be enabled, e.g. CRYPTO_POLYVAL_ARM64_CE and
370371
CRYPTO_AES_ARM64_CE_BLK for ARM64.
371372

373+
SM4 is a Chinese block cipher that is an alternative to AES. It has
374+
not seen as much security review as AES, and it only has a 128-bit key
375+
size. It may be useful in cases where its use is mandated.
376+
Otherwise, it should not be used. For SM4 support to be available, it
377+
also needs to be enabled in the kernel crypto API.
378+
372379
New encryption modes can be added relatively easily, without changes
373380
to individual filesystems. However, authenticated encryption (AE)
374381
modes are not currently supported because of the difficulty of dealing

block/blk-crypto.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const struct blk_crypto_mode blk_crypto_modes[] = {
3636
.keysize = 32,
3737
.ivsize = 32,
3838
},
39+
[BLK_ENCRYPTION_MODE_SM4_XTS] = {
40+
.name = "SM4-XTS",
41+
.cipher_str = "xts(sm4)",
42+
.keysize = 32,
43+
.ivsize = 16,
44+
},
3945
};
4046

4147
/*

fs/crypto/fscrypt_private.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,7 @@ struct fscrypt_master_key_secret {
439439
struct fscrypt_master_key {
440440

441441
/*
442-
* Back-pointer to the super_block of the filesystem to which this
443-
* master key has been added. Only valid if ->mk_active_refs > 0.
444-
*/
445-
struct super_block *mk_sb;
446-
447-
/*
448-
* Link in ->mk_sb->s_master_keys->key_hashtable.
442+
* Link in ->s_master_keys->key_hashtable.
449443
* Only valid if ->mk_active_refs > 0.
450444
*/
451445
struct hlist_node mk_node;
@@ -456,7 +450,7 @@ struct fscrypt_master_key {
456450
/*
457451
* Active and structural reference counts. An active ref guarantees
458452
* that the struct continues to exist, continues to be in the keyring
459-
* ->mk_sb->s_master_keys, and that any embedded subkeys (e.g.
453+
* ->s_master_keys, and that any embedded subkeys (e.g.
460454
* ->mk_direct_keys) that have been prepared continue to exist.
461455
* A structural ref only guarantees that the struct continues to exist.
462456
*
@@ -569,7 +563,8 @@ static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
569563

570564
void fscrypt_put_master_key(struct fscrypt_master_key *mk);
571565

572-
void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk);
566+
void fscrypt_put_master_key_activeref(struct super_block *sb,
567+
struct fscrypt_master_key *mk);
573568

574569
struct fscrypt_master_key *
575570
fscrypt_find_master_key(struct super_block *sb,

fs/crypto/keyring.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ void fscrypt_put_master_key(struct fscrypt_master_key *mk)
7979
call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
8080
}
8181

82-
void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk)
82+
void fscrypt_put_master_key_activeref(struct super_block *sb,
83+
struct fscrypt_master_key *mk)
8384
{
84-
struct super_block *sb = mk->mk_sb;
85-
struct fscrypt_keyring *keyring = sb->s_master_keys;
8685
size_t i;
8786

8887
if (!refcount_dec_and_test(&mk->mk_active_refs))
@@ -93,9 +92,9 @@ void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk)
9392
* destroying any subkeys embedded in it.
9493
*/
9594

96-
spin_lock(&keyring->lock);
95+
spin_lock(&sb->s_master_keys->lock);
9796
hlist_del_rcu(&mk->mk_node);
98-
spin_unlock(&keyring->lock);
97+
spin_unlock(&sb->s_master_keys->lock);
9998

10099
/*
101100
* ->mk_active_refs == 0 implies that ->mk_secret is not present and
@@ -243,7 +242,7 @@ void fscrypt_destroy_keyring(struct super_block *sb)
243242
WARN_ON(refcount_read(&mk->mk_struct_refs) != 1);
244243
WARN_ON(!is_master_key_secret_present(&mk->mk_secret));
245244
wipe_master_key_secret(&mk->mk_secret);
246-
fscrypt_put_master_key_activeref(mk);
245+
fscrypt_put_master_key_activeref(sb, mk);
247246
}
248247
}
249248
kfree_sensitive(keyring);
@@ -424,7 +423,6 @@ static int add_new_master_key(struct super_block *sb,
424423
if (!mk)
425424
return -ENOMEM;
426425

427-
mk->mk_sb = sb;
428426
init_rwsem(&mk->mk_sem);
429427
refcount_set(&mk->mk_struct_refs, 1);
430428
mk->mk_spec = *mk_spec;
@@ -1068,7 +1066,7 @@ static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
10681066
err = -ENOKEY;
10691067
if (is_master_key_secret_present(&mk->mk_secret)) {
10701068
wipe_master_key_secret(&mk->mk_secret);
1071-
fscrypt_put_master_key_activeref(mk);
1069+
fscrypt_put_master_key_activeref(sb, mk);
10721070
err = 0;
10731071
}
10741072
inodes_remain = refcount_read(&mk->mk_active_refs) > 0;

fs/crypto/keysetup.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ struct fscrypt_mode fscrypt_modes[] = {
4444
.security_strength = 16,
4545
.ivsize = 16,
4646
},
47+
[FSCRYPT_MODE_SM4_XTS] = {
48+
.friendly_name = "SM4-XTS",
49+
.cipher_str = "xts(sm4)",
50+
.keysize = 32,
51+
.security_strength = 16,
52+
.ivsize = 16,
53+
.blk_crypto_mode = BLK_ENCRYPTION_MODE_SM4_XTS,
54+
},
55+
[FSCRYPT_MODE_SM4_CTS] = {
56+
.friendly_name = "SM4-CTS-CBC",
57+
.cipher_str = "cts(cbc(sm4))",
58+
.keysize = 16,
59+
.security_strength = 16,
60+
.ivsize = 16,
61+
},
4762
[FSCRYPT_MODE_ADIANTUM] = {
4863
.friendly_name = "Adiantum",
4964
.cipher_str = "adiantum(xchacha12,aes)",
@@ -509,7 +524,7 @@ static void put_crypt_info(struct fscrypt_info *ci)
509524
spin_lock(&mk->mk_decrypted_inodes_lock);
510525
list_del(&ci->ci_master_key_link);
511526
spin_unlock(&mk->mk_decrypted_inodes_lock);
512-
fscrypt_put_master_key_activeref(mk);
527+
fscrypt_put_master_key_activeref(ci->ci_inode->i_sb, mk);
513528
}
514529
memzero_explicit(ci, sizeof(*ci));
515530
kmem_cache_free(fscrypt_info_cachep, ci);

fs/crypto/policy.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ fscrypt_get_dummy_policy(struct super_block *sb)
6161
return sb->s_cop->get_dummy_policy(sb);
6262
}
6363

64+
/*
65+
* Return %true if the given combination of encryption modes is supported for v1
66+
* (and later) encryption policies.
67+
*
68+
* Do *not* add anything new here, since v1 encryption policies are deprecated.
69+
* New combinations of modes should go in fscrypt_valid_enc_modes_v2() only.
70+
*/
6471
static bool fscrypt_valid_enc_modes_v1(u32 contents_mode, u32 filenames_mode)
6572
{
6673
if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
@@ -83,6 +90,11 @@ static bool fscrypt_valid_enc_modes_v2(u32 contents_mode, u32 filenames_mode)
8390
if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
8491
filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
8592
return true;
93+
94+
if (contents_mode == FSCRYPT_MODE_SM4_XTS &&
95+
filenames_mode == FSCRYPT_MODE_SM4_CTS)
96+
return true;
97+
8698
return fscrypt_valid_enc_modes_v1(contents_mode, filenames_mode);
8799
}
88100

include/linux/blk-crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum blk_crypto_mode_num {
1313
BLK_ENCRYPTION_MODE_AES_256_XTS,
1414
BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
1515
BLK_ENCRYPTION_MODE_ADIANTUM,
16+
BLK_ENCRYPTION_MODE_SM4_XTS,
1617
BLK_ENCRYPTION_MODE_MAX,
1718
};
1819

include/uapi/linux/fscrypt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#define FSCRYPT_MODE_AES_256_CTS 4
2727
#define FSCRYPT_MODE_AES_128_CBC 5
2828
#define FSCRYPT_MODE_AES_128_CTS 6
29+
#define FSCRYPT_MODE_SM4_XTS 7
30+
#define FSCRYPT_MODE_SM4_CTS 8
2931
#define FSCRYPT_MODE_ADIANTUM 9
3032
#define FSCRYPT_MODE_AES_256_HCTR2 10
3133
/* If adding a mode number > 10, update FSCRYPT_MODE_MAX in fscrypt_private.h */
@@ -185,8 +187,6 @@ struct fscrypt_get_key_status_arg {
185187
#define FS_ENCRYPTION_MODE_AES_256_CTS FSCRYPT_MODE_AES_256_CTS
186188
#define FS_ENCRYPTION_MODE_AES_128_CBC FSCRYPT_MODE_AES_128_CBC
187189
#define FS_ENCRYPTION_MODE_AES_128_CTS FSCRYPT_MODE_AES_128_CTS
188-
#define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7 /* removed */
189-
#define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8 /* removed */
190190
#define FS_ENCRYPTION_MODE_ADIANTUM FSCRYPT_MODE_ADIANTUM
191191
#define FS_KEY_DESC_PREFIX FSCRYPT_KEY_DESC_PREFIX
192192
#define FS_KEY_DESC_PREFIX_SIZE FSCRYPT_KEY_DESC_PREFIX_SIZE

0 commit comments

Comments
 (0)