blob: 69855ba0d3b3fcba2190844ae5665ead90dd38f9 [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 *
Kees Cook5395d312017-05-13 04:51:53 -070014 * See Documentation/security/keys/trusted-encrypted.rst
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>
Eric Biggers0f534e42017-06-08 14:48:32 +010033#include <crypto/algapi.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050034#include <crypto/hash.h>
35#include <crypto/sha.h>
Herbert Xuc3917fd2016-01-24 21:18:19 +080036#include <crypto/skcipher.h>
Mimi Zohar7e70cb42010-11-23 18:55:35 -050037
Mimi Zoharb9703442011-01-18 09:07:12 -050038#include "encrypted.h"
Roberto Sassu79a73d12011-06-27 13:45:44 +020039#include "ecryptfs_format.h"
Mimi Zohar7e70cb42010-11-23 18:55:35 -050040
Mimi Zohar3b1826c2010-12-13 16:53:13 -050041static const char KEY_TRUSTED_PREFIX[] = "trusted:";
42static const char KEY_USER_PREFIX[] = "user:";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050043static const char hash_alg[] = "sha256";
44static const char hmac_alg[] = "hmac(sha256)";
45static const char blkcipher_alg[] = "cbc(aes)";
Roberto Sassu4e561d32011-06-27 13:45:42 +020046static const char key_format_default[] = "default";
Roberto Sassu79a73d12011-06-27 13:45:44 +020047static const char key_format_ecryptfs[] = "ecryptfs";
Mimi Zohar7e70cb42010-11-23 18:55:35 -050048static unsigned int ivsize;
49static int blksize;
50
Mimi Zohar3b1826c2010-12-13 16:53:13 -050051#define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
52#define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
Roberto Sassu79a73d12011-06-27 13:45:44 +020053#define KEY_ECRYPTFS_DESC_LEN 16
Mimi Zohar3b1826c2010-12-13 16:53:13 -050054#define HASH_SIZE SHA256_DIGEST_SIZE
55#define MAX_DATA_SIZE 4096
56#define MIN_DATA_SIZE 20
57
Eric Biggers64d107d2017-06-08 14:48:25 +010058static struct crypto_shash *hash_tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -050059
60enum {
61 Opt_err = -1, Opt_new, Opt_load, Opt_update
62};
63
Roberto Sassu4e561d32011-06-27 13:45:42 +020064enum {
Roberto Sassu79a73d12011-06-27 13:45:44 +020065 Opt_error = -1, Opt_default, Opt_ecryptfs
Roberto Sassu4e561d32011-06-27 13:45:42 +020066};
67
68static const match_table_t key_format_tokens = {
69 {Opt_default, "default"},
Roberto Sassu79a73d12011-06-27 13:45:44 +020070 {Opt_ecryptfs, "ecryptfs"},
Roberto Sassu4e561d32011-06-27 13:45:42 +020071 {Opt_error, NULL}
72};
73
Mimi Zohar7e70cb42010-11-23 18:55:35 -050074static const match_table_t key_tokens = {
75 {Opt_new, "new"},
76 {Opt_load, "load"},
77 {Opt_update, "update"},
78 {Opt_err, NULL}
79};
80
81static int aes_get_sizes(void)
82{
Herbert Xuc3917fd2016-01-24 21:18:19 +080083 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -050084
Herbert Xuc3917fd2016-01-24 21:18:19 +080085 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050086 if (IS_ERR(tfm)) {
87 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
88 PTR_ERR(tfm));
89 return PTR_ERR(tfm);
90 }
Herbert Xuc3917fd2016-01-24 21:18:19 +080091 ivsize = crypto_skcipher_ivsize(tfm);
92 blksize = crypto_skcipher_blocksize(tfm);
93 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -050094 return 0;
95}
96
97/*
Roberto Sassu79a73d12011-06-27 13:45:44 +020098 * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
99 *
100 * The description of a encrypted key with format 'ecryptfs' must contain
101 * exactly 16 hexadecimal characters.
102 *
103 */
104static int valid_ecryptfs_desc(const char *ecryptfs_desc)
105{
106 int i;
107
108 if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
109 pr_err("encrypted_key: key description must be %d hexadecimal "
110 "characters long\n", KEY_ECRYPTFS_DESC_LEN);
111 return -EINVAL;
112 }
113
114 for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
115 if (!isxdigit(ecryptfs_desc[i])) {
116 pr_err("encrypted_key: key description must contain "
117 "only hexadecimal characters\n");
118 return -EINVAL;
119 }
120 }
121
122 return 0;
123}
124
125/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500126 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
127 *
Roberto Sassu08fa2aa2011-06-27 13:45:40 +0200128 * key-type:= "trusted:" | "user:"
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500129 * desc:= master-key description
130 *
131 * Verify that 'key-type' is valid and that 'desc' exists. On key update,
132 * only the master key description is permitted to change, not the key-type.
133 * The key-type remains constant.
134 *
135 * On success returns 0, otherwise -EINVAL.
136 */
137static int valid_master_desc(const char *new_desc, const char *orig_desc)
138{
Eric Biggers794b4bc2017-06-08 14:48:18 +0100139 int prefix_len;
140
141 if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
142 prefix_len = KEY_TRUSTED_PREFIX_LEN;
143 else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
144 prefix_len = KEY_USER_PREFIX_LEN;
145 else
146 return -EINVAL;
147
148 if (!new_desc[prefix_len])
149 return -EINVAL;
150
151 if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
152 return -EINVAL;
153
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500154 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500155}
156
157/*
158 * datablob_parse - parse the keyctl data
159 *
160 * datablob format:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200161 * new [<format>] <master-key name> <decrypted data length>
162 * load [<format>] <master-key name> <decrypted data length>
163 * <encrypted iv + data>
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500164 * update <new-master-key name>
165 *
166 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
167 * which is null terminated.
168 *
169 * On success returns 0, otherwise -EINVAL.
170 */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200171static int datablob_parse(char *datablob, const char **format,
172 char **master_desc, char **decrypted_datalen,
173 char **hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500174{
175 substring_t args[MAX_OPT_ARGS];
176 int ret = -EINVAL;
177 int key_cmd;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200178 int key_format;
179 char *p, *keyword;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500180
Roberto Sassu7103dff2011-06-27 13:45:41 +0200181 keyword = strsep(&datablob, " \t");
182 if (!keyword) {
183 pr_info("encrypted_key: insufficient parameters specified\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500184 return ret;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200185 }
186 key_cmd = match_token(keyword, key_tokens, args);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500187
Roberto Sassu79a73d12011-06-27 13:45:44 +0200188 /* Get optional format: default | ecryptfs */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200189 p = strsep(&datablob, " \t");
190 if (!p) {
191 pr_err("encrypted_key: insufficient parameters specified\n");
192 return ret;
193 }
194
195 key_format = match_token(p, key_format_tokens, args);
196 switch (key_format) {
Roberto Sassu79a73d12011-06-27 13:45:44 +0200197 case Opt_ecryptfs:
Roberto Sassu4e561d32011-06-27 13:45:42 +0200198 case Opt_default:
199 *format = p;
200 *master_desc = strsep(&datablob, " \t");
201 break;
202 case Opt_error:
203 *master_desc = p;
204 break;
205 }
206
Roberto Sassu7103dff2011-06-27 13:45:41 +0200207 if (!*master_desc) {
208 pr_info("encrypted_key: master key parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500209 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200210 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500211
Roberto Sassu7103dff2011-06-27 13:45:41 +0200212 if (valid_master_desc(*master_desc, NULL) < 0) {
213 pr_info("encrypted_key: master key parameter \'%s\' "
214 "is invalid\n", *master_desc);
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
218 if (decrypted_datalen) {
219 *decrypted_datalen = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200220 if (!*decrypted_datalen) {
221 pr_info("encrypted_key: keylen parameter is missing\n");
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500222 goto out;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200223 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500224 }
225
226 switch (key_cmd) {
227 case Opt_new:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200228 if (!decrypted_datalen) {
229 pr_info("encrypted_key: keyword \'%s\' not allowed "
230 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500231 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200232 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500233 ret = 0;
234 break;
235 case Opt_load:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200236 if (!decrypted_datalen) {
237 pr_info("encrypted_key: keyword \'%s\' not allowed "
238 "when called from .update method\n", keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500239 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200240 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500241 *hex_encoded_iv = strsep(&datablob, " \t");
Roberto Sassu7103dff2011-06-27 13:45:41 +0200242 if (!*hex_encoded_iv) {
243 pr_info("encrypted_key: hex blob is missing\n");
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 ret = 0;
247 break;
248 case Opt_update:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200249 if (decrypted_datalen) {
250 pr_info("encrypted_key: keyword \'%s\' not allowed "
251 "when called from .instantiate method\n",
252 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500253 break;
Roberto Sassu7103dff2011-06-27 13:45:41 +0200254 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500255 ret = 0;
256 break;
257 case Opt_err:
Roberto Sassu7103dff2011-06-27 13:45:41 +0200258 pr_info("encrypted_key: keyword \'%s\' not recognized\n",
259 keyword);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500260 break;
261 }
262out:
263 return ret;
264}
265
266/*
267 * datablob_format - format as an ascii string, before copying to userspace
268 */
269static char *datablob_format(struct encrypted_key_payload *epayload,
270 size_t asciiblob_len)
271{
272 char *ascii_buf, *bufp;
273 u8 *iv = epayload->iv;
274 int len;
275 int i;
276
277 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
278 if (!ascii_buf)
279 goto out;
280
281 ascii_buf[asciiblob_len] = '\0';
282
283 /* copy datablob master_desc and datalen strings */
Roberto Sassu4e561d32011-06-27 13:45:42 +0200284 len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
285 epayload->master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500286
287 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
288 bufp = &ascii_buf[len];
289 for (i = 0; i < (asciiblob_len - len) / 2; i++)
Andy Shevchenko02473112011-10-31 17:12:55 -0700290 bufp = hex_byte_pack(bufp, iv[i]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500291out:
292 return ascii_buf;
293}
294
295/*
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500296 * request_user_key - request the user key
297 *
298 * Use a user provided key to encrypt/decrypt an encrypted-key.
299 */
David Howells146aa8b2015-10-21 14:04:48 +0100300static struct key *request_user_key(const char *master_desc, const u8 **master_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500301 size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500302{
David Howells146aa8b2015-10-21 14:04:48 +0100303 const struct user_key_payload *upayload;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500304 struct key *ukey;
305
306 ukey = request_key(&key_type_user, master_desc, NULL);
307 if (IS_ERR(ukey))
308 goto error;
309
310 down_read(&ukey->sem);
David Howells0837e492017-03-01 15:11:23 +0000311 upayload = user_key_payload_locked(ukey);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500312 *master_key = upayload->data;
313 *master_keylen = upayload->datalen;
314error:
315 return ukey;
316}
317
Eric Biggers64d107d2017-06-08 14:48:25 +0100318static int calc_hash(struct crypto_shash *tfm, u8 *digest,
319 const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500320{
Eric Biggers64d107d2017-06-08 14:48:25 +0100321 SHASH_DESC_ON_STACK(desc, tfm);
322 int err;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500323
Eric Biggers64d107d2017-06-08 14:48:25 +0100324 desc->tfm = tfm;
325 desc->flags = 0;
326
327 err = crypto_shash_digest(desc, buf, buflen, digest);
328 shash_desc_zero(desc);
329 return err;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500330}
331
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500332static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
333 const u8 *buf, unsigned int buflen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500334{
Eric Biggers64d107d2017-06-08 14:48:25 +0100335 struct crypto_shash *tfm;
336 int err;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500337
Eric Biggers64d107d2017-06-08 14:48:25 +0100338 tfm = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
339 if (IS_ERR(tfm)) {
340 pr_err("encrypted_key: can't alloc %s transform: %ld\n",
341 hmac_alg, PTR_ERR(tfm));
342 return PTR_ERR(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500343 }
344
Eric Biggers64d107d2017-06-08 14:48:25 +0100345 err = crypto_shash_setkey(tfm, key, keylen);
346 if (!err)
347 err = calc_hash(tfm, digest, buf, buflen);
348 crypto_free_shash(tfm);
349 return err;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500350}
351
352enum derived_key_type { ENC_KEY, AUTH_KEY };
353
354/* Derive authentication/encryption key from trusted key */
355static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500356 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500357{
358 u8 *derived_buf;
359 unsigned int derived_buf_len;
360 int ret;
361
362 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
363 if (derived_buf_len < HASH_SIZE)
364 derived_buf_len = HASH_SIZE;
365
366 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
Markus Elfring41f1c532017-06-08 14:47:56 +0100367 if (!derived_buf)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500368 return -ENOMEM;
Markus Elfring41f1c532017-06-08 14:47:56 +0100369
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500370 if (key_type)
371 strcpy(derived_buf, "AUTH_KEY");
372 else
373 strcpy(derived_buf, "ENC_KEY");
374
375 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
376 master_keylen);
Eric Biggers64d107d2017-06-08 14:48:25 +0100377 ret = calc_hash(hash_tfm, derived_key, derived_buf, derived_buf_len);
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100378 kzfree(derived_buf);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500379 return ret;
380}
381
Herbert Xuc3917fd2016-01-24 21:18:19 +0800382static struct skcipher_request *init_skcipher_req(const u8 *key,
383 unsigned int key_len)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500384{
Herbert Xuc3917fd2016-01-24 21:18:19 +0800385 struct skcipher_request *req;
386 struct crypto_skcipher *tfm;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500387 int ret;
388
Herbert Xuc3917fd2016-01-24 21:18:19 +0800389 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
390 if (IS_ERR(tfm)) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500391 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
Herbert Xuc3917fd2016-01-24 21:18:19 +0800392 blkcipher_alg, PTR_ERR(tfm));
393 return ERR_CAST(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500394 }
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500395
Herbert Xuc3917fd2016-01-24 21:18:19 +0800396 ret = crypto_skcipher_setkey(tfm, key, key_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500397 if (ret < 0) {
398 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800399 crypto_free_skcipher(tfm);
400 return ERR_PTR(ret);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500401 }
Herbert Xuc3917fd2016-01-24 21:18:19 +0800402
403 req = skcipher_request_alloc(tfm, GFP_KERNEL);
404 if (!req) {
405 pr_err("encrypted_key: failed to allocate request for %s\n",
406 blkcipher_alg);
407 crypto_free_skcipher(tfm);
408 return ERR_PTR(-ENOMEM);
409 }
410
411 skcipher_request_set_callback(req, 0, NULL, NULL);
412 return req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500413}
414
415static struct key *request_master_key(struct encrypted_key_payload *epayload,
David Howells146aa8b2015-10-21 14:04:48 +0100416 const u8 **master_key, size_t *master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500417{
Dan Carpenter57cb17e2017-02-09 17:17:52 +0000418 struct key *mkey = ERR_PTR(-EINVAL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500419
420 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
421 KEY_TRUSTED_PREFIX_LEN)) {
422 mkey = request_trusted_key(epayload->master_desc +
423 KEY_TRUSTED_PREFIX_LEN,
424 master_key, master_keylen);
425 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
426 KEY_USER_PREFIX_LEN)) {
427 mkey = request_user_key(epayload->master_desc +
428 KEY_USER_PREFIX_LEN,
429 master_key, master_keylen);
430 } else
431 goto out;
432
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200433 if (IS_ERR(mkey)) {
Mimi Zoharf4a0d5a2011-10-24 08:17:42 -0400434 int ret = PTR_ERR(mkey);
Mimi Zohar982e6172011-08-27 22:21:26 -0400435
436 if (ret == -ENOTSUPP)
437 pr_info("encrypted_key: key %s not supported",
438 epayload->master_desc);
439 else
440 pr_info("encrypted_key: key %s not found",
441 epayload->master_desc);
Roberto Sassuf91c2c52011-06-27 13:45:39 +0200442 goto out;
443 }
444
445 dump_master_key(*master_key, *master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500446out:
447 return mkey;
448}
449
450/* Before returning data to userspace, encrypt decrypted data. */
451static int derived_key_encrypt(struct encrypted_key_payload *epayload,
452 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500453 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500454{
455 struct scatterlist sg_in[2];
456 struct scatterlist sg_out[1];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800457 struct crypto_skcipher *tfm;
458 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500459 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800460 u8 iv[AES_BLOCK_SIZE];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500461 int ret;
462
463 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500464
Herbert Xuc3917fd2016-01-24 21:18:19 +0800465 req = init_skcipher_req(derived_key, derived_keylen);
466 ret = PTR_ERR(req);
467 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500468 goto out;
469 dump_decrypted_data(epayload);
470
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500471 sg_init_table(sg_in, 2);
472 sg_set_buf(&sg_in[0], epayload->decrypted_data,
473 epayload->decrypted_datalen);
Eric Biggerse9ff56a2017-06-08 14:48:10 +0100474 sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500475
476 sg_init_table(sg_out, 1);
477 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
478
Herbert Xu456bee92016-09-20 20:35:55 +0800479 memcpy(iv, epayload->iv, sizeof(iv));
480 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800481 ret = crypto_skcipher_encrypt(req);
482 tfm = crypto_skcipher_reqtfm(req);
483 skcipher_request_free(req);
484 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500485 if (ret < 0)
486 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
487 else
488 dump_encrypted_data(epayload, encrypted_datalen);
489out:
490 return ret;
491}
492
493static int datablob_hmac_append(struct encrypted_key_payload *epayload,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500494 const u8 *master_key, size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500495{
496 u8 derived_key[HASH_SIZE];
497 u8 *digest;
498 int ret;
499
500 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
501 if (ret < 0)
502 goto out;
503
Roberto Sassu4e561d32011-06-27 13:45:42 +0200504 digest = epayload->format + epayload->datablob_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500505 ret = calc_hmac(digest, derived_key, sizeof derived_key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200506 epayload->format, epayload->datablob_len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500507 if (!ret)
508 dump_hmac(NULL, digest, HASH_SIZE);
509out:
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100510 memzero_explicit(derived_key, sizeof(derived_key));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500511 return ret;
512}
513
514/* verify HMAC before decrypting encrypted key */
515static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200516 const u8 *format, const u8 *master_key,
517 size_t master_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500518{
519 u8 derived_key[HASH_SIZE];
520 u8 digest[HASH_SIZE];
521 int ret;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200522 char *p;
523 unsigned short len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500524
525 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
526 if (ret < 0)
527 goto out;
528
Roberto Sassu4e561d32011-06-27 13:45:42 +0200529 len = epayload->datablob_len;
530 if (!format) {
531 p = epayload->master_desc;
532 len -= strlen(epayload->format) + 1;
533 } else
534 p = epayload->format;
535
536 ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500537 if (ret < 0)
538 goto out;
Eric Biggers0f534e42017-06-08 14:48:32 +0100539 ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
540 sizeof(digest));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500541 if (ret) {
542 ret = -EINVAL;
543 dump_hmac("datablob",
Roberto Sassu4e561d32011-06-27 13:45:42 +0200544 epayload->format + epayload->datablob_len,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500545 HASH_SIZE);
546 dump_hmac("calc", digest, HASH_SIZE);
547 }
548out:
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100549 memzero_explicit(derived_key, sizeof(derived_key));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500550 return ret;
551}
552
553static int derived_key_decrypt(struct encrypted_key_payload *epayload,
554 const u8 *derived_key,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500555 unsigned int derived_keylen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500556{
557 struct scatterlist sg_in[1];
558 struct scatterlist sg_out[2];
Herbert Xuc3917fd2016-01-24 21:18:19 +0800559 struct crypto_skcipher *tfm;
560 struct skcipher_request *req;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500561 unsigned int encrypted_datalen;
Herbert Xu456bee92016-09-20 20:35:55 +0800562 u8 iv[AES_BLOCK_SIZE];
Eric Biggerse9ff56a2017-06-08 14:48:10 +0100563 u8 *pad;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500564 int ret;
565
Eric Biggerse9ff56a2017-06-08 14:48:10 +0100566 /* Throwaway buffer to hold the unused zero padding at the end */
567 pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
568 if (!pad)
569 return -ENOMEM;
570
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500571 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800572 req = init_skcipher_req(derived_key, derived_keylen);
573 ret = PTR_ERR(req);
574 if (IS_ERR(req))
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500575 goto out;
576 dump_encrypted_data(epayload, encrypted_datalen);
577
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500578 sg_init_table(sg_in, 1);
579 sg_init_table(sg_out, 2);
580 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
581 sg_set_buf(&sg_out[0], epayload->decrypted_data,
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500582 epayload->decrypted_datalen);
Eric Biggerse9ff56a2017-06-08 14:48:10 +0100583 sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500584
Herbert Xu456bee92016-09-20 20:35:55 +0800585 memcpy(iv, epayload->iv, sizeof(iv));
586 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
Herbert Xuc3917fd2016-01-24 21:18:19 +0800587 ret = crypto_skcipher_decrypt(req);
588 tfm = crypto_skcipher_reqtfm(req);
589 skcipher_request_free(req);
590 crypto_free_skcipher(tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500591 if (ret < 0)
592 goto out;
593 dump_decrypted_data(epayload);
594out:
Eric Biggerse9ff56a2017-06-08 14:48:10 +0100595 kfree(pad);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500596 return ret;
597}
598
599/* Allocate memory for decrypted key and datablob. */
600static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200601 const char *format,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500602 const char *master_desc,
603 const char *datalen)
604{
605 struct encrypted_key_payload *epayload = NULL;
606 unsigned short datablob_len;
607 unsigned short decrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200608 unsigned short payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500609 unsigned int encrypted_datalen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200610 unsigned int format_len;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500611 long dlen;
612 int ret;
613
Jingoo Han29707b22014-02-05 15:13:14 +0900614 ret = kstrtol(datalen, 10, &dlen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500615 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
616 return ERR_PTR(-EINVAL);
617
Roberto Sassu4e561d32011-06-27 13:45:42 +0200618 format_len = (!format) ? strlen(key_format_default) : strlen(format);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500619 decrypted_datalen = dlen;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200620 payload_datalen = decrypted_datalen;
Roberto Sassu79a73d12011-06-27 13:45:44 +0200621 if (format && !strcmp(format, key_format_ecryptfs)) {
622 if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
623 pr_err("encrypted_key: keylen for the ecryptfs format "
624 "must be equal to %d bytes\n",
625 ECRYPTFS_MAX_KEY_BYTES);
626 return ERR_PTR(-EINVAL);
627 }
628 decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
629 payload_datalen = sizeof(struct ecryptfs_auth_tok);
630 }
631
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500632 encrypted_datalen = roundup(decrypted_datalen, blksize);
633
Roberto Sassu4e561d32011-06-27 13:45:42 +0200634 datablob_len = format_len + 1 + strlen(master_desc) + 1
635 + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500636
Roberto Sassu4e561d32011-06-27 13:45:42 +0200637 ret = key_payload_reserve(key, payload_datalen + datablob_len
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500638 + HASH_SIZE + 1);
639 if (ret < 0)
640 return ERR_PTR(ret);
641
Roberto Sassu4e561d32011-06-27 13:45:42 +0200642 epayload = kzalloc(sizeof(*epayload) + payload_datalen +
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500643 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
644 if (!epayload)
645 return ERR_PTR(-ENOMEM);
646
Roberto Sassu4e561d32011-06-27 13:45:42 +0200647 epayload->payload_datalen = payload_datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500648 epayload->decrypted_datalen = decrypted_datalen;
649 epayload->datablob_len = datablob_len;
650 return epayload;
651}
652
653static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200654 const char *format, const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500655{
656 struct key *mkey;
657 u8 derived_key[HASH_SIZE];
David Howells146aa8b2015-10-21 14:04:48 +0100658 const u8 *master_key;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500659 u8 *hmac;
Mimi Zohar1f350652010-12-13 16:53:12 -0500660 const char *hex_encoded_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500661 unsigned int encrypted_datalen;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500662 size_t master_keylen;
Mimi Zohar1f350652010-12-13 16:53:12 -0500663 size_t asciilen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500664 int ret;
665
666 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
Mimi Zohar1f350652010-12-13 16:53:12 -0500667 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
668 if (strlen(hex_encoded_iv) != asciilen)
669 return -EINVAL;
670
671 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400672 ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
673 if (ret < 0)
674 return -EINVAL;
675 ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
676 encrypted_datalen);
677 if (ret < 0)
678 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500679
Roberto Sassu4e561d32011-06-27 13:45:42 +0200680 hmac = epayload->format + epayload->datablob_len;
Mimi Zohar2b3ff632011-09-20 11:23:55 -0400681 ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
682 HASH_SIZE);
683 if (ret < 0)
684 return -EINVAL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500685
686 mkey = request_master_key(epayload, &master_key, &master_keylen);
687 if (IS_ERR(mkey))
688 return PTR_ERR(mkey);
689
Roberto Sassu4e561d32011-06-27 13:45:42 +0200690 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500691 if (ret < 0) {
692 pr_err("encrypted_key: bad hmac (%d)\n", ret);
693 goto out;
694 }
695
696 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
697 if (ret < 0)
698 goto out;
699
700 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
701 if (ret < 0)
702 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
703out:
704 up_read(&mkey->sem);
705 key_put(mkey);
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100706 memzero_explicit(derived_key, sizeof(derived_key));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500707 return ret;
708}
709
710static void __ekey_init(struct encrypted_key_payload *epayload,
Roberto Sassu4e561d32011-06-27 13:45:42 +0200711 const char *format, const char *master_desc,
712 const char *datalen)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500713{
Roberto Sassu4e561d32011-06-27 13:45:42 +0200714 unsigned int format_len;
715
716 format_len = (!format) ? strlen(key_format_default) : strlen(format);
717 epayload->format = epayload->payload_data + epayload->payload_datalen;
718 epayload->master_desc = epayload->format + format_len + 1;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500719 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
720 epayload->iv = epayload->datalen + strlen(datalen) + 1;
721 epayload->encrypted_data = epayload->iv + ivsize + 1;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200722 epayload->decrypted_data = epayload->payload_data;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500723
Roberto Sassu4e561d32011-06-27 13:45:42 +0200724 if (!format)
725 memcpy(epayload->format, key_format_default, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200726 else {
727 if (!strcmp(format, key_format_ecryptfs))
728 epayload->decrypted_data =
729 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
730
Roberto Sassu4e561d32011-06-27 13:45:42 +0200731 memcpy(epayload->format, format, format_len);
Roberto Sassu79a73d12011-06-27 13:45:44 +0200732 }
733
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500734 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
735 memcpy(epayload->datalen, datalen, strlen(datalen));
736}
737
738/*
739 * encrypted_init - initialize an encrypted key
740 *
741 * For a new key, use a random number for both the iv and data
742 * itself. For an old key, decrypt the hex encoded data.
743 */
744static int encrypted_init(struct encrypted_key_payload *epayload,
Roberto Sassu79a73d12011-06-27 13:45:44 +0200745 const char *key_desc, const char *format,
746 const char *master_desc, const char *datalen,
747 const char *hex_encoded_iv)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500748{
749 int ret = 0;
750
Roberto Sassu79a73d12011-06-27 13:45:44 +0200751 if (format && !strcmp(format, key_format_ecryptfs)) {
752 ret = valid_ecryptfs_desc(key_desc);
753 if (ret < 0)
754 return ret;
755
756 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
757 key_desc);
758 }
759
Roberto Sassu4e561d32011-06-27 13:45:42 +0200760 __ekey_init(epayload, format, master_desc, datalen);
Mimi Zohar1f350652010-12-13 16:53:12 -0500761 if (!hex_encoded_iv) {
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500762 get_random_bytes(epayload->iv, ivsize);
763
764 get_random_bytes(epayload->decrypted_data,
765 epayload->decrypted_datalen);
766 } else
Roberto Sassu4e561d32011-06-27 13:45:42 +0200767 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500768 return ret;
769}
770
771/*
772 * encrypted_instantiate - instantiate an encrypted key
773 *
774 * Decrypt an existing encrypted datablob or create a new encrypted key
775 * based on a kernel random number.
776 *
777 * On success, return 0. Otherwise return errno.
778 */
David Howellscf7f6012012-09-13 13:06:29 +0100779static int encrypted_instantiate(struct key *key,
780 struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500781{
782 struct encrypted_key_payload *epayload = NULL;
783 char *datablob = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200784 const char *format = NULL;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500785 char *master_desc = NULL;
786 char *decrypted_datalen = NULL;
787 char *hex_encoded_iv = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100788 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500789 int ret;
790
David Howellscf7f6012012-09-13 13:06:29 +0100791 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500792 return -EINVAL;
793
794 datablob = kmalloc(datalen + 1, GFP_KERNEL);
795 if (!datablob)
796 return -ENOMEM;
797 datablob[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100798 memcpy(datablob, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200799 ret = datablob_parse(datablob, &format, &master_desc,
800 &decrypted_datalen, &hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500801 if (ret < 0)
802 goto out;
803
Roberto Sassu4e561d32011-06-27 13:45:42 +0200804 epayload = encrypted_key_alloc(key, format, master_desc,
805 decrypted_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500806 if (IS_ERR(epayload)) {
807 ret = PTR_ERR(epayload);
808 goto out;
809 }
Roberto Sassu79a73d12011-06-27 13:45:44 +0200810 ret = encrypted_init(epayload, key->description, format, master_desc,
811 decrypted_datalen, hex_encoded_iv);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500812 if (ret < 0) {
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100813 kzfree(epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500814 goto out;
815 }
816
Mimi Zoharb64cc5f2014-07-26 23:21:02 -0400817 rcu_assign_keypointer(key, epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500818out:
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100819 kzfree(datablob);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500820 return ret;
821}
822
823static void encrypted_rcu_free(struct rcu_head *rcu)
824{
825 struct encrypted_key_payload *epayload;
826
827 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100828 kzfree(epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500829}
830
831/*
832 * encrypted_update - update the master key description
833 *
834 * Change the master key description for an existing encrypted key.
835 * The next read will return an encrypted datablob using the new
836 * master key description.
837 *
838 * On success, return 0. Otherwise return errno.
839 */
David Howellscf7f6012012-09-13 13:06:29 +0100840static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500841{
David Howells146aa8b2015-10-21 14:04:48 +0100842 struct encrypted_key_payload *epayload = key->payload.data[0];
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500843 struct encrypted_key_payload *new_epayload;
844 char *buf;
845 char *new_master_desc = NULL;
Roberto Sassu4e561d32011-06-27 13:45:42 +0200846 const char *format = NULL;
David Howellscf7f6012012-09-13 13:06:29 +0100847 size_t datalen = prep->datalen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500848 int ret = 0;
849
David Howells096fe9e2015-11-24 21:36:31 +0000850 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
851 return -ENOKEY;
David Howellscf7f6012012-09-13 13:06:29 +0100852 if (datalen <= 0 || datalen > 32767 || !prep->data)
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500853 return -EINVAL;
854
855 buf = kmalloc(datalen + 1, GFP_KERNEL);
856 if (!buf)
857 return -ENOMEM;
858
859 buf[datalen] = 0;
David Howellscf7f6012012-09-13 13:06:29 +0100860 memcpy(buf, prep->data, datalen);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200861 ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500862 if (ret < 0)
863 goto out;
864
865 ret = valid_master_desc(new_master_desc, epayload->master_desc);
866 if (ret < 0)
867 goto out;
868
Roberto Sassu4e561d32011-06-27 13:45:42 +0200869 new_epayload = encrypted_key_alloc(key, epayload->format,
870 new_master_desc, epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500871 if (IS_ERR(new_epayload)) {
872 ret = PTR_ERR(new_epayload);
873 goto out;
874 }
875
Roberto Sassu4e561d32011-06-27 13:45:42 +0200876 __ekey_init(new_epayload, epayload->format, new_master_desc,
877 epayload->datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500878
879 memcpy(new_epayload->iv, epayload->iv, ivsize);
Roberto Sassu4e561d32011-06-27 13:45:42 +0200880 memcpy(new_epayload->payload_data, epayload->payload_data,
881 epayload->payload_datalen);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500882
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000883 rcu_assign_keypointer(key, new_epayload);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500884 call_rcu(&epayload->rcu, encrypted_rcu_free);
885out:
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100886 kzfree(buf);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500887 return ret;
888}
889
890/*
891 * encrypted_read - format and copy the encrypted data to userspace
892 *
893 * The resulting datablob format is:
894 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
895 *
896 * On success, return to userspace the encrypted key datablob size.
897 */
898static long encrypted_read(const struct key *key, char __user *buffer,
899 size_t buflen)
900{
901 struct encrypted_key_payload *epayload;
902 struct key *mkey;
David Howells146aa8b2015-10-21 14:04:48 +0100903 const u8 *master_key;
Mimi Zohar3b1826c2010-12-13 16:53:13 -0500904 size_t master_keylen;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500905 char derived_key[HASH_SIZE];
906 char *ascii_buf;
907 size_t asciiblob_len;
908 int ret;
909
David Howells0837e492017-03-01 15:11:23 +0000910 epayload = dereference_key_locked(key);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500911
912 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
913 asciiblob_len = epayload->datablob_len + ivsize + 1
914 + roundup(epayload->decrypted_datalen, blksize)
915 + (HASH_SIZE * 2);
916
917 if (!buffer || buflen < asciiblob_len)
918 return asciiblob_len;
919
920 mkey = request_master_key(epayload, &master_key, &master_keylen);
921 if (IS_ERR(mkey))
922 return PTR_ERR(mkey);
923
924 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
925 if (ret < 0)
926 goto out;
927
928 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
929 if (ret < 0)
930 goto out;
931
932 ret = datablob_hmac_append(epayload, master_key, master_keylen);
933 if (ret < 0)
934 goto out;
935
936 ascii_buf = datablob_format(epayload, asciiblob_len);
937 if (!ascii_buf) {
938 ret = -ENOMEM;
939 goto out;
940 }
941
942 up_read(&mkey->sem);
943 key_put(mkey);
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100944 memzero_explicit(derived_key, sizeof(derived_key));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500945
946 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
947 ret = -EFAULT;
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100948 kzfree(ascii_buf);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500949
950 return asciiblob_len;
951out:
952 up_read(&mkey->sem);
953 key_put(mkey);
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100954 memzero_explicit(derived_key, sizeof(derived_key));
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500955 return ret;
956}
957
958/*
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100959 * encrypted_destroy - clear and free the key's payload
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500960 */
961static void encrypted_destroy(struct key *key)
962{
Eric Biggersa9dd74b2017-06-08 14:49:11 +0100963 kzfree(key->payload.data[0]);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500964}
965
966struct key_type key_type_encrypted = {
967 .name = "encrypted",
968 .instantiate = encrypted_instantiate,
969 .update = encrypted_update,
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500970 .destroy = encrypted_destroy,
971 .describe = user_describe,
972 .read = encrypted_read,
973};
974EXPORT_SYMBOL_GPL(key_type_encrypted);
975
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500976static int __init init_encrypted(void)
977{
978 int ret;
979
Eric Biggers64d107d2017-06-08 14:48:25 +0100980 hash_tfm = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
981 if (IS_ERR(hash_tfm)) {
982 pr_err("encrypted_key: can't allocate %s transform: %ld\n",
983 hash_alg, PTR_ERR(hash_tfm));
984 return PTR_ERR(hash_tfm);
985 }
986
Takashi Iwaib26bdde2014-12-04 18:25:19 +0100987 ret = aes_get_sizes();
988 if (ret < 0)
989 goto out;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500990 ret = register_key_type(&key_type_encrypted);
991 if (ret < 0)
992 goto out;
Takashi Iwaib26bdde2014-12-04 18:25:19 +0100993 return 0;
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500994out:
Eric Biggers64d107d2017-06-08 14:48:25 +0100995 crypto_free_shash(hash_tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500996 return ret;
Mimi Zoharb9703442011-01-18 09:07:12 -0500997
Mimi Zohar7e70cb42010-11-23 18:55:35 -0500998}
999
1000static void __exit cleanup_encrypted(void)
1001{
Eric Biggers64d107d2017-06-08 14:48:25 +01001002 crypto_free_shash(hash_tfm);
Mimi Zohar7e70cb42010-11-23 18:55:35 -05001003 unregister_key_type(&key_type_encrypted);
1004}
1005
1006late_initcall(init_encrypted);
1007module_exit(cleanup_encrypted);
1008
1009MODULE_LICENSE("GPL");