blob: 5adbfc32242f81b0f6396fd7d0656c5aa4eca6ff [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>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050032#include <crypto/hash.h>
33#include <crypto/sha.h>
Herbert Xuc3917fd2016-01-24 21:18:19 +080034#include <crypto/skcipher.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050035
Mimi Zoharb9703442011-01-18 09:07:12 -050036#include "encrypted.h"
Roberto Sassu79a73d12011-06-27 13:45:44 +020037#include "ecryptfs_format.h"
Mimi Zohar7e70cb42010-11-23 18:55:35 -050038
Mimi Zohar3b1826c2010-12-13 16:53:13 -050039static const char KEY_TRUSTED_PREFIX[] = "trusted:";
40static const char KEY_USER_PREFIX[] = "user:";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050041static const char hash_alg[] = "sha256";
42static const char hmac_alg[] = "hmac(sha256)";
43static const char blkcipher_alg[] = "cbc(aes)";
Roberto Sassu4e561d32011-06-27 13:45:42 +020044static const char key_format_default[] = "default";
Roberto Sassu79a73d12011-06-27 13:45:44 +020045static const char key_format_ecryptfs[] = "ecryptfs";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050046static unsigned int ivsize;
47static int blksize;
48
Mimi Zohar3b1826c2010-12-13 16:53:13 -050049#define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
50#define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
Roberto Sassu79a73d12011-06-27 13:45:44 +020051#define KEY_ECRYPTFS_DESC_LEN 16
Mimi Zohar3b1826c2010-12-13 16:53:13 -050052#define HASH_SIZE SHA256_DIGEST_SIZE
53#define MAX_DATA_SIZE 4096
54#define MIN_DATA_SIZE 20
55
Mimi Zohar7e70cb42010-11-23 18:55:35 -050056struct sdesc {
57 struct shash_desc shash;
58 char ctx[];
59};
60
61static struct crypto_shash *hashalg;
62static struct crypto_shash *hmacalg;
63
64enum {
65 Opt_err = -1, Opt_new, Opt_load, Opt_update
66};
67
Roberto Sassu4e561d32011-06-27 13:45:42 +020068enum {
Roberto Sassu79a73d12011-06-27 13:45:44 +020069 Opt_error = -1, Opt_default, Opt_ecryptfs
Roberto Sassu4e561d32011-06-27 13:45:42 +020070};
71
72static const match_table_t key_format_tokens = {
73 {Opt_default, "default"},
Roberto Sassu79a73d12011-06-27 13:45:44 +020074 {Opt_ecryptfs, "ecryptfs"},
Roberto Sassu4e561d32011-06-27 13:45:42 +020075 {Opt_error, NULL}
76};
77
Mimi Zohar7e70cb42010-11-23 18:55:35 -050078static const match_table_t key_tokens = {
79 {Opt_new, "new"},
80 {Opt_load, "load"},
81 {Opt_update, "update"},
82 {Opt_err, NULL}
83};
84
85static int aes_get_sizes(void)
86{
Herbert Xuc3917fd2016-01-24 21:18:19 +080087 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -050088
Herbert Xuc3917fd2016-01-24 21:18:19 +080089 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050090 if (IS_ERR(tfm)) {
91 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
92 PTR_ERR(tfm));
93 return PTR_ERR(tfm);
94 }
Herbert Xuc3917fd2016-01-24 21:18:19 +080095 ivsize = crypto_skcipher_ivsize(tfm);
96 blksize = crypto_skcipher_blocksize(tfm);
97 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050098 return 0;
99}
100
101/*
Roberto Sassu79a73d12011-06-27 13:45:44 +0200102 * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
103 *
104 * The description of a encrypted key with format 'ecryptfs' must contain
105 * exactly 16 hexadecimal characters.
106 *
107 */
108static int valid_ecryptfs_desc(const char *ecryptfs_desc)
109{
110 int i;
111
112 if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
113 pr_err("encrypted_key: key description must be %d hexadecimal "
114 "characters long\n", KEY_ECRYPTFS_DESC_LEN);
115 return -EINVAL;
116 }
117
118 for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
119 if (!isxdigit(ecryptfs_desc[i])) {
120 pr_err("encrypted_key: key description must contain "
121 "only hexadecimal characters\n");
122 return -EINVAL;
123 }
124 }
125
126 return 0;
127}
128
129/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500130 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
131 *
Roberto Sassu08fa2aa2011-06-27 13:45:40 +0200132 * key-type:= "trusted:" | "user:"
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500133 * desc:= master-key description
134 *
135 * Verify that 'key-type' is valid and that 'desc' exists. On key update,
136 * only the master key description is permitted to change, not the key-type.
137 * The key-type remains constant.
138 *
139 * On success returns 0, otherwise -EINVAL.
140 */
141static int valid_master_desc(const char *new_desc, const char *orig_desc)
142{
143 if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) {
144 if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
145 goto out;
146 if (orig_desc)
147 if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
148 goto out;
149 } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
150 if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
151 goto out;
152 if (orig_desc)
153 if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
154 goto out;
155 } else
156 goto out;
157 return 0;
158out:
159 return -EINVAL;
160}
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);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500317 *master_key = upayload->data;
318 *master_keylen = upayload->datalen;
319error:
320 return ukey;
321}
322
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500323static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500324{
325 struct sdesc *sdesc;
326 int size;
327
328 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
329 sdesc = kmalloc(size, GFP_KERNEL);
330 if (!sdesc)
331 return ERR_PTR(-ENOMEM);
332 sdesc->shash.tfm = alg;
333 sdesc->shash.flags = 0x0;
334 return sdesc;
335}
336
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500337static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
338 const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500339{
340 struct sdesc *sdesc;
341 int ret;
342
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500343 sdesc = alloc_sdesc(hmacalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500344 if (IS_ERR(sdesc)) {
345 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
346 return PTR_ERR(sdesc);
347 }
348
349 ret = crypto_shash_setkey(hmacalg, key, keylen);
350 if (!ret)
351 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
352 kfree(sdesc);
353 return ret;
354}
355
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500356static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500357{
358 struct sdesc *sdesc;
359 int ret;
360
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500361 sdesc = alloc_sdesc(hashalg);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500362 if (IS_ERR(sdesc)) {
363 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
364 return PTR_ERR(sdesc);
365 }
366
367 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
368 kfree(sdesc);
369 return ret;
370}
371
372enum derived_key_type { ENC_KEY, AUTH_KEY };
373
374/* Derive authentication/encryption key from trusted key */
375static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500376 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500377{
378 u8 *derived_buf;
379 unsigned int derived_buf_len;
380 int ret;
381
382 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
383 if (derived_buf_len < HASH_SIZE)
384 derived_buf_len = HASH_SIZE;
385
386 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
387 if (!derived_buf) {
388 pr_err("encrypted_key: out of memory\n");
389 return -ENOMEM;
390 }
391 if (key_type)
392 strcpy(derived_buf, "AUTH_KEY");
393 else
394 strcpy(derived_buf, "ENC_KEY");
395
396 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
397 master_keylen);
398 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
399 kfree(derived_buf);
400 return ret;
401}
402
Herbert Xuc3917fd2016-01-24 21:18:19 +0800403static struct skcipher_request *init_skcipher_req(const u8 *key,
404 unsigned int key_len)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500405{
Herbert Xuc3917fd2016-01-24 21:18:19 +0800406 struct skcipher_request *req;
407 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500408 int ret;
409
Herbert Xuc3917fd2016-01-24 21:18:19 +0800410 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
411 if (IS_ERR(tfm)) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500412 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
Herbert Xuc3917fd2016-01-24 21:18:19 +0800413 blkcipher_alg, PTR_ERR(tfm));
414 return ERR_CAST(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500415 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500416
Herbert Xuc3917fd2016-01-24 21:18:19 +0800417 ret = crypto_skcipher_setkey(tfm, key, key_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500418 if (ret < 0) {
419 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800420 crypto_free_skcipher(tfm);
421 return ERR_PTR(ret);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500422 }
Herbert Xuc3917fd2016-01-24 21:18:19 +0800423
424 req = skcipher_request_alloc(tfm, GFP_KERNEL);
425 if (!req) {
426 pr_err("encrypted_key: failed to allocate request for %s\n",
427 blkcipher_alg);
428 crypto_free_skcipher(tfm);
429 return ERR_PTR(-ENOMEM);
430 }
431
432 skcipher_request_set_callback(req, 0, NULL, NULL);
433 return req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500434}
435
436static struct key *request_master_key(struct encrypted_key_payload *epayload,
David Howells146aa8b2015-10-21 14:04:48 +0100437 const u8 **master_key, size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500438{
439 struct key *mkey = NULL;
440
441 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
442 KEY_TRUSTED_PREFIX_LEN)) {
443 mkey = request_trusted_key(epayload->master_desc +
444 KEY_TRUSTED_PREFIX_LEN,
445 master_key, master_keylen);
446 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
447 KEY_USER_PREFIX_LEN)) {
448 mkey = request_user_key(epayload->master_desc +
449 KEY_USER_PREFIX_LEN,
450 master_key, master_keylen);
451 } else
452 goto out;
453
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200454 if (IS_ERR(mkey)) {
Mimi Zoharf4a0d5a2011-10-24 08:17:42 -0400455 int ret = PTR_ERR(mkey);
Mimi Zohar982e6172011-08-27 22:21:26 -0400456
457 if (ret == -ENOTSUPP)
458 pr_info("encrypted_key: key %s not supported",
459 epayload->master_desc);
460 else
461 pr_info("encrypted_key: key %s not found",
462 epayload->master_desc);
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200463 goto out;
464 }
465
466 dump_master_key(*master_key, *master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500467out:
468 return mkey;
469}
470
471/* Before returning data to userspace, encrypt decrypted data. */
472static int derived_key_encrypt(struct encrypted_key_payload *epayload,
473 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500474 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500475{
476 struct scatterlist sg_in[2];
477 struct scatterlist sg_out[1];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800478 struct crypto_skcipher *tfm;
479 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500480 unsigned int encrypted_datalen;
481 unsigned int padlen;
482 char pad[16];
483 int ret;
484
485 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
486 padlen = encrypted_datalen - epayload->decrypted_datalen;
487
Herbert Xuc3917fd2016-01-24 21:18:19 +0800488 req = init_skcipher_req(derived_key, derived_keylen);
489 ret = PTR_ERR(req);
490 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500491 goto out;
492 dump_decrypted_data(epayload);
493
494 memset(pad, 0, sizeof pad);
495 sg_init_table(sg_in, 2);
496 sg_set_buf(&sg_in[0], epayload->decrypted_data,
497 epayload->decrypted_datalen);
498 sg_set_buf(&sg_in[1], pad, padlen);
499
500 sg_init_table(sg_out, 1);
501 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
502
Herbert Xuc3917fd2016-01-24 21:18:19 +0800503 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen,
504 epayload->iv);
505 ret = crypto_skcipher_encrypt(req);
506 tfm = crypto_skcipher_reqtfm(req);
507 skcipher_request_free(req);
508 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500509 if (ret < 0)
510 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
511 else
512 dump_encrypted_data(epayload, encrypted_datalen);
513out:
514 return ret;
515}
516
517static int datablob_hmac_append(struct encrypted_key_payload *epayload,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500518 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500519{
520 u8 derived_key[HASH_SIZE];
521 u8 *digest;
522 int ret;
523
524 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
525 if (ret < 0)
526 goto out;
527
Roberto Sassu4e561d32011-06-27 13:45:42 +0200528 digest = epayload->format + epayload->datablob_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500529 ret = calc_hmac(digest, derived_key, sizeof derived_key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200530 epayload->format, epayload->datablob_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500531 if (!ret)
532 dump_hmac(NULL, digest, HASH_SIZE);
533out:
534 return ret;
535}
536
537/* verify HMAC before decrypting encrypted key */
538static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200539 const u8 *format, const u8 *master_key,
540 size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500541{
542 u8 derived_key[HASH_SIZE];
543 u8 digest[HASH_SIZE];
544 int ret;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200545 char *p;
546 unsigned short len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500547
548 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
549 if (ret < 0)
550 goto out;
551
Roberto Sassu4e561d32011-06-27 13:45:42 +0200552 len = epayload->datablob_len;
553 if (!format) {
554 p = epayload->master_desc;
555 len -= strlen(epayload->format) + 1;
556 } else
557 p = epayload->format;
558
559 ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500560 if (ret < 0)
561 goto out;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200562 ret = memcmp(digest, epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500563 sizeof digest);
564 if (ret) {
565 ret = -EINVAL;
566 dump_hmac("datablob",
Roberto Sassu4e561d32011-06-27 13:45:42 +0200567 epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500568 HASH_SIZE);
569 dump_hmac("calc", digest, HASH_SIZE);
570 }
571out:
572 return ret;
573}
574
575static int derived_key_decrypt(struct encrypted_key_payload *epayload,
576 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500577 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500578{
579 struct scatterlist sg_in[1];
580 struct scatterlist sg_out[2];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800581 struct crypto_skcipher *tfm;
582 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500583 unsigned int encrypted_datalen;
584 char pad[16];
585 int ret;
586
587 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800588 req = init_skcipher_req(derived_key, derived_keylen);
589 ret = PTR_ERR(req);
590 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500591 goto out;
592 dump_encrypted_data(epayload, encrypted_datalen);
593
594 memset(pad, 0, sizeof pad);
595 sg_init_table(sg_in, 1);
596 sg_init_table(sg_out, 2);
597 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
598 sg_set_buf(&sg_out[0], epayload->decrypted_data,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500599 epayload->decrypted_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500600 sg_set_buf(&sg_out[1], pad, sizeof pad);
601
Herbert Xuc3917fd2016-01-24 21:18:19 +0800602 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen,
603 epayload->iv);
604 ret = crypto_skcipher_decrypt(req);
605 tfm = crypto_skcipher_reqtfm(req);
606 skcipher_request_free(req);
607 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500608 if (ret < 0)
609 goto out;
610 dump_decrypted_data(epayload);
611out:
612 return ret;
613}
614
615/* Allocate memory for decrypted key and datablob. */
616static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200617 const char *format,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500618 const char *master_desc,
619 const char *datalen)
620{
621 struct encrypted_key_payload *epayload = NULL;
622 unsigned short datablob_len;
623 unsigned short decrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200624 unsigned short payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500625 unsigned int encrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200626 unsigned int format_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500627 long dlen;
628 int ret;
629
Jingoo Han29707b22014-02-05 15:13:14 +0900630 ret = kstrtol(datalen, 10, &dlen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500631 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
632 return ERR_PTR(-EINVAL);
633
Roberto Sassu4e561d32011-06-27 13:45:42 +0200634 format_len = (!format) ? strlen(key_format_default) : strlen(format);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500635 decrypted_datalen = dlen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200636 payload_datalen = decrypted_datalen;
Roberto Sassu79a73d12011-06-27 13:45:44 +0200637 if (format && !strcmp(format, key_format_ecryptfs)) {
638 if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
639 pr_err("encrypted_key: keylen for the ecryptfs format "
640 "must be equal to %d bytes\n",
641 ECRYPTFS_MAX_KEY_BYTES);
642 return ERR_PTR(-EINVAL);
643 }
644 decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
645 payload_datalen = sizeof(struct ecryptfs_auth_tok);
646 }
647
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500648 encrypted_datalen = roundup(decrypted_datalen, blksize);
649
Roberto Sassu4e561d32011-06-27 13:45:42 +0200650 datablob_len = format_len + 1 + strlen(master_desc) + 1
651 + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500652
Roberto Sassu4e561d32011-06-27 13:45:42 +0200653 ret = key_payload_reserve(key, payload_datalen + datablob_len
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500654 + HASH_SIZE + 1);
655 if (ret < 0)
656 return ERR_PTR(ret);
657
Roberto Sassu4e561d32011-06-27 13:45:42 +0200658 epayload = kzalloc(sizeof(*epayload) + payload_datalen +
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500659 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
660 if (!epayload)
661 return ERR_PTR(-ENOMEM);
662
Roberto Sassu4e561d32011-06-27 13:45:42 +0200663 epayload->payload_datalen = payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500664 epayload->decrypted_datalen = decrypted_datalen;
665 epayload->datablob_len = datablob_len;
666 return epayload;
667}
668
669static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200670 const char *format, const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500671{
672 struct key *mkey;
673 u8 derived_key[HASH_SIZE];
David Howells146aa8b2015-10-21 14:04:48 +0100674 const u8 *master_key;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500675 u8 *hmac;
Mimi Zohar1f350652010-12-13 16:53:12 -0500676 const char *hex_encoded_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500677 unsigned int encrypted_datalen;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500678 size_t master_keylen;
Mimi Zohar1f350652010-12-13 16:53:12 -0500679 size_t asciilen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500680 int ret;
681
682 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar1f350652010-12-13 16:53:12 -0500683 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
684 if (strlen(hex_encoded_iv) != asciilen)
685 return -EINVAL;
686
687 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400688 ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
689 if (ret < 0)
690 return -EINVAL;
691 ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
692 encrypted_datalen);
693 if (ret < 0)
694 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500695
Roberto Sassu4e561d32011-06-27 13:45:42 +0200696 hmac = epayload->format + epayload->datablob_len;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400697 ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
698 HASH_SIZE);
699 if (ret < 0)
700 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500701
702 mkey = request_master_key(epayload, &master_key, &master_keylen);
703 if (IS_ERR(mkey))
704 return PTR_ERR(mkey);
705
Roberto Sassu4e561d32011-06-27 13:45:42 +0200706 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500707 if (ret < 0) {
708 pr_err("encrypted_key: bad hmac (%d)\n", ret);
709 goto out;
710 }
711
712 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
713 if (ret < 0)
714 goto out;
715
716 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
717 if (ret < 0)
718 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
719out:
720 up_read(&mkey->sem);
721 key_put(mkey);
722 return ret;
723}
724
725static void __ekey_init(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200726 const char *format, const char *master_desc,
727 const char *datalen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500728{
Roberto Sassu4e561d32011-06-27 13:45:42 +0200729 unsigned int format_len;
730
731 format_len = (!format) ? strlen(key_format_default) : strlen(format);
732 epayload->format = epayload->payload_data + epayload->payload_datalen;
733 epayload->master_desc = epayload->format + format_len + 1;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500734 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
735 epayload->iv = epayload->datalen + strlen(datalen) + 1;
736 epayload->encrypted_data = epayload->iv + ivsize + 1;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200737 epayload->decrypted_data = epayload->payload_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500738
Roberto Sassu4e561d32011-06-27 13:45:42 +0200739 if (!format)
740 memcpy(epayload->format, key_format_default, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200741 else {
742 if (!strcmp(format, key_format_ecryptfs))
743 epayload->decrypted_data =
744 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
745
Roberto Sassu4e561d32011-06-27 13:45:42 +0200746 memcpy(epayload->format, format, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200747 }
748
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500749 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
750 memcpy(epayload->datalen, datalen, strlen(datalen));
751}
752
753/*
754 * encrypted_init - initialize an encrypted key
755 *
756 * For a new key, use a random number for both the iv and data
757 * itself. For an old key, decrypt the hex encoded data.
758 */
759static int encrypted_init(struct encrypted_key_payload *epayload,
Roberto Sassu79a73d12011-06-27 13:45:44 +0200760 const char *key_desc, const char *format,
761 const char *master_desc, const char *datalen,
762 const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500763{
764 int ret = 0;
765
Roberto Sassu79a73d12011-06-27 13:45:44 +0200766 if (format && !strcmp(format, key_format_ecryptfs)) {
767 ret = valid_ecryptfs_desc(key_desc);
768 if (ret < 0)
769 return ret;
770
771 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
772 key_desc);
773 }
774
Roberto Sassu4e561d32011-06-27 13:45:42 +0200775 __ekey_init(epayload, format, master_desc, datalen);
Mimi Zohar1f350652010-12-13 16:53:12 -0500776 if (!hex_encoded_iv) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500777 get_random_bytes(epayload->iv, ivsize);
778
779 get_random_bytes(epayload->decrypted_data,
780 epayload->decrypted_datalen);
781 } else
Roberto Sassu4e561d32011-06-27 13:45:42 +0200782 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500783 return ret;
784}
785
786/*
787 * encrypted_instantiate - instantiate an encrypted key
788 *
789 * Decrypt an existing encrypted datablob or create a new encrypted key
790 * based on a kernel random number.
791 *
792 * On success, return 0. Otherwise return errno.
793 */
David Howellscf7f6012012-09-13 13:06:29 +0100794static int encrypted_instantiate(struct key *key,
795 struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500796{
797 struct encrypted_key_payload *epayload = NULL;
798 char *datablob = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200799 const char *format = NULL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500800 char *master_desc = NULL;
801 char *decrypted_datalen = NULL;
802 char *hex_encoded_iv = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100803 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500804 int ret;
805
David Howellscf7f6012012-09-13 13:06:29 +0100806 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500807 return -EINVAL;
808
809 datablob = kmalloc(datalen + 1, GFP_KERNEL);
810 if (!datablob)
811 return -ENOMEM;
812 datablob[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100813 memcpy(datablob, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200814 ret = datablob_parse(datablob, &format, &master_desc,
815 &decrypted_datalen, &hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500816 if (ret < 0)
817 goto out;
818
Roberto Sassu4e561d32011-06-27 13:45:42 +0200819 epayload = encrypted_key_alloc(key, format, master_desc,
820 decrypted_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500821 if (IS_ERR(epayload)) {
822 ret = PTR_ERR(epayload);
823 goto out;
824 }
Roberto Sassu79a73d12011-06-27 13:45:44 +0200825 ret = encrypted_init(epayload, key->description, format, master_desc,
826 decrypted_datalen, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500827 if (ret < 0) {
828 kfree(epayload);
829 goto out;
830 }
831
Mimi Zoharb64cc5f2014-07-26 23:21:02 -0400832 rcu_assign_keypointer(key, epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500833out:
834 kfree(datablob);
835 return ret;
836}
837
838static void encrypted_rcu_free(struct rcu_head *rcu)
839{
840 struct encrypted_key_payload *epayload;
841
842 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
843 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
844 kfree(epayload);
845}
846
847/*
848 * encrypted_update - update the master key description
849 *
850 * Change the master key description for an existing encrypted key.
851 * The next read will return an encrypted datablob using the new
852 * master key description.
853 *
854 * On success, return 0. Otherwise return errno.
855 */
David Howellscf7f6012012-09-13 13:06:29 +0100856static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500857{
David Howells146aa8b2015-10-21 14:04:48 +0100858 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500859 struct encrypted_key_payload *new_epayload;
860 char *buf;
861 char *new_master_desc = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200862 const char *format = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100863 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500864 int ret = 0;
865
David Howells096fe9e2015-11-24 21:36:31 +0000866 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
867 return -ENOKEY;
David Howellscf7f6012012-09-13 13:06:29 +0100868 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500869 return -EINVAL;
870
871 buf = kmalloc(datalen + 1, GFP_KERNEL);
872 if (!buf)
873 return -ENOMEM;
874
875 buf[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100876 memcpy(buf, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200877 ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500878 if (ret < 0)
879 goto out;
880
881 ret = valid_master_desc(new_master_desc, epayload->master_desc);
882 if (ret < 0)
883 goto out;
884
Roberto Sassu4e561d32011-06-27 13:45:42 +0200885 new_epayload = encrypted_key_alloc(key, epayload->format,
886 new_master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500887 if (IS_ERR(new_epayload)) {
888 ret = PTR_ERR(new_epayload);
889 goto out;
890 }
891
Roberto Sassu4e561d32011-06-27 13:45:42 +0200892 __ekey_init(new_epayload, epayload->format, new_master_desc,
893 epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500894
895 memcpy(new_epayload->iv, epayload->iv, ivsize);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200896 memcpy(new_epayload->payload_data, epayload->payload_data,
897 epayload->payload_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500898
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000899 rcu_assign_keypointer(key, new_epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500900 call_rcu(&epayload->rcu, encrypted_rcu_free);
901out:
902 kfree(buf);
903 return ret;
904}
905
906/*
907 * encrypted_read - format and copy the encrypted data to userspace
908 *
909 * The resulting datablob format is:
910 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
911 *
912 * On success, return to userspace the encrypted key datablob size.
913 */
914static long encrypted_read(const struct key *key, char __user *buffer,
915 size_t buflen)
916{
917 struct encrypted_key_payload *epayload;
918 struct key *mkey;
David Howells146aa8b2015-10-21 14:04:48 +0100919 const u8 *master_key;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500920 size_t master_keylen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500921 char derived_key[HASH_SIZE];
922 char *ascii_buf;
923 size_t asciiblob_len;
924 int ret;
925
David Howells633e8042011-03-07 15:05:51 +0000926 epayload = rcu_dereference_key(key);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500927
928 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
929 asciiblob_len = epayload->datablob_len + ivsize + 1
930 + roundup(epayload->decrypted_datalen, blksize)
931 + (HASH_SIZE * 2);
932
933 if (!buffer || buflen < asciiblob_len)
934 return asciiblob_len;
935
936 mkey = request_master_key(epayload, &master_key, &master_keylen);
937 if (IS_ERR(mkey))
938 return PTR_ERR(mkey);
939
940 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
941 if (ret < 0)
942 goto out;
943
944 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
945 if (ret < 0)
946 goto out;
947
948 ret = datablob_hmac_append(epayload, master_key, master_keylen);
949 if (ret < 0)
950 goto out;
951
952 ascii_buf = datablob_format(epayload, asciiblob_len);
953 if (!ascii_buf) {
954 ret = -ENOMEM;
955 goto out;
956 }
957
958 up_read(&mkey->sem);
959 key_put(mkey);
960
961 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
962 ret = -EFAULT;
963 kfree(ascii_buf);
964
965 return asciiblob_len;
966out:
967 up_read(&mkey->sem);
968 key_put(mkey);
969 return ret;
970}
971
972/*
973 * encrypted_destroy - before freeing the key, clear the decrypted data
974 *
975 * Before freeing the key, clear the memory containing the decrypted
976 * key data.
977 */
978static void encrypted_destroy(struct key *key)
979{
David Howells146aa8b2015-10-21 14:04:48 +0100980 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500981
982 if (!epayload)
983 return;
984
985 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
David Howells146aa8b2015-10-21 14:04:48 +0100986 kfree(key->payload.data[0]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500987}
988
989struct key_type key_type_encrypted = {
990 .name = "encrypted",
991 .instantiate = encrypted_instantiate,
992 .update = encrypted_update,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500993 .destroy = encrypted_destroy,
994 .describe = user_describe,
995 .read = encrypted_read,
996};
997EXPORT_SYMBOL_GPL(key_type_encrypted);
998
999static void encrypted_shash_release(void)
1000{
1001 if (hashalg)
1002 crypto_free_shash(hashalg);
1003 if (hmacalg)
1004 crypto_free_shash(hmacalg);
1005}
1006
1007static int __init encrypted_shash_alloc(void)
1008{
1009 int ret;
1010
1011 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1012 if (IS_ERR(hmacalg)) {
1013 pr_info("encrypted_key: could not allocate crypto %s\n",
1014 hmac_alg);
1015 return PTR_ERR(hmacalg);
1016 }
1017
1018 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1019 if (IS_ERR(hashalg)) {
1020 pr_info("encrypted_key: could not allocate crypto %s\n",
1021 hash_alg);
1022 ret = PTR_ERR(hashalg);
1023 goto hashalg_fail;
1024 }
1025
1026 return 0;
1027
1028hashalg_fail:
1029 crypto_free_shash(hmacalg);
1030 return ret;
1031}
1032
1033static int __init init_encrypted(void)
1034{
1035 int ret;
1036
1037 ret = encrypted_shash_alloc();
1038 if (ret < 0)
1039 return ret;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001040 ret = aes_get_sizes();
1041 if (ret < 0)
1042 goto out;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001043 ret = register_key_type(&key_type_encrypted);
1044 if (ret < 0)
1045 goto out;
Takashi Iwaib26bdde2014-12-04 18:25:19 +01001046 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001047out:
1048 encrypted_shash_release();
1049 return ret;
Mimi Zoharb9703442011-01-18 09:07:12 -05001050
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001051}
1052
1053static void __exit cleanup_encrypted(void)
1054{
1055 encrypted_shash_release();
1056 unregister_key_type(&key_type_encrypted);
1057}
1058
1059late_initcall(init_encrypted);
1060module_exit(cleanup_encrypted);
1061
1062MODULE_LICENSE("GPL");