trusted-keys: additional TSS return code and other error handling
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / security / keys / trusted_defined.c
CommitLineData
d00a1c72
MZ
1/*
2 * Copyright (C) 2010 IBM Corporation
3 *
4 * Author:
5 * David Safford <safford@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * See Documentation/keys-trusted-encrypted.txt
12 */
13
14#include <linux/uaccess.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/parser.h>
19#include <linux/string.h>
93ae86e7 20#include <linux/err.h>
d00a1c72
MZ
21#include <keys/user-type.h>
22#include <keys/trusted-type.h>
23#include <linux/key-type.h>
24#include <linux/rcupdate.h>
25#include <linux/crypto.h>
26#include <crypto/hash.h>
27#include <crypto/sha.h>
28#include <linux/capability.h>
29#include <linux/tpm.h>
30#include <linux/tpm_command.h>
31
32#include "trusted_defined.h"
33
34static const char hmac_alg[] = "hmac(sha1)";
35static const char hash_alg[] = "sha1";
36
37struct sdesc {
38 struct shash_desc shash;
39 char ctx[];
40};
41
42static struct crypto_shash *hashalg;
43static struct crypto_shash *hmacalg;
44
45static struct sdesc *init_sdesc(struct crypto_shash *alg)
46{
47 struct sdesc *sdesc;
48 int size;
49
50 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
51 sdesc = kmalloc(size, GFP_KERNEL);
52 if (!sdesc)
53 return ERR_PTR(-ENOMEM);
54 sdesc->shash.tfm = alg;
55 sdesc->shash.flags = 0x0;
56 return sdesc;
57}
58
59static int TSS_sha1(const unsigned char *data, const unsigned int datalen,
60 unsigned char *digest)
61{
62 struct sdesc *sdesc;
63 int ret;
64
65 sdesc = init_sdesc(hashalg);
66 if (IS_ERR(sdesc)) {
67 pr_info("trusted_key: can't alloc %s\n", hash_alg);
68 return PTR_ERR(sdesc);
69 }
70
71 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
72 kfree(sdesc);
73 return ret;
74}
75
76static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
77 const unsigned int keylen, ...)
78{
79 struct sdesc *sdesc;
80 va_list argp;
81 unsigned int dlen;
82 unsigned char *data;
83 int ret;
84
85 sdesc = init_sdesc(hmacalg);
86 if (IS_ERR(sdesc)) {
87 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
88 return PTR_ERR(sdesc);
89 }
90
91 ret = crypto_shash_setkey(hmacalg, key, keylen);
92 if (ret < 0)
93 goto out;
94 ret = crypto_shash_init(&sdesc->shash);
95 if (ret < 0)
96 goto out;
97
98 va_start(argp, keylen);
99 for (;;) {
100 dlen = va_arg(argp, unsigned int);
101 if (dlen == 0)
102 break;
103 data = va_arg(argp, unsigned char *);
104 if (data == NULL)
105 return -EINVAL;
106 ret = crypto_shash_update(&sdesc->shash, data, dlen);
107 if (ret < 0)
108 goto out;
109 }
110 va_end(argp);
bc5e0af0
MZ
111 if (!ret)
112 ret = crypto_shash_final(&sdesc->shash, digest);
d00a1c72
MZ
113out:
114 kfree(sdesc);
115 return ret;
116}
117
118/*
119 * calculate authorization info fields to send to TPM
120 */
bc5e0af0
MZ
121static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
122 const unsigned int keylen, unsigned char *h1,
123 unsigned char *h2, unsigned char h3, ...)
d00a1c72
MZ
124{
125 unsigned char paramdigest[SHA1_DIGEST_SIZE];
126 struct sdesc *sdesc;
127 unsigned int dlen;
128 unsigned char *data;
129 unsigned char c;
130 int ret;
131 va_list argp;
132
133 sdesc = init_sdesc(hashalg);
134 if (IS_ERR(sdesc)) {
135 pr_info("trusted_key: can't alloc %s\n", hash_alg);
136 return PTR_ERR(sdesc);
137 }
138
139 c = h3;
140 ret = crypto_shash_init(&sdesc->shash);
141 if (ret < 0)
142 goto out;
143 va_start(argp, h3);
144 for (;;) {
145 dlen = va_arg(argp, unsigned int);
146 if (dlen == 0)
147 break;
148 data = va_arg(argp, unsigned char *);
149 ret = crypto_shash_update(&sdesc->shash, data, dlen);
bc5e0af0
MZ
150 if (ret < 0) {
151 va_end(argp);
d00a1c72 152 goto out;
bc5e0af0 153 }
d00a1c72
MZ
154 }
155 va_end(argp);
156 ret = crypto_shash_final(&sdesc->shash, paramdigest);
157 if (!ret)
bc5e0af0
MZ
158 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
159 paramdigest, TPM_NONCE_SIZE, h1,
160 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
d00a1c72
MZ
161out:
162 kfree(sdesc);
163 return ret;
164}
165
166/*
167 * verify the AUTH1_COMMAND (Seal) result from TPM
168 */
bc5e0af0
MZ
169static int TSS_checkhmac1(unsigned char *buffer,
170 const uint32_t command,
171 const unsigned char *ononce,
172 const unsigned char *key,
173 const unsigned int keylen, ...)
d00a1c72
MZ
174{
175 uint32_t bufsize;
176 uint16_t tag;
177 uint32_t ordinal;
178 uint32_t result;
179 unsigned char *enonce;
180 unsigned char *continueflag;
181 unsigned char *authdata;
182 unsigned char testhmac[SHA1_DIGEST_SIZE];
183 unsigned char paramdigest[SHA1_DIGEST_SIZE];
184 struct sdesc *sdesc;
185 unsigned int dlen;
186 unsigned int dpos;
187 va_list argp;
188 int ret;
189
190 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
191 tag = LOAD16(buffer, 0);
192 ordinal = command;
193 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
194 if (tag == TPM_TAG_RSP_COMMAND)
195 return 0;
196 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
197 return -EINVAL;
198 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
199 continueflag = authdata - 1;
200 enonce = continueflag - TPM_NONCE_SIZE;
201
202 sdesc = init_sdesc(hashalg);
203 if (IS_ERR(sdesc)) {
204 pr_info("trusted_key: can't alloc %s\n", hash_alg);
205 return PTR_ERR(sdesc);
206 }
207 ret = crypto_shash_init(&sdesc->shash);
208 if (ret < 0)
209 goto out;
210 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
211 sizeof result);
212 if (ret < 0)
213 goto out;
214 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
215 sizeof ordinal);
216 if (ret < 0)
217 goto out;
218 va_start(argp, keylen);
219 for (;;) {
220 dlen = va_arg(argp, unsigned int);
221 if (dlen == 0)
222 break;
223 dpos = va_arg(argp, unsigned int);
224 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
225 if (ret < 0) {
226 va_end(argp);
d00a1c72 227 goto out;
bc5e0af0 228 }
d00a1c72
MZ
229 }
230 va_end(argp);
231 ret = crypto_shash_final(&sdesc->shash, paramdigest);
232 if (ret < 0)
233 goto out;
bc5e0af0 234
d00a1c72
MZ
235 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
236 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
237 1, continueflag, 0, 0);
238 if (ret < 0)
239 goto out;
bc5e0af0 240
d00a1c72
MZ
241 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
242 ret = -EINVAL;
243out:
244 kfree(sdesc);
245 return ret;
246}
247
248/*
249 * verify the AUTH2_COMMAND (unseal) result from TPM
250 */
bc5e0af0
MZ
251static int TSS_checkhmac2(unsigned char *buffer,
252 const uint32_t command,
253 const unsigned char *ononce,
254 const unsigned char *key1,
255 const unsigned int keylen1,
256 const unsigned char *key2,
257 const unsigned int keylen2, ...)
d00a1c72
MZ
258{
259 uint32_t bufsize;
260 uint16_t tag;
261 uint32_t ordinal;
262 uint32_t result;
263 unsigned char *enonce1;
264 unsigned char *continueflag1;
265 unsigned char *authdata1;
266 unsigned char *enonce2;
267 unsigned char *continueflag2;
268 unsigned char *authdata2;
269 unsigned char testhmac1[SHA1_DIGEST_SIZE];
270 unsigned char testhmac2[SHA1_DIGEST_SIZE];
271 unsigned char paramdigest[SHA1_DIGEST_SIZE];
272 struct sdesc *sdesc;
273 unsigned int dlen;
274 unsigned int dpos;
275 va_list argp;
276 int ret;
277
278 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
279 tag = LOAD16(buffer, 0);
280 ordinal = command;
281 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
282
283 if (tag == TPM_TAG_RSP_COMMAND)
284 return 0;
285 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
286 return -EINVAL;
287 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
288 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
289 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
290 continueflag1 = authdata1 - 1;
291 continueflag2 = authdata2 - 1;
292 enonce1 = continueflag1 - TPM_NONCE_SIZE;
293 enonce2 = continueflag2 - TPM_NONCE_SIZE;
294
295 sdesc = init_sdesc(hashalg);
296 if (IS_ERR(sdesc)) {
297 pr_info("trusted_key: can't alloc %s\n", hash_alg);
298 return PTR_ERR(sdesc);
299 }
300 ret = crypto_shash_init(&sdesc->shash);
301 if (ret < 0)
302 goto out;
303 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
304 sizeof result);
305 if (ret < 0)
306 goto out;
307 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
308 sizeof ordinal);
309 if (ret < 0)
310 goto out;
311
312 va_start(argp, keylen2);
313 for (;;) {
314 dlen = va_arg(argp, unsigned int);
315 if (dlen == 0)
316 break;
317 dpos = va_arg(argp, unsigned int);
318 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
319 if (ret < 0) {
320 va_end(argp);
d00a1c72 321 goto out;
bc5e0af0 322 }
d00a1c72 323 }
bc5e0af0 324 va_end(argp);
d00a1c72
MZ
325 ret = crypto_shash_final(&sdesc->shash, paramdigest);
326 if (ret < 0)
327 goto out;
328
329 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
330 paramdigest, TPM_NONCE_SIZE, enonce1,
331 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
bc5e0af0
MZ
332 if (ret < 0)
333 goto out;
d00a1c72
MZ
334 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
335 ret = -EINVAL;
336 goto out;
337 }
338 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
339 paramdigest, TPM_NONCE_SIZE, enonce2,
340 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
bc5e0af0
MZ
341 if (ret < 0)
342 goto out;
d00a1c72
MZ
343 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
344 ret = -EINVAL;
345out:
346 kfree(sdesc);
347 return ret;
348}
349
350/*
351 * For key specific tpm requests, we will generate and send our
352 * own TPM command packets using the drivers send function.
353 */
354static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
355 size_t buflen)
356{
357 int rc;
358
359 dump_tpm_buf(cmd);
360 rc = tpm_send(chip_num, cmd, buflen);
361 dump_tpm_buf(cmd);
362 if (rc > 0)
363 /* Can't return positive return codes values to keyctl */
364 rc = -EPERM;
365 return rc;
366}
367
368/*
369 * get a random value from TPM
370 */
371static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len)
372{
373 int ret;
374
375 INIT_BUF(tb);
376 store16(tb, TPM_TAG_RQU_COMMAND);
377 store32(tb, TPM_GETRANDOM_SIZE);
378 store32(tb, TPM_ORD_GETRANDOM);
379 store32(tb, len);
380 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data);
bc5e0af0
MZ
381 if (!ret)
382 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
d00a1c72
MZ
383 return ret;
384}
385
386static int my_get_random(unsigned char *buf, int len)
387{
388 struct tpm_buf *tb;
389 int ret;
390
391 tb = kzalloc(sizeof *tb, GFP_KERNEL);
392 if (!tb)
393 return -ENOMEM;
394 ret = tpm_get_random(tb, buf, len);
395
396 kfree(tb);
397 return ret;
398}
399
400/*
401 * Lock a trusted key, by extending a selected PCR.
402 *
403 * Prevents a trusted key that is sealed to PCRs from being accessed.
404 * This uses the tpm driver's extend function.
405 */
406static int pcrlock(const int pcrnum)
407{
408 unsigned char hash[SHA1_DIGEST_SIZE];
bc5e0af0 409 int ret;
d00a1c72
MZ
410
411 if (!capable(CAP_SYS_ADMIN))
412 return -EPERM;
bc5e0af0
MZ
413 ret = my_get_random(hash, SHA1_DIGEST_SIZE);
414 if (ret < 0)
415 return ret;
d00a1c72
MZ
416 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
417}
418
419/*
420 * Create an object specific authorisation protocol (OSAP) session
421 */
422static int osap(struct tpm_buf *tb, struct osapsess *s,
423 const unsigned char *key, const uint16_t type,
424 const uint32_t handle)
425{
426 unsigned char enonce[TPM_NONCE_SIZE];
427 unsigned char ononce[TPM_NONCE_SIZE];
428 int ret;
429
430 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
431 if (ret < 0)
432 return ret;
433
434 INIT_BUF(tb);
435 store16(tb, TPM_TAG_RQU_COMMAND);
436 store32(tb, TPM_OSAP_SIZE);
437 store32(tb, TPM_ORD_OSAP);
438 store16(tb, type);
439 store32(tb, handle);
440 storebytes(tb, ononce, TPM_NONCE_SIZE);
441
442 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
443 if (ret < 0)
444 return ret;
445
446 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
447 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
448 TPM_NONCE_SIZE);
449 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
450 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
bc5e0af0
MZ
451 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
452 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
d00a1c72
MZ
453}
454
455/*
456 * Create an object independent authorisation protocol (oiap) session
457 */
458static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
459{
460 int ret;
461
462 INIT_BUF(tb);
463 store16(tb, TPM_TAG_RQU_COMMAND);
464 store32(tb, TPM_OIAP_SIZE);
465 store32(tb, TPM_ORD_OIAP);
466 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
467 if (ret < 0)
468 return ret;
469
470 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
471 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
472 TPM_NONCE_SIZE);
bc5e0af0 473 return 0;
d00a1c72
MZ
474}
475
476struct tpm_digests {
477 unsigned char encauth[SHA1_DIGEST_SIZE];
478 unsigned char pubauth[SHA1_DIGEST_SIZE];
479 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
480 unsigned char xorhash[SHA1_DIGEST_SIZE];
481 unsigned char nonceodd[TPM_NONCE_SIZE];
482};
483
484/*
485 * Have the TPM seal(encrypt) the trusted key, possibly based on
486 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
487 */
488static int tpm_seal(struct tpm_buf *tb, const uint16_t keytype,
489 const uint32_t keyhandle, const unsigned char *keyauth,
490 const unsigned char *data, const uint32_t datalen,
491 unsigned char *blob, uint32_t *bloblen,
492 const unsigned char *blobauth,
493 const unsigned char *pcrinfo, const uint32_t pcrinfosize)
494{
495 struct osapsess sess;
496 struct tpm_digests *td;
497 unsigned char cont;
498 uint32_t ordinal;
499 uint32_t pcrsize;
500 uint32_t datsize;
501 int sealinfosize;
502 int encdatasize;
503 int storedsize;
504 int ret;
505 int i;
506
507 /* alloc some work space for all the hashes */
508 td = kmalloc(sizeof *td, GFP_KERNEL);
509 if (!td)
510 return -ENOMEM;
511
512 /* get session for sealing key */
513 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
514 if (ret < 0)
515 return ret;
516 dump_sess(&sess);
517
518 /* calculate encrypted authorization value */
519 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
520 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
521 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
522 if (ret < 0)
523 return ret;
524
525 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
526 if (ret < 0)
527 return ret;
528 ordinal = htonl(TPM_ORD_SEAL);
529 datsize = htonl(datalen);
530 pcrsize = htonl(pcrinfosize);
531 cont = 0;
532
533 /* encrypt data authorization key */
534 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
535 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
536
537 /* calculate authorization HMAC value */
538 if (pcrinfosize == 0) {
539 /* no pcr info specified */
bc5e0af0
MZ
540 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
541 sess.enonce, td->nonceodd, cont,
542 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
543 td->encauth, sizeof(uint32_t), &pcrsize,
544 sizeof(uint32_t), &datsize, datalen, data, 0,
545 0);
d00a1c72
MZ
546 } else {
547 /* pcr info specified */
bc5e0af0
MZ
548 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
549 sess.enonce, td->nonceodd, cont,
550 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
551 td->encauth, sizeof(uint32_t), &pcrsize,
552 pcrinfosize, pcrinfo, sizeof(uint32_t),
553 &datsize, datalen, data, 0, 0);
d00a1c72 554 }
bc5e0af0
MZ
555 if (ret < 0)
556 return ret;
d00a1c72
MZ
557
558 /* build and send the TPM request packet */
559 INIT_BUF(tb);
560 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
561 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
562 store32(tb, TPM_ORD_SEAL);
563 store32(tb, keyhandle);
564 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
565 store32(tb, pcrinfosize);
566 storebytes(tb, pcrinfo, pcrinfosize);
567 store32(tb, datalen);
568 storebytes(tb, data, datalen);
569 store32(tb, sess.handle);
570 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
571 store8(tb, cont);
572 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
573
574 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
575 if (ret < 0)
576 return ret;
577
578 /* calculate the size of the returned Blob */
579 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
580 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
581 sizeof(uint32_t) + sealinfosize);
582 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
583 sizeof(uint32_t) + encdatasize;
584
585 /* check the HMAC in the response */
586 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
587 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
588 0);
589
590 /* copy the returned blob to caller */
bc5e0af0
MZ
591 if (!ret) {
592 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
593 *bloblen = storedsize;
594 }
d00a1c72
MZ
595 return ret;
596}
597
598/*
599 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
600 */
601static int tpm_unseal(struct tpm_buf *tb,
602 const uint32_t keyhandle, const unsigned char *keyauth,
603 const unsigned char *blob, const int bloblen,
604 const unsigned char *blobauth,
605 unsigned char *data, unsigned int *datalen)
606{
607 unsigned char nonceodd[TPM_NONCE_SIZE];
608 unsigned char enonce1[TPM_NONCE_SIZE];
609 unsigned char enonce2[TPM_NONCE_SIZE];
610 unsigned char authdata1[SHA1_DIGEST_SIZE];
611 unsigned char authdata2[SHA1_DIGEST_SIZE];
612 uint32_t authhandle1 = 0;
613 uint32_t authhandle2 = 0;
614 unsigned char cont = 0;
615 uint32_t ordinal;
616 uint32_t keyhndl;
617 int ret;
618
619 /* sessions for unsealing key and data */
620 ret = oiap(tb, &authhandle1, enonce1);
621 if (ret < 0) {
622 pr_info("trusted_key: oiap failed (%d)\n", ret);
623 return ret;
624 }
625 ret = oiap(tb, &authhandle2, enonce2);
626 if (ret < 0) {
627 pr_info("trusted_key: oiap failed (%d)\n", ret);
628 return ret;
629 }
630
631 ordinal = htonl(TPM_ORD_UNSEAL);
632 keyhndl = htonl(SRKHANDLE);
633 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
634 if (ret < 0) {
635 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
636 return ret;
637 }
bc5e0af0
MZ
638 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
639 enonce1, nonceodd, cont, sizeof(uint32_t),
640 &ordinal, bloblen, blob, 0, 0);
641 if (ret < 0)
642 return ret;
643 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
644 enonce2, nonceodd, cont, sizeof(uint32_t),
645 &ordinal, bloblen, blob, 0, 0);
646 if (ret < 0)
647 return ret;
d00a1c72
MZ
648
649 /* build and send TPM request packet */
650 INIT_BUF(tb);
651 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
652 store32(tb, TPM_UNSEAL_SIZE + bloblen);
653 store32(tb, TPM_ORD_UNSEAL);
654 store32(tb, keyhandle);
655 storebytes(tb, blob, bloblen);
656 store32(tb, authhandle1);
657 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
658 store8(tb, cont);
659 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
660 store32(tb, authhandle2);
661 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
662 store8(tb, cont);
663 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
664
665 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
666 if (ret < 0) {
667 pr_info("trusted_key: authhmac failed (%d)\n", ret);
668 return ret;
669 }
670
671 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
672 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
673 keyauth, SHA1_DIGEST_SIZE,
674 blobauth, SHA1_DIGEST_SIZE,
675 sizeof(uint32_t), TPM_DATA_OFFSET,
676 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
677 0);
bc5e0af0 678 if (ret < 0) {
d00a1c72 679 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
bc5e0af0
MZ
680 return ret;
681 }
d00a1c72 682 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
bc5e0af0 683 return 0;
d00a1c72
MZ
684}
685
686/*
687 * Have the TPM seal(encrypt) the symmetric key
688 */
689static int key_seal(struct trusted_key_payload *p,
690 struct trusted_key_options *o)
691{
692 struct tpm_buf *tb;
693 int ret;
694
695 tb = kzalloc(sizeof *tb, GFP_KERNEL);
696 if (!tb)
697 return -ENOMEM;
698
699 /* include migratable flag at end of sealed key */
700 p->key[p->key_len] = p->migratable;
701
702 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
703 p->key, p->key_len + 1, p->blob, &p->blob_len,
704 o->blobauth, o->pcrinfo, o->pcrinfo_len);
705 if (ret < 0)
706 pr_info("trusted_key: srkseal failed (%d)\n", ret);
707
708 kfree(tb);
709 return ret;
710}
711
712/*
713 * Have the TPM unseal(decrypt) the symmetric key
714 */
715static int key_unseal(struct trusted_key_payload *p,
716 struct trusted_key_options *o)
717{
718 struct tpm_buf *tb;
719 int ret;
720
721 tb = kzalloc(sizeof *tb, GFP_KERNEL);
722 if (!tb)
723 return -ENOMEM;
724
725 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
726 o->blobauth, p->key, &p->key_len);
d00a1c72
MZ
727 if (ret < 0)
728 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
bc5e0af0
MZ
729 else
730 /* pull migratable flag out of sealed key */
731 p->migratable = p->key[--p->key_len];
d00a1c72
MZ
732
733 kfree(tb);
734 return ret;
735}
736
737enum {
738 Opt_err = -1,
739 Opt_new, Opt_load, Opt_update,
740 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
741 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
742};
743
744static const match_table_t key_tokens = {
745 {Opt_new, "new"},
746 {Opt_load, "load"},
747 {Opt_update, "update"},
748 {Opt_keyhandle, "keyhandle=%s"},
749 {Opt_keyauth, "keyauth=%s"},
750 {Opt_blobauth, "blobauth=%s"},
751 {Opt_pcrinfo, "pcrinfo=%s"},
752 {Opt_pcrlock, "pcrlock=%s"},
753 {Opt_migratable, "migratable=%s"},
754 {Opt_err, NULL}
755};
756
757/* can have zero or more token= options */
758static int getoptions(char *c, struct trusted_key_payload *pay,
759 struct trusted_key_options *opt)
760{
761 substring_t args[MAX_OPT_ARGS];
762 char *p = c;
763 int token;
764 int res;
765 unsigned long handle;
766 unsigned long lock;
767
768 while ((p = strsep(&c, " \t"))) {
769 if (*p == '\0' || *p == ' ' || *p == '\t')
770 continue;
771 token = match_token(p, key_tokens, args);
772
773 switch (token) {
774 case Opt_pcrinfo:
775 opt->pcrinfo_len = strlen(args[0].from) / 2;
776 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
777 return -EINVAL;
778 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
779 break;
780 case Opt_keyhandle:
781 res = strict_strtoul(args[0].from, 16, &handle);
782 if (res < 0)
783 return -EINVAL;
784 opt->keytype = SEAL_keytype;
785 opt->keyhandle = handle;
786 break;
787 case Opt_keyauth:
788 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
789 return -EINVAL;
790 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
791 break;
792 case Opt_blobauth:
793 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
794 return -EINVAL;
795 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
796 break;
797 case Opt_migratable:
798 if (*args[0].from == '0')
799 pay->migratable = 0;
800 else
801 return -EINVAL;
802 break;
803 case Opt_pcrlock:
804 res = strict_strtoul(args[0].from, 10, &lock);
805 if (res < 0)
806 return -EINVAL;
807 opt->pcrlock = lock;
808 break;
809 default:
810 return -EINVAL;
811 }
812 }
813 return 0;
814}
815
816/*
817 * datablob_parse - parse the keyctl data and fill in the
818 * payload and options structures
819 *
820 * On success returns 0, otherwise -EINVAL.
821 */
822static int datablob_parse(char *datablob, struct trusted_key_payload *p,
823 struct trusted_key_options *o)
824{
825 substring_t args[MAX_OPT_ARGS];
826 long keylen;
827 int ret = -EINVAL;
828 int key_cmd;
829 char *c;
830
831 /* main command */
832 c = strsep(&datablob, " \t");
833 if (!c)
834 return -EINVAL;
835 key_cmd = match_token(c, key_tokens, args);
836 switch (key_cmd) {
837 case Opt_new:
838 /* first argument is key size */
839 c = strsep(&datablob, " \t");
840 if (!c)
841 return -EINVAL;
842 ret = strict_strtol(c, 10, &keylen);
843 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
844 return -EINVAL;
845 p->key_len = keylen;
846 ret = getoptions(datablob, p, o);
847 if (ret < 0)
848 return ret;
849 ret = Opt_new;
850 break;
851 case Opt_load:
852 /* first argument is sealed blob */
853 c = strsep(&datablob, " \t");
854 if (!c)
855 return -EINVAL;
856 p->blob_len = strlen(c) / 2;
857 if (p->blob_len > MAX_BLOB_SIZE)
858 return -EINVAL;
859 hex2bin(p->blob, c, p->blob_len);
860 ret = getoptions(datablob, p, o);
861 if (ret < 0)
862 return ret;
863 ret = Opt_load;
864 break;
865 case Opt_update:
866 /* all arguments are options */
867 ret = getoptions(datablob, p, o);
868 if (ret < 0)
869 return ret;
870 ret = Opt_update;
871 break;
872 case Opt_err:
873 return -EINVAL;
874 break;
875 }
876 return ret;
877}
878
879static struct trusted_key_options *trusted_options_alloc(void)
880{
881 struct trusted_key_options *options;
882
883 options = kzalloc(sizeof *options, GFP_KERNEL);
bc5e0af0
MZ
884 if (options) {
885 /* set any non-zero defaults */
886 options->keytype = SRK_keytype;
887 options->keyhandle = SRKHANDLE;
888 }
d00a1c72
MZ
889 return options;
890}
891
892static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
893{
894 struct trusted_key_payload *p = NULL;
895 int ret;
896
897 ret = key_payload_reserve(key, sizeof *p);
898 if (ret < 0)
899 return p;
900 p = kzalloc(sizeof *p, GFP_KERNEL);
bc5e0af0
MZ
901 if (p)
902 p->migratable = 1; /* migratable by default */
d00a1c72
MZ
903 return p;
904}
905
906/*
907 * trusted_instantiate - create a new trusted key
908 *
909 * Unseal an existing trusted blob or, for a new key, get a
910 * random key, then seal and create a trusted key-type key,
911 * adding it to the specified keyring.
912 *
913 * On success, return 0. Otherwise return errno.
914 */
915static int trusted_instantiate(struct key *key, const void *data,
916 const size_t datalen)
917{
918 struct trusted_key_payload *payload = NULL;
919 struct trusted_key_options *options = NULL;
920 char *datablob;
921 int ret = 0;
922 int key_cmd;
923
924 if (datalen <= 0 || datalen > 32767 || !data)
925 return -EINVAL;
926
927 datablob = kmalloc(datalen + 1, GFP_KERNEL);
928 if (!datablob)
929 return -ENOMEM;
930 memcpy(datablob, data, datalen);
931 datablob[datalen] = '\0';
932
933 options = trusted_options_alloc();
934 if (!options) {
935 ret = -ENOMEM;
936 goto out;
937 }
938 payload = trusted_payload_alloc(key);
939 if (!payload) {
940 ret = -ENOMEM;
941 goto out;
942 }
943
944 key_cmd = datablob_parse(datablob, payload, options);
945 if (key_cmd < 0) {
946 ret = key_cmd;
947 goto out;
948 }
949
950 dump_payload(payload);
951 dump_options(options);
952
953 switch (key_cmd) {
954 case Opt_load:
955 ret = key_unseal(payload, options);
956 dump_payload(payload);
957 dump_options(options);
958 if (ret < 0)
959 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
960 break;
961 case Opt_new:
962 ret = my_get_random(payload->key, payload->key_len);
963 if (ret < 0) {
964 pr_info("trusted_key: key_create failed (%d)\n", ret);
965 goto out;
966 }
967 ret = key_seal(payload, options);
968 if (ret < 0)
969 pr_info("trusted_key: key_seal failed (%d)\n", ret);
970 break;
971 default:
972 ret = -EINVAL;
973 goto out;
974 }
975 if (!ret && options->pcrlock)
976 ret = pcrlock(options->pcrlock);
977out:
978 kfree(datablob);
979 kfree(options);
980 if (!ret)
981 rcu_assign_pointer(key->payload.data, payload);
982 else
983 kfree(payload);
984 return ret;
985}
986
987static void trusted_rcu_free(struct rcu_head *rcu)
988{
989 struct trusted_key_payload *p;
990
991 p = container_of(rcu, struct trusted_key_payload, rcu);
992 memset(p->key, 0, p->key_len);
993 kfree(p);
994}
995
996/*
997 * trusted_update - reseal an existing key with new PCR values
998 */
999static int trusted_update(struct key *key, const void *data,
1000 const size_t datalen)
1001{
1002 struct trusted_key_payload *p = key->payload.data;
1003 struct trusted_key_payload *new_p;
1004 struct trusted_key_options *new_o;
1005 char *datablob;
1006 int ret = 0;
1007
1008 if (!p->migratable)
1009 return -EPERM;
1010 if (datalen <= 0 || datalen > 32767 || !data)
1011 return -EINVAL;
1012
1013 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1014 if (!datablob)
1015 return -ENOMEM;
1016 new_o = trusted_options_alloc();
1017 if (!new_o) {
1018 ret = -ENOMEM;
1019 goto out;
1020 }
1021 new_p = trusted_payload_alloc(key);
1022 if (!new_p) {
1023 ret = -ENOMEM;
1024 goto out;
1025 }
1026
1027 memcpy(datablob, data, datalen);
1028 datablob[datalen] = '\0';
1029 ret = datablob_parse(datablob, new_p, new_o);
1030 if (ret != Opt_update) {
1031 ret = -EINVAL;
1032 goto out;
1033 }
1034 /* copy old key values, and reseal with new pcrs */
1035 new_p->migratable = p->migratable;
1036 new_p->key_len = p->key_len;
1037 memcpy(new_p->key, p->key, p->key_len);
1038 dump_payload(p);
1039 dump_payload(new_p);
1040
1041 ret = key_seal(new_p, new_o);
1042 if (ret < 0) {
1043 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1044 kfree(new_p);
1045 goto out;
1046 }
1047 if (new_o->pcrlock) {
1048 ret = pcrlock(new_o->pcrlock);
1049 if (ret < 0) {
1050 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1051 kfree(new_p);
1052 goto out;
1053 }
1054 }
1055 rcu_assign_pointer(key->payload.data, new_p);
1056 call_rcu(&p->rcu, trusted_rcu_free);
1057out:
1058 kfree(datablob);
1059 kfree(new_o);
1060 return ret;
1061}
1062
1063/*
1064 * trusted_read - copy the sealed blob data to userspace in hex.
1065 * On success, return to userspace the trusted key datablob size.
1066 */
1067static long trusted_read(const struct key *key, char __user *buffer,
1068 size_t buflen)
1069{
1070 struct trusted_key_payload *p;
1071 char *ascii_buf;
1072 char *bufp;
1073 int i;
1074
1075 p = rcu_dereference_protected(key->payload.data,
1076 rwsem_is_locked(&((struct key *)key)->sem));
1077 if (!p)
1078 return -EINVAL;
1079 if (!buffer || buflen <= 0)
1080 return 2 * p->blob_len;
1081 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1082 if (!ascii_buf)
1083 return -ENOMEM;
1084
1085 bufp = ascii_buf;
1086 for (i = 0; i < p->blob_len; i++)
1087 bufp = pack_hex_byte(bufp, p->blob[i]);
1088 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1089 kfree(ascii_buf);
1090 return -EFAULT;
1091 }
1092 kfree(ascii_buf);
1093 return 2 * p->blob_len;
1094}
1095
1096/*
1097 * trusted_destroy - before freeing the key, clear the decrypted data
1098 */
1099static void trusted_destroy(struct key *key)
1100{
1101 struct trusted_key_payload *p = key->payload.data;
1102
1103 if (!p)
1104 return;
1105 memset(p->key, 0, p->key_len);
1106 kfree(key->payload.data);
1107}
1108
1109struct key_type key_type_trusted = {
1110 .name = "trusted",
1111 .instantiate = trusted_instantiate,
1112 .update = trusted_update,
1113 .match = user_match,
1114 .destroy = trusted_destroy,
1115 .describe = user_describe,
1116 .read = trusted_read,
1117};
1118
1119EXPORT_SYMBOL_GPL(key_type_trusted);
1120
1121static void trusted_shash_release(void)
1122{
1123 if (hashalg)
1124 crypto_free_shash(hashalg);
1125 if (hmacalg)
1126 crypto_free_shash(hmacalg);
1127}
1128
1129static int __init trusted_shash_alloc(void)
1130{
1131 int ret;
1132
1133 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1134 if (IS_ERR(hmacalg)) {
1135 pr_info("trusted_key: could not allocate crypto %s\n",
1136 hmac_alg);
1137 return PTR_ERR(hmacalg);
1138 }
1139
1140 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1141 if (IS_ERR(hashalg)) {
1142 pr_info("trusted_key: could not allocate crypto %s\n",
1143 hash_alg);
1144 ret = PTR_ERR(hashalg);
1145 goto hashalg_fail;
1146 }
1147
1148 return 0;
1149
1150hashalg_fail:
1151 crypto_free_shash(hmacalg);
1152 return ret;
1153}
1154
1155static int __init init_trusted(void)
1156{
1157 int ret;
1158
1159 ret = trusted_shash_alloc();
1160 if (ret < 0)
1161 return ret;
1162 ret = register_key_type(&key_type_trusted);
1163 if (ret < 0)
1164 trusted_shash_release();
1165 return ret;
1166}
1167
1168static void __exit cleanup_trusted(void)
1169{
1170 trusted_shash_release();
1171 unregister_key_type(&key_type_trusted);
1172}
1173
1174late_initcall(init_trusted);
1175module_exit(cleanup_trusted);
1176
1177MODULE_LICENSE("GPL");