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