blob: ead2fd60244d1ea49f19a4b1f82eb7a2d38bd8dc [file] [log] [blame]
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001/*
2 * Copyright (C) 2010 IBM Corporation
Roberto Sassu4e561d32011-06-27 13:45:42 +02003 * Copyright (C) 2010 Politecnico di Torino, Italy
4 * TORSEC group -- http://security.polito.it
Mimi Zohar7e70cb42010-11-23 18:55:35 -05005 *
Roberto Sassu4e561d32011-06-27 13:45:42 +02006 * Authors:
Mimi Zohar7e70cb42010-11-23 18:55:35 -05007 * Mimi Zohar <zohar@us.ibm.com>
Roberto Sassu4e561d32011-06-27 13:45:42 +02008 * Roberto Sassu <roberto.sassu@polito.it>
Mimi Zohar7e70cb42010-11-23 18:55:35 -05009 *
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 *
Randy Dunlapd410fa42011-05-19 15:59:38 -070014 * See Documentation/security/keys-trusted-encrypted.txt
Mimi Zohar7e70cb42010-11-23 18:55:35 -050015 */
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>
Mimi Zohar93ae86e2010-11-29 16:20:04 -050023#include <linux/err.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050024#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>
Roberto Sassu79a73d12011-06-27 13:45:44 +020031#include <linux/ctype.h>
Herbert Xu456bee92016-09-20 20:35:55 +080032#include <crypto/aes.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050033#include <crypto/hash.h>
34#include <crypto/sha.h>
Herbert Xuc3917fd2016-01-24 21:18:19 +080035#include <crypto/skcipher.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050036
Mimi Zoharb9703442011-01-18 09:07:12 -050037#include "encrypted.h"
Roberto Sassu79a73d12011-06-27 13:45:44 +020038#include "ecryptfs_format.h"
Mimi Zohar7e70cb42010-11-23 18:55:35 -050039
Mimi Zohar3b1826c2010-12-13 16:53:13 -050040static const char KEY_TRUSTED_PREFIX[] = "trusted:";
41static const char KEY_USER_PREFIX[] = "user:";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050042static const char hash_alg[] = "sha256";
43static const char hmac_alg[] = "hmac(sha256)";
44static const char blkcipher_alg[] = "cbc(aes)";
Roberto Sassu4e561d32011-06-27 13:45:42 +020045static const char key_format_default[] = "default";
Roberto Sassu79a73d12011-06-27 13:45:44 +020046static const char key_format_ecryptfs[] = "ecryptfs";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050047static unsigned int ivsize;
48static int blksize;
49
Mimi Zohar3b1826c2010-12-13 16:53:13 -050050#define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
51#define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
Roberto Sassu79a73d12011-06-27 13:45:44 +020052#define KEY_ECRYPTFS_DESC_LEN 16
Mimi Zohar3b1826c2010-12-13 16:53:13 -050053#define HASH_SIZE SHA256_DIGEST_SIZE
54#define MAX_DATA_SIZE 4096
55#define MIN_DATA_SIZE 20
56
Mimi Zohar7e70cb42010-11-23 18:55:35 -050057struct sdesc {
58 struct shash_desc shash;
59 char ctx[];
60};
61
62static struct crypto_shash *hashalg;
63static struct crypto_shash *hmacalg;
64
65enum {
66 Opt_err = -1, Opt_new, Opt_load, Opt_update
67};
68
Roberto Sassu4e561d32011-06-27 13:45:42 +020069enum {
Roberto Sassu79a73d12011-06-27 13:45:44 +020070 Opt_error = -1, Opt_default, Opt_ecryptfs
Roberto Sassu4e561d32011-06-27 13:45:42 +020071};
72
73static const match_table_t key_format_tokens = {
74 {Opt_default, "default"},
Roberto Sassu79a73d12011-06-27 13:45:44 +020075 {Opt_ecryptfs, "ecryptfs"},
Roberto Sassu4e561d32011-06-27 13:45:42 +020076 {Opt_error, NULL}
77};
78
Mimi Zohar7e70cb42010-11-23 18:55:35 -050079static const match_table_t key_tokens = {
80 {Opt_new, "new"},
81 {Opt_load, "load"},
82 {Opt_update, "update"},
83 {Opt_err, NULL}
84};
85
86static int aes_get_sizes(void)
87{
Herbert Xuc3917fd2016-01-24 21:18:19 +080088 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -050089
Herbert Xuc3917fd2016-01-24 21:18:19 +080090 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050091 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 }
Herbert Xuc3917fd2016-01-24 21:18:19 +080096 ivsize = crypto_skcipher_ivsize(tfm);
97 blksize = crypto_skcipher_blocksize(tfm);
98 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050099 return 0;
100}
101
102/*
Roberto Sassu79a73d12011-06-27 13:45:44 +0200103 * 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 */
109static 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
130/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500131 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
132 *
Roberto Sassu08fa2aa2011-06-27 13:45:40 +0200133 * key-type:= "trusted:" | "user:"
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500134 * 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 */
142static int valid_master_desc(const char *new_desc, const char *orig_desc)
143{
Eric Biggers96926022017-06-08 14:48:18 +0100144 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
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500159 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500160}
161
162/*
163 * datablob_parse - parse the keyctl data
164 *
165 * datablob format:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200166 * new [<format>] <master-key name> <decrypted data length>
167 * load [<format>] <master-key name> <decrypted data length>
168 * <encrypted iv + data>
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500169 * 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 */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200176static int datablob_parse(char *datablob, const char **format,
177 char **master_desc, char **decrypted_datalen,
178 char **hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500179{
180 substring_t args[MAX_OPT_ARGS];
181 int ret = -EINVAL;
182 int key_cmd;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200183 int key_format;
184 char *p, *keyword;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500185
Roberto Sassu7103dff2011-06-27 13:45:41 +0200186 keyword = strsep(&datablob, " \t");
187 if (!keyword) {
188 pr_info("encrypted_key: insufficient parameters specified\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500189 return ret;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200190 }
191 key_cmd = match_token(keyword, key_tokens, args);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500192
Roberto Sassu79a73d12011-06-27 13:45:44 +0200193 /* Get optional format: default | ecryptfs */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200194 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) {
Roberto Sassu79a73d12011-06-27 13:45:44 +0200202 case Opt_ecryptfs:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200203 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
Roberto Sassu7103dff2011-06-27 13:45:41 +0200212 if (!*master_desc) {
213 pr_info("encrypted_key: master key parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500214 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200215 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500216
Roberto Sassu7103dff2011-06-27 13:45:41 +0200217 if (valid_master_desc(*master_desc, NULL) < 0) {
218 pr_info("encrypted_key: master key parameter \'%s\' "
219 "is invalid\n", *master_desc);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500220 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200221 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500222
223 if (decrypted_datalen) {
224 *decrypted_datalen = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200225 if (!*decrypted_datalen) {
226 pr_info("encrypted_key: keylen parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500227 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200228 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500229 }
230
231 switch (key_cmd) {
232 case Opt_new:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200233 if (!decrypted_datalen) {
234 pr_info("encrypted_key: keyword \'%s\' not allowed "
235 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500236 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200237 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500238 ret = 0;
239 break;
240 case Opt_load:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200241 if (!decrypted_datalen) {
242 pr_info("encrypted_key: keyword \'%s\' not allowed "
243 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500244 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200245 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500246 *hex_encoded_iv = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200247 if (!*hex_encoded_iv) {
248 pr_info("encrypted_key: hex blob is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500249 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200250 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500251 ret = 0;
252 break;
253 case Opt_update:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200254 if (decrypted_datalen) {
255 pr_info("encrypted_key: keyword \'%s\' not allowed "
256 "when called from .instantiate method\n",
257 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500258 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200259 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500260 ret = 0;
261 break;
262 case Opt_err:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200263 pr_info("encrypted_key: keyword \'%s\' not recognized\n",
264 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500265 break;
266 }
267out:
268 return ret;
269}
270
271/*
272 * datablob_format - format as an ascii string, before copying to userspace
273 */
274static 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 */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200289 len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
290 epayload->master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500291
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++)
Andy Shevchenko02473112011-10-31 17:12:55 -0700295 bufp = hex_byte_pack(bufp, iv[i]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500296out:
297 return ascii_buf;
298}
299
300/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500301 * request_user_key - request the user key
302 *
303 * Use a user provided key to encrypt/decrypt an encrypted-key.
304 */
David Howells146aa8b2015-10-21 14:04:48 +0100305static struct key *request_user_key(const char *master_desc, const u8 **master_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500306 size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500307{
David Howells146aa8b2015-10-21 14:04:48 +0100308 const struct user_key_payload *upayload;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500309 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);
David Howells146aa8b2015-10-21 14:04:48 +0100316 upayload = user_key_payload(ukey);
Eric Biggersfec442e2017-10-09 12:37:49 -0700317 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 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500324 *master_key = upayload->data;
325 *master_keylen = upayload->datalen;
326error:
327 return ukey;
328}
329
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500330static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500331{
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
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500344static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
345 const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500346{
347 struct sdesc *sdesc;
348 int ret;
349
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500350 sdesc = alloc_sdesc(hmacalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500351 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
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500363static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500364{
365 struct sdesc *sdesc;
366 int ret;
367
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500368 sdesc = alloc_sdesc(hashalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500369 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
379enum derived_key_type { ENC_KEY, AUTH_KEY };
380
381/* Derive authentication/encryption key from trusted key */
382static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500383 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500384{
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
Herbert Xuc3917fd2016-01-24 21:18:19 +0800410static struct skcipher_request *init_skcipher_req(const u8 *key,
411 unsigned int key_len)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500412{
Herbert Xuc3917fd2016-01-24 21:18:19 +0800413 struct skcipher_request *req;
414 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500415 int ret;
416
Herbert Xuc3917fd2016-01-24 21:18:19 +0800417 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
418 if (IS_ERR(tfm)) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500419 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
Herbert Xuc3917fd2016-01-24 21:18:19 +0800420 blkcipher_alg, PTR_ERR(tfm));
421 return ERR_CAST(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500422 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500423
Herbert Xuc3917fd2016-01-24 21:18:19 +0800424 ret = crypto_skcipher_setkey(tfm, key, key_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500425 if (ret < 0) {
426 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800427 crypto_free_skcipher(tfm);
428 return ERR_PTR(ret);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500429 }
Herbert Xuc3917fd2016-01-24 21:18:19 +0800430
431 req = skcipher_request_alloc(tfm, GFP_KERNEL);
432 if (!req) {
433 pr_err("encrypted_key: failed to allocate request for %s\n",
434 blkcipher_alg);
435 crypto_free_skcipher(tfm);
436 return ERR_PTR(-ENOMEM);
437 }
438
439 skcipher_request_set_callback(req, 0, NULL, NULL);
440 return req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500441}
442
443static struct key *request_master_key(struct encrypted_key_payload *epayload,
David Howells146aa8b2015-10-21 14:04:48 +0100444 const u8 **master_key, size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500445{
Dan Carpenter73a0a682017-02-09 17:17:52 +0000446 struct key *mkey = ERR_PTR(-EINVAL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500447
448 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
449 KEY_TRUSTED_PREFIX_LEN)) {
450 mkey = request_trusted_key(epayload->master_desc +
451 KEY_TRUSTED_PREFIX_LEN,
452 master_key, master_keylen);
453 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
454 KEY_USER_PREFIX_LEN)) {
455 mkey = request_user_key(epayload->master_desc +
456 KEY_USER_PREFIX_LEN,
457 master_key, master_keylen);
458 } else
459 goto out;
460
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200461 if (IS_ERR(mkey)) {
Mimi Zoharf4a0d5a2011-10-24 08:17:42 -0400462 int ret = PTR_ERR(mkey);
Mimi Zohar982e6172011-08-27 22:21:26 -0400463
464 if (ret == -ENOTSUPP)
465 pr_info("encrypted_key: key %s not supported",
466 epayload->master_desc);
467 else
468 pr_info("encrypted_key: key %s not found",
469 epayload->master_desc);
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200470 goto out;
471 }
472
473 dump_master_key(*master_key, *master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500474out:
475 return mkey;
476}
477
478/* Before returning data to userspace, encrypt decrypted data. */
479static int derived_key_encrypt(struct encrypted_key_payload *epayload,
480 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500481 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500482{
483 struct scatterlist sg_in[2];
484 struct scatterlist sg_out[1];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800485 struct crypto_skcipher *tfm;
486 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500487 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800488 u8 iv[AES_BLOCK_SIZE];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500489 int ret;
490
491 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500492
Herbert Xuc3917fd2016-01-24 21:18:19 +0800493 req = init_skcipher_req(derived_key, derived_keylen);
494 ret = PTR_ERR(req);
495 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500496 goto out;
497 dump_decrypted_data(epayload);
498
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500499 sg_init_table(sg_in, 2);
500 sg_set_buf(&sg_in[0], epayload->decrypted_data,
501 epayload->decrypted_datalen);
Eric Biggersd24c1c12017-06-08 14:48:10 +0100502 sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500503
504 sg_init_table(sg_out, 1);
505 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
506
Herbert Xu456bee92016-09-20 20:35:55 +0800507 memcpy(iv, epayload->iv, sizeof(iv));
508 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800509 ret = crypto_skcipher_encrypt(req);
510 tfm = crypto_skcipher_reqtfm(req);
511 skcipher_request_free(req);
512 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500513 if (ret < 0)
514 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
515 else
516 dump_encrypted_data(epayload, encrypted_datalen);
517out:
518 return ret;
519}
520
521static int datablob_hmac_append(struct encrypted_key_payload *epayload,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500522 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500523{
524 u8 derived_key[HASH_SIZE];
525 u8 *digest;
526 int ret;
527
528 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
529 if (ret < 0)
530 goto out;
531
Roberto Sassu4e561d32011-06-27 13:45:42 +0200532 digest = epayload->format + epayload->datablob_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500533 ret = calc_hmac(digest, derived_key, sizeof derived_key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200534 epayload->format, epayload->datablob_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500535 if (!ret)
536 dump_hmac(NULL, digest, HASH_SIZE);
537out:
538 return ret;
539}
540
541/* verify HMAC before decrypting encrypted key */
542static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200543 const u8 *format, const u8 *master_key,
544 size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500545{
546 u8 derived_key[HASH_SIZE];
547 u8 digest[HASH_SIZE];
548 int ret;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200549 char *p;
550 unsigned short len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500551
552 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
553 if (ret < 0)
554 goto out;
555
Roberto Sassu4e561d32011-06-27 13:45:42 +0200556 len = epayload->datablob_len;
557 if (!format) {
558 p = epayload->master_desc;
559 len -= strlen(epayload->format) + 1;
560 } else
561 p = epayload->format;
562
563 ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500564 if (ret < 0)
565 goto out;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200566 ret = memcmp(digest, epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500567 sizeof digest);
568 if (ret) {
569 ret = -EINVAL;
570 dump_hmac("datablob",
Roberto Sassu4e561d32011-06-27 13:45:42 +0200571 epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500572 HASH_SIZE);
573 dump_hmac("calc", digest, HASH_SIZE);
574 }
575out:
576 return ret;
577}
578
579static int derived_key_decrypt(struct encrypted_key_payload *epayload,
580 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500581 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500582{
583 struct scatterlist sg_in[1];
584 struct scatterlist sg_out[2];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800585 struct crypto_skcipher *tfm;
586 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500587 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800588 u8 iv[AES_BLOCK_SIZE];
Eric Biggersd24c1c12017-06-08 14:48:10 +0100589 u8 *pad;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500590 int ret;
591
Eric Biggersd24c1c12017-06-08 14:48:10 +0100592 /* Throwaway buffer to hold the unused zero padding at the end */
593 pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
594 if (!pad)
595 return -ENOMEM;
596
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500597 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800598 req = init_skcipher_req(derived_key, derived_keylen);
599 ret = PTR_ERR(req);
600 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500601 goto out;
602 dump_encrypted_data(epayload, encrypted_datalen);
603
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500604 sg_init_table(sg_in, 1);
605 sg_init_table(sg_out, 2);
606 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
607 sg_set_buf(&sg_out[0], epayload->decrypted_data,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500608 epayload->decrypted_datalen);
Eric Biggersd24c1c12017-06-08 14:48:10 +0100609 sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500610
Herbert Xu456bee92016-09-20 20:35:55 +0800611 memcpy(iv, epayload->iv, sizeof(iv));
612 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800613 ret = crypto_skcipher_decrypt(req);
614 tfm = crypto_skcipher_reqtfm(req);
615 skcipher_request_free(req);
616 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500617 if (ret < 0)
618 goto out;
619 dump_decrypted_data(epayload);
620out:
Eric Biggersd24c1c12017-06-08 14:48:10 +0100621 kfree(pad);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500622 return ret;
623}
624
625/* Allocate memory for decrypted key and datablob. */
626static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200627 const char *format,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500628 const char *master_desc,
629 const char *datalen)
630{
631 struct encrypted_key_payload *epayload = NULL;
632 unsigned short datablob_len;
633 unsigned short decrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200634 unsigned short payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500635 unsigned int encrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200636 unsigned int format_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500637 long dlen;
638 int ret;
639
Jingoo Han29707b22014-02-05 15:13:14 +0900640 ret = kstrtol(datalen, 10, &dlen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500641 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
642 return ERR_PTR(-EINVAL);
643
Roberto Sassu4e561d32011-06-27 13:45:42 +0200644 format_len = (!format) ? strlen(key_format_default) : strlen(format);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500645 decrypted_datalen = dlen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200646 payload_datalen = decrypted_datalen;
Roberto Sassu79a73d12011-06-27 13:45:44 +0200647 if (format && !strcmp(format, key_format_ecryptfs)) {
648 if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
649 pr_err("encrypted_key: keylen for the ecryptfs format "
650 "must be equal to %d bytes\n",
651 ECRYPTFS_MAX_KEY_BYTES);
652 return ERR_PTR(-EINVAL);
653 }
654 decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
655 payload_datalen = sizeof(struct ecryptfs_auth_tok);
656 }
657
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500658 encrypted_datalen = roundup(decrypted_datalen, blksize);
659
Roberto Sassu4e561d32011-06-27 13:45:42 +0200660 datablob_len = format_len + 1 + strlen(master_desc) + 1
661 + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500662
Roberto Sassu4e561d32011-06-27 13:45:42 +0200663 ret = key_payload_reserve(key, payload_datalen + datablob_len
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500664 + HASH_SIZE + 1);
665 if (ret < 0)
666 return ERR_PTR(ret);
667
Roberto Sassu4e561d32011-06-27 13:45:42 +0200668 epayload = kzalloc(sizeof(*epayload) + payload_datalen +
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500669 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
670 if (!epayload)
671 return ERR_PTR(-ENOMEM);
672
Roberto Sassu4e561d32011-06-27 13:45:42 +0200673 epayload->payload_datalen = payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500674 epayload->decrypted_datalen = decrypted_datalen;
675 epayload->datablob_len = datablob_len;
676 return epayload;
677}
678
679static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200680 const char *format, const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500681{
682 struct key *mkey;
683 u8 derived_key[HASH_SIZE];
David Howells146aa8b2015-10-21 14:04:48 +0100684 const u8 *master_key;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500685 u8 *hmac;
Mimi Zohar1f350652010-12-13 16:53:12 -0500686 const char *hex_encoded_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500687 unsigned int encrypted_datalen;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500688 size_t master_keylen;
Mimi Zohar1f350652010-12-13 16:53:12 -0500689 size_t asciilen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500690 int ret;
691
692 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar1f350652010-12-13 16:53:12 -0500693 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
694 if (strlen(hex_encoded_iv) != asciilen)
695 return -EINVAL;
696
697 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400698 ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
699 if (ret < 0)
700 return -EINVAL;
701 ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
702 encrypted_datalen);
703 if (ret < 0)
704 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500705
Roberto Sassu4e561d32011-06-27 13:45:42 +0200706 hmac = epayload->format + epayload->datablob_len;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400707 ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
708 HASH_SIZE);
709 if (ret < 0)
710 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500711
712 mkey = request_master_key(epayload, &master_key, &master_keylen);
713 if (IS_ERR(mkey))
714 return PTR_ERR(mkey);
715
Roberto Sassu4e561d32011-06-27 13:45:42 +0200716 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500717 if (ret < 0) {
718 pr_err("encrypted_key: bad hmac (%d)\n", ret);
719 goto out;
720 }
721
722 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
723 if (ret < 0)
724 goto out;
725
726 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
727 if (ret < 0)
728 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
729out:
730 up_read(&mkey->sem);
731 key_put(mkey);
732 return ret;
733}
734
735static void __ekey_init(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200736 const char *format, const char *master_desc,
737 const char *datalen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500738{
Roberto Sassu4e561d32011-06-27 13:45:42 +0200739 unsigned int format_len;
740
741 format_len = (!format) ? strlen(key_format_default) : strlen(format);
742 epayload->format = epayload->payload_data + epayload->payload_datalen;
743 epayload->master_desc = epayload->format + format_len + 1;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500744 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
745 epayload->iv = epayload->datalen + strlen(datalen) + 1;
746 epayload->encrypted_data = epayload->iv + ivsize + 1;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200747 epayload->decrypted_data = epayload->payload_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500748
Roberto Sassu4e561d32011-06-27 13:45:42 +0200749 if (!format)
750 memcpy(epayload->format, key_format_default, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200751 else {
752 if (!strcmp(format, key_format_ecryptfs))
753 epayload->decrypted_data =
754 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
755
Roberto Sassu4e561d32011-06-27 13:45:42 +0200756 memcpy(epayload->format, format, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200757 }
758
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500759 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
760 memcpy(epayload->datalen, datalen, strlen(datalen));
761}
762
763/*
764 * encrypted_init - initialize an encrypted key
765 *
766 * For a new key, use a random number for both the iv and data
767 * itself. For an old key, decrypt the hex encoded data.
768 */
769static int encrypted_init(struct encrypted_key_payload *epayload,
Roberto Sassu79a73d12011-06-27 13:45:44 +0200770 const char *key_desc, const char *format,
771 const char *master_desc, const char *datalen,
772 const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500773{
774 int ret = 0;
775
Roberto Sassu79a73d12011-06-27 13:45:44 +0200776 if (format && !strcmp(format, key_format_ecryptfs)) {
777 ret = valid_ecryptfs_desc(key_desc);
778 if (ret < 0)
779 return ret;
780
781 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
782 key_desc);
783 }
784
Roberto Sassu4e561d32011-06-27 13:45:42 +0200785 __ekey_init(epayload, format, master_desc, datalen);
Mimi Zohar1f350652010-12-13 16:53:12 -0500786 if (!hex_encoded_iv) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500787 get_random_bytes(epayload->iv, ivsize);
788
789 get_random_bytes(epayload->decrypted_data,
790 epayload->decrypted_datalen);
791 } else
Roberto Sassu4e561d32011-06-27 13:45:42 +0200792 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500793 return ret;
794}
795
796/*
797 * encrypted_instantiate - instantiate an encrypted key
798 *
799 * Decrypt an existing encrypted datablob or create a new encrypted key
800 * based on a kernel random number.
801 *
802 * On success, return 0. Otherwise return errno.
803 */
David Howellscf7f6012012-09-13 13:06:29 +0100804static int encrypted_instantiate(struct key *key,
805 struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500806{
807 struct encrypted_key_payload *epayload = NULL;
808 char *datablob = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200809 const char *format = NULL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500810 char *master_desc = NULL;
811 char *decrypted_datalen = NULL;
812 char *hex_encoded_iv = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100813 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500814 int ret;
815
David Howellscf7f6012012-09-13 13:06:29 +0100816 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500817 return -EINVAL;
818
819 datablob = kmalloc(datalen + 1, GFP_KERNEL);
820 if (!datablob)
821 return -ENOMEM;
822 datablob[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100823 memcpy(datablob, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200824 ret = datablob_parse(datablob, &format, &master_desc,
825 &decrypted_datalen, &hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500826 if (ret < 0)
827 goto out;
828
Roberto Sassu4e561d32011-06-27 13:45:42 +0200829 epayload = encrypted_key_alloc(key, format, master_desc,
830 decrypted_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500831 if (IS_ERR(epayload)) {
832 ret = PTR_ERR(epayload);
833 goto out;
834 }
Roberto Sassu79a73d12011-06-27 13:45:44 +0200835 ret = encrypted_init(epayload, key->description, format, master_desc,
836 decrypted_datalen, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500837 if (ret < 0) {
838 kfree(epayload);
839 goto out;
840 }
841
Mimi Zoharb64cc5f2014-07-26 23:21:02 -0400842 rcu_assign_keypointer(key, epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500843out:
844 kfree(datablob);
845 return ret;
846}
847
848static void encrypted_rcu_free(struct rcu_head *rcu)
849{
850 struct encrypted_key_payload *epayload;
851
852 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
853 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
854 kfree(epayload);
855}
856
857/*
858 * encrypted_update - update the master key description
859 *
860 * Change the master key description for an existing encrypted key.
861 * The next read will return an encrypted datablob using the new
862 * master key description.
863 *
864 * On success, return 0. Otherwise return errno.
865 */
David Howellscf7f6012012-09-13 13:06:29 +0100866static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500867{
David Howells146aa8b2015-10-21 14:04:48 +0100868 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500869 struct encrypted_key_payload *new_epayload;
870 char *buf;
871 char *new_master_desc = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200872 const char *format = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100873 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500874 int ret = 0;
875
David Howells63c8e452017-10-04 16:43:25 +0100876 if (key_is_negative(key))
David Howells096fe9e2015-11-24 21:36:31 +0000877 return -ENOKEY;
David Howellscf7f6012012-09-13 13:06:29 +0100878 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500879 return -EINVAL;
880
881 buf = kmalloc(datalen + 1, GFP_KERNEL);
882 if (!buf)
883 return -ENOMEM;
884
885 buf[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100886 memcpy(buf, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200887 ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500888 if (ret < 0)
889 goto out;
890
891 ret = valid_master_desc(new_master_desc, epayload->master_desc);
892 if (ret < 0)
893 goto out;
894
Roberto Sassu4e561d32011-06-27 13:45:42 +0200895 new_epayload = encrypted_key_alloc(key, epayload->format,
896 new_master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500897 if (IS_ERR(new_epayload)) {
898 ret = PTR_ERR(new_epayload);
899 goto out;
900 }
901
Roberto Sassu4e561d32011-06-27 13:45:42 +0200902 __ekey_init(new_epayload, epayload->format, new_master_desc,
903 epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500904
905 memcpy(new_epayload->iv, epayload->iv, ivsize);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200906 memcpy(new_epayload->payload_data, epayload->payload_data,
907 epayload->payload_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500908
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000909 rcu_assign_keypointer(key, new_epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500910 call_rcu(&epayload->rcu, encrypted_rcu_free);
911out:
912 kfree(buf);
913 return ret;
914}
915
916/*
917 * encrypted_read - format and copy the encrypted data to userspace
918 *
919 * The resulting datablob format is:
920 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
921 *
922 * On success, return to userspace the encrypted key datablob size.
923 */
924static long encrypted_read(const struct key *key, char __user *buffer,
925 size_t buflen)
926{
927 struct encrypted_key_payload *epayload;
928 struct key *mkey;
David Howells146aa8b2015-10-21 14:04:48 +0100929 const u8 *master_key;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500930 size_t master_keylen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500931 char derived_key[HASH_SIZE];
932 char *ascii_buf;
933 size_t asciiblob_len;
934 int ret;
935
David Howells633e8042011-03-07 15:05:51 +0000936 epayload = rcu_dereference_key(key);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500937
938 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
939 asciiblob_len = epayload->datablob_len + ivsize + 1
940 + roundup(epayload->decrypted_datalen, blksize)
941 + (HASH_SIZE * 2);
942
943 if (!buffer || buflen < asciiblob_len)
944 return asciiblob_len;
945
946 mkey = request_master_key(epayload, &master_key, &master_keylen);
947 if (IS_ERR(mkey))
948 return PTR_ERR(mkey);
949
950 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
951 if (ret < 0)
952 goto out;
953
954 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
955 if (ret < 0)
956 goto out;
957
958 ret = datablob_hmac_append(epayload, master_key, master_keylen);
959 if (ret < 0)
960 goto out;
961
962 ascii_buf = datablob_format(epayload, asciiblob_len);
963 if (!ascii_buf) {
964 ret = -ENOMEM;
965 goto out;
966 }
967
968 up_read(&mkey->sem);
969 key_put(mkey);
970
971 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
972 ret = -EFAULT;
973 kfree(ascii_buf);
974
975 return asciiblob_len;
976out:
977 up_read(&mkey->sem);
978 key_put(mkey);
979 return ret;
980}
981
982/*
983 * encrypted_destroy - before freeing the key, clear the decrypted data
984 *
985 * Before freeing the key, clear the memory containing the decrypted
986 * key data.
987 */
988static void encrypted_destroy(struct key *key)
989{
David Howells146aa8b2015-10-21 14:04:48 +0100990 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500991
992 if (!epayload)
993 return;
994
995 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
David Howells146aa8b2015-10-21 14:04:48 +0100996 kfree(key->payload.data[0]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500997}
998
999struct key_type key_type_encrypted = {
1000 .name = "encrypted",
1001 .instantiate = encrypted_instantiate,
1002 .update = encrypted_update,
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001003 .destroy = encrypted_destroy,
1004 .describe = user_describe,
1005 .read = encrypted_read,
1006};
1007EXPORT_SYMBOL_GPL(key_type_encrypted);
1008
1009static void encrypted_shash_release(void)
1010{
1011 if (hashalg)
1012 crypto_free_shash(hashalg);
1013 if (hmacalg)
1014 crypto_free_shash(hmacalg);
1015}
1016
1017static int __init encrypted_shash_alloc(void)
1018{
1019 int ret;
1020
1021 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1022 if (IS_ERR(hmacalg)) {
1023 pr_info("encrypted_key: could not allocate crypto %s\n",
1024 hmac_alg);
1025 return PTR_ERR(hmacalg);
1026 }
1027
1028 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1029 if (IS_ERR(hashalg)) {
1030 pr_info("encrypted_key: could not allocate crypto %s\n",
1031 hash_alg);
1032 ret = PTR_ERR(hashalg);
1033 goto hashalg_fail;
1034 }
1035
1036 return 0;
1037
1038hashalg_fail:
1039 crypto_free_shash(hmacalg);
1040 return ret;
1041}
1042
1043static int __init init_encrypted(void)
1044{
1045 int ret;
1046
1047 ret = encrypted_shash_alloc();
1048 if (ret < 0)
1049 return ret;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001050 ret = aes_get_sizes();
1051 if (ret < 0)
1052 goto out;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001053 ret = register_key_type(&key_type_encrypted);
1054 if (ret < 0)
1055 goto out;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001056 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001057out:
1058 encrypted_shash_release();
1059 return ret;
Mimi Zoharb9703442011-01-18 09:07:12 -05001060
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001061}
1062
1063static void __exit cleanup_encrypted(void)
1064{
1065 encrypted_shash_release();
1066 unregister_key_type(&key_type_encrypted);
1067}
1068
1069late_initcall(init_encrypted);
1070module_exit(cleanup_encrypted);
1071
1072MODULE_LICENSE("GPL");