Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7e70cb49 MZ |
2 | /* |
3 | * Copyright (C) 2010 IBM Corporation | |
4e561d38 | 4 | * Copyright (C) 2010 Politecnico di Torino, Italy |
c9fecf50 | 5 | * TORSEC group -- https://security.polito.it |
7e70cb49 | 6 | * |
4e561d38 | 7 | * Authors: |
7e70cb49 | 8 | * Mimi Zohar <zohar@us.ibm.com> |
4e561d38 | 9 | * Roberto Sassu <roberto.sassu@polito.it> |
7e70cb49 | 10 | * |
5395d312 | 11 | * See Documentation/security/keys/trusted-encrypted.rst |
7e70cb49 MZ |
12 | */ |
13 | ||
14 | #include <linux/uaccess.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/parser.h> | |
19 | #include <linux/string.h> | |
93ae86e7 | 20 | #include <linux/err.h> |
7e70cb49 MZ |
21 | #include <keys/user-type.h> |
22 | #include <keys/trusted-type.h> | |
23 | #include <keys/encrypted-type.h> | |
24 | #include <linux/key-type.h> | |
25 | #include <linux/random.h> | |
26 | #include <linux/rcupdate.h> | |
27 | #include <linux/scatterlist.h> | |
79a73d18 | 28 | #include <linux/ctype.h> |
456bee98 | 29 | #include <crypto/aes.h> |
7e70cb49 | 30 | #include <crypto/hash.h> |
a24d22b2 | 31 | #include <crypto/sha2.h> |
c3917fd9 | 32 | #include <crypto/skcipher.h> |
fb3bc06a | 33 | #include <crypto/utils.h> |
7e70cb49 | 34 | |
b9703449 | 35 | #include "encrypted.h" |
79a73d18 | 36 | #include "ecryptfs_format.h" |
7e70cb49 | 37 | |
3b1826ce MZ |
38 | static const char KEY_TRUSTED_PREFIX[] = "trusted:"; |
39 | static const char KEY_USER_PREFIX[] = "user:"; | |
7e70cb49 MZ |
40 | static const char hash_alg[] = "sha256"; |
41 | static const char hmac_alg[] = "hmac(sha256)"; | |
42 | static const char blkcipher_alg[] = "cbc(aes)"; | |
4e561d38 | 43 | static const char key_format_default[] = "default"; |
79a73d18 | 44 | static const char key_format_ecryptfs[] = "ecryptfs"; |
9db67581 | 45 | static const char key_format_enc32[] = "enc32"; |
7e70cb49 MZ |
46 | static unsigned int ivsize; |
47 | static int blksize; | |
48 | ||
3b1826ce MZ |
49 | #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1) |
50 | #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1) | |
79a73d18 | 51 | #define KEY_ECRYPTFS_DESC_LEN 16 |
3b1826ce MZ |
52 | #define HASH_SIZE SHA256_DIGEST_SIZE |
53 | #define MAX_DATA_SIZE 4096 | |
54 | #define MIN_DATA_SIZE 20 | |
9db67581 | 55 | #define KEY_ENC32_PAYLOAD_LEN 32 |
3b1826ce | 56 | |
64d107d3 | 57 | static struct crypto_shash *hash_tfm; |
7e70cb49 MZ |
58 | |
59 | enum { | |
107dfa2e | 60 | Opt_new, Opt_load, Opt_update, Opt_err |
7e70cb49 MZ |
61 | }; |
62 | ||
4e561d38 | 63 | enum { |
107dfa2e | 64 | Opt_default, Opt_ecryptfs, Opt_enc32, Opt_error |
4e561d38 RS |
65 | }; |
66 | ||
67 | static const match_table_t key_format_tokens = { | |
68 | {Opt_default, "default"}, | |
79a73d18 | 69 | {Opt_ecryptfs, "ecryptfs"}, |
9db67581 | 70 | {Opt_enc32, "enc32"}, |
4e561d38 RS |
71 | {Opt_error, NULL} |
72 | }; | |
73 | ||
7e70cb49 MZ |
74 | static const match_table_t key_tokens = { |
75 | {Opt_new, "new"}, | |
76 | {Opt_load, "load"}, | |
77 | {Opt_update, "update"}, | |
78 | {Opt_err, NULL} | |
79 | }; | |
80 | ||
cd3bc044 YT |
81 | static bool user_decrypted_data = IS_ENABLED(CONFIG_USER_DECRYPTED_DATA); |
82 | module_param(user_decrypted_data, bool, 0); | |
83 | MODULE_PARM_DESC(user_decrypted_data, | |
84 | "Allow instantiation of encrypted keys using provided decrypted data"); | |
85 | ||
7e70cb49 MZ |
86 | static int aes_get_sizes(void) |
87 | { | |
c3917fd9 | 88 | struct crypto_skcipher *tfm; |
7e70cb49 | 89 | |
c3917fd9 | 90 | tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
7e70cb49 MZ |
91 | if (IS_ERR(tfm)) { |
92 | pr_err("encrypted_key: failed to alloc_cipher (%ld)\n", | |
93 | PTR_ERR(tfm)); | |
94 | return PTR_ERR(tfm); | |
95 | } | |
c3917fd9 HX |
96 | ivsize = crypto_skcipher_ivsize(tfm); |
97 | blksize = crypto_skcipher_blocksize(tfm); | |
98 | crypto_free_skcipher(tfm); | |
7e70cb49 MZ |
99 | return 0; |
100 | } | |
101 | ||
79a73d18 RS |
102 | /* |
103 | * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key | |
104 | * | |
105 | * The description of a encrypted key with format 'ecryptfs' must contain | |
106 | * exactly 16 hexadecimal characters. | |
107 | * | |
108 | */ | |
109 | static int valid_ecryptfs_desc(const char *ecryptfs_desc) | |
110 | { | |
111 | int i; | |
112 | ||
113 | if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) { | |
114 | pr_err("encrypted_key: key description must be %d hexadecimal " | |
115 | "characters long\n", KEY_ECRYPTFS_DESC_LEN); | |
116 | return -EINVAL; | |
117 | } | |
118 | ||
119 | for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) { | |
120 | if (!isxdigit(ecryptfs_desc[i])) { | |
121 | pr_err("encrypted_key: key description must contain " | |
122 | "only hexadecimal characters\n"); | |
123 | return -EINVAL; | |
124 | } | |
125 | } | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
7e70cb49 MZ |
130 | /* |
131 | * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key | |
132 | * | |
08fa2aa5 | 133 | * key-type:= "trusted:" | "user:" |
7e70cb49 MZ |
134 | * desc:= master-key description |
135 | * | |
136 | * Verify that 'key-type' is valid and that 'desc' exists. On key update, | |
137 | * only the master key description is permitted to change, not the key-type. | |
138 | * The key-type remains constant. | |
139 | * | |
140 | * On success returns 0, otherwise -EINVAL. | |
141 | */ | |
142 | static int valid_master_desc(const char *new_desc, const char *orig_desc) | |
143 | { | |
794b4bc2 EB |
144 | int prefix_len; |
145 | ||
146 | if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) | |
147 | prefix_len = KEY_TRUSTED_PREFIX_LEN; | |
148 | else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) | |
149 | prefix_len = KEY_USER_PREFIX_LEN; | |
150 | else | |
151 | return -EINVAL; | |
152 | ||
153 | if (!new_desc[prefix_len]) | |
154 | return -EINVAL; | |
155 | ||
156 | if (orig_desc && strncmp(new_desc, orig_desc, prefix_len)) | |
157 | return -EINVAL; | |
158 | ||
7e70cb49 | 159 | return 0; |
7e70cb49 MZ |
160 | } |
161 | ||
162 | /* | |
163 | * datablob_parse - parse the keyctl data | |
164 | * | |
165 | * datablob format: | |
cd3bc044 | 166 | * new [<format>] <master-key name> <decrypted data length> [<decrypted data>] |
4e561d38 RS |
167 | * load [<format>] <master-key name> <decrypted data length> |
168 | * <encrypted iv + data> | |
7e70cb49 MZ |
169 | * update <new-master-key name> |
170 | * | |
171 | * Tokenizes a copy of the keyctl data, returning a pointer to each token, | |
172 | * which is null terminated. | |
173 | * | |
174 | * On success returns 0, otherwise -EINVAL. | |
175 | */ | |
4e561d38 RS |
176 | static int datablob_parse(char *datablob, const char **format, |
177 | char **master_desc, char **decrypted_datalen, | |
cd3bc044 | 178 | char **hex_encoded_iv, char **decrypted_data) |
7e70cb49 MZ |
179 | { |
180 | substring_t args[MAX_OPT_ARGS]; | |
181 | int ret = -EINVAL; | |
182 | int key_cmd; | |
4e561d38 RS |
183 | int key_format; |
184 | char *p, *keyword; | |
7e70cb49 | 185 | |
7103dff0 RS |
186 | keyword = strsep(&datablob, " \t"); |
187 | if (!keyword) { | |
188 | pr_info("encrypted_key: insufficient parameters specified\n"); | |
7e70cb49 | 189 | return ret; |
7103dff0 RS |
190 | } |
191 | key_cmd = match_token(keyword, key_tokens, args); | |
7e70cb49 | 192 | |
79a73d18 | 193 | /* Get optional format: default | ecryptfs */ |
4e561d38 RS |
194 | p = strsep(&datablob, " \t"); |
195 | if (!p) { | |
196 | pr_err("encrypted_key: insufficient parameters specified\n"); | |
197 | return ret; | |
198 | } | |
199 | ||
200 | key_format = match_token(p, key_format_tokens, args); | |
201 | switch (key_format) { | |
79a73d18 | 202 | case Opt_ecryptfs: |
9db67581 | 203 | case Opt_enc32: |
4e561d38 RS |
204 | case Opt_default: |
205 | *format = p; | |
206 | *master_desc = strsep(&datablob, " \t"); | |
207 | break; | |
208 | case Opt_error: | |
209 | *master_desc = p; | |
210 | break; | |
211 | } | |
212 | ||
7103dff0 RS |
213 | if (!*master_desc) { |
214 | pr_info("encrypted_key: master key parameter is missing\n"); | |
7e70cb49 | 215 | goto out; |
7103dff0 | 216 | } |
7e70cb49 | 217 | |
7103dff0 RS |
218 | if (valid_master_desc(*master_desc, NULL) < 0) { |
219 | pr_info("encrypted_key: master key parameter \'%s\' " | |
220 | "is invalid\n", *master_desc); | |
7e70cb49 | 221 | goto out; |
7103dff0 | 222 | } |
7e70cb49 MZ |
223 | |
224 | if (decrypted_datalen) { | |
225 | *decrypted_datalen = strsep(&datablob, " \t"); | |
7103dff0 RS |
226 | if (!*decrypted_datalen) { |
227 | pr_info("encrypted_key: keylen parameter is missing\n"); | |
7e70cb49 | 228 | goto out; |
7103dff0 | 229 | } |
7e70cb49 MZ |
230 | } |
231 | ||
232 | switch (key_cmd) { | |
233 | case Opt_new: | |
7103dff0 RS |
234 | if (!decrypted_datalen) { |
235 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
236 | "when called from .update method\n", keyword); | |
7e70cb49 | 237 | break; |
7103dff0 | 238 | } |
cd3bc044 | 239 | *decrypted_data = strsep(&datablob, " \t"); |
7e70cb49 MZ |
240 | ret = 0; |
241 | break; | |
242 | case Opt_load: | |
7103dff0 RS |
243 | if (!decrypted_datalen) { |
244 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
245 | "when called from .update method\n", keyword); | |
7e70cb49 | 246 | break; |
7103dff0 | 247 | } |
7e70cb49 | 248 | *hex_encoded_iv = strsep(&datablob, " \t"); |
7103dff0 RS |
249 | if (!*hex_encoded_iv) { |
250 | pr_info("encrypted_key: hex blob is missing\n"); | |
7e70cb49 | 251 | break; |
7103dff0 | 252 | } |
7e70cb49 MZ |
253 | ret = 0; |
254 | break; | |
255 | case Opt_update: | |
7103dff0 RS |
256 | if (decrypted_datalen) { |
257 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
258 | "when called from .instantiate method\n", | |
259 | keyword); | |
7e70cb49 | 260 | break; |
7103dff0 | 261 | } |
7e70cb49 MZ |
262 | ret = 0; |
263 | break; | |
264 | case Opt_err: | |
7103dff0 RS |
265 | pr_info("encrypted_key: keyword \'%s\' not recognized\n", |
266 | keyword); | |
7e70cb49 MZ |
267 | break; |
268 | } | |
269 | out: | |
270 | return ret; | |
271 | } | |
272 | ||
273 | /* | |
274 | * datablob_format - format as an ascii string, before copying to userspace | |
275 | */ | |
276 | static char *datablob_format(struct encrypted_key_payload *epayload, | |
277 | size_t asciiblob_len) | |
278 | { | |
279 | char *ascii_buf, *bufp; | |
280 | u8 *iv = epayload->iv; | |
281 | int len; | |
282 | int i; | |
283 | ||
284 | ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL); | |
285 | if (!ascii_buf) | |
286 | goto out; | |
287 | ||
288 | ascii_buf[asciiblob_len] = '\0'; | |
289 | ||
290 | /* copy datablob master_desc and datalen strings */ | |
4e561d38 RS |
291 | len = sprintf(ascii_buf, "%s %s %s ", epayload->format, |
292 | epayload->master_desc, epayload->datalen); | |
7e70cb49 MZ |
293 | |
294 | /* convert the hex encoded iv, encrypted-data and HMAC to ascii */ | |
295 | bufp = &ascii_buf[len]; | |
296 | for (i = 0; i < (asciiblob_len - len) / 2; i++) | |
02473119 | 297 | bufp = hex_byte_pack(bufp, iv[i]); |
7e70cb49 MZ |
298 | out: |
299 | return ascii_buf; | |
300 | } | |
301 | ||
7e70cb49 MZ |
302 | /* |
303 | * request_user_key - request the user key | |
304 | * | |
305 | * Use a user provided key to encrypt/decrypt an encrypted-key. | |
306 | */ | |
146aa8b1 | 307 | static struct key *request_user_key(const char *master_desc, const u8 **master_key, |
3b1826ce | 308 | size_t *master_keylen) |
7e70cb49 | 309 | { |
146aa8b1 | 310 | const struct user_key_payload *upayload; |
7e70cb49 MZ |
311 | struct key *ukey; |
312 | ||
028db3e2 | 313 | ukey = request_key(&key_type_user, master_desc, NULL); |
7e70cb49 MZ |
314 | if (IS_ERR(ukey)) |
315 | goto error; | |
316 | ||
317 | down_read(&ukey->sem); | |
0837e49a | 318 | upayload = user_key_payload_locked(ukey); |
13923d08 EB |
319 | if (!upayload) { |
320 | /* key was revoked before we acquired its semaphore */ | |
321 | up_read(&ukey->sem); | |
322 | key_put(ukey); | |
323 | ukey = ERR_PTR(-EKEYREVOKED); | |
324 | goto error; | |
325 | } | |
7e70cb49 MZ |
326 | *master_key = upayload->data; |
327 | *master_keylen = upayload->datalen; | |
328 | error: | |
329 | return ukey; | |
330 | } | |
331 | ||
64d107d3 EB |
332 | static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen, |
333 | const u8 *buf, unsigned int buflen) | |
7e70cb49 | 334 | { |
64d107d3 EB |
335 | struct crypto_shash *tfm; |
336 | int err; | |
7e70cb49 | 337 | |
3d234b33 | 338 | tfm = crypto_alloc_shash(hmac_alg, 0, 0); |
64d107d3 EB |
339 | if (IS_ERR(tfm)) { |
340 | pr_err("encrypted_key: can't alloc %s transform: %ld\n", | |
341 | hmac_alg, PTR_ERR(tfm)); | |
342 | return PTR_ERR(tfm); | |
7e70cb49 MZ |
343 | } |
344 | ||
64d107d3 EB |
345 | err = crypto_shash_setkey(tfm, key, keylen); |
346 | if (!err) | |
bce395ee | 347 | err = crypto_shash_tfm_digest(tfm, buf, buflen, digest); |
64d107d3 EB |
348 | crypto_free_shash(tfm); |
349 | return err; | |
7e70cb49 MZ |
350 | } |
351 | ||
352 | enum derived_key_type { ENC_KEY, AUTH_KEY }; | |
353 | ||
354 | /* Derive authentication/encryption key from trusted key */ | |
355 | static int get_derived_key(u8 *derived_key, enum derived_key_type key_type, | |
3b1826ce | 356 | const u8 *master_key, size_t master_keylen) |
7e70cb49 MZ |
357 | { |
358 | u8 *derived_buf; | |
359 | unsigned int derived_buf_len; | |
360 | int ret; | |
361 | ||
362 | derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen; | |
363 | if (derived_buf_len < HASH_SIZE) | |
364 | derived_buf_len = HASH_SIZE; | |
365 | ||
366 | derived_buf = kzalloc(derived_buf_len, GFP_KERNEL); | |
41f1c53e | 367 | if (!derived_buf) |
7e70cb49 | 368 | return -ENOMEM; |
41f1c53e | 369 | |
7e70cb49 MZ |
370 | if (key_type) |
371 | strcpy(derived_buf, "AUTH_KEY"); | |
372 | else | |
373 | strcpy(derived_buf, "ENC_KEY"); | |
374 | ||
375 | memcpy(derived_buf + strlen(derived_buf) + 1, master_key, | |
376 | master_keylen); | |
bce395ee EB |
377 | ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len, |
378 | derived_key); | |
453431a5 | 379 | kfree_sensitive(derived_buf); |
7e70cb49 MZ |
380 | return ret; |
381 | } | |
382 | ||
c3917fd9 HX |
383 | static struct skcipher_request *init_skcipher_req(const u8 *key, |
384 | unsigned int key_len) | |
7e70cb49 | 385 | { |
c3917fd9 HX |
386 | struct skcipher_request *req; |
387 | struct crypto_skcipher *tfm; | |
7e70cb49 MZ |
388 | int ret; |
389 | ||
c3917fd9 HX |
390 | tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
391 | if (IS_ERR(tfm)) { | |
7e70cb49 | 392 | pr_err("encrypted_key: failed to load %s transform (%ld)\n", |
c3917fd9 HX |
393 | blkcipher_alg, PTR_ERR(tfm)); |
394 | return ERR_CAST(tfm); | |
7e70cb49 | 395 | } |
7e70cb49 | 396 | |
c3917fd9 | 397 | ret = crypto_skcipher_setkey(tfm, key, key_len); |
7e70cb49 MZ |
398 | if (ret < 0) { |
399 | pr_err("encrypted_key: failed to setkey (%d)\n", ret); | |
c3917fd9 HX |
400 | crypto_free_skcipher(tfm); |
401 | return ERR_PTR(ret); | |
7e70cb49 | 402 | } |
c3917fd9 HX |
403 | |
404 | req = skcipher_request_alloc(tfm, GFP_KERNEL); | |
405 | if (!req) { | |
406 | pr_err("encrypted_key: failed to allocate request for %s\n", | |
407 | blkcipher_alg); | |
408 | crypto_free_skcipher(tfm); | |
409 | return ERR_PTR(-ENOMEM); | |
410 | } | |
411 | ||
412 | skcipher_request_set_callback(req, 0, NULL, NULL); | |
413 | return req; | |
7e70cb49 MZ |
414 | } |
415 | ||
416 | static struct key *request_master_key(struct encrypted_key_payload *epayload, | |
146aa8b1 | 417 | const u8 **master_key, size_t *master_keylen) |
7e70cb49 | 418 | { |
57cb17e7 | 419 | struct key *mkey = ERR_PTR(-EINVAL); |
7e70cb49 MZ |
420 | |
421 | if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX, | |
422 | KEY_TRUSTED_PREFIX_LEN)) { | |
423 | mkey = request_trusted_key(epayload->master_desc + | |
424 | KEY_TRUSTED_PREFIX_LEN, | |
425 | master_key, master_keylen); | |
426 | } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX, | |
427 | KEY_USER_PREFIX_LEN)) { | |
428 | mkey = request_user_key(epayload->master_desc + | |
429 | KEY_USER_PREFIX_LEN, | |
430 | master_key, master_keylen); | |
431 | } else | |
432 | goto out; | |
433 | ||
f91c2c5c | 434 | if (IS_ERR(mkey)) { |
f4a0d5ab | 435 | int ret = PTR_ERR(mkey); |
982e617a MZ |
436 | |
437 | if (ret == -ENOTSUPP) | |
438 | pr_info("encrypted_key: key %s not supported", | |
439 | epayload->master_desc); | |
440 | else | |
441 | pr_info("encrypted_key: key %s not found", | |
442 | epayload->master_desc); | |
f91c2c5c RS |
443 | goto out; |
444 | } | |
445 | ||
446 | dump_master_key(*master_key, *master_keylen); | |
7e70cb49 MZ |
447 | out: |
448 | return mkey; | |
449 | } | |
450 | ||
451 | /* Before returning data to userspace, encrypt decrypted data. */ | |
452 | static int derived_key_encrypt(struct encrypted_key_payload *epayload, | |
453 | const u8 *derived_key, | |
3b1826ce | 454 | unsigned int derived_keylen) |
7e70cb49 MZ |
455 | { |
456 | struct scatterlist sg_in[2]; | |
457 | struct scatterlist sg_out[1]; | |
c3917fd9 HX |
458 | struct crypto_skcipher *tfm; |
459 | struct skcipher_request *req; | |
7e70cb49 | 460 | unsigned int encrypted_datalen; |
456bee98 | 461 | u8 iv[AES_BLOCK_SIZE]; |
7e70cb49 MZ |
462 | int ret; |
463 | ||
464 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); | |
7e70cb49 | 465 | |
c3917fd9 HX |
466 | req = init_skcipher_req(derived_key, derived_keylen); |
467 | ret = PTR_ERR(req); | |
468 | if (IS_ERR(req)) | |
7e70cb49 MZ |
469 | goto out; |
470 | dump_decrypted_data(epayload); | |
471 | ||
7e70cb49 MZ |
472 | sg_init_table(sg_in, 2); |
473 | sg_set_buf(&sg_in[0], epayload->decrypted_data, | |
474 | epayload->decrypted_datalen); | |
e9ff56ac | 475 | sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0); |
7e70cb49 MZ |
476 | |
477 | sg_init_table(sg_out, 1); | |
478 | sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen); | |
479 | ||
456bee98 HX |
480 | memcpy(iv, epayload->iv, sizeof(iv)); |
481 | skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv); | |
c3917fd9 HX |
482 | ret = crypto_skcipher_encrypt(req); |
483 | tfm = crypto_skcipher_reqtfm(req); | |
484 | skcipher_request_free(req); | |
485 | crypto_free_skcipher(tfm); | |
7e70cb49 MZ |
486 | if (ret < 0) |
487 | pr_err("encrypted_key: failed to encrypt (%d)\n", ret); | |
488 | else | |
489 | dump_encrypted_data(epayload, encrypted_datalen); | |
490 | out: | |
491 | return ret; | |
492 | } | |
493 | ||
494 | static int datablob_hmac_append(struct encrypted_key_payload *epayload, | |
3b1826ce | 495 | const u8 *master_key, size_t master_keylen) |
7e70cb49 MZ |
496 | { |
497 | u8 derived_key[HASH_SIZE]; | |
498 | u8 *digest; | |
499 | int ret; | |
500 | ||
501 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); | |
502 | if (ret < 0) | |
503 | goto out; | |
504 | ||
4e561d38 | 505 | digest = epayload->format + epayload->datablob_len; |
7e70cb49 | 506 | ret = calc_hmac(digest, derived_key, sizeof derived_key, |
4e561d38 | 507 | epayload->format, epayload->datablob_len); |
7e70cb49 MZ |
508 | if (!ret) |
509 | dump_hmac(NULL, digest, HASH_SIZE); | |
510 | out: | |
a9dd74b2 | 511 | memzero_explicit(derived_key, sizeof(derived_key)); |
7e70cb49 MZ |
512 | return ret; |
513 | } | |
514 | ||
515 | /* verify HMAC before decrypting encrypted key */ | |
516 | static int datablob_hmac_verify(struct encrypted_key_payload *epayload, | |
4e561d38 RS |
517 | const u8 *format, const u8 *master_key, |
518 | size_t master_keylen) | |
7e70cb49 MZ |
519 | { |
520 | u8 derived_key[HASH_SIZE]; | |
521 | u8 digest[HASH_SIZE]; | |
522 | int ret; | |
4e561d38 RS |
523 | char *p; |
524 | unsigned short len; | |
7e70cb49 MZ |
525 | |
526 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); | |
527 | if (ret < 0) | |
528 | goto out; | |
529 | ||
4e561d38 RS |
530 | len = epayload->datablob_len; |
531 | if (!format) { | |
532 | p = epayload->master_desc; | |
533 | len -= strlen(epayload->format) + 1; | |
534 | } else | |
535 | p = epayload->format; | |
536 | ||
537 | ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len); | |
7e70cb49 MZ |
538 | if (ret < 0) |
539 | goto out; | |
0f534e4a EB |
540 | ret = crypto_memneq(digest, epayload->format + epayload->datablob_len, |
541 | sizeof(digest)); | |
7e70cb49 MZ |
542 | if (ret) { |
543 | ret = -EINVAL; | |
544 | dump_hmac("datablob", | |
4e561d38 | 545 | epayload->format + epayload->datablob_len, |
7e70cb49 MZ |
546 | HASH_SIZE); |
547 | dump_hmac("calc", digest, HASH_SIZE); | |
548 | } | |
549 | out: | |
a9dd74b2 | 550 | memzero_explicit(derived_key, sizeof(derived_key)); |
7e70cb49 MZ |
551 | return ret; |
552 | } | |
553 | ||
554 | static int derived_key_decrypt(struct encrypted_key_payload *epayload, | |
555 | const u8 *derived_key, | |
3b1826ce | 556 | unsigned int derived_keylen) |
7e70cb49 MZ |
557 | { |
558 | struct scatterlist sg_in[1]; | |
559 | struct scatterlist sg_out[2]; | |
c3917fd9 HX |
560 | struct crypto_skcipher *tfm; |
561 | struct skcipher_request *req; | |
7e70cb49 | 562 | unsigned int encrypted_datalen; |
456bee98 | 563 | u8 iv[AES_BLOCK_SIZE]; |
e9ff56ac | 564 | u8 *pad; |
7e70cb49 MZ |
565 | int ret; |
566 | ||
e9ff56ac EB |
567 | /* Throwaway buffer to hold the unused zero padding at the end */ |
568 | pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL); | |
569 | if (!pad) | |
570 | return -ENOMEM; | |
571 | ||
7e70cb49 | 572 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
c3917fd9 HX |
573 | req = init_skcipher_req(derived_key, derived_keylen); |
574 | ret = PTR_ERR(req); | |
575 | if (IS_ERR(req)) | |
7e70cb49 MZ |
576 | goto out; |
577 | dump_encrypted_data(epayload, encrypted_datalen); | |
578 | ||
7e70cb49 MZ |
579 | sg_init_table(sg_in, 1); |
580 | sg_init_table(sg_out, 2); | |
581 | sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen); | |
582 | sg_set_buf(&sg_out[0], epayload->decrypted_data, | |
3b1826ce | 583 | epayload->decrypted_datalen); |
e9ff56ac | 584 | sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE); |
7e70cb49 | 585 | |
456bee98 HX |
586 | memcpy(iv, epayload->iv, sizeof(iv)); |
587 | skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv); | |
c3917fd9 HX |
588 | ret = crypto_skcipher_decrypt(req); |
589 | tfm = crypto_skcipher_reqtfm(req); | |
590 | skcipher_request_free(req); | |
591 | crypto_free_skcipher(tfm); | |
7e70cb49 MZ |
592 | if (ret < 0) |
593 | goto out; | |
594 | dump_decrypted_data(epayload); | |
595 | out: | |
e9ff56ac | 596 | kfree(pad); |
7e70cb49 MZ |
597 | return ret; |
598 | } | |
599 | ||
600 | /* Allocate memory for decrypted key and datablob. */ | |
601 | static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, | |
4e561d38 | 602 | const char *format, |
7e70cb49 | 603 | const char *master_desc, |
cd3bc044 YT |
604 | const char *datalen, |
605 | const char *decrypted_data) | |
7e70cb49 MZ |
606 | { |
607 | struct encrypted_key_payload *epayload = NULL; | |
608 | unsigned short datablob_len; | |
609 | unsigned short decrypted_datalen; | |
4e561d38 | 610 | unsigned short payload_datalen; |
7e70cb49 | 611 | unsigned int encrypted_datalen; |
4e561d38 | 612 | unsigned int format_len; |
7e70cb49 | 613 | long dlen; |
cd3bc044 | 614 | int i; |
7e70cb49 MZ |
615 | int ret; |
616 | ||
29707b20 | 617 | ret = kstrtol(datalen, 10, &dlen); |
7e70cb49 MZ |
618 | if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE) |
619 | return ERR_PTR(-EINVAL); | |
620 | ||
4e561d38 | 621 | format_len = (!format) ? strlen(key_format_default) : strlen(format); |
7e70cb49 | 622 | decrypted_datalen = dlen; |
4e561d38 | 623 | payload_datalen = decrypted_datalen; |
cd3bc044 YT |
624 | |
625 | if (decrypted_data) { | |
626 | if (!user_decrypted_data) { | |
627 | pr_err("encrypted key: instantiation of keys using provided decrypted data is disabled since CONFIG_USER_DECRYPTED_DATA is set to false\n"); | |
628 | return ERR_PTR(-EINVAL); | |
629 | } | |
5adedd42 | 630 | if (strlen(decrypted_data) != decrypted_datalen * 2) { |
cd3bc044 YT |
631 | pr_err("encrypted key: decrypted data provided does not match decrypted data length provided\n"); |
632 | return ERR_PTR(-EINVAL); | |
633 | } | |
634 | for (i = 0; i < strlen(decrypted_data); i++) { | |
635 | if (!isxdigit(decrypted_data[i])) { | |
636 | pr_err("encrypted key: decrypted data provided must contain only hexadecimal characters\n"); | |
637 | return ERR_PTR(-EINVAL); | |
638 | } | |
639 | } | |
640 | } | |
641 | ||
9db67581 DJ |
642 | if (format) { |
643 | if (!strcmp(format, key_format_ecryptfs)) { | |
644 | if (dlen != ECRYPTFS_MAX_KEY_BYTES) { | |
645 | pr_err("encrypted_key: keylen for the ecryptfs format must be equal to %d bytes\n", | |
646 | ECRYPTFS_MAX_KEY_BYTES); | |
647 | return ERR_PTR(-EINVAL); | |
648 | } | |
649 | decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES; | |
650 | payload_datalen = sizeof(struct ecryptfs_auth_tok); | |
651 | } else if (!strcmp(format, key_format_enc32)) { | |
652 | if (decrypted_datalen != KEY_ENC32_PAYLOAD_LEN) { | |
653 | pr_err("encrypted_key: enc32 key payload incorrect length: %d\n", | |
654 | decrypted_datalen); | |
655 | return ERR_PTR(-EINVAL); | |
656 | } | |
79a73d18 | 657 | } |
79a73d18 RS |
658 | } |
659 | ||
7e70cb49 MZ |
660 | encrypted_datalen = roundup(decrypted_datalen, blksize); |
661 | ||
4e561d38 RS |
662 | datablob_len = format_len + 1 + strlen(master_desc) + 1 |
663 | + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen; | |
7e70cb49 | 664 | |
4e561d38 | 665 | ret = key_payload_reserve(key, payload_datalen + datablob_len |
7e70cb49 MZ |
666 | + HASH_SIZE + 1); |
667 | if (ret < 0) | |
668 | return ERR_PTR(ret); | |
669 | ||
4e561d38 | 670 | epayload = kzalloc(sizeof(*epayload) + payload_datalen + |
7e70cb49 MZ |
671 | datablob_len + HASH_SIZE + 1, GFP_KERNEL); |
672 | if (!epayload) | |
673 | return ERR_PTR(-ENOMEM); | |
674 | ||
4e561d38 | 675 | epayload->payload_datalen = payload_datalen; |
7e70cb49 MZ |
676 | epayload->decrypted_datalen = decrypted_datalen; |
677 | epayload->datablob_len = datablob_len; | |
678 | return epayload; | |
679 | } | |
680 | ||
681 | static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, | |
4e561d38 | 682 | const char *format, const char *hex_encoded_iv) |
7e70cb49 MZ |
683 | { |
684 | struct key *mkey; | |
685 | u8 derived_key[HASH_SIZE]; | |
146aa8b1 | 686 | const u8 *master_key; |
7e70cb49 | 687 | u8 *hmac; |
1f35065a | 688 | const char *hex_encoded_data; |
7e70cb49 | 689 | unsigned int encrypted_datalen; |
3b1826ce | 690 | size_t master_keylen; |
1f35065a | 691 | size_t asciilen; |
7e70cb49 MZ |
692 | int ret; |
693 | ||
694 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); | |
1f35065a MZ |
695 | asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2; |
696 | if (strlen(hex_encoded_iv) != asciilen) | |
697 | return -EINVAL; | |
698 | ||
699 | hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2; | |
2b3ff631 MZ |
700 | ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize); |
701 | if (ret < 0) | |
702 | return -EINVAL; | |
703 | ret = hex2bin(epayload->encrypted_data, hex_encoded_data, | |
704 | encrypted_datalen); | |
705 | if (ret < 0) | |
706 | return -EINVAL; | |
7e70cb49 | 707 | |
4e561d38 | 708 | hmac = epayload->format + epayload->datablob_len; |
2b3ff631 MZ |
709 | ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), |
710 | HASH_SIZE); | |
711 | if (ret < 0) | |
712 | return -EINVAL; | |
7e70cb49 MZ |
713 | |
714 | mkey = request_master_key(epayload, &master_key, &master_keylen); | |
715 | if (IS_ERR(mkey)) | |
716 | return PTR_ERR(mkey); | |
717 | ||
4e561d38 | 718 | ret = datablob_hmac_verify(epayload, format, master_key, master_keylen); |
7e70cb49 MZ |
719 | if (ret < 0) { |
720 | pr_err("encrypted_key: bad hmac (%d)\n", ret); | |
721 | goto out; | |
722 | } | |
723 | ||
724 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); | |
725 | if (ret < 0) | |
726 | goto out; | |
727 | ||
728 | ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key); | |
729 | if (ret < 0) | |
730 | pr_err("encrypted_key: failed to decrypt key (%d)\n", ret); | |
731 | out: | |
732 | up_read(&mkey->sem); | |
733 | key_put(mkey); | |
a9dd74b2 | 734 | memzero_explicit(derived_key, sizeof(derived_key)); |
7e70cb49 MZ |
735 | return ret; |
736 | } | |
737 | ||
738 | static void __ekey_init(struct encrypted_key_payload *epayload, | |
4e561d38 RS |
739 | const char *format, const char *master_desc, |
740 | const char *datalen) | |
7e70cb49 | 741 | { |
4e561d38 RS |
742 | unsigned int format_len; |
743 | ||
744 | format_len = (!format) ? strlen(key_format_default) : strlen(format); | |
745 | epayload->format = epayload->payload_data + epayload->payload_datalen; | |
746 | epayload->master_desc = epayload->format + format_len + 1; | |
7e70cb49 MZ |
747 | epayload->datalen = epayload->master_desc + strlen(master_desc) + 1; |
748 | epayload->iv = epayload->datalen + strlen(datalen) + 1; | |
749 | epayload->encrypted_data = epayload->iv + ivsize + 1; | |
4e561d38 | 750 | epayload->decrypted_data = epayload->payload_data; |
7e70cb49 | 751 | |
4e561d38 RS |
752 | if (!format) |
753 | memcpy(epayload->format, key_format_default, format_len); | |
79a73d18 RS |
754 | else { |
755 | if (!strcmp(format, key_format_ecryptfs)) | |
756 | epayload->decrypted_data = | |
757 | ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data); | |
758 | ||
4e561d38 | 759 | memcpy(epayload->format, format, format_len); |
79a73d18 RS |
760 | } |
761 | ||
7e70cb49 MZ |
762 | memcpy(epayload->master_desc, master_desc, strlen(master_desc)); |
763 | memcpy(epayload->datalen, datalen, strlen(datalen)); | |
764 | } | |
765 | ||
766 | /* | |
767 | * encrypted_init - initialize an encrypted key | |
768 | * | |
cd3bc044 YT |
769 | * For a new key, use either a random number or user-provided decrypted data in |
770 | * case it is provided. A random number is used for the iv in both cases. For | |
771 | * an old key, decrypt the hex encoded data. | |
7e70cb49 MZ |
772 | */ |
773 | static int encrypted_init(struct encrypted_key_payload *epayload, | |
79a73d18 RS |
774 | const char *key_desc, const char *format, |
775 | const char *master_desc, const char *datalen, | |
cd3bc044 | 776 | const char *hex_encoded_iv, const char *decrypted_data) |
7e70cb49 MZ |
777 | { |
778 | int ret = 0; | |
779 | ||
79a73d18 RS |
780 | if (format && !strcmp(format, key_format_ecryptfs)) { |
781 | ret = valid_ecryptfs_desc(key_desc); | |
782 | if (ret < 0) | |
783 | return ret; | |
784 | ||
785 | ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data, | |
786 | key_desc); | |
787 | } | |
788 | ||
4e561d38 | 789 | __ekey_init(epayload, format, master_desc, datalen); |
cd3bc044 | 790 | if (hex_encoded_iv) { |
4e561d38 | 791 | ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv); |
cd3bc044 YT |
792 | } else if (decrypted_data) { |
793 | get_random_bytes(epayload->iv, ivsize); | |
5adedd42 NV |
794 | ret = hex2bin(epayload->decrypted_data, decrypted_data, |
795 | epayload->decrypted_datalen); | |
cd3bc044 YT |
796 | } else { |
797 | get_random_bytes(epayload->iv, ivsize); | |
798 | get_random_bytes(epayload->decrypted_data, epayload->decrypted_datalen); | |
799 | } | |
7e70cb49 MZ |
800 | return ret; |
801 | } | |
802 | ||
803 | /* | |
804 | * encrypted_instantiate - instantiate an encrypted key | |
805 | * | |
cd3bc044 YT |
806 | * Instantiates the key: |
807 | * - by decrypting an existing encrypted datablob, or | |
808 | * - by creating a new encrypted key based on a kernel random number, or | |
809 | * - using provided decrypted data. | |
7e70cb49 MZ |
810 | * |
811 | * On success, return 0. Otherwise return errno. | |
812 | */ | |
cf7f601c DH |
813 | static int encrypted_instantiate(struct key *key, |
814 | struct key_preparsed_payload *prep) | |
7e70cb49 MZ |
815 | { |
816 | struct encrypted_key_payload *epayload = NULL; | |
817 | char *datablob = NULL; | |
4e561d38 | 818 | const char *format = NULL; |
7e70cb49 MZ |
819 | char *master_desc = NULL; |
820 | char *decrypted_datalen = NULL; | |
821 | char *hex_encoded_iv = NULL; | |
cd3bc044 | 822 | char *decrypted_data = NULL; |
cf7f601c | 823 | size_t datalen = prep->datalen; |
7e70cb49 MZ |
824 | int ret; |
825 | ||
cf7f601c | 826 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
7e70cb49 MZ |
827 | return -EINVAL; |
828 | ||
829 | datablob = kmalloc(datalen + 1, GFP_KERNEL); | |
830 | if (!datablob) | |
831 | return -ENOMEM; | |
832 | datablob[datalen] = 0; | |
cf7f601c | 833 | memcpy(datablob, prep->data, datalen); |
4e561d38 | 834 | ret = datablob_parse(datablob, &format, &master_desc, |
cd3bc044 | 835 | &decrypted_datalen, &hex_encoded_iv, &decrypted_data); |
7e70cb49 MZ |
836 | if (ret < 0) |
837 | goto out; | |
838 | ||
4e561d38 | 839 | epayload = encrypted_key_alloc(key, format, master_desc, |
cd3bc044 | 840 | decrypted_datalen, decrypted_data); |
7e70cb49 MZ |
841 | if (IS_ERR(epayload)) { |
842 | ret = PTR_ERR(epayload); | |
843 | goto out; | |
844 | } | |
79a73d18 | 845 | ret = encrypted_init(epayload, key->description, format, master_desc, |
cd3bc044 | 846 | decrypted_datalen, hex_encoded_iv, decrypted_data); |
7e70cb49 | 847 | if (ret < 0) { |
453431a5 | 848 | kfree_sensitive(epayload); |
7e70cb49 MZ |
849 | goto out; |
850 | } | |
851 | ||
b64cc5fb | 852 | rcu_assign_keypointer(key, epayload); |
7e70cb49 | 853 | out: |
453431a5 | 854 | kfree_sensitive(datablob); |
7e70cb49 MZ |
855 | return ret; |
856 | } | |
857 | ||
858 | static void encrypted_rcu_free(struct rcu_head *rcu) | |
859 | { | |
860 | struct encrypted_key_payload *epayload; | |
861 | ||
862 | epayload = container_of(rcu, struct encrypted_key_payload, rcu); | |
453431a5 | 863 | kfree_sensitive(epayload); |
7e70cb49 MZ |
864 | } |
865 | ||
866 | /* | |
867 | * encrypted_update - update the master key description | |
868 | * | |
869 | * Change the master key description for an existing encrypted key. | |
870 | * The next read will return an encrypted datablob using the new | |
871 | * master key description. | |
872 | * | |
873 | * On success, return 0. Otherwise return errno. | |
874 | */ | |
cf7f601c | 875 | static int encrypted_update(struct key *key, struct key_preparsed_payload *prep) |
7e70cb49 | 876 | { |
146aa8b1 | 877 | struct encrypted_key_payload *epayload = key->payload.data[0]; |
7e70cb49 MZ |
878 | struct encrypted_key_payload *new_epayload; |
879 | char *buf; | |
880 | char *new_master_desc = NULL; | |
4e561d38 | 881 | const char *format = NULL; |
cf7f601c | 882 | size_t datalen = prep->datalen; |
7e70cb49 MZ |
883 | int ret = 0; |
884 | ||
363b02da | 885 | if (key_is_negative(key)) |
096fe9ea | 886 | return -ENOKEY; |
cf7f601c | 887 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
7e70cb49 MZ |
888 | return -EINVAL; |
889 | ||
890 | buf = kmalloc(datalen + 1, GFP_KERNEL); | |
891 | if (!buf) | |
892 | return -ENOMEM; | |
893 | ||
894 | buf[datalen] = 0; | |
cf7f601c | 895 | memcpy(buf, prep->data, datalen); |
cd3bc044 | 896 | ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL, NULL); |
7e70cb49 MZ |
897 | if (ret < 0) |
898 | goto out; | |
899 | ||
900 | ret = valid_master_desc(new_master_desc, epayload->master_desc); | |
901 | if (ret < 0) | |
902 | goto out; | |
903 | ||
4e561d38 | 904 | new_epayload = encrypted_key_alloc(key, epayload->format, |
cd3bc044 | 905 | new_master_desc, epayload->datalen, NULL); |
7e70cb49 MZ |
906 | if (IS_ERR(new_epayload)) { |
907 | ret = PTR_ERR(new_epayload); | |
908 | goto out; | |
909 | } | |
910 | ||
4e561d38 RS |
911 | __ekey_init(new_epayload, epayload->format, new_master_desc, |
912 | epayload->datalen); | |
7e70cb49 MZ |
913 | |
914 | memcpy(new_epayload->iv, epayload->iv, ivsize); | |
4e561d38 RS |
915 | memcpy(new_epayload->payload_data, epayload->payload_data, |
916 | epayload->payload_datalen); | |
7e70cb49 | 917 | |
ee0b31a2 | 918 | rcu_assign_keypointer(key, new_epayload); |
7e70cb49 MZ |
919 | call_rcu(&epayload->rcu, encrypted_rcu_free); |
920 | out: | |
453431a5 | 921 | kfree_sensitive(buf); |
7e70cb49 MZ |
922 | return ret; |
923 | } | |
924 | ||
925 | /* | |
d3ec10aa | 926 | * encrypted_read - format and copy out the encrypted data |
7e70cb49 MZ |
927 | * |
928 | * The resulting datablob format is: | |
929 | * <master-key name> <decrypted data length> <encrypted iv> <encrypted data> | |
930 | * | |
931 | * On success, return to userspace the encrypted key datablob size. | |
932 | */ | |
d3ec10aa | 933 | static long encrypted_read(const struct key *key, char *buffer, |
7e70cb49 MZ |
934 | size_t buflen) |
935 | { | |
936 | struct encrypted_key_payload *epayload; | |
937 | struct key *mkey; | |
146aa8b1 | 938 | const u8 *master_key; |
3b1826ce | 939 | size_t master_keylen; |
7e70cb49 MZ |
940 | char derived_key[HASH_SIZE]; |
941 | char *ascii_buf; | |
942 | size_t asciiblob_len; | |
943 | int ret; | |
944 | ||
0837e49a | 945 | epayload = dereference_key_locked(key); |
7e70cb49 MZ |
946 | |
947 | /* returns the hex encoded iv, encrypted-data, and hmac as ascii */ | |
948 | asciiblob_len = epayload->datablob_len + ivsize + 1 | |
949 | + roundup(epayload->decrypted_datalen, blksize) | |
950 | + (HASH_SIZE * 2); | |
951 | ||
952 | if (!buffer || buflen < asciiblob_len) | |
953 | return asciiblob_len; | |
954 | ||
955 | mkey = request_master_key(epayload, &master_key, &master_keylen); | |
956 | if (IS_ERR(mkey)) | |
957 | return PTR_ERR(mkey); | |
958 | ||
959 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); | |
960 | if (ret < 0) | |
961 | goto out; | |
962 | ||
963 | ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key); | |
964 | if (ret < 0) | |
965 | goto out; | |
966 | ||
967 | ret = datablob_hmac_append(epayload, master_key, master_keylen); | |
968 | if (ret < 0) | |
969 | goto out; | |
970 | ||
971 | ascii_buf = datablob_format(epayload, asciiblob_len); | |
972 | if (!ascii_buf) { | |
973 | ret = -ENOMEM; | |
974 | goto out; | |
975 | } | |
976 | ||
977 | up_read(&mkey->sem); | |
978 | key_put(mkey); | |
a9dd74b2 | 979 | memzero_explicit(derived_key, sizeof(derived_key)); |
7e70cb49 | 980 | |
d3ec10aa | 981 | memcpy(buffer, ascii_buf, asciiblob_len); |
453431a5 | 982 | kfree_sensitive(ascii_buf); |
7e70cb49 MZ |
983 | |
984 | return asciiblob_len; | |
985 | out: | |
986 | up_read(&mkey->sem); | |
987 | key_put(mkey); | |
a9dd74b2 | 988 | memzero_explicit(derived_key, sizeof(derived_key)); |
7e70cb49 MZ |
989 | return ret; |
990 | } | |
991 | ||
992 | /* | |
a9dd74b2 | 993 | * encrypted_destroy - clear and free the key's payload |
7e70cb49 MZ |
994 | */ |
995 | static void encrypted_destroy(struct key *key) | |
996 | { | |
453431a5 | 997 | kfree_sensitive(key->payload.data[0]); |
7e70cb49 MZ |
998 | } |
999 | ||
1000 | struct key_type key_type_encrypted = { | |
1001 | .name = "encrypted", | |
1002 | .instantiate = encrypted_instantiate, | |
1003 | .update = encrypted_update, | |
7e70cb49 MZ |
1004 | .destroy = encrypted_destroy, |
1005 | .describe = user_describe, | |
1006 | .read = encrypted_read, | |
1007 | }; | |
1008 | EXPORT_SYMBOL_GPL(key_type_encrypted); | |
1009 | ||
64d107d3 | 1010 | static int __init init_encrypted(void) |
7e70cb49 MZ |
1011 | { |
1012 | int ret; | |
1013 | ||
3d234b33 | 1014 | hash_tfm = crypto_alloc_shash(hash_alg, 0, 0); |
64d107d3 EB |
1015 | if (IS_ERR(hash_tfm)) { |
1016 | pr_err("encrypted_key: can't allocate %s transform: %ld\n", | |
1017 | hash_alg, PTR_ERR(hash_tfm)); | |
1018 | return PTR_ERR(hash_tfm); | |
7e70cb49 MZ |
1019 | } |
1020 | ||
b26bdde5 TI |
1021 | ret = aes_get_sizes(); |
1022 | if (ret < 0) | |
1023 | goto out; | |
7e70cb49 MZ |
1024 | ret = register_key_type(&key_type_encrypted); |
1025 | if (ret < 0) | |
1026 | goto out; | |
b26bdde5 | 1027 | return 0; |
7e70cb49 | 1028 | out: |
64d107d3 | 1029 | crypto_free_shash(hash_tfm); |
7e70cb49 | 1030 | return ret; |
b9703449 | 1031 | |
7e70cb49 MZ |
1032 | } |
1033 | ||
1034 | static void __exit cleanup_encrypted(void) | |
1035 | { | |
64d107d3 | 1036 | crypto_free_shash(hash_tfm); |
7e70cb49 MZ |
1037 | unregister_key_type(&key_type_encrypted); |
1038 | } | |
1039 | ||
1040 | late_initcall(init_encrypted); | |
1041 | module_exit(cleanup_encrypted); | |
1042 | ||
84edd7ad | 1043 | MODULE_DESCRIPTION("Encrypted key type"); |
7e70cb49 | 1044 | MODULE_LICENSE("GPL"); |