Commit | Line | Data |
---|---|---|
7e70cb49 MZ |
1 | /* |
2 | * Copyright (C) 2010 IBM Corporation | |
4e561d38 RS |
3 | * Copyright (C) 2010 Politecnico di Torino, Italy |
4 | * TORSEC group -- http://security.polito.it | |
7e70cb49 | 5 | * |
4e561d38 | 6 | * Authors: |
7e70cb49 | 7 | * Mimi Zohar <zohar@us.ibm.com> |
4e561d38 | 8 | * Roberto Sassu <roberto.sassu@polito.it> |
7e70cb49 MZ |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation, version 2 of the License. | |
13 | * | |
d410fa4e | 14 | * See Documentation/security/keys-trusted-encrypted.txt |
7e70cb49 MZ |
15 | */ |
16 | ||
17 | #include <linux/uaccess.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/parser.h> | |
22 | #include <linux/string.h> | |
93ae86e7 | 23 | #include <linux/err.h> |
7e70cb49 MZ |
24 | #include <keys/user-type.h> |
25 | #include <keys/trusted-type.h> | |
26 | #include <keys/encrypted-type.h> | |
27 | #include <linux/key-type.h> | |
28 | #include <linux/random.h> | |
29 | #include <linux/rcupdate.h> | |
30 | #include <linux/scatterlist.h> | |
31 | #include <linux/crypto.h> | |
79a73d18 | 32 | #include <linux/ctype.h> |
7e70cb49 MZ |
33 | #include <crypto/hash.h> |
34 | #include <crypto/sha.h> | |
35 | #include <crypto/aes.h> | |
36 | ||
b9703449 | 37 | #include "encrypted.h" |
79a73d18 | 38 | #include "ecryptfs_format.h" |
7e70cb49 | 39 | |
3b1826ce MZ |
40 | static const char KEY_TRUSTED_PREFIX[] = "trusted:"; |
41 | static const char KEY_USER_PREFIX[] = "user:"; | |
7e70cb49 MZ |
42 | static const char hash_alg[] = "sha256"; |
43 | static const char hmac_alg[] = "hmac(sha256)"; | |
44 | static const char blkcipher_alg[] = "cbc(aes)"; | |
4e561d38 | 45 | static const char key_format_default[] = "default"; |
79a73d18 | 46 | static const char key_format_ecryptfs[] = "ecryptfs"; |
7e70cb49 MZ |
47 | static unsigned int ivsize; |
48 | static int blksize; | |
49 | ||
3b1826ce MZ |
50 | #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1) |
51 | #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1) | |
79a73d18 | 52 | #define KEY_ECRYPTFS_DESC_LEN 16 |
3b1826ce MZ |
53 | #define HASH_SIZE SHA256_DIGEST_SIZE |
54 | #define MAX_DATA_SIZE 4096 | |
55 | #define MIN_DATA_SIZE 20 | |
56 | ||
7e70cb49 MZ |
57 | struct sdesc { |
58 | struct shash_desc shash; | |
59 | char ctx[]; | |
60 | }; | |
61 | ||
62 | static struct crypto_shash *hashalg; | |
63 | static struct crypto_shash *hmacalg; | |
64 | ||
65 | enum { | |
66 | Opt_err = -1, Opt_new, Opt_load, Opt_update | |
67 | }; | |
68 | ||
4e561d38 | 69 | enum { |
79a73d18 | 70 | Opt_error = -1, Opt_default, Opt_ecryptfs |
4e561d38 RS |
71 | }; |
72 | ||
73 | static const match_table_t key_format_tokens = { | |
74 | {Opt_default, "default"}, | |
79a73d18 | 75 | {Opt_ecryptfs, "ecryptfs"}, |
4e561d38 RS |
76 | {Opt_error, NULL} |
77 | }; | |
78 | ||
7e70cb49 MZ |
79 | static const match_table_t key_tokens = { |
80 | {Opt_new, "new"}, | |
81 | {Opt_load, "load"}, | |
82 | {Opt_update, "update"}, | |
83 | {Opt_err, NULL} | |
84 | }; | |
85 | ||
86 | static int aes_get_sizes(void) | |
87 | { | |
88 | struct crypto_blkcipher *tfm; | |
89 | ||
90 | tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); | |
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 | } | |
96 | ivsize = crypto_blkcipher_ivsize(tfm); | |
97 | blksize = crypto_blkcipher_blocksize(tfm); | |
98 | crypto_free_blkcipher(tfm); | |
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 | { | |
88ed18f3 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: | |
4e561d38 RS |
166 | * new [<format>] <master-key name> <decrypted data length> |
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, | |
178 | char **hex_encoded_iv) | |
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: |
4e561d38 RS |
203 | case Opt_default: |
204 | *format = p; | |
205 | *master_desc = strsep(&datablob, " \t"); | |
206 | break; | |
207 | case Opt_error: | |
208 | *master_desc = p; | |
209 | break; | |
210 | } | |
211 | ||
7103dff0 RS |
212 | if (!*master_desc) { |
213 | pr_info("encrypted_key: master key parameter is missing\n"); | |
7e70cb49 | 214 | goto out; |
7103dff0 | 215 | } |
7e70cb49 | 216 | |
7103dff0 RS |
217 | if (valid_master_desc(*master_desc, NULL) < 0) { |
218 | pr_info("encrypted_key: master key parameter \'%s\' " | |
219 | "is invalid\n", *master_desc); | |
7e70cb49 | 220 | goto out; |
7103dff0 | 221 | } |
7e70cb49 MZ |
222 | |
223 | if (decrypted_datalen) { | |
224 | *decrypted_datalen = strsep(&datablob, " \t"); | |
7103dff0 RS |
225 | if (!*decrypted_datalen) { |
226 | pr_info("encrypted_key: keylen parameter is missing\n"); | |
7e70cb49 | 227 | goto out; |
7103dff0 | 228 | } |
7e70cb49 MZ |
229 | } |
230 | ||
231 | switch (key_cmd) { | |
232 | case Opt_new: | |
7103dff0 RS |
233 | if (!decrypted_datalen) { |
234 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
235 | "when called from .update method\n", keyword); | |
7e70cb49 | 236 | break; |
7103dff0 | 237 | } |
7e70cb49 MZ |
238 | ret = 0; |
239 | break; | |
240 | case Opt_load: | |
7103dff0 RS |
241 | if (!decrypted_datalen) { |
242 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
243 | "when called from .update method\n", keyword); | |
7e70cb49 | 244 | break; |
7103dff0 | 245 | } |
7e70cb49 | 246 | *hex_encoded_iv = strsep(&datablob, " \t"); |
7103dff0 RS |
247 | if (!*hex_encoded_iv) { |
248 | pr_info("encrypted_key: hex blob is missing\n"); | |
7e70cb49 | 249 | break; |
7103dff0 | 250 | } |
7e70cb49 MZ |
251 | ret = 0; |
252 | break; | |
253 | case Opt_update: | |
7103dff0 RS |
254 | if (decrypted_datalen) { |
255 | pr_info("encrypted_key: keyword \'%s\' not allowed " | |
256 | "when called from .instantiate method\n", | |
257 | keyword); | |
7e70cb49 | 258 | break; |
7103dff0 | 259 | } |
7e70cb49 MZ |
260 | ret = 0; |
261 | break; | |
262 | case Opt_err: | |
7103dff0 RS |
263 | pr_info("encrypted_key: keyword \'%s\' not recognized\n", |
264 | keyword); | |
7e70cb49 MZ |
265 | break; |
266 | } | |
267 | out: | |
268 | return ret; | |
269 | } | |
270 | ||
271 | /* | |
272 | * datablob_format - format as an ascii string, before copying to userspace | |
273 | */ | |
274 | static char *datablob_format(struct encrypted_key_payload *epayload, | |
275 | size_t asciiblob_len) | |
276 | { | |
277 | char *ascii_buf, *bufp; | |
278 | u8 *iv = epayload->iv; | |
279 | int len; | |
280 | int i; | |
281 | ||
282 | ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL); | |
283 | if (!ascii_buf) | |
284 | goto out; | |
285 | ||
286 | ascii_buf[asciiblob_len] = '\0'; | |
287 | ||
288 | /* copy datablob master_desc and datalen strings */ | |
4e561d38 RS |
289 | len = sprintf(ascii_buf, "%s %s %s ", epayload->format, |
290 | epayload->master_desc, epayload->datalen); | |
7e70cb49 MZ |
291 | |
292 | /* convert the hex encoded iv, encrypted-data and HMAC to ascii */ | |
293 | bufp = &ascii_buf[len]; | |
294 | for (i = 0; i < (asciiblob_len - len) / 2; i++) | |
02473119 | 295 | bufp = hex_byte_pack(bufp, iv[i]); |
7e70cb49 MZ |
296 | out: |
297 | return ascii_buf; | |
298 | } | |
299 | ||
7e70cb49 MZ |
300 | /* |
301 | * request_user_key - request the user key | |
302 | * | |
303 | * Use a user provided key to encrypt/decrypt an encrypted-key. | |
304 | */ | |
305 | static struct key *request_user_key(const char *master_desc, u8 **master_key, | |
3b1826ce | 306 | size_t *master_keylen) |
7e70cb49 MZ |
307 | { |
308 | struct user_key_payload *upayload; | |
309 | struct key *ukey; | |
310 | ||
311 | ukey = request_key(&key_type_user, master_desc, NULL); | |
312 | if (IS_ERR(ukey)) | |
313 | goto error; | |
314 | ||
315 | down_read(&ukey->sem); | |
6ac6172a | 316 | upayload = ukey->payload.data; |
adca1bfb EB |
317 | if (!upayload) { |
318 | /* key was revoked before we acquired its semaphore */ | |
319 | up_read(&ukey->sem); | |
320 | key_put(ukey); | |
321 | ukey = ERR_PTR(-EKEYREVOKED); | |
322 | goto error; | |
323 | } | |
7e70cb49 MZ |
324 | *master_key = upayload->data; |
325 | *master_keylen = upayload->datalen; | |
326 | error: | |
327 | return ukey; | |
328 | } | |
329 | ||
3b1826ce | 330 | static struct sdesc *alloc_sdesc(struct crypto_shash *alg) |
7e70cb49 MZ |
331 | { |
332 | struct sdesc *sdesc; | |
333 | int size; | |
334 | ||
335 | size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); | |
336 | sdesc = kmalloc(size, GFP_KERNEL); | |
337 | if (!sdesc) | |
338 | return ERR_PTR(-ENOMEM); | |
339 | sdesc->shash.tfm = alg; | |
340 | sdesc->shash.flags = 0x0; | |
341 | return sdesc; | |
342 | } | |
343 | ||
3b1826ce MZ |
344 | static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen, |
345 | const u8 *buf, unsigned int buflen) | |
7e70cb49 MZ |
346 | { |
347 | struct sdesc *sdesc; | |
348 | int ret; | |
349 | ||
3b1826ce | 350 | sdesc = alloc_sdesc(hmacalg); |
7e70cb49 MZ |
351 | if (IS_ERR(sdesc)) { |
352 | pr_info("encrypted_key: can't alloc %s\n", hmac_alg); | |
353 | return PTR_ERR(sdesc); | |
354 | } | |
355 | ||
356 | ret = crypto_shash_setkey(hmacalg, key, keylen); | |
357 | if (!ret) | |
358 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); | |
359 | kfree(sdesc); | |
360 | return ret; | |
361 | } | |
362 | ||
3b1826ce | 363 | static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen) |
7e70cb49 MZ |
364 | { |
365 | struct sdesc *sdesc; | |
366 | int ret; | |
367 | ||
3b1826ce | 368 | sdesc = alloc_sdesc(hashalg); |
7e70cb49 MZ |
369 | if (IS_ERR(sdesc)) { |
370 | pr_info("encrypted_key: can't alloc %s\n", hash_alg); | |
371 | return PTR_ERR(sdesc); | |
372 | } | |
373 | ||
374 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); | |
375 | kfree(sdesc); | |
376 | return ret; | |
377 | } | |
378 | ||
379 | enum derived_key_type { ENC_KEY, AUTH_KEY }; | |
380 | ||
381 | /* Derive authentication/encryption key from trusted key */ | |
382 | static int get_derived_key(u8 *derived_key, enum derived_key_type key_type, | |
3b1826ce | 383 | const u8 *master_key, size_t master_keylen) |
7e70cb49 MZ |
384 | { |
385 | u8 *derived_buf; | |
386 | unsigned int derived_buf_len; | |
387 | int ret; | |
388 | ||
389 | derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen; | |
390 | if (derived_buf_len < HASH_SIZE) | |
391 | derived_buf_len = HASH_SIZE; | |
392 | ||
393 | derived_buf = kzalloc(derived_buf_len, GFP_KERNEL); | |
394 | if (!derived_buf) { | |
395 | pr_err("encrypted_key: out of memory\n"); | |
396 | return -ENOMEM; | |
397 | } | |
398 | if (key_type) | |
399 | strcpy(derived_buf, "AUTH_KEY"); | |
400 | else | |
401 | strcpy(derived_buf, "ENC_KEY"); | |
402 | ||
403 | memcpy(derived_buf + strlen(derived_buf) + 1, master_key, | |
404 | master_keylen); | |
405 | ret = calc_hash(derived_key, derived_buf, derived_buf_len); | |
406 | kfree(derived_buf); | |
407 | return ret; | |
408 | } | |
409 | ||
410 | static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key, | |
3b1826ce MZ |
411 | unsigned int key_len, const u8 *iv, |
412 | unsigned int ivsize) | |
7e70cb49 MZ |
413 | { |
414 | int ret; | |
415 | ||
416 | desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); | |
417 | if (IS_ERR(desc->tfm)) { | |
418 | pr_err("encrypted_key: failed to load %s transform (%ld)\n", | |
419 | blkcipher_alg, PTR_ERR(desc->tfm)); | |
420 | return PTR_ERR(desc->tfm); | |
421 | } | |
422 | desc->flags = 0; | |
423 | ||
424 | ret = crypto_blkcipher_setkey(desc->tfm, key, key_len); | |
425 | if (ret < 0) { | |
426 | pr_err("encrypted_key: failed to setkey (%d)\n", ret); | |
427 | crypto_free_blkcipher(desc->tfm); | |
428 | return ret; | |
429 | } | |
430 | crypto_blkcipher_set_iv(desc->tfm, iv, ivsize); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | static struct key *request_master_key(struct encrypted_key_payload *epayload, | |
3b1826ce | 435 | u8 **master_key, size_t *master_keylen) |
7e70cb49 | 436 | { |
11b4f437 | 437 | struct key *mkey = ERR_PTR(-EINVAL); |
7e70cb49 MZ |
438 | |
439 | if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX, | |
440 | KEY_TRUSTED_PREFIX_LEN)) { | |
441 | mkey = request_trusted_key(epayload->master_desc + | |
442 | KEY_TRUSTED_PREFIX_LEN, | |
443 | master_key, master_keylen); | |
444 | } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX, | |
445 | KEY_USER_PREFIX_LEN)) { | |
446 | mkey = request_user_key(epayload->master_desc + | |
447 | KEY_USER_PREFIX_LEN, | |
448 | master_key, master_keylen); | |
449 | } else | |
450 | goto out; | |
451 | ||
f91c2c5c | 452 | if (IS_ERR(mkey)) { |
f4a0d5ab | 453 | int ret = PTR_ERR(mkey); |
982e617a MZ |
454 | |
455 | if (ret == -ENOTSUPP) | |
456 | pr_info("encrypted_key: key %s not supported", | |
457 | epayload->master_desc); | |
458 | else | |
459 | pr_info("encrypted_key: key %s not found", | |
460 | epayload->master_desc); | |
f91c2c5c RS |
461 | goto out; |
462 | } | |
463 | ||
464 | dump_master_key(*master_key, *master_keylen); | |
7e70cb49 MZ |
465 | out: |
466 | return mkey; | |
467 | } | |
468 | ||
469 | /* Before returning data to userspace, encrypt decrypted data. */ | |
470 | static int derived_key_encrypt(struct encrypted_key_payload *epayload, | |
471 | const u8 *derived_key, | |
3b1826ce | 472 | unsigned int derived_keylen) |
7e70cb49 MZ |
473 | { |
474 | struct scatterlist sg_in[2]; | |
475 | struct scatterlist sg_out[1]; | |
476 | struct blkcipher_desc desc; | |
477 | unsigned int encrypted_datalen; | |
478 | unsigned int padlen; | |
479 | char pad[16]; | |
480 | int ret; | |
481 | ||
482 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); | |
483 | padlen = encrypted_datalen - epayload->decrypted_datalen; | |
484 | ||
485 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, | |
486 | epayload->iv, ivsize); | |
487 | if (ret < 0) | |
488 | goto out; | |
489 | dump_decrypted_data(epayload); | |
490 | ||
491 | memset(pad, 0, sizeof pad); | |
492 | sg_init_table(sg_in, 2); | |
493 | sg_set_buf(&sg_in[0], epayload->decrypted_data, | |
494 | epayload->decrypted_datalen); | |
495 | sg_set_buf(&sg_in[1], pad, padlen); | |
496 | ||
497 | sg_init_table(sg_out, 1); | |
498 | sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen); | |
499 | ||
500 | ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen); | |
501 | crypto_free_blkcipher(desc.tfm); | |
502 | if (ret < 0) | |
503 | pr_err("encrypted_key: failed to encrypt (%d)\n", ret); | |
504 | else | |
505 | dump_encrypted_data(epayload, encrypted_datalen); | |
506 | out: | |
507 | return ret; | |
508 | } | |
509 | ||
510 | static int datablob_hmac_append(struct encrypted_key_payload *epayload, | |
3b1826ce | 511 | const u8 *master_key, size_t master_keylen) |
7e70cb49 MZ |
512 | { |
513 | u8 derived_key[HASH_SIZE]; | |
514 | u8 *digest; | |
515 | int ret; | |
516 | ||
517 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); | |
518 | if (ret < 0) | |
519 | goto out; | |
520 | ||
4e561d38 | 521 | digest = epayload->format + epayload->datablob_len; |
7e70cb49 | 522 | ret = calc_hmac(digest, derived_key, sizeof derived_key, |
4e561d38 | 523 | epayload->format, epayload->datablob_len); |
7e70cb49 MZ |
524 | if (!ret) |
525 | dump_hmac(NULL, digest, HASH_SIZE); | |
526 | out: | |
527 | return ret; | |
528 | } | |
529 | ||
530 | /* verify HMAC before decrypting encrypted key */ | |
531 | static int datablob_hmac_verify(struct encrypted_key_payload *epayload, | |
4e561d38 RS |
532 | const u8 *format, const u8 *master_key, |
533 | size_t master_keylen) | |
7e70cb49 MZ |
534 | { |
535 | u8 derived_key[HASH_SIZE]; | |
536 | u8 digest[HASH_SIZE]; | |
537 | int ret; | |
4e561d38 RS |
538 | char *p; |
539 | unsigned short len; | |
7e70cb49 MZ |
540 | |
541 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); | |
542 | if (ret < 0) | |
543 | goto out; | |
544 | ||
4e561d38 RS |
545 | len = epayload->datablob_len; |
546 | if (!format) { | |
547 | p = epayload->master_desc; | |
548 | len -= strlen(epayload->format) + 1; | |
549 | } else | |
550 | p = epayload->format; | |
551 | ||
552 | ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len); | |
7e70cb49 MZ |
553 | if (ret < 0) |
554 | goto out; | |
4e561d38 | 555 | ret = memcmp(digest, epayload->format + epayload->datablob_len, |
7e70cb49 MZ |
556 | sizeof digest); |
557 | if (ret) { | |
558 | ret = -EINVAL; | |
559 | dump_hmac("datablob", | |
4e561d38 | 560 | epayload->format + epayload->datablob_len, |
7e70cb49 MZ |
561 | HASH_SIZE); |
562 | dump_hmac("calc", digest, HASH_SIZE); | |
563 | } | |
564 | out: | |
565 | return ret; | |
566 | } | |
567 | ||
568 | static int derived_key_decrypt(struct encrypted_key_payload *epayload, | |
569 | const u8 *derived_key, | |
3b1826ce | 570 | unsigned int derived_keylen) |
7e70cb49 MZ |
571 | { |
572 | struct scatterlist sg_in[1]; | |
573 | struct scatterlist sg_out[2]; | |
574 | struct blkcipher_desc desc; | |
575 | unsigned int encrypted_datalen; | |
576 | char pad[16]; | |
577 | int ret; | |
578 | ||
579 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); | |
580 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, | |
581 | epayload->iv, ivsize); | |
582 | if (ret < 0) | |
583 | goto out; | |
584 | dump_encrypted_data(epayload, encrypted_datalen); | |
585 | ||
586 | memset(pad, 0, sizeof pad); | |
587 | sg_init_table(sg_in, 1); | |
588 | sg_init_table(sg_out, 2); | |
589 | sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen); | |
590 | sg_set_buf(&sg_out[0], epayload->decrypted_data, | |
3b1826ce | 591 | epayload->decrypted_datalen); |
7e70cb49 MZ |
592 | sg_set_buf(&sg_out[1], pad, sizeof pad); |
593 | ||
594 | ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen); | |
595 | crypto_free_blkcipher(desc.tfm); | |
596 | if (ret < 0) | |
597 | goto out; | |
598 | dump_decrypted_data(epayload); | |
599 | out: | |
600 | return ret; | |
601 | } | |
602 | ||
603 | /* Allocate memory for decrypted key and datablob. */ | |
604 | static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, | |
4e561d38 | 605 | const char *format, |
7e70cb49 MZ |
606 | const char *master_desc, |
607 | const char *datalen) | |
608 | { | |
609 | struct encrypted_key_payload *epayload = NULL; | |
610 | unsigned short datablob_len; | |
611 | unsigned short decrypted_datalen; | |
4e561d38 | 612 | unsigned short payload_datalen; |
7e70cb49 | 613 | unsigned int encrypted_datalen; |
4e561d38 | 614 | unsigned int format_len; |
7e70cb49 MZ |
615 | long dlen; |
616 | int ret; | |
617 | ||
618 | ret = strict_strtol(datalen, 10, &dlen); | |
619 | if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE) | |
620 | return ERR_PTR(-EINVAL); | |
621 | ||
4e561d38 | 622 | format_len = (!format) ? strlen(key_format_default) : strlen(format); |
7e70cb49 | 623 | decrypted_datalen = dlen; |
4e561d38 | 624 | payload_datalen = decrypted_datalen; |
79a73d18 RS |
625 | if (format && !strcmp(format, key_format_ecryptfs)) { |
626 | if (dlen != ECRYPTFS_MAX_KEY_BYTES) { | |
627 | pr_err("encrypted_key: keylen for the ecryptfs format " | |
628 | "must be equal to %d bytes\n", | |
629 | ECRYPTFS_MAX_KEY_BYTES); | |
630 | return ERR_PTR(-EINVAL); | |
631 | } | |
632 | decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES; | |
633 | payload_datalen = sizeof(struct ecryptfs_auth_tok); | |
634 | } | |
635 | ||
7e70cb49 MZ |
636 | encrypted_datalen = roundup(decrypted_datalen, blksize); |
637 | ||
4e561d38 RS |
638 | datablob_len = format_len + 1 + strlen(master_desc) + 1 |
639 | + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen; | |
7e70cb49 | 640 | |
4e561d38 | 641 | ret = key_payload_reserve(key, payload_datalen + datablob_len |
7e70cb49 MZ |
642 | + HASH_SIZE + 1); |
643 | if (ret < 0) | |
644 | return ERR_PTR(ret); | |
645 | ||
4e561d38 | 646 | epayload = kzalloc(sizeof(*epayload) + payload_datalen + |
7e70cb49 MZ |
647 | datablob_len + HASH_SIZE + 1, GFP_KERNEL); |
648 | if (!epayload) | |
649 | return ERR_PTR(-ENOMEM); | |
650 | ||
4e561d38 | 651 | epayload->payload_datalen = payload_datalen; |
7e70cb49 MZ |
652 | epayload->decrypted_datalen = decrypted_datalen; |
653 | epayload->datablob_len = datablob_len; | |
654 | return epayload; | |
655 | } | |
656 | ||
657 | static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, | |
4e561d38 | 658 | const char *format, const char *hex_encoded_iv) |
7e70cb49 MZ |
659 | { |
660 | struct key *mkey; | |
661 | u8 derived_key[HASH_SIZE]; | |
662 | u8 *master_key; | |
663 | u8 *hmac; | |
1f35065a | 664 | const char *hex_encoded_data; |
7e70cb49 | 665 | unsigned int encrypted_datalen; |
3b1826ce | 666 | size_t master_keylen; |
1f35065a | 667 | size_t asciilen; |
7e70cb49 MZ |
668 | int ret; |
669 | ||
670 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); | |
1f35065a MZ |
671 | asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2; |
672 | if (strlen(hex_encoded_iv) != asciilen) | |
673 | return -EINVAL; | |
674 | ||
675 | hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2; | |
2b3ff631 MZ |
676 | ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize); |
677 | if (ret < 0) | |
678 | return -EINVAL; | |
679 | ret = hex2bin(epayload->encrypted_data, hex_encoded_data, | |
680 | encrypted_datalen); | |
681 | if (ret < 0) | |
682 | return -EINVAL; | |
7e70cb49 | 683 | |
4e561d38 | 684 | hmac = epayload->format + epayload->datablob_len; |
2b3ff631 MZ |
685 | ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), |
686 | HASH_SIZE); | |
687 | if (ret < 0) | |
688 | return -EINVAL; | |
7e70cb49 MZ |
689 | |
690 | mkey = request_master_key(epayload, &master_key, &master_keylen); | |
691 | if (IS_ERR(mkey)) | |
692 | return PTR_ERR(mkey); | |
693 | ||
4e561d38 | 694 | ret = datablob_hmac_verify(epayload, format, master_key, master_keylen); |
7e70cb49 MZ |
695 | if (ret < 0) { |
696 | pr_err("encrypted_key: bad hmac (%d)\n", ret); | |
697 | goto out; | |
698 | } | |
699 | ||
700 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); | |
701 | if (ret < 0) | |
702 | goto out; | |
703 | ||
704 | ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key); | |
705 | if (ret < 0) | |
706 | pr_err("encrypted_key: failed to decrypt key (%d)\n", ret); | |
707 | out: | |
708 | up_read(&mkey->sem); | |
709 | key_put(mkey); | |
710 | return ret; | |
711 | } | |
712 | ||
713 | static void __ekey_init(struct encrypted_key_payload *epayload, | |
4e561d38 RS |
714 | const char *format, const char *master_desc, |
715 | const char *datalen) | |
7e70cb49 | 716 | { |
4e561d38 RS |
717 | unsigned int format_len; |
718 | ||
719 | format_len = (!format) ? strlen(key_format_default) : strlen(format); | |
720 | epayload->format = epayload->payload_data + epayload->payload_datalen; | |
721 | epayload->master_desc = epayload->format + format_len + 1; | |
7e70cb49 MZ |
722 | epayload->datalen = epayload->master_desc + strlen(master_desc) + 1; |
723 | epayload->iv = epayload->datalen + strlen(datalen) + 1; | |
724 | epayload->encrypted_data = epayload->iv + ivsize + 1; | |
4e561d38 | 725 | epayload->decrypted_data = epayload->payload_data; |
7e70cb49 | 726 | |
4e561d38 RS |
727 | if (!format) |
728 | memcpy(epayload->format, key_format_default, format_len); | |
79a73d18 RS |
729 | else { |
730 | if (!strcmp(format, key_format_ecryptfs)) | |
731 | epayload->decrypted_data = | |
732 | ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data); | |
733 | ||
4e561d38 | 734 | memcpy(epayload->format, format, format_len); |
79a73d18 RS |
735 | } |
736 | ||
7e70cb49 MZ |
737 | memcpy(epayload->master_desc, master_desc, strlen(master_desc)); |
738 | memcpy(epayload->datalen, datalen, strlen(datalen)); | |
739 | } | |
740 | ||
741 | /* | |
742 | * encrypted_init - initialize an encrypted key | |
743 | * | |
744 | * For a new key, use a random number for both the iv and data | |
745 | * itself. For an old key, decrypt the hex encoded data. | |
746 | */ | |
747 | static int encrypted_init(struct encrypted_key_payload *epayload, | |
79a73d18 RS |
748 | const char *key_desc, const char *format, |
749 | const char *master_desc, const char *datalen, | |
750 | const char *hex_encoded_iv) | |
7e70cb49 MZ |
751 | { |
752 | int ret = 0; | |
753 | ||
79a73d18 RS |
754 | if (format && !strcmp(format, key_format_ecryptfs)) { |
755 | ret = valid_ecryptfs_desc(key_desc); | |
756 | if (ret < 0) | |
757 | return ret; | |
758 | ||
759 | ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data, | |
760 | key_desc); | |
761 | } | |
762 | ||
4e561d38 | 763 | __ekey_init(epayload, format, master_desc, datalen); |
1f35065a | 764 | if (!hex_encoded_iv) { |
7e70cb49 MZ |
765 | get_random_bytes(epayload->iv, ivsize); |
766 | ||
767 | get_random_bytes(epayload->decrypted_data, | |
768 | epayload->decrypted_datalen); | |
769 | } else | |
4e561d38 | 770 | ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv); |
7e70cb49 MZ |
771 | return ret; |
772 | } | |
773 | ||
774 | /* | |
775 | * encrypted_instantiate - instantiate an encrypted key | |
776 | * | |
777 | * Decrypt an existing encrypted datablob or create a new encrypted key | |
778 | * based on a kernel random number. | |
779 | * | |
780 | * On success, return 0. Otherwise return errno. | |
781 | */ | |
cf7f601c DH |
782 | static int encrypted_instantiate(struct key *key, |
783 | struct key_preparsed_payload *prep) | |
7e70cb49 MZ |
784 | { |
785 | struct encrypted_key_payload *epayload = NULL; | |
786 | char *datablob = NULL; | |
4e561d38 | 787 | const char *format = NULL; |
7e70cb49 MZ |
788 | char *master_desc = NULL; |
789 | char *decrypted_datalen = NULL; | |
790 | char *hex_encoded_iv = NULL; | |
cf7f601c | 791 | size_t datalen = prep->datalen; |
7e70cb49 MZ |
792 | int ret; |
793 | ||
cf7f601c | 794 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
7e70cb49 MZ |
795 | return -EINVAL; |
796 | ||
797 | datablob = kmalloc(datalen + 1, GFP_KERNEL); | |
798 | if (!datablob) | |
799 | return -ENOMEM; | |
800 | datablob[datalen] = 0; | |
cf7f601c | 801 | memcpy(datablob, prep->data, datalen); |
4e561d38 RS |
802 | ret = datablob_parse(datablob, &format, &master_desc, |
803 | &decrypted_datalen, &hex_encoded_iv); | |
7e70cb49 MZ |
804 | if (ret < 0) |
805 | goto out; | |
806 | ||
4e561d38 RS |
807 | epayload = encrypted_key_alloc(key, format, master_desc, |
808 | decrypted_datalen); | |
7e70cb49 MZ |
809 | if (IS_ERR(epayload)) { |
810 | ret = PTR_ERR(epayload); | |
811 | goto out; | |
812 | } | |
79a73d18 RS |
813 | ret = encrypted_init(epayload, key->description, format, master_desc, |
814 | decrypted_datalen, hex_encoded_iv); | |
7e70cb49 MZ |
815 | if (ret < 0) { |
816 | kfree(epayload); | |
817 | goto out; | |
818 | } | |
819 | ||
ee0b31a2 | 820 | rcu_assign_keypointer(key, epayload); |
7e70cb49 MZ |
821 | out: |
822 | kfree(datablob); | |
823 | return ret; | |
824 | } | |
825 | ||
826 | static void encrypted_rcu_free(struct rcu_head *rcu) | |
827 | { | |
828 | struct encrypted_key_payload *epayload; | |
829 | ||
830 | epayload = container_of(rcu, struct encrypted_key_payload, rcu); | |
831 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); | |
832 | kfree(epayload); | |
833 | } | |
834 | ||
835 | /* | |
836 | * encrypted_update - update the master key description | |
837 | * | |
838 | * Change the master key description for an existing encrypted key. | |
839 | * The next read will return an encrypted datablob using the new | |
840 | * master key description. | |
841 | * | |
842 | * On success, return 0. Otherwise return errno. | |
843 | */ | |
cf7f601c | 844 | static int encrypted_update(struct key *key, struct key_preparsed_payload *prep) |
7e70cb49 MZ |
845 | { |
846 | struct encrypted_key_payload *epayload = key->payload.data; | |
847 | struct encrypted_key_payload *new_epayload; | |
848 | char *buf; | |
849 | char *new_master_desc = NULL; | |
4e561d38 | 850 | const char *format = NULL; |
cf7f601c | 851 | size_t datalen = prep->datalen; |
7e70cb49 MZ |
852 | int ret = 0; |
853 | ||
cf7f601c | 854 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
7e70cb49 MZ |
855 | return -EINVAL; |
856 | ||
857 | buf = kmalloc(datalen + 1, GFP_KERNEL); | |
858 | if (!buf) | |
859 | return -ENOMEM; | |
860 | ||
861 | buf[datalen] = 0; | |
cf7f601c | 862 | memcpy(buf, prep->data, datalen); |
4e561d38 | 863 | ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL); |
7e70cb49 MZ |
864 | if (ret < 0) |
865 | goto out; | |
866 | ||
867 | ret = valid_master_desc(new_master_desc, epayload->master_desc); | |
868 | if (ret < 0) | |
869 | goto out; | |
870 | ||
4e561d38 RS |
871 | new_epayload = encrypted_key_alloc(key, epayload->format, |
872 | new_master_desc, epayload->datalen); | |
7e70cb49 MZ |
873 | if (IS_ERR(new_epayload)) { |
874 | ret = PTR_ERR(new_epayload); | |
875 | goto out; | |
876 | } | |
877 | ||
4e561d38 RS |
878 | __ekey_init(new_epayload, epayload->format, new_master_desc, |
879 | epayload->datalen); | |
7e70cb49 MZ |
880 | |
881 | memcpy(new_epayload->iv, epayload->iv, ivsize); | |
4e561d38 RS |
882 | memcpy(new_epayload->payload_data, epayload->payload_data, |
883 | epayload->payload_datalen); | |
7e70cb49 | 884 | |
ee0b31a2 | 885 | rcu_assign_keypointer(key, new_epayload); |
7e70cb49 MZ |
886 | call_rcu(&epayload->rcu, encrypted_rcu_free); |
887 | out: | |
888 | kfree(buf); | |
889 | return ret; | |
890 | } | |
891 | ||
892 | /* | |
893 | * encrypted_read - format and copy the encrypted data to userspace | |
894 | * | |
895 | * The resulting datablob format is: | |
896 | * <master-key name> <decrypted data length> <encrypted iv> <encrypted data> | |
897 | * | |
898 | * On success, return to userspace the encrypted key datablob size. | |
899 | */ | |
900 | static long encrypted_read(const struct key *key, char __user *buffer, | |
901 | size_t buflen) | |
902 | { | |
903 | struct encrypted_key_payload *epayload; | |
904 | struct key *mkey; | |
905 | u8 *master_key; | |
3b1826ce | 906 | size_t master_keylen; |
7e70cb49 MZ |
907 | char derived_key[HASH_SIZE]; |
908 | char *ascii_buf; | |
909 | size_t asciiblob_len; | |
910 | int ret; | |
911 | ||
633e804e | 912 | epayload = rcu_dereference_key(key); |
7e70cb49 MZ |
913 | |
914 | /* returns the hex encoded iv, encrypted-data, and hmac as ascii */ | |
915 | asciiblob_len = epayload->datablob_len + ivsize + 1 | |
916 | + roundup(epayload->decrypted_datalen, blksize) | |
917 | + (HASH_SIZE * 2); | |
918 | ||
919 | if (!buffer || buflen < asciiblob_len) | |
920 | return asciiblob_len; | |
921 | ||
922 | mkey = request_master_key(epayload, &master_key, &master_keylen); | |
923 | if (IS_ERR(mkey)) | |
924 | return PTR_ERR(mkey); | |
925 | ||
926 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); | |
927 | if (ret < 0) | |
928 | goto out; | |
929 | ||
930 | ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key); | |
931 | if (ret < 0) | |
932 | goto out; | |
933 | ||
934 | ret = datablob_hmac_append(epayload, master_key, master_keylen); | |
935 | if (ret < 0) | |
936 | goto out; | |
937 | ||
938 | ascii_buf = datablob_format(epayload, asciiblob_len); | |
939 | if (!ascii_buf) { | |
940 | ret = -ENOMEM; | |
941 | goto out; | |
942 | } | |
943 | ||
944 | up_read(&mkey->sem); | |
945 | key_put(mkey); | |
946 | ||
947 | if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0) | |
948 | ret = -EFAULT; | |
949 | kfree(ascii_buf); | |
950 | ||
951 | return asciiblob_len; | |
952 | out: | |
953 | up_read(&mkey->sem); | |
954 | key_put(mkey); | |
955 | return ret; | |
956 | } | |
957 | ||
958 | /* | |
959 | * encrypted_destroy - before freeing the key, clear the decrypted data | |
960 | * | |
961 | * Before freeing the key, clear the memory containing the decrypted | |
962 | * key data. | |
963 | */ | |
964 | static void encrypted_destroy(struct key *key) | |
965 | { | |
966 | struct encrypted_key_payload *epayload = key->payload.data; | |
967 | ||
968 | if (!epayload) | |
969 | return; | |
970 | ||
971 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); | |
972 | kfree(key->payload.data); | |
973 | } | |
974 | ||
975 | struct key_type key_type_encrypted = { | |
976 | .name = "encrypted", | |
977 | .instantiate = encrypted_instantiate, | |
978 | .update = encrypted_update, | |
979 | .match = user_match, | |
980 | .destroy = encrypted_destroy, | |
981 | .describe = user_describe, | |
982 | .read = encrypted_read, | |
983 | }; | |
984 | EXPORT_SYMBOL_GPL(key_type_encrypted); | |
985 | ||
986 | static void encrypted_shash_release(void) | |
987 | { | |
988 | if (hashalg) | |
989 | crypto_free_shash(hashalg); | |
990 | if (hmacalg) | |
991 | crypto_free_shash(hmacalg); | |
992 | } | |
993 | ||
994 | static int __init encrypted_shash_alloc(void) | |
995 | { | |
996 | int ret; | |
997 | ||
998 | hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC); | |
999 | if (IS_ERR(hmacalg)) { | |
1000 | pr_info("encrypted_key: could not allocate crypto %s\n", | |
1001 | hmac_alg); | |
1002 | return PTR_ERR(hmacalg); | |
1003 | } | |
1004 | ||
1005 | hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC); | |
1006 | if (IS_ERR(hashalg)) { | |
1007 | pr_info("encrypted_key: could not allocate crypto %s\n", | |
1008 | hash_alg); | |
1009 | ret = PTR_ERR(hashalg); | |
1010 | goto hashalg_fail; | |
1011 | } | |
1012 | ||
1013 | return 0; | |
1014 | ||
1015 | hashalg_fail: | |
1016 | crypto_free_shash(hmacalg); | |
1017 | return ret; | |
1018 | } | |
1019 | ||
1020 | static int __init init_encrypted(void) | |
1021 | { | |
1022 | int ret; | |
1023 | ||
1024 | ret = encrypted_shash_alloc(); | |
1025 | if (ret < 0) | |
1026 | return ret; | |
ebadec83 | 1027 | ret = aes_get_sizes(); |
1028 | if (ret < 0) | |
1029 | goto out; | |
7e70cb49 MZ |
1030 | ret = register_key_type(&key_type_encrypted); |
1031 | if (ret < 0) | |
1032 | goto out; | |
ebadec83 | 1033 | return 0; |
7e70cb49 MZ |
1034 | out: |
1035 | encrypted_shash_release(); | |
1036 | return ret; | |
b9703449 | 1037 | |
7e70cb49 MZ |
1038 | } |
1039 | ||
1040 | static void __exit cleanup_encrypted(void) | |
1041 | { | |
1042 | encrypted_shash_release(); | |
1043 | unregister_key_type(&key_type_encrypted); | |
1044 | } | |
1045 | ||
1046 | late_initcall(init_encrypted); | |
1047 | module_exit(cleanup_encrypted); | |
1048 | ||
1049 | MODULE_LICENSE("GPL"); |