2 * linux/fs/ext4/crypto_key.c
4 * Copyright (C) 2015, Google, Inc.
6 * This contains encryption key functions for ext4
8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
11 #include <keys/encrypted-type.h>
12 #include <keys/user-type.h>
13 #include <linux/random.h>
14 #include <linux/scatterlist.h>
15 #include <uapi/linux/keyctl.h>
20 static void derive_crypt_complete(struct crypto_async_request
*req
, int rc
)
22 struct ext4_completion_result
*ecr
= req
->data
;
24 if (rc
== -EINPROGRESS
)
28 complete(&ecr
->completion
);
32 * ext4_derive_key_aes() - Derive a key using AES-128-ECB
33 * @deriving_key: Encryption key used for derivatio.
34 * @source_key: Source key to which to apply derivation.
35 * @derived_key: Derived key.
37 * Return: Zero on success; non-zero otherwise.
39 static int ext4_derive_key_aes(char deriving_key
[EXT4_AES_128_ECB_KEY_SIZE
],
40 char source_key
[EXT4_AES_256_XTS_KEY_SIZE
],
41 char derived_key
[EXT4_AES_256_XTS_KEY_SIZE
])
44 struct ablkcipher_request
*req
= NULL
;
45 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
46 struct scatterlist src_sg
, dst_sg
;
47 struct crypto_ablkcipher
*tfm
= crypto_alloc_ablkcipher("ecb(aes)", 0,
55 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
56 req
= ablkcipher_request_alloc(tfm
, GFP_NOFS
);
61 ablkcipher_request_set_callback(req
,
62 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
63 derive_crypt_complete
, &ecr
);
64 res
= crypto_ablkcipher_setkey(tfm
, deriving_key
,
65 EXT4_AES_128_ECB_KEY_SIZE
);
68 sg_init_one(&src_sg
, source_key
, EXT4_AES_256_XTS_KEY_SIZE
);
69 sg_init_one(&dst_sg
, derived_key
, EXT4_AES_256_XTS_KEY_SIZE
);
70 ablkcipher_request_set_crypt(req
, &src_sg
, &dst_sg
,
71 EXT4_AES_256_XTS_KEY_SIZE
, NULL
);
72 res
= crypto_ablkcipher_encrypt(req
);
73 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
74 BUG_ON(req
->base
.data
!= &ecr
);
75 wait_for_completion(&ecr
.completion
);
81 ablkcipher_request_free(req
);
83 crypto_free_ablkcipher(tfm
);
88 * ext4_generate_encryption_key() - generates an encryption key
89 * @inode: The inode to generate the encryption key for.
91 int ext4_generate_encryption_key(struct inode
*inode
)
93 struct ext4_inode_info
*ei
= EXT4_I(inode
);
94 struct ext4_encryption_key
*crypt_key
= &ei
->i_encryption_key
;
95 char full_key_descriptor
[EXT4_KEY_DESC_PREFIX_SIZE
+
96 (EXT4_KEY_DESCRIPTOR_SIZE
* 2) + 1];
97 struct key
*keyring_key
= NULL
;
98 struct ext4_encryption_key
*master_key
;
99 struct ext4_encryption_context ctx
;
100 struct user_key_payload
*ukp
;
101 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
102 int res
= ext4_xattr_get(inode
, EXT4_XATTR_INDEX_ENCRYPTION
,
103 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT
,
106 if (res
!= sizeof(ctx
)) {
113 ei
->i_crypt_policy_flags
= ctx
.flags
;
114 if (S_ISREG(inode
->i_mode
))
115 crypt_key
->mode
= ctx
.contents_encryption_mode
;
116 else if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
117 crypt_key
->mode
= ctx
.filenames_encryption_mode
;
119 printk(KERN_ERR
"ext4 crypto: Unsupported inode type.\n");
122 crypt_key
->size
= ext4_encryption_key_size(crypt_key
->mode
);
123 BUG_ON(!crypt_key
->size
);
124 if (DUMMY_ENCRYPTION_ENABLED(sbi
)) {
125 memset(crypt_key
->raw
, 0x42, EXT4_AES_256_XTS_KEY_SIZE
);
128 memcpy(full_key_descriptor
, EXT4_KEY_DESC_PREFIX
,
129 EXT4_KEY_DESC_PREFIX_SIZE
);
130 sprintf(full_key_descriptor
+ EXT4_KEY_DESC_PREFIX_SIZE
,
131 "%*phN", EXT4_KEY_DESCRIPTOR_SIZE
,
132 ctx
.master_key_descriptor
);
133 full_key_descriptor
[EXT4_KEY_DESC_PREFIX_SIZE
+
134 (2 * EXT4_KEY_DESCRIPTOR_SIZE
)] = '\0';
135 keyring_key
= request_key(&key_type_logon
, full_key_descriptor
, NULL
);
136 if (IS_ERR(keyring_key
)) {
137 res
= PTR_ERR(keyring_key
);
141 BUG_ON(keyring_key
->type
!= &key_type_logon
);
142 ukp
= ((struct user_key_payload
*)keyring_key
->payload
.data
);
143 if (ukp
->datalen
!= sizeof(struct ext4_encryption_key
)) {
147 master_key
= (struct ext4_encryption_key
*)ukp
->data
;
148 BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE
!=
149 EXT4_KEY_DERIVATION_NONCE_SIZE
);
150 BUG_ON(master_key
->size
!= EXT4_AES_256_XTS_KEY_SIZE
);
151 res
= ext4_derive_key_aes(ctx
.nonce
, master_key
->raw
, crypt_key
->raw
);
154 key_put(keyring_key
);
156 crypt_key
->mode
= EXT4_ENCRYPTION_MODE_INVALID
;
160 int ext4_has_encryption_key(struct inode
*inode
)
162 struct ext4_inode_info
*ei
= EXT4_I(inode
);
163 struct ext4_encryption_key
*crypt_key
= &ei
->i_encryption_key
;
165 return (crypt_key
->mode
!= EXT4_ENCRYPTION_MODE_INVALID
);