blob: a871159bf03cc74e84f541ced1674af75086fcce [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{
144 if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) {
145 if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
146 goto out;
147 if (orig_desc)
148 if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
149 goto out;
150 } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
151 if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
152 goto out;
153 if (orig_desc)
154 if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
155 goto out;
156 } else
157 goto out;
158 return 0;
159out:
160 return -EINVAL;
161}
162
163/*
164 * datablob_parse - parse the keyctl data
165 *
166 * datablob format:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200167 * new [<format>] <master-key name> <decrypted data length>
168 * load [<format>] <master-key name> <decrypted data length>
169 * <encrypted iv + data>
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500170 * update <new-master-key name>
171 *
172 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
173 * which is null terminated.
174 *
175 * On success returns 0, otherwise -EINVAL.
176 */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200177static int datablob_parse(char *datablob, const char **format,
178 char **master_desc, char **decrypted_datalen,
179 char **hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500180{
181 substring_t args[MAX_OPT_ARGS];
182 int ret = -EINVAL;
183 int key_cmd;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200184 int key_format;
185 char *p, *keyword;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500186
Roberto Sassu7103dff2011-06-27 13:45:41 +0200187 keyword = strsep(&datablob, " \t");
188 if (!keyword) {
189 pr_info("encrypted_key: insufficient parameters specified\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500190 return ret;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200191 }
192 key_cmd = match_token(keyword, key_tokens, args);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500193
Roberto Sassu79a73d12011-06-27 13:45:44 +0200194 /* Get optional format: default | ecryptfs */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200195 p = strsep(&datablob, " \t");
196 if (!p) {
197 pr_err("encrypted_key: insufficient parameters specified\n");
198 return ret;
199 }
200
201 key_format = match_token(p, key_format_tokens, args);
202 switch (key_format) {
Roberto Sassu79a73d12011-06-27 13:45:44 +0200203 case Opt_ecryptfs:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200204 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
Roberto Sassu7103dff2011-06-27 13:45:41 +0200213 if (!*master_desc) {
214 pr_info("encrypted_key: master key parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500215 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200216 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500217
Roberto Sassu7103dff2011-06-27 13:45:41 +0200218 if (valid_master_desc(*master_desc, NULL) < 0) {
219 pr_info("encrypted_key: master key parameter \'%s\' "
220 "is invalid\n", *master_desc);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500221 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200222 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500223
224 if (decrypted_datalen) {
225 *decrypted_datalen = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200226 if (!*decrypted_datalen) {
227 pr_info("encrypted_key: keylen parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500228 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200229 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500230 }
231
232 switch (key_cmd) {
233 case Opt_new:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200234 if (!decrypted_datalen) {
235 pr_info("encrypted_key: keyword \'%s\' not allowed "
236 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500237 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200238 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500239 ret = 0;
240 break;
241 case Opt_load:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200242 if (!decrypted_datalen) {
243 pr_info("encrypted_key: keyword \'%s\' not allowed "
244 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500245 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200246 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500247 *hex_encoded_iv = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200248 if (!*hex_encoded_iv) {
249 pr_info("encrypted_key: hex blob is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500250 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200251 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500252 ret = 0;
253 break;
254 case Opt_update:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200255 if (decrypted_datalen) {
256 pr_info("encrypted_key: keyword \'%s\' not allowed "
257 "when called from .instantiate method\n",
258 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500259 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200260 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500261 ret = 0;
262 break;
263 case Opt_err:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200264 pr_info("encrypted_key: keyword \'%s\' not recognized\n",
265 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500266 break;
267 }
268out:
269 return ret;
270}
271
272/*
273 * datablob_format - format as an ascii string, before copying to userspace
274 */
275static char *datablob_format(struct encrypted_key_payload *epayload,
276 size_t asciiblob_len)
277{
278 char *ascii_buf, *bufp;
279 u8 *iv = epayload->iv;
280 int len;
281 int i;
282
283 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
284 if (!ascii_buf)
285 goto out;
286
287 ascii_buf[asciiblob_len] = '\0';
288
289 /* copy datablob master_desc and datalen strings */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200290 len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
291 epayload->master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500292
293 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
294 bufp = &ascii_buf[len];
295 for (i = 0; i < (asciiblob_len - len) / 2; i++)
Andy Shevchenko02473112011-10-31 17:12:55 -0700296 bufp = hex_byte_pack(bufp, iv[i]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500297out:
298 return ascii_buf;
299}
300
301/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500302 * request_user_key - request the user key
303 *
304 * Use a user provided key to encrypt/decrypt an encrypted-key.
305 */
David Howells146aa8b2015-10-21 14:04:48 +0100306static struct key *request_user_key(const char *master_desc, const u8 **master_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500307 size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500308{
David Howells146aa8b2015-10-21 14:04:48 +0100309 const struct user_key_payload *upayload;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500310 struct key *ukey;
311
312 ukey = request_key(&key_type_user, master_desc, NULL);
313 if (IS_ERR(ukey))
314 goto error;
315
316 down_read(&ukey->sem);
David Howells146aa8b2015-10-21 14:04:48 +0100317 upayload = user_key_payload(ukey);
Eric Biggersfec442e2017-10-09 12:37:49 -0700318 if (!upayload) {
319 /* key was revoked before we acquired its semaphore */
320 up_read(&ukey->sem);
321 key_put(ukey);
322 ukey = ERR_PTR(-EKEYREVOKED);
323 goto error;
324 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500325 *master_key = upayload->data;
326 *master_keylen = upayload->datalen;
327error:
328 return ukey;
329}
330
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500331static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500332{
333 struct sdesc *sdesc;
334 int size;
335
336 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
337 sdesc = kmalloc(size, GFP_KERNEL);
338 if (!sdesc)
339 return ERR_PTR(-ENOMEM);
340 sdesc->shash.tfm = alg;
341 sdesc->shash.flags = 0x0;
342 return sdesc;
343}
344
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500345static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
346 const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500347{
348 struct sdesc *sdesc;
349 int ret;
350
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500351 sdesc = alloc_sdesc(hmacalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500352 if (IS_ERR(sdesc)) {
353 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
354 return PTR_ERR(sdesc);
355 }
356
357 ret = crypto_shash_setkey(hmacalg, key, keylen);
358 if (!ret)
359 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
360 kfree(sdesc);
361 return ret;
362}
363
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500364static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500365{
366 struct sdesc *sdesc;
367 int ret;
368
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500369 sdesc = alloc_sdesc(hashalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500370 if (IS_ERR(sdesc)) {
371 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
372 return PTR_ERR(sdesc);
373 }
374
375 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
376 kfree(sdesc);
377 return ret;
378}
379
380enum derived_key_type { ENC_KEY, AUTH_KEY };
381
382/* Derive authentication/encryption key from trusted key */
383static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500384 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500385{
386 u8 *derived_buf;
387 unsigned int derived_buf_len;
388 int ret;
389
390 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
391 if (derived_buf_len < HASH_SIZE)
392 derived_buf_len = HASH_SIZE;
393
394 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
395 if (!derived_buf) {
396 pr_err("encrypted_key: out of memory\n");
397 return -ENOMEM;
398 }
399 if (key_type)
400 strcpy(derived_buf, "AUTH_KEY");
401 else
402 strcpy(derived_buf, "ENC_KEY");
403
404 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
405 master_keylen);
406 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
407 kfree(derived_buf);
408 return ret;
409}
410
Herbert Xuc3917fd2016-01-24 21:18:19 +0800411static struct skcipher_request *init_skcipher_req(const u8 *key,
412 unsigned int key_len)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500413{
Herbert Xuc3917fd2016-01-24 21:18:19 +0800414 struct skcipher_request *req;
415 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500416 int ret;
417
Herbert Xuc3917fd2016-01-24 21:18:19 +0800418 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
419 if (IS_ERR(tfm)) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500420 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
Herbert Xuc3917fd2016-01-24 21:18:19 +0800421 blkcipher_alg, PTR_ERR(tfm));
422 return ERR_CAST(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500423 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500424
Herbert Xuc3917fd2016-01-24 21:18:19 +0800425 ret = crypto_skcipher_setkey(tfm, key, key_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500426 if (ret < 0) {
427 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800428 crypto_free_skcipher(tfm);
429 return ERR_PTR(ret);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500430 }
Herbert Xuc3917fd2016-01-24 21:18:19 +0800431
432 req = skcipher_request_alloc(tfm, GFP_KERNEL);
433 if (!req) {
434 pr_err("encrypted_key: failed to allocate request for %s\n",
435 blkcipher_alg);
436 crypto_free_skcipher(tfm);
437 return ERR_PTR(-ENOMEM);
438 }
439
440 skcipher_request_set_callback(req, 0, NULL, NULL);
441 return req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500442}
443
444static struct key *request_master_key(struct encrypted_key_payload *epayload,
David Howells146aa8b2015-10-21 14:04:48 +0100445 const u8 **master_key, size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500446{
Dan Carpenter73a0a682017-02-09 17:17:52 +0000447 struct key *mkey = ERR_PTR(-EINVAL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500448
449 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
450 KEY_TRUSTED_PREFIX_LEN)) {
451 mkey = request_trusted_key(epayload->master_desc +
452 KEY_TRUSTED_PREFIX_LEN,
453 master_key, master_keylen);
454 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
455 KEY_USER_PREFIX_LEN)) {
456 mkey = request_user_key(epayload->master_desc +
457 KEY_USER_PREFIX_LEN,
458 master_key, master_keylen);
459 } else
460 goto out;
461
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200462 if (IS_ERR(mkey)) {
Mimi Zoharf4a0d5a2011-10-24 08:17:42 -0400463 int ret = PTR_ERR(mkey);
Mimi Zohar982e6172011-08-27 22:21:26 -0400464
465 if (ret == -ENOTSUPP)
466 pr_info("encrypted_key: key %s not supported",
467 epayload->master_desc);
468 else
469 pr_info("encrypted_key: key %s not found",
470 epayload->master_desc);
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200471 goto out;
472 }
473
474 dump_master_key(*master_key, *master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500475out:
476 return mkey;
477}
478
479/* Before returning data to userspace, encrypt decrypted data. */
480static int derived_key_encrypt(struct encrypted_key_payload *epayload,
481 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500482 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500483{
484 struct scatterlist sg_in[2];
485 struct scatterlist sg_out[1];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800486 struct crypto_skcipher *tfm;
487 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500488 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800489 u8 iv[AES_BLOCK_SIZE];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500490 int ret;
491
492 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500493
Herbert Xuc3917fd2016-01-24 21:18:19 +0800494 req = init_skcipher_req(derived_key, derived_keylen);
495 ret = PTR_ERR(req);
496 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500497 goto out;
498 dump_decrypted_data(epayload);
499
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500500 sg_init_table(sg_in, 2);
501 sg_set_buf(&sg_in[0], epayload->decrypted_data,
502 epayload->decrypted_datalen);
Eric Biggersd24c1c12017-06-08 14:48:10 +0100503 sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500504
505 sg_init_table(sg_out, 1);
506 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
507
Herbert Xu456bee92016-09-20 20:35:55 +0800508 memcpy(iv, epayload->iv, sizeof(iv));
509 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800510 ret = crypto_skcipher_encrypt(req);
511 tfm = crypto_skcipher_reqtfm(req);
512 skcipher_request_free(req);
513 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500514 if (ret < 0)
515 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
516 else
517 dump_encrypted_data(epayload, encrypted_datalen);
518out:
519 return ret;
520}
521
522static int datablob_hmac_append(struct encrypted_key_payload *epayload,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500523 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500524{
525 u8 derived_key[HASH_SIZE];
526 u8 *digest;
527 int ret;
528
529 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
530 if (ret < 0)
531 goto out;
532
Roberto Sassu4e561d32011-06-27 13:45:42 +0200533 digest = epayload->format + epayload->datablob_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500534 ret = calc_hmac(digest, derived_key, sizeof derived_key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200535 epayload->format, epayload->datablob_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500536 if (!ret)
537 dump_hmac(NULL, digest, HASH_SIZE);
538out:
539 return ret;
540}
541
542/* verify HMAC before decrypting encrypted key */
543static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200544 const u8 *format, const u8 *master_key,
545 size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500546{
547 u8 derived_key[HASH_SIZE];
548 u8 digest[HASH_SIZE];
549 int ret;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200550 char *p;
551 unsigned short len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500552
553 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
554 if (ret < 0)
555 goto out;
556
Roberto Sassu4e561d32011-06-27 13:45:42 +0200557 len = epayload->datablob_len;
558 if (!format) {
559 p = epayload->master_desc;
560 len -= strlen(epayload->format) + 1;
561 } else
562 p = epayload->format;
563
564 ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500565 if (ret < 0)
566 goto out;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200567 ret = memcmp(digest, epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500568 sizeof digest);
569 if (ret) {
570 ret = -EINVAL;
571 dump_hmac("datablob",
Roberto Sassu4e561d32011-06-27 13:45:42 +0200572 epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500573 HASH_SIZE);
574 dump_hmac("calc", digest, HASH_SIZE);
575 }
576out:
577 return ret;
578}
579
580static int derived_key_decrypt(struct encrypted_key_payload *epayload,
581 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500582 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500583{
584 struct scatterlist sg_in[1];
585 struct scatterlist sg_out[2];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800586 struct crypto_skcipher *tfm;
587 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500588 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800589 u8 iv[AES_BLOCK_SIZE];
Eric Biggersd24c1c12017-06-08 14:48:10 +0100590 u8 *pad;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500591 int ret;
592
Eric Biggersd24c1c12017-06-08 14:48:10 +0100593 /* Throwaway buffer to hold the unused zero padding at the end */
594 pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
595 if (!pad)
596 return -ENOMEM;
597
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500598 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800599 req = init_skcipher_req(derived_key, derived_keylen);
600 ret = PTR_ERR(req);
601 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500602 goto out;
603 dump_encrypted_data(epayload, encrypted_datalen);
604
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500605 sg_init_table(sg_in, 1);
606 sg_init_table(sg_out, 2);
607 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
608 sg_set_buf(&sg_out[0], epayload->decrypted_data,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500609 epayload->decrypted_datalen);
Eric Biggersd24c1c12017-06-08 14:48:10 +0100610 sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500611
Herbert Xu456bee92016-09-20 20:35:55 +0800612 memcpy(iv, epayload->iv, sizeof(iv));
613 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800614 ret = crypto_skcipher_decrypt(req);
615 tfm = crypto_skcipher_reqtfm(req);
616 skcipher_request_free(req);
617 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500618 if (ret < 0)
619 goto out;
620 dump_decrypted_data(epayload);
621out:
Eric Biggersd24c1c12017-06-08 14:48:10 +0100622 kfree(pad);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500623 return ret;
624}
625
626/* Allocate memory for decrypted key and datablob. */
627static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200628 const char *format,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500629 const char *master_desc,
630 const char *datalen)
631{
632 struct encrypted_key_payload *epayload = NULL;
633 unsigned short datablob_len;
634 unsigned short decrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200635 unsigned short payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500636 unsigned int encrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200637 unsigned int format_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500638 long dlen;
639 int ret;
640
Jingoo Han29707b22014-02-05 15:13:14 +0900641 ret = kstrtol(datalen, 10, &dlen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500642 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
643 return ERR_PTR(-EINVAL);
644
Roberto Sassu4e561d32011-06-27 13:45:42 +0200645 format_len = (!format) ? strlen(key_format_default) : strlen(format);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500646 decrypted_datalen = dlen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200647 payload_datalen = decrypted_datalen;
Roberto Sassu79a73d12011-06-27 13:45:44 +0200648 if (format && !strcmp(format, key_format_ecryptfs)) {
649 if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
650 pr_err("encrypted_key: keylen for the ecryptfs format "
651 "must be equal to %d bytes\n",
652 ECRYPTFS_MAX_KEY_BYTES);
653 return ERR_PTR(-EINVAL);
654 }
655 decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
656 payload_datalen = sizeof(struct ecryptfs_auth_tok);
657 }
658
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500659 encrypted_datalen = roundup(decrypted_datalen, blksize);
660
Roberto Sassu4e561d32011-06-27 13:45:42 +0200661 datablob_len = format_len + 1 + strlen(master_desc) + 1
662 + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500663
Roberto Sassu4e561d32011-06-27 13:45:42 +0200664 ret = key_payload_reserve(key, payload_datalen + datablob_len
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500665 + HASH_SIZE + 1);
666 if (ret < 0)
667 return ERR_PTR(ret);
668
Roberto Sassu4e561d32011-06-27 13:45:42 +0200669 epayload = kzalloc(sizeof(*epayload) + payload_datalen +
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500670 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
671 if (!epayload)
672 return ERR_PTR(-ENOMEM);
673
Roberto Sassu4e561d32011-06-27 13:45:42 +0200674 epayload->payload_datalen = payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500675 epayload->decrypted_datalen = decrypted_datalen;
676 epayload->datablob_len = datablob_len;
677 return epayload;
678}
679
680static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200681 const char *format, const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500682{
683 struct key *mkey;
684 u8 derived_key[HASH_SIZE];
David Howells146aa8b2015-10-21 14:04:48 +0100685 const u8 *master_key;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500686 u8 *hmac;
Mimi Zohar1f350652010-12-13 16:53:12 -0500687 const char *hex_encoded_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500688 unsigned int encrypted_datalen;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500689 size_t master_keylen;
Mimi Zohar1f350652010-12-13 16:53:12 -0500690 size_t asciilen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500691 int ret;
692
693 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar1f350652010-12-13 16:53:12 -0500694 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
695 if (strlen(hex_encoded_iv) != asciilen)
696 return -EINVAL;
697
698 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400699 ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
700 if (ret < 0)
701 return -EINVAL;
702 ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
703 encrypted_datalen);
704 if (ret < 0)
705 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500706
Roberto Sassu4e561d32011-06-27 13:45:42 +0200707 hmac = epayload->format + epayload->datablob_len;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400708 ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
709 HASH_SIZE);
710 if (ret < 0)
711 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500712
713 mkey = request_master_key(epayload, &master_key, &master_keylen);
714 if (IS_ERR(mkey))
715 return PTR_ERR(mkey);
716
Roberto Sassu4e561d32011-06-27 13:45:42 +0200717 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500718 if (ret < 0) {
719 pr_err("encrypted_key: bad hmac (%d)\n", ret);
720 goto out;
721 }
722
723 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
724 if (ret < 0)
725 goto out;
726
727 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
728 if (ret < 0)
729 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
730out:
731 up_read(&mkey->sem);
732 key_put(mkey);
733 return ret;
734}
735
736static void __ekey_init(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200737 const char *format, const char *master_desc,
738 const char *datalen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500739{
Roberto Sassu4e561d32011-06-27 13:45:42 +0200740 unsigned int format_len;
741
742 format_len = (!format) ? strlen(key_format_default) : strlen(format);
743 epayload->format = epayload->payload_data + epayload->payload_datalen;
744 epayload->master_desc = epayload->format + format_len + 1;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500745 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
746 epayload->iv = epayload->datalen + strlen(datalen) + 1;
747 epayload->encrypted_data = epayload->iv + ivsize + 1;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200748 epayload->decrypted_data = epayload->payload_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500749
Roberto Sassu4e561d32011-06-27 13:45:42 +0200750 if (!format)
751 memcpy(epayload->format, key_format_default, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200752 else {
753 if (!strcmp(format, key_format_ecryptfs))
754 epayload->decrypted_data =
755 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
756
Roberto Sassu4e561d32011-06-27 13:45:42 +0200757 memcpy(epayload->format, format, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200758 }
759
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500760 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
761 memcpy(epayload->datalen, datalen, strlen(datalen));
762}
763
764/*
765 * encrypted_init - initialize an encrypted key
766 *
767 * For a new key, use a random number for both the iv and data
768 * itself. For an old key, decrypt the hex encoded data.
769 */
770static int encrypted_init(struct encrypted_key_payload *epayload,
Roberto Sassu79a73d12011-06-27 13:45:44 +0200771 const char *key_desc, const char *format,
772 const char *master_desc, const char *datalen,
773 const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500774{
775 int ret = 0;
776
Roberto Sassu79a73d12011-06-27 13:45:44 +0200777 if (format && !strcmp(format, key_format_ecryptfs)) {
778 ret = valid_ecryptfs_desc(key_desc);
779 if (ret < 0)
780 return ret;
781
782 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
783 key_desc);
784 }
785
Roberto Sassu4e561d32011-06-27 13:45:42 +0200786 __ekey_init(epayload, format, master_desc, datalen);
Mimi Zohar1f350652010-12-13 16:53:12 -0500787 if (!hex_encoded_iv) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500788 get_random_bytes(epayload->iv, ivsize);
789
790 get_random_bytes(epayload->decrypted_data,
791 epayload->decrypted_datalen);
792 } else
Roberto Sassu4e561d32011-06-27 13:45:42 +0200793 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500794 return ret;
795}
796
797/*
798 * encrypted_instantiate - instantiate an encrypted key
799 *
800 * Decrypt an existing encrypted datablob or create a new encrypted key
801 * based on a kernel random number.
802 *
803 * On success, return 0. Otherwise return errno.
804 */
David Howellscf7f6012012-09-13 13:06:29 +0100805static int encrypted_instantiate(struct key *key,
806 struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500807{
808 struct encrypted_key_payload *epayload = NULL;
809 char *datablob = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200810 const char *format = NULL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500811 char *master_desc = NULL;
812 char *decrypted_datalen = NULL;
813 char *hex_encoded_iv = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100814 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500815 int ret;
816
David Howellscf7f6012012-09-13 13:06:29 +0100817 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500818 return -EINVAL;
819
820 datablob = kmalloc(datalen + 1, GFP_KERNEL);
821 if (!datablob)
822 return -ENOMEM;
823 datablob[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100824 memcpy(datablob, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200825 ret = datablob_parse(datablob, &format, &master_desc,
826 &decrypted_datalen, &hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500827 if (ret < 0)
828 goto out;
829
Roberto Sassu4e561d32011-06-27 13:45:42 +0200830 epayload = encrypted_key_alloc(key, format, master_desc,
831 decrypted_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500832 if (IS_ERR(epayload)) {
833 ret = PTR_ERR(epayload);
834 goto out;
835 }
Roberto Sassu79a73d12011-06-27 13:45:44 +0200836 ret = encrypted_init(epayload, key->description, format, master_desc,
837 decrypted_datalen, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500838 if (ret < 0) {
839 kfree(epayload);
840 goto out;
841 }
842
Mimi Zoharb64cc5f2014-07-26 23:21:02 -0400843 rcu_assign_keypointer(key, epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500844out:
845 kfree(datablob);
846 return ret;
847}
848
849static void encrypted_rcu_free(struct rcu_head *rcu)
850{
851 struct encrypted_key_payload *epayload;
852
853 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
854 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
855 kfree(epayload);
856}
857
858/*
859 * encrypted_update - update the master key description
860 *
861 * Change the master key description for an existing encrypted key.
862 * The next read will return an encrypted datablob using the new
863 * master key description.
864 *
865 * On success, return 0. Otherwise return errno.
866 */
David Howellscf7f6012012-09-13 13:06:29 +0100867static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500868{
David Howells146aa8b2015-10-21 14:04:48 +0100869 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500870 struct encrypted_key_payload *new_epayload;
871 char *buf;
872 char *new_master_desc = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200873 const char *format = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100874 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500875 int ret = 0;
876
David Howells63c8e452017-10-04 16:43:25 +0100877 if (key_is_negative(key))
David Howells096fe9e2015-11-24 21:36:31 +0000878 return -ENOKEY;
David Howellscf7f6012012-09-13 13:06:29 +0100879 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500880 return -EINVAL;
881
882 buf = kmalloc(datalen + 1, GFP_KERNEL);
883 if (!buf)
884 return -ENOMEM;
885
886 buf[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100887 memcpy(buf, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200888 ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500889 if (ret < 0)
890 goto out;
891
892 ret = valid_master_desc(new_master_desc, epayload->master_desc);
893 if (ret < 0)
894 goto out;
895
Roberto Sassu4e561d32011-06-27 13:45:42 +0200896 new_epayload = encrypted_key_alloc(key, epayload->format,
897 new_master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500898 if (IS_ERR(new_epayload)) {
899 ret = PTR_ERR(new_epayload);
900 goto out;
901 }
902
Roberto Sassu4e561d32011-06-27 13:45:42 +0200903 __ekey_init(new_epayload, epayload->format, new_master_desc,
904 epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500905
906 memcpy(new_epayload->iv, epayload->iv, ivsize);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200907 memcpy(new_epayload->payload_data, epayload->payload_data,
908 epayload->payload_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500909
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000910 rcu_assign_keypointer(key, new_epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500911 call_rcu(&epayload->rcu, encrypted_rcu_free);
912out:
913 kfree(buf);
914 return ret;
915}
916
917/*
918 * encrypted_read - format and copy the encrypted data to userspace
919 *
920 * The resulting datablob format is:
921 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
922 *
923 * On success, return to userspace the encrypted key datablob size.
924 */
925static long encrypted_read(const struct key *key, char __user *buffer,
926 size_t buflen)
927{
928 struct encrypted_key_payload *epayload;
929 struct key *mkey;
David Howells146aa8b2015-10-21 14:04:48 +0100930 const u8 *master_key;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500931 size_t master_keylen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500932 char derived_key[HASH_SIZE];
933 char *ascii_buf;
934 size_t asciiblob_len;
935 int ret;
936
David Howells633e8042011-03-07 15:05:51 +0000937 epayload = rcu_dereference_key(key);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500938
939 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
940 asciiblob_len = epayload->datablob_len + ivsize + 1
941 + roundup(epayload->decrypted_datalen, blksize)
942 + (HASH_SIZE * 2);
943
944 if (!buffer || buflen < asciiblob_len)
945 return asciiblob_len;
946
947 mkey = request_master_key(epayload, &master_key, &master_keylen);
948 if (IS_ERR(mkey))
949 return PTR_ERR(mkey);
950
951 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
952 if (ret < 0)
953 goto out;
954
955 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
956 if (ret < 0)
957 goto out;
958
959 ret = datablob_hmac_append(epayload, master_key, master_keylen);
960 if (ret < 0)
961 goto out;
962
963 ascii_buf = datablob_format(epayload, asciiblob_len);
964 if (!ascii_buf) {
965 ret = -ENOMEM;
966 goto out;
967 }
968
969 up_read(&mkey->sem);
970 key_put(mkey);
971
972 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
973 ret = -EFAULT;
974 kfree(ascii_buf);
975
976 return asciiblob_len;
977out:
978 up_read(&mkey->sem);
979 key_put(mkey);
980 return ret;
981}
982
983/*
984 * encrypted_destroy - before freeing the key, clear the decrypted data
985 *
986 * Before freeing the key, clear the memory containing the decrypted
987 * key data.
988 */
989static void encrypted_destroy(struct key *key)
990{
David Howells146aa8b2015-10-21 14:04:48 +0100991 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500992
993 if (!epayload)
994 return;
995
996 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
David Howells146aa8b2015-10-21 14:04:48 +0100997 kfree(key->payload.data[0]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500998}
999
1000struct key_type key_type_encrypted = {
1001 .name = "encrypted",
1002 .instantiate = encrypted_instantiate,
1003 .update = encrypted_update,
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001004 .destroy = encrypted_destroy,
1005 .describe = user_describe,
1006 .read = encrypted_read,
1007};
1008EXPORT_SYMBOL_GPL(key_type_encrypted);
1009
1010static void encrypted_shash_release(void)
1011{
1012 if (hashalg)
1013 crypto_free_shash(hashalg);
1014 if (hmacalg)
1015 crypto_free_shash(hmacalg);
1016}
1017
1018static int __init encrypted_shash_alloc(void)
1019{
1020 int ret;
1021
1022 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1023 if (IS_ERR(hmacalg)) {
1024 pr_info("encrypted_key: could not allocate crypto %s\n",
1025 hmac_alg);
1026 return PTR_ERR(hmacalg);
1027 }
1028
1029 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1030 if (IS_ERR(hashalg)) {
1031 pr_info("encrypted_key: could not allocate crypto %s\n",
1032 hash_alg);
1033 ret = PTR_ERR(hashalg);
1034 goto hashalg_fail;
1035 }
1036
1037 return 0;
1038
1039hashalg_fail:
1040 crypto_free_shash(hmacalg);
1041 return ret;
1042}
1043
1044static int __init init_encrypted(void)
1045{
1046 int ret;
1047
1048 ret = encrypted_shash_alloc();
1049 if (ret < 0)
1050 return ret;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001051 ret = aes_get_sizes();
1052 if (ret < 0)
1053 goto out;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001054 ret = register_key_type(&key_type_encrypted);
1055 if (ret < 0)
1056 goto out;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001057 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001058out:
1059 encrypted_shash_release();
1060 return ret;
Mimi Zoharb9703442011-01-18 09:07:12 -05001061
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001062}
1063
1064static void __exit cleanup_encrypted(void)
1065{
1066 encrypted_shash_release();
1067 unregister_key_type(&key_type_encrypted);
1068}
1069
1070late_initcall(init_encrypted);
1071module_exit(cleanup_encrypted);
1072
1073MODULE_LICENSE("GPL");