blob: 975e9f29a52c63f98bf7f0416daeaf7bba17d872 [file] [log] [blame]
Mimi Zohard00a1c72010-11-23 17:50:34 -05001/*
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>
Mimi Zohar93ae86e2010-11-29 16:20:04 -050020#include <linux/err.h>
Mimi Zohard00a1c72010-11-23 17:50:34 -050021#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
Mimi Zohar1bdbb402010-12-13 16:53:11 -050059static int TSS_sha1(const unsigned char *data, unsigned int datalen,
Mimi Zohard00a1c72010-11-23 17:50:34 -050060 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,
Mimi Zohar1bdbb402010-12-13 16:53:11 -050077 unsigned int keylen, ...)
Mimi Zohard00a1c72010-11-23 17:50:34 -050078{
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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500111 if (!ret)
112 ret = crypto_shash_final(&sdesc->shash, digest);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500113out:
114 kfree(sdesc);
115 return ret;
116}
117
118/*
119 * calculate authorization info fields to send to TPM
120 */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500121static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500122 unsigned int keylen, unsigned char *h1,
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500123 unsigned char *h2, unsigned char h3, ...)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500124{
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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500150 if (ret < 0) {
151 va_end(argp);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500152 goto out;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500153 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500154 }
155 va_end(argp);
156 ret = crypto_shash_final(&sdesc->shash, paramdigest);
157 if (!ret)
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500158 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
159 paramdigest, TPM_NONCE_SIZE, h1,
160 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500161out:
162 kfree(sdesc);
163 return ret;
164}
165
166/*
167 * verify the AUTH1_COMMAND (Seal) result from TPM
168 */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500169static int TSS_checkhmac1(unsigned char *buffer,
170 const uint32_t command,
171 const unsigned char *ononce,
172 const unsigned char *key,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500173 unsigned int keylen, ...)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500174{
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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500225 if (ret < 0) {
226 va_end(argp);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500227 goto out;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500228 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500229 }
230 va_end(argp);
231 ret = crypto_shash_final(&sdesc->shash, paramdigest);
232 if (ret < 0)
233 goto out;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500234
Mimi Zohard00a1c72010-11-23 17:50:34 -0500235 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;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500240
Mimi Zohard00a1c72010-11-23 17:50:34 -0500241 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 */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500251static int TSS_checkhmac2(unsigned char *buffer,
252 const uint32_t command,
253 const unsigned char *ononce,
254 const unsigned char *key1,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500255 unsigned int keylen1,
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500256 const unsigned char *key2,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500257 unsigned int keylen2, ...)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500258{
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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500319 if (ret < 0) {
320 va_end(argp);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500321 goto out;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500322 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500323 }
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500324 va_end(argp);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500325 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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500332 if (ret < 0)
333 goto out;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500334 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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500341 if (ret < 0)
342 goto out;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500343 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);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500381 if (!ret)
382 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500383 return ret;
384}
385
386static int my_get_random(unsigned char *buf, int len)
387{
388 struct tpm_buf *tb;
389 int ret;
390
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500391 tb = kmalloc(sizeof *tb, GFP_KERNEL);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500392 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];
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500409 int ret;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500410
411 if (!capable(CAP_SYS_ADMIN))
412 return -EPERM;
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500413 ret = my_get_random(hash, SHA1_DIGEST_SIZE);
414 if (ret < 0)
415 return ret;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500416 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,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500423 const unsigned char *key, uint16_t type, uint32_t handle)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500424{
425 unsigned char enonce[TPM_NONCE_SIZE];
426 unsigned char ononce[TPM_NONCE_SIZE];
427 int ret;
428
429 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
430 if (ret < 0)
431 return ret;
432
433 INIT_BUF(tb);
434 store16(tb, TPM_TAG_RQU_COMMAND);
435 store32(tb, TPM_OSAP_SIZE);
436 store32(tb, TPM_ORD_OSAP);
437 store16(tb, type);
438 store32(tb, handle);
439 storebytes(tb, ononce, TPM_NONCE_SIZE);
440
441 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
442 if (ret < 0)
443 return ret;
444
445 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
446 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
447 TPM_NONCE_SIZE);
448 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
449 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500450 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
451 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500452}
453
454/*
455 * Create an object independent authorisation protocol (oiap) session
456 */
457static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
458{
459 int ret;
460
461 INIT_BUF(tb);
462 store16(tb, TPM_TAG_RQU_COMMAND);
463 store32(tb, TPM_OIAP_SIZE);
464 store32(tb, TPM_ORD_OIAP);
465 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
466 if (ret < 0)
467 return ret;
468
469 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
470 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
471 TPM_NONCE_SIZE);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500472 return 0;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500473}
474
475struct tpm_digests {
476 unsigned char encauth[SHA1_DIGEST_SIZE];
477 unsigned char pubauth[SHA1_DIGEST_SIZE];
478 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
479 unsigned char xorhash[SHA1_DIGEST_SIZE];
480 unsigned char nonceodd[TPM_NONCE_SIZE];
481};
482
483/*
484 * Have the TPM seal(encrypt) the trusted key, possibly based on
485 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
486 */
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500487static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
488 uint32_t keyhandle, const unsigned char *keyauth,
489 const unsigned char *data, uint32_t datalen,
Mimi Zohard00a1c72010-11-23 17:50:34 -0500490 unsigned char *blob, uint32_t *bloblen,
491 const unsigned char *blobauth,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500492 const unsigned char *pcrinfo, uint32_t pcrinfosize)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500493{
494 struct osapsess sess;
495 struct tpm_digests *td;
496 unsigned char cont;
497 uint32_t ordinal;
498 uint32_t pcrsize;
499 uint32_t datsize;
500 int sealinfosize;
501 int encdatasize;
502 int storedsize;
503 int ret;
504 int i;
505
506 /* alloc some work space for all the hashes */
507 td = kmalloc(sizeof *td, GFP_KERNEL);
508 if (!td)
509 return -ENOMEM;
510
511 /* get session for sealing key */
512 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
513 if (ret < 0)
514 return ret;
515 dump_sess(&sess);
516
517 /* calculate encrypted authorization value */
518 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
519 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
520 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
521 if (ret < 0)
522 return ret;
523
524 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
525 if (ret < 0)
526 return ret;
527 ordinal = htonl(TPM_ORD_SEAL);
528 datsize = htonl(datalen);
529 pcrsize = htonl(pcrinfosize);
530 cont = 0;
531
532 /* encrypt data authorization key */
533 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
534 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
535
536 /* calculate authorization HMAC value */
537 if (pcrinfosize == 0) {
538 /* no pcr info specified */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500539 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
540 sess.enonce, td->nonceodd, cont,
541 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
542 td->encauth, sizeof(uint32_t), &pcrsize,
543 sizeof(uint32_t), &datsize, datalen, data, 0,
544 0);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500545 } else {
546 /* pcr info specified */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500547 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
548 sess.enonce, td->nonceodd, cont,
549 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
550 td->encauth, sizeof(uint32_t), &pcrsize,
551 pcrinfosize, pcrinfo, sizeof(uint32_t),
552 &datsize, datalen, data, 0, 0);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500553 }
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500554 if (ret < 0)
555 return ret;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500556
557 /* build and send the TPM request packet */
558 INIT_BUF(tb);
559 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
560 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
561 store32(tb, TPM_ORD_SEAL);
562 store32(tb, keyhandle);
563 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
564 store32(tb, pcrinfosize);
565 storebytes(tb, pcrinfo, pcrinfosize);
566 store32(tb, datalen);
567 storebytes(tb, data, datalen);
568 store32(tb, sess.handle);
569 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
570 store8(tb, cont);
571 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
572
573 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
574 if (ret < 0)
575 return ret;
576
577 /* calculate the size of the returned Blob */
578 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
579 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
580 sizeof(uint32_t) + sealinfosize);
581 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
582 sizeof(uint32_t) + encdatasize;
583
584 /* check the HMAC in the response */
585 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
586 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
587 0);
588
589 /* copy the returned blob to caller */
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500590 if (!ret) {
591 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
592 *bloblen = storedsize;
593 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500594 return ret;
595}
596
597/*
598 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
599 */
600static int tpm_unseal(struct tpm_buf *tb,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500601 uint32_t keyhandle, const unsigned char *keyauth,
602 const unsigned char *blob, int bloblen,
Mimi Zohard00a1c72010-11-23 17:50:34 -0500603 const unsigned char *blobauth,
604 unsigned char *data, unsigned int *datalen)
605{
606 unsigned char nonceodd[TPM_NONCE_SIZE];
607 unsigned char enonce1[TPM_NONCE_SIZE];
608 unsigned char enonce2[TPM_NONCE_SIZE];
609 unsigned char authdata1[SHA1_DIGEST_SIZE];
610 unsigned char authdata2[SHA1_DIGEST_SIZE];
611 uint32_t authhandle1 = 0;
612 uint32_t authhandle2 = 0;
613 unsigned char cont = 0;
614 uint32_t ordinal;
615 uint32_t keyhndl;
616 int ret;
617
618 /* sessions for unsealing key and data */
619 ret = oiap(tb, &authhandle1, enonce1);
620 if (ret < 0) {
621 pr_info("trusted_key: oiap failed (%d)\n", ret);
622 return ret;
623 }
624 ret = oiap(tb, &authhandle2, enonce2);
625 if (ret < 0) {
626 pr_info("trusted_key: oiap failed (%d)\n", ret);
627 return ret;
628 }
629
630 ordinal = htonl(TPM_ORD_UNSEAL);
631 keyhndl = htonl(SRKHANDLE);
632 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
633 if (ret < 0) {
634 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
635 return ret;
636 }
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500637 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
638 enonce1, nonceodd, cont, sizeof(uint32_t),
639 &ordinal, bloblen, blob, 0, 0);
640 if (ret < 0)
641 return ret;
642 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
643 enonce2, nonceodd, cont, sizeof(uint32_t),
644 &ordinal, bloblen, blob, 0, 0);
645 if (ret < 0)
646 return ret;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500647
648 /* build and send TPM request packet */
649 INIT_BUF(tb);
650 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
651 store32(tb, TPM_UNSEAL_SIZE + bloblen);
652 store32(tb, TPM_ORD_UNSEAL);
653 store32(tb, keyhandle);
654 storebytes(tb, blob, bloblen);
655 store32(tb, authhandle1);
656 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
657 store8(tb, cont);
658 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
659 store32(tb, authhandle2);
660 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
661 store8(tb, cont);
662 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
663
664 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
665 if (ret < 0) {
666 pr_info("trusted_key: authhmac failed (%d)\n", ret);
667 return ret;
668 }
669
670 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
671 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
672 keyauth, SHA1_DIGEST_SIZE,
673 blobauth, SHA1_DIGEST_SIZE,
674 sizeof(uint32_t), TPM_DATA_OFFSET,
675 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
676 0);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500677 if (ret < 0) {
Mimi Zohard00a1c72010-11-23 17:50:34 -0500678 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500679 return ret;
680 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500681 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500682 return 0;
Mimi Zohard00a1c72010-11-23 17:50:34 -0500683}
684
685/*
686 * Have the TPM seal(encrypt) the symmetric key
687 */
688static int key_seal(struct trusted_key_payload *p,
689 struct trusted_key_options *o)
690{
691 struct tpm_buf *tb;
692 int ret;
693
694 tb = kzalloc(sizeof *tb, GFP_KERNEL);
695 if (!tb)
696 return -ENOMEM;
697
698 /* include migratable flag at end of sealed key */
699 p->key[p->key_len] = p->migratable;
700
701 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
702 p->key, p->key_len + 1, p->blob, &p->blob_len,
703 o->blobauth, o->pcrinfo, o->pcrinfo_len);
704 if (ret < 0)
705 pr_info("trusted_key: srkseal failed (%d)\n", ret);
706
707 kfree(tb);
708 return ret;
709}
710
711/*
712 * Have the TPM unseal(decrypt) the symmetric key
713 */
714static int key_unseal(struct trusted_key_payload *p,
715 struct trusted_key_options *o)
716{
717 struct tpm_buf *tb;
718 int ret;
719
720 tb = kzalloc(sizeof *tb, GFP_KERNEL);
721 if (!tb)
722 return -ENOMEM;
723
724 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
725 o->blobauth, p->key, &p->key_len);
Mimi Zohard00a1c72010-11-23 17:50:34 -0500726 if (ret < 0)
727 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500728 else
729 /* pull migratable flag out of sealed key */
730 p->migratable = p->key[--p->key_len];
Mimi Zohard00a1c72010-11-23 17:50:34 -0500731
732 kfree(tb);
733 return ret;
734}
735
736enum {
737 Opt_err = -1,
738 Opt_new, Opt_load, Opt_update,
739 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
740 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
741};
742
743static const match_table_t key_tokens = {
744 {Opt_new, "new"},
745 {Opt_load, "load"},
746 {Opt_update, "update"},
747 {Opt_keyhandle, "keyhandle=%s"},
748 {Opt_keyauth, "keyauth=%s"},
749 {Opt_blobauth, "blobauth=%s"},
750 {Opt_pcrinfo, "pcrinfo=%s"},
751 {Opt_pcrlock, "pcrlock=%s"},
752 {Opt_migratable, "migratable=%s"},
753 {Opt_err, NULL}
754};
755
756/* can have zero or more token= options */
757static int getoptions(char *c, struct trusted_key_payload *pay,
758 struct trusted_key_options *opt)
759{
760 substring_t args[MAX_OPT_ARGS];
761 char *p = c;
762 int token;
763 int res;
764 unsigned long handle;
765 unsigned long lock;
766
767 while ((p = strsep(&c, " \t"))) {
768 if (*p == '\0' || *p == ' ' || *p == '\t')
769 continue;
770 token = match_token(p, key_tokens, args);
771
772 switch (token) {
773 case Opt_pcrinfo:
774 opt->pcrinfo_len = strlen(args[0].from) / 2;
775 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
776 return -EINVAL;
777 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
778 break;
779 case Opt_keyhandle:
780 res = strict_strtoul(args[0].from, 16, &handle);
781 if (res < 0)
782 return -EINVAL;
783 opt->keytype = SEAL_keytype;
784 opt->keyhandle = handle;
785 break;
786 case Opt_keyauth:
787 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
788 return -EINVAL;
789 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
790 break;
791 case Opt_blobauth:
792 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
793 return -EINVAL;
794 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
795 break;
796 case Opt_migratable:
797 if (*args[0].from == '0')
798 pay->migratable = 0;
799 else
800 return -EINVAL;
801 break;
802 case Opt_pcrlock:
803 res = strict_strtoul(args[0].from, 10, &lock);
804 if (res < 0)
805 return -EINVAL;
806 opt->pcrlock = lock;
807 break;
808 default:
809 return -EINVAL;
810 }
811 }
812 return 0;
813}
814
815/*
816 * datablob_parse - parse the keyctl data and fill in the
817 * payload and options structures
818 *
819 * On success returns 0, otherwise -EINVAL.
820 */
821static int datablob_parse(char *datablob, struct trusted_key_payload *p,
822 struct trusted_key_options *o)
823{
824 substring_t args[MAX_OPT_ARGS];
825 long keylen;
826 int ret = -EINVAL;
827 int key_cmd;
828 char *c;
829
830 /* main command */
831 c = strsep(&datablob, " \t");
832 if (!c)
833 return -EINVAL;
834 key_cmd = match_token(c, key_tokens, args);
835 switch (key_cmd) {
836 case Opt_new:
837 /* first argument is key size */
838 c = strsep(&datablob, " \t");
839 if (!c)
840 return -EINVAL;
841 ret = strict_strtol(c, 10, &keylen);
842 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
843 return -EINVAL;
844 p->key_len = keylen;
845 ret = getoptions(datablob, p, o);
846 if (ret < 0)
847 return ret;
848 ret = Opt_new;
849 break;
850 case Opt_load:
851 /* first argument is sealed blob */
852 c = strsep(&datablob, " \t");
853 if (!c)
854 return -EINVAL;
855 p->blob_len = strlen(c) / 2;
856 if (p->blob_len > MAX_BLOB_SIZE)
857 return -EINVAL;
858 hex2bin(p->blob, c, p->blob_len);
859 ret = getoptions(datablob, p, o);
860 if (ret < 0)
861 return ret;
862 ret = Opt_load;
863 break;
864 case Opt_update:
865 /* all arguments are options */
866 ret = getoptions(datablob, p, o);
867 if (ret < 0)
868 return ret;
869 ret = Opt_update;
870 break;
871 case Opt_err:
872 return -EINVAL;
873 break;
874 }
875 return ret;
876}
877
878static struct trusted_key_options *trusted_options_alloc(void)
879{
880 struct trusted_key_options *options;
881
882 options = kzalloc(sizeof *options, GFP_KERNEL);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500883 if (options) {
884 /* set any non-zero defaults */
885 options->keytype = SRK_keytype;
886 options->keyhandle = SRKHANDLE;
887 }
Mimi Zohard00a1c72010-11-23 17:50:34 -0500888 return options;
889}
890
891static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
892{
893 struct trusted_key_payload *p = NULL;
894 int ret;
895
896 ret = key_payload_reserve(key, sizeof *p);
897 if (ret < 0)
898 return p;
899 p = kzalloc(sizeof *p, GFP_KERNEL);
Mimi Zoharbc5e0af2010-12-13 16:53:10 -0500900 if (p)
901 p->migratable = 1; /* migratable by default */
Mimi Zohard00a1c72010-11-23 17:50:34 -0500902 return p;
903}
904
905/*
906 * trusted_instantiate - create a new trusted key
907 *
908 * Unseal an existing trusted blob or, for a new key, get a
909 * random key, then seal and create a trusted key-type key,
910 * adding it to the specified keyring.
911 *
912 * On success, return 0. Otherwise return errno.
913 */
914static int trusted_instantiate(struct key *key, const void *data,
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500915 size_t datalen)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500916{
917 struct trusted_key_payload *payload = NULL;
918 struct trusted_key_options *options = NULL;
919 char *datablob;
920 int ret = 0;
921 int key_cmd;
922
923 if (datalen <= 0 || datalen > 32767 || !data)
924 return -EINVAL;
925
926 datablob = kmalloc(datalen + 1, GFP_KERNEL);
927 if (!datablob)
928 return -ENOMEM;
929 memcpy(datablob, data, datalen);
930 datablob[datalen] = '\0';
931
932 options = trusted_options_alloc();
933 if (!options) {
934 ret = -ENOMEM;
935 goto out;
936 }
937 payload = trusted_payload_alloc(key);
938 if (!payload) {
939 ret = -ENOMEM;
940 goto out;
941 }
942
943 key_cmd = datablob_parse(datablob, payload, options);
944 if (key_cmd < 0) {
945 ret = key_cmd;
946 goto out;
947 }
948
949 dump_payload(payload);
950 dump_options(options);
951
952 switch (key_cmd) {
953 case Opt_load:
954 ret = key_unseal(payload, options);
955 dump_payload(payload);
956 dump_options(options);
957 if (ret < 0)
958 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
959 break;
960 case Opt_new:
961 ret = my_get_random(payload->key, payload->key_len);
962 if (ret < 0) {
963 pr_info("trusted_key: key_create failed (%d)\n", ret);
964 goto out;
965 }
966 ret = key_seal(payload, options);
967 if (ret < 0)
968 pr_info("trusted_key: key_seal failed (%d)\n", ret);
969 break;
970 default:
971 ret = -EINVAL;
972 goto out;
973 }
974 if (!ret && options->pcrlock)
975 ret = pcrlock(options->pcrlock);
976out:
977 kfree(datablob);
978 kfree(options);
979 if (!ret)
980 rcu_assign_pointer(key->payload.data, payload);
981 else
982 kfree(payload);
983 return ret;
984}
985
986static void trusted_rcu_free(struct rcu_head *rcu)
987{
988 struct trusted_key_payload *p;
989
990 p = container_of(rcu, struct trusted_key_payload, rcu);
991 memset(p->key, 0, p->key_len);
992 kfree(p);
993}
994
995/*
996 * trusted_update - reseal an existing key with new PCR values
997 */
Mimi Zohar1bdbb402010-12-13 16:53:11 -0500998static int trusted_update(struct key *key, const void *data, size_t datalen)
Mimi Zohard00a1c72010-11-23 17:50:34 -0500999{
1000 struct trusted_key_payload *p = key->payload.data;
1001 struct trusted_key_payload *new_p;
1002 struct trusted_key_options *new_o;
1003 char *datablob;
1004 int ret = 0;
1005
1006 if (!p->migratable)
1007 return -EPERM;
1008 if (datalen <= 0 || datalen > 32767 || !data)
1009 return -EINVAL;
1010
1011 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1012 if (!datablob)
1013 return -ENOMEM;
1014 new_o = trusted_options_alloc();
1015 if (!new_o) {
1016 ret = -ENOMEM;
1017 goto out;
1018 }
1019 new_p = trusted_payload_alloc(key);
1020 if (!new_p) {
1021 ret = -ENOMEM;
1022 goto out;
1023 }
1024
1025 memcpy(datablob, data, datalen);
1026 datablob[datalen] = '\0';
1027 ret = datablob_parse(datablob, new_p, new_o);
1028 if (ret != Opt_update) {
1029 ret = -EINVAL;
1030 goto out;
1031 }
1032 /* copy old key values, and reseal with new pcrs */
1033 new_p->migratable = p->migratable;
1034 new_p->key_len = p->key_len;
1035 memcpy(new_p->key, p->key, p->key_len);
1036 dump_payload(p);
1037 dump_payload(new_p);
1038
1039 ret = key_seal(new_p, new_o);
1040 if (ret < 0) {
1041 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1042 kfree(new_p);
1043 goto out;
1044 }
1045 if (new_o->pcrlock) {
1046 ret = pcrlock(new_o->pcrlock);
1047 if (ret < 0) {
1048 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1049 kfree(new_p);
1050 goto out;
1051 }
1052 }
1053 rcu_assign_pointer(key->payload.data, new_p);
1054 call_rcu(&p->rcu, trusted_rcu_free);
1055out:
1056 kfree(datablob);
1057 kfree(new_o);
1058 return ret;
1059}
1060
1061/*
1062 * trusted_read - copy the sealed blob data to userspace in hex.
1063 * On success, return to userspace the trusted key datablob size.
1064 */
1065static long trusted_read(const struct key *key, char __user *buffer,
1066 size_t buflen)
1067{
1068 struct trusted_key_payload *p;
1069 char *ascii_buf;
1070 char *bufp;
1071 int i;
1072
1073 p = rcu_dereference_protected(key->payload.data,
1074 rwsem_is_locked(&((struct key *)key)->sem));
1075 if (!p)
1076 return -EINVAL;
1077 if (!buffer || buflen <= 0)
1078 return 2 * p->blob_len;
1079 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1080 if (!ascii_buf)
1081 return -ENOMEM;
1082
1083 bufp = ascii_buf;
1084 for (i = 0; i < p->blob_len; i++)
1085 bufp = pack_hex_byte(bufp, p->blob[i]);
1086 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1087 kfree(ascii_buf);
1088 return -EFAULT;
1089 }
1090 kfree(ascii_buf);
1091 return 2 * p->blob_len;
1092}
1093
1094/*
1095 * trusted_destroy - before freeing the key, clear the decrypted data
1096 */
1097static void trusted_destroy(struct key *key)
1098{
1099 struct trusted_key_payload *p = key->payload.data;
1100
1101 if (!p)
1102 return;
1103 memset(p->key, 0, p->key_len);
1104 kfree(key->payload.data);
1105}
1106
1107struct key_type key_type_trusted = {
1108 .name = "trusted",
1109 .instantiate = trusted_instantiate,
1110 .update = trusted_update,
1111 .match = user_match,
1112 .destroy = trusted_destroy,
1113 .describe = user_describe,
1114 .read = trusted_read,
1115};
1116
1117EXPORT_SYMBOL_GPL(key_type_trusted);
1118
1119static void trusted_shash_release(void)
1120{
1121 if (hashalg)
1122 crypto_free_shash(hashalg);
1123 if (hmacalg)
1124 crypto_free_shash(hmacalg);
1125}
1126
1127static int __init trusted_shash_alloc(void)
1128{
1129 int ret;
1130
1131 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1132 if (IS_ERR(hmacalg)) {
1133 pr_info("trusted_key: could not allocate crypto %s\n",
1134 hmac_alg);
1135 return PTR_ERR(hmacalg);
1136 }
1137
1138 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1139 if (IS_ERR(hashalg)) {
1140 pr_info("trusted_key: could not allocate crypto %s\n",
1141 hash_alg);
1142 ret = PTR_ERR(hashalg);
1143 goto hashalg_fail;
1144 }
1145
1146 return 0;
1147
1148hashalg_fail:
1149 crypto_free_shash(hmacalg);
1150 return ret;
1151}
1152
1153static int __init init_trusted(void)
1154{
1155 int ret;
1156
1157 ret = trusted_shash_alloc();
1158 if (ret < 0)
1159 return ret;
1160 ret = register_key_type(&key_type_trusted);
1161 if (ret < 0)
1162 trusted_shash_release();
1163 return ret;
1164}
1165
1166static void __exit cleanup_trusted(void)
1167{
1168 trusted_shash_release();
1169 unregister_key_type(&key_type_trusted);
1170}
1171
1172late_initcall(init_trusted);
1173module_exit(cleanup_trusted);
1174
1175MODULE_LICENSE("GPL");