1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
9 #include <tpm-common.h>
11 #include <linux/bitops.h>
12 #include "tpm-utils.h"
14 u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode)
16 const u8 command_v2[12] = {
17 tpm_u16(TPM2_ST_NO_SESSIONS),
19 tpm_u32(TPM2_CC_STARTUP),
25 * Note TPM2_Startup command will return RC_SUCCESS the first time,
26 * but will return RC_INITIALIZE otherwise.
28 ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
29 if (ret && ret != TPM2_RC_INITIALIZE)
35 u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test)
37 const u8 command_v2[12] = {
38 tpm_u16(TPM2_ST_NO_SESSIONS),
40 tpm_u32(TPM2_CC_SELF_TEST),
44 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
47 u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
50 /* Length of the message header, up to start of password */
52 u8 command_v2[COMMAND_BUFFER_SIZE] = {
53 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
54 tpm_u32(offset + pw_sz), /* Length */
55 tpm_u32(TPM2_CC_CLEAR), /* Command code */
58 tpm_u32(handle), /* TPM resource handle */
61 tpm_u32(9 + pw_sz), /* Authorization size */
62 tpm_u32(TPM2_RS_PW), /* Session handle */
63 tpm_u16(0), /* Size of <nonce> */
64 /* <nonce> (if any) */
65 0, /* Attributes: Cont/Excl/Rst */
66 tpm_u16(pw_sz), /* Size of <hmac/password> */
67 /* STRING(pw) <hmac/password> (if any) */
72 * Fill the command structure starting from the first buffer:
73 * - the password (if any)
75 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
81 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
84 u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
85 const u8 *digest, u32 digest_len)
87 /* Length of the message header, up to start of digest */
89 u8 command_v2[COMMAND_BUFFER_SIZE] = {
90 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
91 tpm_u32(offset + digest_len), /* Length */
92 tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
95 tpm_u32(index), /* Handle (PCR Index) */
98 tpm_u32(9), /* Authorization size */
99 tpm_u32(TPM2_RS_PW), /* Session handle */
100 tpm_u16(0), /* Size of <nonce> */
101 /* <nonce> (if any) */
102 0, /* Attributes: Cont/Excl/Rst */
103 tpm_u16(0), /* Size of <hmac/password> */
104 /* <hmac/password> (if any) */
107 tpm_u32(1), /* Count (number of hashes) */
108 tpm_u16(algorithm), /* Algorithm of the hash */
109 /* STRING(digest) Digest */
114 * Fill the command structure starting from the first buffer:
117 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
118 offset, digest, digest_len);
120 return TPM_LIB_ERROR;
122 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
125 u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
126 void *data, unsigned int *updates)
128 u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
129 u8 command_v2[COMMAND_BUFFER_SIZE] = {
130 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
131 tpm_u32(17 + idx_array_sz), /* Length */
132 tpm_u32(TPM2_CC_PCR_READ), /* Command code */
134 /* TPML_PCR_SELECTION */
135 tpm_u32(1), /* Number of selections */
136 tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
137 idx_array_sz, /* Array size for selection */
138 /* bitmap(idx) Selected PCR bitmap */
140 size_t response_len = COMMAND_BUFFER_SIZE;
141 u8 response[COMMAND_BUFFER_SIZE];
142 unsigned int pcr_sel_idx = idx / 8;
143 u8 pcr_sel_bit = BIT(idx % 8);
144 unsigned int counter = 0;
147 if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
148 17 + pcr_sel_idx, pcr_sel_bit))
149 return TPM_LIB_ERROR;
151 ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
155 if (unpack_byte_string(response, response_len, "ds",
157 response_len - TPM2_DIGEST_LEN, data,
159 return TPM_LIB_ERROR;
167 u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
168 void *buf, size_t prop_count)
170 u8 command_v2[COMMAND_BUFFER_SIZE] = {
171 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
172 tpm_u32(22), /* Length */
173 tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
175 tpm_u32(capability), /* Capability */
176 tpm_u32(property), /* Property */
177 tpm_u32(prop_count), /* Property count */
179 u8 response[COMMAND_BUFFER_SIZE];
180 size_t response_len = COMMAND_BUFFER_SIZE;
181 unsigned int properties_off;
184 ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
189 * In the response buffer, the properties are located after the:
190 * tag (u16), response size (u32), response code (u32),
191 * YES/NO flag (u8), TPM_CAP (u32).
193 properties_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
194 sizeof(u8) + sizeof(u32);
195 memcpy(buf, &response[properties_off], response_len - properties_off);
200 u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz)
202 u8 command_v2[COMMAND_BUFFER_SIZE] = {
203 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
204 tpm_u32(27 + pw_sz), /* Length */
205 tpm_u32(TPM2_CC_DAM_RESET), /* Command code */
208 tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
211 tpm_u32(9 + pw_sz), /* Authorization size */
212 tpm_u32(TPM2_RS_PW), /* Session handle */
213 tpm_u16(0), /* Size of <nonce> */
214 /* <nonce> (if any) */
215 0, /* Attributes: Cont/Excl/Rst */
216 tpm_u16(pw_sz), /* Size of <hmac/password> */
217 /* STRING(pw) <hmac/password> (if any) */
219 unsigned int offset = 27;
223 * Fill the command structure starting from the first buffer:
224 * - the password (if any)
226 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
230 return TPM_LIB_ERROR;
232 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
235 u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
236 const ssize_t pw_sz, unsigned int max_tries,
237 unsigned int recovery_time,
238 unsigned int lockout_recovery)
240 u8 command_v2[COMMAND_BUFFER_SIZE] = {
241 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
242 tpm_u32(27 + pw_sz + 12), /* Length */
243 tpm_u32(TPM2_CC_DAM_PARAMETERS), /* Command code */
246 tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
249 tpm_u32(9 + pw_sz), /* Authorization size */
250 tpm_u32(TPM2_RS_PW), /* Session handle */
251 tpm_u16(0), /* Size of <nonce> */
252 /* <nonce> (if any) */
253 0, /* Attributes: Cont/Excl/Rst */
254 tpm_u16(pw_sz), /* Size of <hmac/password> */
255 /* STRING(pw) <hmac/password> (if any) */
257 /* LOCKOUT PARAMETERS */
258 /* tpm_u32(max_tries) Max tries (0, always lock) */
259 /* tpm_u32(recovery_time) Recovery time (0, no lock) */
260 /* tpm_u32(lockout_recovery) Lockout recovery */
262 unsigned int offset = 27;
266 * Fill the command structure starting from the first buffer:
267 * - the password (if any)
272 ret = pack_byte_string(command_v2, sizeof(command_v2), "sddd",
274 offset + pw_sz, max_tries,
275 offset + pw_sz + 4, recovery_time,
276 offset + pw_sz + 8, lockout_recovery);
277 offset += pw_sz + 12;
279 return TPM_LIB_ERROR;
281 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
284 int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
285 const ssize_t newpw_sz, const char *oldpw,
286 const ssize_t oldpw_sz)
288 unsigned int offset = 27;
289 u8 command_v2[COMMAND_BUFFER_SIZE] = {
290 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
291 tpm_u32(offset + oldpw_sz + 2 + newpw_sz), /* Length */
292 tpm_u32(TPM2_CC_HIERCHANGEAUTH), /* Command code */
295 tpm_u32(handle), /* TPM resource handle */
298 tpm_u32(9 + oldpw_sz), /* Authorization size */
299 tpm_u32(TPM2_RS_PW), /* Session handle */
300 tpm_u16(0), /* Size of <nonce> */
301 /* <nonce> (if any) */
302 0, /* Attributes: Cont/Excl/Rst */
303 tpm_u16(oldpw_sz) /* Size of <hmac/password> */
304 /* STRING(oldpw) <hmac/password> (if any) */
306 /* TPM2B_AUTH (TPM2B_DIGEST) */
307 /* tpm_u16(newpw_sz) Digest size, new pw length */
308 /* STRING(newpw) Digest buffer, new pw */
313 * Fill the command structure starting from the first buffer:
314 * - the old password (if any)
315 * - size of the new password
318 ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
319 offset, oldpw, oldpw_sz,
320 offset + oldpw_sz, newpw_sz,
321 offset + oldpw_sz + 2, newpw, newpw_sz);
322 offset += oldpw_sz + 2 + newpw_sz;
324 return TPM_LIB_ERROR;
326 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
329 u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
330 const ssize_t pw_sz, u32 index, const char *key)
332 u8 command_v2[COMMAND_BUFFER_SIZE] = {
333 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
334 tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
335 tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
338 tpm_u32(TPM2_RH_PLATFORM), /* TPM resource handle */
341 tpm_u32(9 + pw_sz), /* Authorization size */
342 tpm_u32(TPM2_RS_PW), /* session handle */
343 tpm_u16(0), /* Size of <nonce> */
344 /* <nonce> (if any) */
345 0, /* Attributes: Cont/Excl/Rst */
346 tpm_u16(pw_sz) /* Size of <hmac/password> */
347 /* STRING(pw) <hmac/password> (if any) */
349 /* TPM2B_AUTH (TPM2B_DIGEST) */
350 /* tpm_u16(TPM2_DIGEST_LEN) Digest size length */
351 /* STRING(key) Digest buffer (PCR key) */
354 /* tpm_u16(TPM2_ALG_SHA256) Algorithm of the hash */
357 /* tpm_u32(index), PCR Index */
359 unsigned int offset = 27;
363 * Fill the command structure starting from the first buffer:
364 * - the password (if any)
365 * - the PCR key length
367 * - the hash algorithm
370 ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
372 offset + pw_sz, TPM2_DIGEST_LEN,
373 offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
374 offset + pw_sz + 2 + TPM2_DIGEST_LEN,
376 offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
377 offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
379 return TPM_LIB_ERROR;
381 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
384 u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
385 const ssize_t pw_sz, u32 index, const char *key,
386 const ssize_t key_sz)
388 u8 command_v2[COMMAND_BUFFER_SIZE] = {
389 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
390 tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
391 tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
394 tpm_u32(index), /* Handle (PCR Index) */
397 tpm_u32(9 + pw_sz), /* Authorization size */
398 tpm_u32(TPM2_RS_PW), /* session handle */
399 tpm_u16(0), /* Size of <nonce> */
400 /* <nonce> (if any) */
401 0, /* Attributes: Cont/Excl/Rst */
402 tpm_u16(pw_sz), /* Size of <hmac/password> */
403 /* STRING(pw) <hmac/password> (if any) */
406 /* tpm_u16(key_sz) Key length */
407 /* STRING(key) Key */
409 unsigned int offset = 27;
413 * Fill the command structure starting from the first buffer:
414 * - the password (if any)
415 * - the number of digests, 1 in our case
416 * - the algorithm, sha256 in our case
417 * - the digest (64 bytes)
419 ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
421 offset + pw_sz, key_sz,
422 offset + pw_sz + 2, key, key_sz);
423 offset += pw_sz + 2 + key_sz;
425 return TPM_LIB_ERROR;
427 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
430 u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
432 const u8 command_v2[10] = {
433 tpm_u16(TPM2_ST_NO_SESSIONS),
435 tpm_u32(TPM2_CC_GET_RANDOM),
437 u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
439 const size_t data_size_offset = 10;
440 const size_t data_offset = 12;
441 size_t response_length = sizeof(response);
446 u32 this_bytes = min((size_t)count,
447 sizeof(response) - data_offset);
450 if (pack_byte_string(buf, sizeof(buf), "sw",
451 0, command_v2, sizeof(command_v2),
452 sizeof(command_v2), this_bytes))
453 return TPM_LIB_ERROR;
454 err = tpm_sendrecv_command(dev, buf, response,
458 if (unpack_byte_string(response, response_length, "w",
459 data_size_offset, &data_size))
460 return TPM_LIB_ERROR;
461 if (data_size > this_bytes)
462 return TPM_LIB_ERROR;
463 if (unpack_byte_string(response, response_length, "s",
464 data_offset, out, data_size))
465 return TPM_LIB_ERROR;