ext4 crypto: use slab caches
[deliverable/linux.git] / fs / ext4 / crypto_key.c
CommitLineData
88bd6ccd
MH
1/*
2 * linux/fs/ext4/crypto_key.c
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains encryption key functions for ext4
7 *
8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
9 */
10
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>
16
17#include "ext4.h"
18#include "xattr.h"
19
20static void derive_crypt_complete(struct crypto_async_request *req, int rc)
21{
22 struct ext4_completion_result *ecr = req->data;
23
24 if (rc == -EINPROGRESS)
25 return;
26
27 ecr->res = rc;
28 complete(&ecr->completion);
29}
30
31/**
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.
36 *
37 * Return: Zero on success; non-zero otherwise.
38 */
39static 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])
42{
43 int res = 0;
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,
48 0);
49
50 if (IS_ERR(tfm)) {
51 res = PTR_ERR(tfm);
52 tfm = NULL;
53 goto out;
54 }
55 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
56 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
57 if (!req) {
58 res = -ENOMEM;
59 goto out;
60 }
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);
66 if (res < 0)
67 goto out;
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);
76 res = ecr.res;
77 }
78
79out:
80 if (req)
81 ablkcipher_request_free(req);
82 if (tfm)
83 crypto_free_ablkcipher(tfm);
84 return res;
85}
86
b7236e21
TT
87void ext4_free_encryption_info(struct inode *inode)
88{
89 struct ext4_inode_info *ei = EXT4_I(inode);
90 struct ext4_crypt_info *ci = ei->i_crypt_info;
91
92 if (!ci)
93 return;
94
95 if (ci->ci_keyring_key)
96 key_put(ci->ci_keyring_key);
97 crypto_free_ablkcipher(ci->ci_ctfm);
98 memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
8ee03714 99 kmem_cache_free(ext4_crypt_info_cachep, ci);
b7236e21
TT
100 ei->i_crypt_info = NULL;
101}
102
103int _ext4_get_encryption_info(struct inode *inode)
88bd6ccd
MH
104{
105 struct ext4_inode_info *ei = EXT4_I(inode);
b7236e21 106 struct ext4_crypt_info *crypt_info;
88bd6ccd
MH
107 char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
108 (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
109 struct key *keyring_key = NULL;
110 struct ext4_encryption_key *master_key;
111 struct ext4_encryption_context ctx;
112 struct user_key_payload *ukp;
6ddb2447 113 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
b7236e21 114 int res;
88bd6ccd 115
8ee03714
TT
116 if (!ext4_read_workqueue) {
117 res = ext4_init_crypto();
118 if (res)
119 return res;
120 }
121
b7236e21
TT
122 if (ei->i_crypt_info) {
123 if (!ei->i_crypt_info->ci_keyring_key ||
124 key_validate(ei->i_crypt_info->ci_keyring_key) == 0)
125 return 0;
126 ext4_free_encryption_info(inode);
88bd6ccd 127 }
b7236e21
TT
128
129 res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
130 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
131 &ctx, sizeof(ctx));
132 if (res < 0) {
133 if (!DUMMY_ENCRYPTION_ENABLED(sbi))
134 return res;
135 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
136 ctx.filenames_encryption_mode =
137 EXT4_ENCRYPTION_MODE_AES_256_CTS;
138 ctx.flags = 0;
139 } else if (res != sizeof(ctx))
140 return -EINVAL;
88bd6ccd
MH
141 res = 0;
142
8ee03714 143 crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
b7236e21
TT
144 if (!crypt_info)
145 return -ENOMEM;
146
a44cd7a0 147 ei->i_crypt_policy_flags = ctx.flags;
b7236e21
TT
148 crypt_info->ci_flags = ctx.flags;
149 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
150 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
151 crypt_info->ci_ctfm = NULL;
152 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
153 sizeof(crypt_info->ci_master_key));
6ddb2447 154 if (S_ISREG(inode->i_mode))
e2881b1b 155 crypt_info->ci_mode = ctx.contents_encryption_mode;
6ddb2447 156 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
e2881b1b 157 crypt_info->ci_mode = ctx.filenames_encryption_mode;
6ddb2447
TT
158 else {
159 printk(KERN_ERR "ext4 crypto: Unsupported inode type.\n");
160 BUG();
161 }
e2881b1b
TT
162 crypt_info->ci_size = ext4_encryption_key_size(crypt_info->ci_mode);
163 BUG_ON(!crypt_info->ci_size);
6ddb2447 164 if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
e2881b1b 165 memset(crypt_info->ci_raw, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
6ddb2447
TT
166 goto out;
167 }
88bd6ccd
MH
168 memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
169 EXT4_KEY_DESC_PREFIX_SIZE);
170 sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
171 "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
172 ctx.master_key_descriptor);
173 full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
174 (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
175 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
176 if (IS_ERR(keyring_key)) {
177 res = PTR_ERR(keyring_key);
178 keyring_key = NULL;
179 goto out;
180 }
181 BUG_ON(keyring_key->type != &key_type_logon);
182 ukp = ((struct user_key_payload *)keyring_key->payload.data);
183 if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
184 res = -EINVAL;
185 goto out;
186 }
187 master_key = (struct ext4_encryption_key *)ukp->data;
88bd6ccd
MH
188 BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
189 EXT4_KEY_DERIVATION_NONCE_SIZE);
190 BUG_ON(master_key->size != EXT4_AES_256_XTS_KEY_SIZE);
e2881b1b
TT
191 res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
192 crypt_info->ci_raw);
88bd6ccd 193out:
b7236e21
TT
194 if (res < 0) {
195 if (res == -ENOKEY)
196 res = 0;
8ee03714 197 kmem_cache_free(ext4_crypt_info_cachep, crypt_info);
b7236e21
TT
198 } else {
199 ei->i_crypt_info = crypt_info;
200 crypt_info->ci_keyring_key = keyring_key;
201 keyring_key = NULL;
202 }
88bd6ccd
MH
203 if (keyring_key)
204 key_put(keyring_key);
88bd6ccd
MH
205 return res;
206}
207
208int ext4_has_encryption_key(struct inode *inode)
209{
210 struct ext4_inode_info *ei = EXT4_I(inode);
88bd6ccd 211
b7236e21 212 return (ei->i_crypt_info != NULL);
88bd6ccd 213}
This page took 0.036769 seconds and 5 git commands to generate.