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