Commit | Line | Data |
---|---|---|
7a1d7e6d | 1 | /* |
954650ef | 2 | * Copyright (C) 2014, 2015 Intel Corporation |
7a1d7e6d JS |
3 | * |
4 | * Authors: | |
5 | * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> | |
6 | * | |
7 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> | |
8 | * | |
9 | * This file contains TPM2 protocol implementations of the commands | |
10 | * used by the kernel internally. | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; version 2 | |
15 | * of the License. | |
16 | */ | |
17 | ||
18 | #include "tpm.h" | |
5ca4c20c | 19 | #include <crypto/hash_info.h> |
954650ef JS |
20 | #include <keys/trusted-type.h> |
21 | ||
22 | enum tpm2_object_attributes { | |
23 | TPM2_ATTR_USER_WITH_AUTH = BIT(6), | |
24 | }; | |
7a1d7e6d JS |
25 | |
26 | struct tpm2_startup_in { | |
27 | __be16 startup_type; | |
28 | } __packed; | |
29 | ||
30 | struct tpm2_self_test_in { | |
31 | u8 full_test; | |
32 | } __packed; | |
33 | ||
34 | struct tpm2_pcr_read_in { | |
35 | __be32 pcr_selects_cnt; | |
36 | __be16 hash_alg; | |
37 | u8 pcr_select_size; | |
38 | u8 pcr_select[TPM2_PCR_SELECT_MIN]; | |
39 | } __packed; | |
40 | ||
41 | struct tpm2_pcr_read_out { | |
42 | __be32 update_cnt; | |
43 | __be32 pcr_selects_cnt; | |
44 | __be16 hash_alg; | |
45 | u8 pcr_select_size; | |
46 | u8 pcr_select[TPM2_PCR_SELECT_MIN]; | |
47 | __be32 digests_cnt; | |
48 | __be16 digest_size; | |
49 | u8 digest[TPM_DIGEST_SIZE]; | |
50 | } __packed; | |
51 | ||
52 | struct tpm2_null_auth_area { | |
53 | __be32 handle; | |
54 | __be16 nonce_size; | |
55 | u8 attributes; | |
56 | __be16 auth_size; | |
57 | } __packed; | |
58 | ||
59 | struct tpm2_pcr_extend_in { | |
60 | __be32 pcr_idx; | |
61 | __be32 auth_area_size; | |
62 | struct tpm2_null_auth_area auth_area; | |
63 | __be32 digest_cnt; | |
64 | __be16 hash_alg; | |
65 | u8 digest[TPM_DIGEST_SIZE]; | |
66 | } __packed; | |
67 | ||
68 | struct tpm2_get_tpm_pt_in { | |
69 | __be32 cap_id; | |
70 | __be32 property_id; | |
71 | __be32 property_cnt; | |
72 | } __packed; | |
73 | ||
74 | struct tpm2_get_tpm_pt_out { | |
75 | u8 more_data; | |
76 | __be32 subcap_id; | |
77 | __be32 property_cnt; | |
78 | __be32 property_id; | |
79 | __be32 value; | |
80 | } __packed; | |
81 | ||
82 | struct tpm2_get_random_in { | |
83 | __be16 size; | |
84 | } __packed; | |
85 | ||
86 | struct tpm2_get_random_out { | |
87 | __be16 size; | |
88 | u8 buffer[TPM_MAX_RNG_DATA]; | |
89 | } __packed; | |
90 | ||
91 | union tpm2_cmd_params { | |
92 | struct tpm2_startup_in startup_in; | |
93 | struct tpm2_self_test_in selftest_in; | |
94 | struct tpm2_pcr_read_in pcrread_in; | |
95 | struct tpm2_pcr_read_out pcrread_out; | |
96 | struct tpm2_pcr_extend_in pcrextend_in; | |
97 | struct tpm2_get_tpm_pt_in get_tpm_pt_in; | |
98 | struct tpm2_get_tpm_pt_out get_tpm_pt_out; | |
99 | struct tpm2_get_random_in getrandom_in; | |
100 | struct tpm2_get_random_out getrandom_out; | |
101 | }; | |
102 | ||
103 | struct tpm2_cmd { | |
104 | tpm_cmd_header header; | |
105 | union tpm2_cmd_params params; | |
106 | } __packed; | |
107 | ||
5ca4c20c JS |
108 | struct tpm2_hash { |
109 | unsigned int crypto_id; | |
110 | unsigned int tpm_id; | |
111 | }; | |
112 | ||
113 | static struct tpm2_hash tpm2_hash_map[] = { | |
114 | {HASH_ALGO_SHA1, TPM2_ALG_SHA1}, | |
115 | {HASH_ALGO_SHA256, TPM2_ALG_SHA256}, | |
116 | {HASH_ALGO_SHA384, TPM2_ALG_SHA384}, | |
117 | {HASH_ALGO_SHA512, TPM2_ALG_SHA512}, | |
118 | {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256}, | |
119 | }; | |
120 | ||
7a1d7e6d JS |
121 | /* |
122 | * Array with one entry per ordinal defining the maximum amount | |
123 | * of time the chip could take to return the result. The values | |
124 | * of the SHORT, MEDIUM, and LONG durations are taken from the | |
125 | * PC Client Profile (PTP) specification. | |
126 | */ | |
127 | static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = { | |
128 | TPM_UNDEFINED, /* 11F */ | |
129 | TPM_UNDEFINED, /* 120 */ | |
130 | TPM_LONG, /* 121 */ | |
131 | TPM_UNDEFINED, /* 122 */ | |
132 | TPM_UNDEFINED, /* 123 */ | |
133 | TPM_UNDEFINED, /* 124 */ | |
134 | TPM_UNDEFINED, /* 125 */ | |
135 | TPM_UNDEFINED, /* 126 */ | |
136 | TPM_UNDEFINED, /* 127 */ | |
137 | TPM_UNDEFINED, /* 128 */ | |
138 | TPM_LONG, /* 129 */ | |
139 | TPM_UNDEFINED, /* 12a */ | |
140 | TPM_UNDEFINED, /* 12b */ | |
141 | TPM_UNDEFINED, /* 12c */ | |
142 | TPM_UNDEFINED, /* 12d */ | |
143 | TPM_UNDEFINED, /* 12e */ | |
144 | TPM_UNDEFINED, /* 12f */ | |
145 | TPM_UNDEFINED, /* 130 */ | |
146 | TPM_UNDEFINED, /* 131 */ | |
147 | TPM_UNDEFINED, /* 132 */ | |
148 | TPM_UNDEFINED, /* 133 */ | |
149 | TPM_UNDEFINED, /* 134 */ | |
150 | TPM_UNDEFINED, /* 135 */ | |
151 | TPM_UNDEFINED, /* 136 */ | |
152 | TPM_UNDEFINED, /* 137 */ | |
153 | TPM_UNDEFINED, /* 138 */ | |
154 | TPM_UNDEFINED, /* 139 */ | |
155 | TPM_UNDEFINED, /* 13a */ | |
156 | TPM_UNDEFINED, /* 13b */ | |
157 | TPM_UNDEFINED, /* 13c */ | |
158 | TPM_UNDEFINED, /* 13d */ | |
159 | TPM_MEDIUM, /* 13e */ | |
160 | TPM_UNDEFINED, /* 13f */ | |
161 | TPM_UNDEFINED, /* 140 */ | |
162 | TPM_UNDEFINED, /* 141 */ | |
163 | TPM_UNDEFINED, /* 142 */ | |
164 | TPM_LONG, /* 143 */ | |
165 | TPM_MEDIUM, /* 144 */ | |
166 | TPM_UNDEFINED, /* 145 */ | |
167 | TPM_UNDEFINED, /* 146 */ | |
168 | TPM_UNDEFINED, /* 147 */ | |
169 | TPM_UNDEFINED, /* 148 */ | |
170 | TPM_UNDEFINED, /* 149 */ | |
171 | TPM_UNDEFINED, /* 14a */ | |
172 | TPM_UNDEFINED, /* 14b */ | |
173 | TPM_UNDEFINED, /* 14c */ | |
174 | TPM_UNDEFINED, /* 14d */ | |
175 | TPM_LONG, /* 14e */ | |
176 | TPM_UNDEFINED, /* 14f */ | |
177 | TPM_UNDEFINED, /* 150 */ | |
178 | TPM_UNDEFINED, /* 151 */ | |
179 | TPM_UNDEFINED, /* 152 */ | |
180 | TPM_UNDEFINED, /* 153 */ | |
181 | TPM_UNDEFINED, /* 154 */ | |
182 | TPM_UNDEFINED, /* 155 */ | |
183 | TPM_UNDEFINED, /* 156 */ | |
184 | TPM_UNDEFINED, /* 157 */ | |
185 | TPM_UNDEFINED, /* 158 */ | |
186 | TPM_UNDEFINED, /* 159 */ | |
187 | TPM_UNDEFINED, /* 15a */ | |
188 | TPM_UNDEFINED, /* 15b */ | |
189 | TPM_MEDIUM, /* 15c */ | |
190 | TPM_UNDEFINED, /* 15d */ | |
191 | TPM_UNDEFINED, /* 15e */ | |
192 | TPM_UNDEFINED, /* 15f */ | |
193 | TPM_UNDEFINED, /* 160 */ | |
194 | TPM_UNDEFINED, /* 161 */ | |
195 | TPM_UNDEFINED, /* 162 */ | |
196 | TPM_UNDEFINED, /* 163 */ | |
197 | TPM_UNDEFINED, /* 164 */ | |
198 | TPM_UNDEFINED, /* 165 */ | |
199 | TPM_UNDEFINED, /* 166 */ | |
200 | TPM_UNDEFINED, /* 167 */ | |
201 | TPM_UNDEFINED, /* 168 */ | |
202 | TPM_UNDEFINED, /* 169 */ | |
203 | TPM_UNDEFINED, /* 16a */ | |
204 | TPM_UNDEFINED, /* 16b */ | |
205 | TPM_UNDEFINED, /* 16c */ | |
206 | TPM_UNDEFINED, /* 16d */ | |
207 | TPM_UNDEFINED, /* 16e */ | |
208 | TPM_UNDEFINED, /* 16f */ | |
209 | TPM_UNDEFINED, /* 170 */ | |
210 | TPM_UNDEFINED, /* 171 */ | |
211 | TPM_UNDEFINED, /* 172 */ | |
212 | TPM_UNDEFINED, /* 173 */ | |
213 | TPM_UNDEFINED, /* 174 */ | |
214 | TPM_UNDEFINED, /* 175 */ | |
215 | TPM_UNDEFINED, /* 176 */ | |
216 | TPM_LONG, /* 177 */ | |
217 | TPM_UNDEFINED, /* 178 */ | |
218 | TPM_UNDEFINED, /* 179 */ | |
219 | TPM_MEDIUM, /* 17a */ | |
220 | TPM_LONG, /* 17b */ | |
221 | TPM_UNDEFINED, /* 17c */ | |
222 | TPM_UNDEFINED, /* 17d */ | |
223 | TPM_UNDEFINED, /* 17e */ | |
224 | TPM_UNDEFINED, /* 17f */ | |
225 | TPM_UNDEFINED, /* 180 */ | |
226 | TPM_UNDEFINED, /* 181 */ | |
227 | TPM_MEDIUM, /* 182 */ | |
228 | TPM_UNDEFINED, /* 183 */ | |
229 | TPM_UNDEFINED, /* 184 */ | |
230 | TPM_MEDIUM, /* 185 */ | |
231 | TPM_MEDIUM, /* 186 */ | |
232 | TPM_UNDEFINED, /* 187 */ | |
233 | TPM_UNDEFINED, /* 188 */ | |
234 | TPM_UNDEFINED, /* 189 */ | |
235 | TPM_UNDEFINED, /* 18a */ | |
236 | TPM_UNDEFINED, /* 18b */ | |
237 | TPM_UNDEFINED, /* 18c */ | |
238 | TPM_UNDEFINED, /* 18d */ | |
239 | TPM_UNDEFINED, /* 18e */ | |
240 | TPM_UNDEFINED /* 18f */ | |
241 | }; | |
242 | ||
243 | #define TPM2_PCR_READ_IN_SIZE \ | |
244 | (sizeof(struct tpm_input_header) + \ | |
245 | sizeof(struct tpm2_pcr_read_in)) | |
246 | ||
247 | static const struct tpm_input_header tpm2_pcrread_header = { | |
248 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
249 | .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE), | |
250 | .ordinal = cpu_to_be32(TPM2_CC_PCR_READ) | |
251 | }; | |
252 | ||
253 | /** | |
254 | * tpm2_pcr_read() - read a PCR value | |
255 | * @chip: TPM chip to use. | |
256 | * @pcr_idx: index of the PCR to read. | |
257 | * @ref_buf: buffer to store the resulting hash, | |
258 | * | |
259 | * 0 is returned when the operation is successful. If a negative number is | |
260 | * returned it remarks a POSIX error code. If a positive number is returned | |
261 | * it remarks a TPM error. | |
262 | */ | |
263 | int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) | |
264 | { | |
265 | int rc; | |
266 | struct tpm2_cmd cmd; | |
267 | u8 *buf; | |
268 | ||
269 | if (pcr_idx >= TPM2_PLATFORM_PCR) | |
270 | return -EINVAL; | |
271 | ||
272 | cmd.header.in = tpm2_pcrread_header; | |
273 | cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1); | |
274 | cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1); | |
275 | cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN; | |
276 | ||
277 | memset(cmd.params.pcrread_in.pcr_select, 0, | |
278 | sizeof(cmd.params.pcrread_in.pcr_select)); | |
279 | cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); | |
280 | ||
281 | rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), | |
282 | "attempting to read a pcr value"); | |
283 | if (rc == 0) { | |
284 | buf = cmd.params.pcrread_out.digest; | |
285 | memcpy(res_buf, buf, TPM_DIGEST_SIZE); | |
286 | } | |
287 | ||
288 | return rc; | |
289 | } | |
290 | ||
291 | #define TPM2_GET_PCREXTEND_IN_SIZE \ | |
292 | (sizeof(struct tpm_input_header) + \ | |
293 | sizeof(struct tpm2_pcr_extend_in)) | |
294 | ||
295 | static const struct tpm_input_header tpm2_pcrextend_header = { | |
296 | .tag = cpu_to_be16(TPM2_ST_SESSIONS), | |
297 | .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE), | |
298 | .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND) | |
299 | }; | |
300 | ||
301 | /** | |
302 | * tpm2_pcr_extend() - extend a PCR value | |
303 | * @chip: TPM chip to use. | |
304 | * @pcr_idx: index of the PCR. | |
305 | * @hash: hash value to use for the extend operation. | |
306 | * | |
307 | * 0 is returned when the operation is successful. If a negative number is | |
308 | * returned it remarks a POSIX error code. If a positive number is returned | |
309 | * it remarks a TPM error. | |
310 | */ | |
311 | int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash) | |
312 | { | |
313 | struct tpm2_cmd cmd; | |
314 | int rc; | |
315 | ||
316 | cmd.header.in = tpm2_pcrextend_header; | |
317 | cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); | |
318 | cmd.params.pcrextend_in.auth_area_size = | |
319 | cpu_to_be32(sizeof(struct tpm2_null_auth_area)); | |
320 | cmd.params.pcrextend_in.auth_area.handle = | |
321 | cpu_to_be32(TPM2_RS_PW); | |
322 | cmd.params.pcrextend_in.auth_area.nonce_size = 0; | |
323 | cmd.params.pcrextend_in.auth_area.attributes = 0; | |
324 | cmd.params.pcrextend_in.auth_area.auth_size = 0; | |
325 | cmd.params.pcrextend_in.digest_cnt = cpu_to_be32(1); | |
326 | cmd.params.pcrextend_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1); | |
327 | memcpy(cmd.params.pcrextend_in.digest, hash, TPM_DIGEST_SIZE); | |
328 | ||
329 | rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), | |
330 | "attempting extend a PCR value"); | |
331 | ||
332 | return rc; | |
333 | } | |
334 | ||
335 | #define TPM2_GETRANDOM_IN_SIZE \ | |
336 | (sizeof(struct tpm_input_header) + \ | |
337 | sizeof(struct tpm2_get_random_in)) | |
338 | ||
339 | static const struct tpm_input_header tpm2_getrandom_header = { | |
340 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
341 | .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE), | |
342 | .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM) | |
343 | }; | |
344 | ||
345 | /** | |
346 | * tpm2_get_random() - get random bytes from the TPM RNG | |
347 | * @chip: TPM chip to use | |
348 | * @out: destination buffer for the random bytes | |
349 | * @max: the max number of bytes to write to @out | |
350 | * | |
351 | * 0 is returned when the operation is successful. If a negative number is | |
352 | * returned it remarks a POSIX error code. If a positive number is returned | |
353 | * it remarks a TPM error. | |
354 | */ | |
355 | int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max) | |
356 | { | |
357 | struct tpm2_cmd cmd; | |
358 | u32 recd; | |
359 | u32 num_bytes; | |
360 | int err; | |
361 | int total = 0; | |
362 | int retries = 5; | |
363 | u8 *dest = out; | |
364 | ||
365 | num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer)); | |
366 | ||
367 | if (!out || !num_bytes || | |
368 | max > sizeof(cmd.params.getrandom_out.buffer)) | |
369 | return -EINVAL; | |
370 | ||
371 | do { | |
372 | cmd.header.in = tpm2_getrandom_header; | |
373 | cmd.params.getrandom_in.size = cpu_to_be16(num_bytes); | |
374 | ||
375 | err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), | |
376 | "attempting get random"); | |
377 | if (err) | |
378 | break; | |
379 | ||
380 | recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size), | |
381 | num_bytes); | |
382 | memcpy(dest, cmd.params.getrandom_out.buffer, recd); | |
383 | ||
384 | dest += recd; | |
385 | total += recd; | |
386 | num_bytes -= recd; | |
387 | } while (retries-- && total < max); | |
388 | ||
389 | return total ? total : -EIO; | |
390 | } | |
391 | ||
392 | #define TPM2_GET_TPM_PT_IN_SIZE \ | |
393 | (sizeof(struct tpm_input_header) + \ | |
394 | sizeof(struct tpm2_get_tpm_pt_in)) | |
395 | ||
396 | static const struct tpm_input_header tpm2_get_tpm_pt_header = { | |
397 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
398 | .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE), | |
399 | .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY) | |
400 | }; | |
401 | ||
954650ef JS |
402 | /** |
403 | * Append TPMS_AUTH_COMMAND to the buffer. The buffer must be allocated with | |
404 | * tpm_buf_alloc(). | |
405 | * | |
406 | * @param buf: an allocated tpm_buf instance | |
407 | * @param nonce: the session nonce, may be NULL if not used | |
408 | * @param nonce_len: the session nonce length, may be 0 if not used | |
409 | * @param attributes: the session attributes | |
410 | * @param hmac: the session HMAC or password, may be NULL if not used | |
411 | * @param hmac_len: the session HMAC or password length, maybe 0 if not used | |
412 | */ | |
413 | static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle, | |
414 | const u8 *nonce, u16 nonce_len, | |
415 | u8 attributes, | |
416 | const u8 *hmac, u16 hmac_len) | |
417 | { | |
418 | tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len); | |
419 | tpm_buf_append_u32(buf, session_handle); | |
420 | tpm_buf_append_u16(buf, nonce_len); | |
421 | ||
422 | if (nonce && nonce_len) | |
423 | tpm_buf_append(buf, nonce, nonce_len); | |
424 | ||
425 | tpm_buf_append_u8(buf, attributes); | |
426 | tpm_buf_append_u16(buf, hmac_len); | |
427 | ||
428 | if (hmac && hmac_len) | |
429 | tpm_buf_append(buf, hmac, hmac_len); | |
430 | } | |
431 | ||
432 | /** | |
433 | * tpm2_seal_trusted() - seal a trusted key | |
434 | * @chip_num: A specific chip number for the request or TPM_ANY_NUM | |
435 | * @options: authentication values and other options | |
436 | * @payload: the key data in clear and encrypted form | |
437 | * | |
438 | * Returns < 0 on error and 0 on success. | |
439 | */ | |
440 | int tpm2_seal_trusted(struct tpm_chip *chip, | |
441 | struct trusted_key_payload *payload, | |
442 | struct trusted_key_options *options) | |
443 | { | |
444 | unsigned int blob_len; | |
445 | struct tpm_buf buf; | |
5ca4c20c JS |
446 | u32 hash; |
447 | int i; | |
954650ef JS |
448 | int rc; |
449 | ||
5ca4c20c JS |
450 | for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) { |
451 | if (options->hash == tpm2_hash_map[i].crypto_id) { | |
452 | hash = tpm2_hash_map[i].tpm_id; | |
453 | break; | |
454 | } | |
455 | } | |
456 | ||
457 | if (i == ARRAY_SIZE(tpm2_hash_map)) | |
458 | return -EINVAL; | |
459 | ||
954650ef JS |
460 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); |
461 | if (rc) | |
462 | return rc; | |
463 | ||
464 | tpm_buf_append_u32(&buf, options->keyhandle); | |
465 | tpm2_buf_append_auth(&buf, TPM2_RS_PW, | |
466 | NULL /* nonce */, 0, | |
467 | 0 /* session_attributes */, | |
468 | options->keyauth /* hmac */, | |
469 | TPM_DIGEST_SIZE); | |
470 | ||
471 | /* sensitive */ | |
2e31125c | 472 | tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1); |
954650ef JS |
473 | |
474 | tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE); | |
475 | tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE); | |
2e31125c | 476 | tpm_buf_append_u16(&buf, payload->key_len + 1); |
954650ef | 477 | tpm_buf_append(&buf, payload->key, payload->key_len); |
2e31125c | 478 | tpm_buf_append_u8(&buf, payload->migratable); |
954650ef JS |
479 | |
480 | /* public */ | |
f3c82ade | 481 | tpm_buf_append_u16(&buf, 14 + options->policydigest_len); |
954650ef | 482 | tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH); |
5ca4c20c | 483 | tpm_buf_append_u16(&buf, hash); |
5beb0c43 JS |
484 | |
485 | /* policy */ | |
f3c82ade | 486 | if (options->policydigest_len) { |
5beb0c43 | 487 | tpm_buf_append_u32(&buf, 0); |
f3c82ade | 488 | tpm_buf_append_u16(&buf, options->policydigest_len); |
5beb0c43 | 489 | tpm_buf_append(&buf, options->policydigest, |
f3c82ade | 490 | options->policydigest_len); |
5beb0c43 JS |
491 | } else { |
492 | tpm_buf_append_u32(&buf, TPM2_ATTR_USER_WITH_AUTH); | |
493 | tpm_buf_append_u16(&buf, 0); | |
494 | } | |
495 | ||
496 | /* public parameters */ | |
954650ef JS |
497 | tpm_buf_append_u16(&buf, TPM2_ALG_NULL); |
498 | tpm_buf_append_u16(&buf, 0); | |
499 | ||
500 | /* outside info */ | |
501 | tpm_buf_append_u16(&buf, 0); | |
502 | ||
503 | /* creation PCR */ | |
504 | tpm_buf_append_u32(&buf, 0); | |
505 | ||
506 | if (buf.flags & TPM_BUF_OVERFLOW) { | |
507 | rc = -E2BIG; | |
508 | goto out; | |
509 | } | |
510 | ||
511 | rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "sealing data"); | |
512 | if (rc) | |
513 | goto out; | |
514 | ||
515 | blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]); | |
516 | if (blob_len > MAX_BLOB_SIZE) { | |
517 | rc = -E2BIG; | |
518 | goto out; | |
519 | } | |
520 | ||
521 | memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len); | |
522 | payload->blob_len = blob_len; | |
523 | ||
524 | out: | |
525 | tpm_buf_destroy(&buf); | |
526 | ||
5ca4c20c JS |
527 | if (rc > 0) { |
528 | if ((rc & TPM2_RC_HASH) == TPM2_RC_HASH) | |
529 | rc = -EINVAL; | |
530 | else | |
531 | rc = -EPERM; | |
532 | } | |
954650ef JS |
533 | |
534 | return rc; | |
535 | } | |
536 | ||
537 | static int tpm2_load(struct tpm_chip *chip, | |
538 | struct trusted_key_payload *payload, | |
539 | struct trusted_key_options *options, | |
540 | u32 *blob_handle) | |
541 | { | |
542 | struct tpm_buf buf; | |
543 | unsigned int private_len; | |
544 | unsigned int public_len; | |
545 | unsigned int blob_len; | |
546 | int rc; | |
547 | ||
548 | private_len = be16_to_cpup((__be16 *) &payload->blob[0]); | |
549 | if (private_len > (payload->blob_len - 2)) | |
550 | return -E2BIG; | |
551 | ||
552 | public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]); | |
553 | blob_len = private_len + public_len + 4; | |
554 | if (blob_len > payload->blob_len) | |
555 | return -E2BIG; | |
556 | ||
557 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); | |
558 | if (rc) | |
559 | return rc; | |
560 | ||
561 | tpm_buf_append_u32(&buf, options->keyhandle); | |
562 | tpm2_buf_append_auth(&buf, TPM2_RS_PW, | |
563 | NULL /* nonce */, 0, | |
564 | 0 /* session_attributes */, | |
565 | options->keyauth /* hmac */, | |
566 | TPM_DIGEST_SIZE); | |
567 | ||
568 | tpm_buf_append(&buf, payload->blob, blob_len); | |
569 | ||
570 | if (buf.flags & TPM_BUF_OVERFLOW) { | |
571 | rc = -E2BIG; | |
572 | goto out; | |
573 | } | |
574 | ||
575 | rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "loading blob"); | |
576 | if (!rc) | |
577 | *blob_handle = be32_to_cpup( | |
578 | (__be32 *) &buf.data[TPM_HEADER_SIZE]); | |
579 | ||
580 | out: | |
581 | tpm_buf_destroy(&buf); | |
582 | ||
583 | if (rc > 0) | |
584 | rc = -EPERM; | |
585 | ||
586 | return rc; | |
587 | } | |
588 | ||
589 | static void tpm2_flush_context(struct tpm_chip *chip, u32 handle) | |
590 | { | |
591 | struct tpm_buf buf; | |
592 | int rc; | |
593 | ||
594 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); | |
595 | if (rc) { | |
596 | dev_warn(chip->pdev, "0x%08x was not flushed, out of memory\n", | |
597 | handle); | |
598 | return; | |
599 | } | |
600 | ||
601 | tpm_buf_append_u32(&buf, handle); | |
602 | ||
603 | rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "flushing context"); | |
604 | if (rc) | |
605 | dev_warn(chip->pdev, "0x%08x was not flushed, rc=%d\n", handle, | |
606 | rc); | |
607 | ||
608 | tpm_buf_destroy(&buf); | |
609 | } | |
610 | ||
611 | static int tpm2_unseal(struct tpm_chip *chip, | |
612 | struct trusted_key_payload *payload, | |
613 | struct trusted_key_options *options, | |
614 | u32 blob_handle) | |
615 | { | |
616 | struct tpm_buf buf; | |
2e31125c JS |
617 | u16 data_len; |
618 | u8 *data; | |
954650ef JS |
619 | int rc; |
620 | ||
621 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); | |
622 | if (rc) | |
623 | return rc; | |
624 | ||
625 | tpm_buf_append_u32(&buf, blob_handle); | |
5beb0c43 JS |
626 | tpm2_buf_append_auth(&buf, |
627 | options->policyhandle ? | |
628 | options->policyhandle : TPM2_RS_PW, | |
954650ef JS |
629 | NULL /* nonce */, 0, |
630 | 0 /* session_attributes */, | |
631 | options->blobauth /* hmac */, | |
632 | TPM_DIGEST_SIZE); | |
633 | ||
634 | rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "unsealing"); | |
635 | if (rc > 0) | |
636 | rc = -EPERM; | |
637 | ||
638 | if (!rc) { | |
2e31125c | 639 | data_len = be16_to_cpup( |
954650ef | 640 | (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]); |
2e31125c | 641 | data = &buf.data[TPM_HEADER_SIZE + 6]; |
954650ef | 642 | |
2e31125c JS |
643 | memcpy(payload->key, data, data_len - 1); |
644 | payload->key_len = data_len - 1; | |
645 | payload->migratable = data[data_len - 1]; | |
954650ef JS |
646 | } |
647 | ||
648 | tpm_buf_destroy(&buf); | |
649 | return rc; | |
650 | } | |
651 | ||
652 | /** | |
653 | * tpm_unseal_trusted() - unseal a trusted key | |
654 | * @chip_num: A specific chip number for the request or TPM_ANY_NUM | |
655 | * @options: authentication values and other options | |
656 | * @payload: the key data in clear and encrypted form | |
657 | * | |
658 | * Returns < 0 on error and 0 on success. | |
659 | */ | |
660 | int tpm2_unseal_trusted(struct tpm_chip *chip, | |
661 | struct trusted_key_payload *payload, | |
662 | struct trusted_key_options *options) | |
663 | { | |
664 | u32 blob_handle; | |
665 | int rc; | |
666 | ||
667 | rc = tpm2_load(chip, payload, options, &blob_handle); | |
668 | if (rc) | |
669 | return rc; | |
670 | ||
671 | rc = tpm2_unseal(chip, payload, options, blob_handle); | |
672 | ||
673 | tpm2_flush_context(chip, blob_handle); | |
674 | ||
675 | return rc; | |
676 | } | |
677 | ||
7a1d7e6d JS |
678 | /** |
679 | * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property | |
680 | * @chip: TPM chip to use. | |
681 | * @property_id: property ID. | |
682 | * @value: output variable. | |
683 | * @desc: passed to tpm_transmit_cmd() | |
684 | * | |
685 | * 0 is returned when the operation is successful. If a negative number is | |
686 | * returned it remarks a POSIX error code. If a positive number is returned | |
687 | * it remarks a TPM error. | |
688 | */ | |
689 | ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, | |
690 | const char *desc) | |
691 | { | |
692 | struct tpm2_cmd cmd; | |
693 | int rc; | |
694 | ||
695 | cmd.header.in = tpm2_get_tpm_pt_header; | |
696 | cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); | |
697 | cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id); | |
698 | cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1); | |
699 | ||
700 | rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), desc); | |
701 | if (!rc) | |
702 | *value = cmd.params.get_tpm_pt_out.value; | |
703 | ||
704 | return rc; | |
705 | } | |
706 | ||
707 | #define TPM2_STARTUP_IN_SIZE \ | |
708 | (sizeof(struct tpm_input_header) + \ | |
709 | sizeof(struct tpm2_startup_in)) | |
710 | ||
711 | static const struct tpm_input_header tpm2_startup_header = { | |
712 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
713 | .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE), | |
714 | .ordinal = cpu_to_be32(TPM2_CC_STARTUP) | |
715 | }; | |
716 | ||
717 | /** | |
718 | * tpm2_startup() - send startup command to the TPM chip | |
719 | * @chip: TPM chip to use. | |
720 | * @startup_type startup type. The value is either | |
721 | * TPM_SU_CLEAR or TPM_SU_STATE. | |
722 | * | |
723 | * 0 is returned when the operation is successful. If a negative number is | |
724 | * returned it remarks a POSIX error code. If a positive number is returned | |
725 | * it remarks a TPM error. | |
726 | */ | |
727 | int tpm2_startup(struct tpm_chip *chip, u16 startup_type) | |
728 | { | |
729 | struct tpm2_cmd cmd; | |
730 | ||
731 | cmd.header.in = tpm2_startup_header; | |
732 | ||
733 | cmd.params.startup_in.startup_type = cpu_to_be16(startup_type); | |
734 | return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), | |
735 | "attempting to start the TPM"); | |
736 | } | |
737 | EXPORT_SYMBOL_GPL(tpm2_startup); | |
738 | ||
739 | #define TPM2_SHUTDOWN_IN_SIZE \ | |
740 | (sizeof(struct tpm_input_header) + \ | |
741 | sizeof(struct tpm2_startup_in)) | |
742 | ||
743 | static const struct tpm_input_header tpm2_shutdown_header = { | |
744 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
745 | .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE), | |
746 | .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN) | |
747 | }; | |
748 | ||
749 | /** | |
750 | * tpm2_shutdown() - send shutdown command to the TPM chip | |
751 | * @chip: TPM chip to use. | |
752 | * @shutdown_type shutdown type. The value is either | |
753 | * TPM_SU_CLEAR or TPM_SU_STATE. | |
7a1d7e6d | 754 | */ |
74d6b3ce | 755 | void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) |
7a1d7e6d JS |
756 | { |
757 | struct tpm2_cmd cmd; | |
74d6b3ce | 758 | int rc; |
7a1d7e6d JS |
759 | |
760 | cmd.header.in = tpm2_shutdown_header; | |
7a1d7e6d | 761 | cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type); |
74d6b3ce JS |
762 | |
763 | rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), "stopping the TPM"); | |
764 | ||
765 | /* In places where shutdown command is sent there's no much we can do | |
766 | * except print the error code on a system failure. | |
767 | */ | |
768 | if (rc < 0) | |
769 | dev_warn(chip->pdev, "transmit returned %d while stopping the TPM", | |
770 | rc); | |
7a1d7e6d JS |
771 | } |
772 | EXPORT_SYMBOL_GPL(tpm2_shutdown); | |
773 | ||
774 | /* | |
775 | * tpm2_calc_ordinal_duration() - maximum duration for a command | |
776 | * @chip: TPM chip to use. | |
777 | * @ordinal: command code number. | |
778 | * | |
779 | * 0 is returned when the operation is successful. If a negative number is | |
780 | * returned it remarks a POSIX error code. If a positive number is returned | |
781 | * it remarks a TPM error. | |
782 | */ | |
783 | unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) | |
784 | { | |
785 | int index = TPM_UNDEFINED; | |
786 | int duration = 0; | |
787 | ||
788 | if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST) | |
789 | index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST]; | |
790 | ||
791 | if (index != TPM_UNDEFINED) | |
792 | duration = chip->vendor.duration[index]; | |
793 | ||
794 | if (duration <= 0) | |
795 | duration = 2 * 60 * HZ; | |
796 | ||
797 | return duration; | |
798 | } | |
799 | EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration); | |
800 | ||
801 | #define TPM2_SELF_TEST_IN_SIZE \ | |
802 | (sizeof(struct tpm_input_header) + \ | |
803 | sizeof(struct tpm2_self_test_in)) | |
804 | ||
805 | static const struct tpm_input_header tpm2_selftest_header = { | |
806 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), | |
807 | .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE), | |
808 | .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST) | |
809 | }; | |
810 | ||
811 | /** | |
812 | * tpm2_continue_selftest() - start a self test | |
813 | * @chip: TPM chip to use | |
814 | * @full: test all commands instead of testing only those that were not | |
815 | * previously tested. | |
816 | * | |
817 | * 0 is returned when the operation is successful. If a negative number is | |
818 | * returned it remarks a POSIX error code. If a positive number is returned | |
819 | * it remarks a TPM error. | |
820 | */ | |
821 | static int tpm2_start_selftest(struct tpm_chip *chip, bool full) | |
822 | { | |
823 | int rc; | |
824 | struct tpm2_cmd cmd; | |
825 | ||
826 | cmd.header.in = tpm2_selftest_header; | |
827 | cmd.params.selftest_in.full_test = full; | |
828 | ||
829 | rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, | |
830 | "continue selftest"); | |
831 | ||
832 | /* At least some prototype chips seem to give RC_TESTING error | |
833 | * immediately. This is a workaround for that. | |
834 | */ | |
835 | if (rc == TPM2_RC_TESTING) { | |
836 | dev_warn(chip->pdev, "Got RC_TESTING, ignoring\n"); | |
837 | rc = 0; | |
838 | } | |
839 | ||
840 | return rc; | |
841 | } | |
842 | ||
843 | /** | |
844 | * tpm2_do_selftest() - run a full self test | |
845 | * @chip: TPM chip to use | |
846 | * | |
847 | * During the self test TPM2 commands return with the error code RC_TESTING. | |
848 | * Waiting is done by issuing PCR read until it executes successfully. | |
849 | * | |
850 | * 0 is returned when the operation is successful. If a negative number is | |
851 | * returned it remarks a POSIX error code. If a positive number is returned | |
852 | * it remarks a TPM error. | |
853 | */ | |
854 | int tpm2_do_selftest(struct tpm_chip *chip) | |
855 | { | |
856 | int rc; | |
857 | unsigned int loops; | |
858 | unsigned int delay_msec = 100; | |
859 | unsigned long duration; | |
860 | struct tpm2_cmd cmd; | |
861 | int i; | |
862 | ||
863 | duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST); | |
864 | ||
865 | loops = jiffies_to_msecs(duration) / delay_msec; | |
866 | ||
867 | rc = tpm2_start_selftest(chip, true); | |
868 | if (rc) | |
869 | return rc; | |
870 | ||
871 | for (i = 0; i < loops; i++) { | |
872 | /* Attempt to read a PCR value */ | |
873 | cmd.header.in = tpm2_pcrread_header; | |
874 | cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1); | |
875 | cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1); | |
876 | cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN; | |
877 | cmd.params.pcrread_in.pcr_select[0] = 0x01; | |
878 | cmd.params.pcrread_in.pcr_select[1] = 0x00; | |
879 | cmd.params.pcrread_in.pcr_select[2] = 0x00; | |
880 | ||
881 | rc = tpm_transmit_cmd(chip, (u8 *) &cmd, sizeof(cmd), NULL); | |
882 | if (rc < 0) | |
883 | break; | |
884 | ||
885 | rc = be32_to_cpu(cmd.header.out.return_code); | |
886 | if (rc != TPM2_RC_TESTING) | |
887 | break; | |
888 | ||
889 | msleep(delay_msec); | |
890 | } | |
891 | ||
892 | return rc; | |
893 | } | |
894 | EXPORT_SYMBOL_GPL(tpm2_do_selftest); | |
895 | ||
896 | /** | |
897 | * tpm2_gen_interrupt() - generate an interrupt | |
898 | * @chip: TPM chip to use | |
7a1d7e6d JS |
899 | * |
900 | * 0 is returned when the operation is successful. If a negative number is | |
901 | * returned it remarks a POSIX error code. If a positive number is returned | |
902 | * it remarks a TPM error. | |
903 | */ | |
4d5f2051 | 904 | int tpm2_gen_interrupt(struct tpm_chip *chip) |
7a1d7e6d | 905 | { |
7a1d7e6d JS |
906 | u32 dummy; |
907 | ||
4d5f2051 JS |
908 | return tpm2_get_tpm_pt(chip, 0x100, &dummy, |
909 | "attempting to generate an interrupt"); | |
7a1d7e6d JS |
910 | } |
911 | EXPORT_SYMBOL_GPL(tpm2_gen_interrupt); | |
4d5f2051 JS |
912 | |
913 | /** | |
914 | * tpm2_probe() - probe TPM 2.0 | |
915 | * @chip: TPM chip to use | |
916 | * | |
917 | * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on | |
918 | * the reply tag. | |
919 | */ | |
920 | int tpm2_probe(struct tpm_chip *chip) | |
921 | { | |
922 | struct tpm2_cmd cmd; | |
923 | int rc; | |
924 | ||
925 | cmd.header.in = tpm2_get_tpm_pt_header; | |
926 | cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); | |
927 | cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100); | |
928 | cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1); | |
929 | ||
930 | rc = tpm_transmit(chip, (const char *) &cmd, sizeof(cmd)); | |
931 | if (rc < 0) | |
932 | return rc; | |
933 | else if (rc < TPM_HEADER_SIZE) | |
934 | return -EFAULT; | |
935 | ||
936 | if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS) | |
937 | chip->flags |= TPM_CHIP_FLAG_TPM2; | |
938 | ||
939 | return 0; | |
940 | } | |
941 | EXPORT_SYMBOL_GPL(tpm2_probe); |