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