tpm2-cmd.c (58472f5cd4f6ff02488c8da3cdbf719e9dd21e48) tpm2-cmd.c (745b361e989af21ad40811c2586b60229f870a68)
1/*
2 * Copyright (C) 2014, 2015 Intel Corporation
3 *
4 * Authors:
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
6 *
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 *

--- 252 unchanged lines hidden (view full) ---

261 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
262 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
263 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
264
265 memset(cmd.params.pcrread_in.pcr_select, 0,
266 sizeof(cmd.params.pcrread_in.pcr_select));
267 cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
268
1/*
2 * Copyright (C) 2014, 2015 Intel Corporation
3 *
4 * Authors:
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
6 *
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 *

--- 252 unchanged lines hidden (view full) ---

261 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
262 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
263 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
264
265 memset(cmd.params.pcrread_in.pcr_select, 0,
266 sizeof(cmd.params.pcrread_in.pcr_select));
267 cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
268
269 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
269 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
270 TPM2_PCR_READ_RESP_BODY_SIZE,
271 0, "attempting to read a pcr value");
272 if (rc == 0) {
273 buf = cmd.params.pcrread_out.digest;
274 memcpy(res_buf, buf, TPM_DIGEST_SIZE);
275 }
276
277 return rc;

--- 50 unchanged lines hidden (view full) ---

328 continue;
329 tpm_buf_append_u16(&buf, digests[i].alg_id);
330 tpm_buf_append(&buf, (const unsigned char
331 *)&digests[i].digest,
332 hash_digest_size[tpm2_hash_map[j].crypto_id]);
333 }
334 }
335
270 TPM2_PCR_READ_RESP_BODY_SIZE,
271 0, "attempting to read a pcr value");
272 if (rc == 0) {
273 buf = cmd.params.pcrread_out.digest;
274 memcpy(res_buf, buf, TPM_DIGEST_SIZE);
275 }
276
277 return rc;

--- 50 unchanged lines hidden (view full) ---

328 continue;
329 tpm_buf_append_u16(&buf, digests[i].alg_id);
330 tpm_buf_append(&buf, (const unsigned char
331 *)&digests[i].digest,
332 hash_digest_size[tpm2_hash_map[j].crypto_id]);
333 }
334 }
335
336 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 0, 0,
336 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
337 "attempting extend a PCR value");
338
339 tpm_buf_destroy(&buf);
340
341 return rc;
342}
343
344

--- 32 unchanged lines hidden (view full) ---

377 if (!out || !num_bytes ||
378 max > sizeof(cmd.params.getrandom_out.buffer))
379 return -EINVAL;
380
381 do {
382 cmd.header.in = tpm2_getrandom_header;
383 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
384
337 "attempting extend a PCR value");
338
339 tpm_buf_destroy(&buf);
340
341 return rc;
342}
343
344

--- 32 unchanged lines hidden (view full) ---

377 if (!out || !num_bytes ||
378 max > sizeof(cmd.params.getrandom_out.buffer))
379 return -EINVAL;
380
381 do {
382 cmd.header.in = tpm2_getrandom_header;
383 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
384
385 err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
385 err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
386 offsetof(struct tpm2_get_random_out,
387 buffer),
388 0, "attempting get random");
389 if (err)
390 break;
391
392 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
393 num_bytes);

--- 42 unchanged lines hidden (view full) ---

436 if (rc) {
437 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
438 handle);
439 return;
440 }
441
442 tpm_buf_append_u32(&buf, handle);
443
386 offsetof(struct tpm2_get_random_out,
387 buffer),
388 0, "attempting get random");
389 if (err)
390 break;
391
392 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
393 num_bytes);

--- 42 unchanged lines hidden (view full) ---

436 if (rc) {
437 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
438 handle);
439 return;
440 }
441
442 tpm_buf_append_u32(&buf, handle);
443
444 (void) tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 0, flags,
444 (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
445 "flushing context");
446
447 tpm_buf_destroy(&buf);
448}
449
450/**
451 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
452 *

--- 99 unchanged lines hidden (view full) ---

552 /* creation PCR */
553 tpm_buf_append_u32(&buf, 0);
554
555 if (buf.flags & TPM_BUF_OVERFLOW) {
556 rc = -E2BIG;
557 goto out;
558 }
559
445 "flushing context");
446
447 tpm_buf_destroy(&buf);
448}
449
450/**
451 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
452 *

--- 99 unchanged lines hidden (view full) ---

552 /* creation PCR */
553 tpm_buf_append_u32(&buf, 0);
554
555 if (buf.flags & TPM_BUF_OVERFLOW) {
556 rc = -E2BIG;
557 goto out;
558 }
559
560 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 4, 0,
560 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
561 "sealing data");
562 if (rc)
563 goto out;
564
565 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
566 if (blob_len > MAX_BLOB_SIZE) {
567 rc = -E2BIG;
568 goto out;

--- 67 unchanged lines hidden (view full) ---

636
637 tpm_buf_append(&buf, payload->blob, blob_len);
638
639 if (buf.flags & TPM_BUF_OVERFLOW) {
640 rc = -E2BIG;
641 goto out;
642 }
643
561 "sealing data");
562 if (rc)
563 goto out;
564
565 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
566 if (blob_len > MAX_BLOB_SIZE) {
567 rc = -E2BIG;
568 goto out;

--- 67 unchanged lines hidden (view full) ---

636
637 tpm_buf_append(&buf, payload->blob, blob_len);
638
639 if (buf.flags & TPM_BUF_OVERFLOW) {
640 rc = -E2BIG;
641 goto out;
642 }
643
644 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 4, flags,
644 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
645 "loading blob");
646 if (!rc)
647 *blob_handle = be32_to_cpup(
648 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
649
650out:
651 tpm_buf_destroy(&buf);
652

--- 35 unchanged lines hidden (view full) ---

688 tpm2_buf_append_auth(&buf,
689 options->policyhandle ?
690 options->policyhandle : TPM2_RS_PW,
691 NULL /* nonce */, 0,
692 TPM2_SA_CONTINUE_SESSION,
693 options->blobauth /* hmac */,
694 TPM_DIGEST_SIZE);
695
645 "loading blob");
646 if (!rc)
647 *blob_handle = be32_to_cpup(
648 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
649
650out:
651 tpm_buf_destroy(&buf);
652

--- 35 unchanged lines hidden (view full) ---

688 tpm2_buf_append_auth(&buf,
689 options->policyhandle ?
690 options->policyhandle : TPM2_RS_PW,
691 NULL /* nonce */, 0,
692 TPM2_SA_CONTINUE_SESSION,
693 options->blobauth /* hmac */,
694 TPM_DIGEST_SIZE);
695
696 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 6, flags,
696 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
697 "unsealing");
698 if (rc > 0)
699 rc = -EPERM;
700
701 if (!rc) {
702 data_len = be16_to_cpup(
703 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
704

--- 60 unchanged lines hidden (view full) ---

765 struct tpm2_cmd cmd;
766 int rc;
767
768 cmd.header.in = tpm2_get_tpm_pt_header;
769 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
770 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
771 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
772
697 "unsealing");
698 if (rc > 0)
699 rc = -EPERM;
700
701 if (!rc) {
702 data_len = be16_to_cpup(
703 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
704

--- 60 unchanged lines hidden (view full) ---

765 struct tpm2_cmd cmd;
766 int rc;
767
768 cmd.header.in = tpm2_get_tpm_pt_header;
769 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
770 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
771 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
772
773 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
773 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
774 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
775 if (!rc)
776 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
777
778 return rc;
779}
780EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
781

--- 18 unchanged lines hidden (view full) ---

800 */
801static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
802{
803 struct tpm2_cmd cmd;
804
805 cmd.header.in = tpm2_startup_header;
806
807 cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
774 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
775 if (!rc)
776 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
777
778 return rc;
779}
780EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
781

--- 18 unchanged lines hidden (view full) ---

800 */
801static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
802{
803 struct tpm2_cmd cmd;
804
805 cmd.header.in = tpm2_startup_header;
806
807 cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
808 return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0,
808 return tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
809 "attempting to start the TPM");
810}
811
812#define TPM2_SHUTDOWN_IN_SIZE \
813 (sizeof(struct tpm_input_header) + \
814 sizeof(struct tpm2_startup_in))
815
816static const struct tpm_input_header tpm2_shutdown_header = {

--- 12 unchanged lines hidden (view full) ---

829void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
830{
831 struct tpm2_cmd cmd;
832 int rc;
833
834 cmd.header.in = tpm2_shutdown_header;
835 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
836
809 "attempting to start the TPM");
810}
811
812#define TPM2_SHUTDOWN_IN_SIZE \
813 (sizeof(struct tpm_input_header) + \
814 sizeof(struct tpm2_startup_in))
815
816static const struct tpm_input_header tpm2_shutdown_header = {

--- 12 unchanged lines hidden (view full) ---

829void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
830{
831 struct tpm2_cmd cmd;
832 int rc;
833
834 cmd.header.in = tpm2_shutdown_header;
835 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
836
837 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0,
837 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
838 "stopping the TPM");
839
840 /* In places where shutdown command is sent there's no much we can do
841 * except print the error code on a system failure.
842 */
843 if (rc < 0)
844 dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
845 rc);

--- 47 unchanged lines hidden (view full) ---

893static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
894{
895 int rc;
896 struct tpm2_cmd cmd;
897
898 cmd.header.in = tpm2_selftest_header;
899 cmd.params.selftest_in.full_test = full;
900
838 "stopping the TPM");
839
840 /* In places where shutdown command is sent there's no much we can do
841 * except print the error code on a system failure.
842 */
843 if (rc < 0)
844 dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
845 rc);

--- 47 unchanged lines hidden (view full) ---

893static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
894{
895 int rc;
896 struct tpm2_cmd cmd;
897
898 cmd.header.in = tpm2_selftest_header;
899 cmd.params.selftest_in.full_test = full;
900
901 rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, 0, 0,
901 rc = tpm_transmit_cmd(chip, NULL, &cmd, TPM2_SELF_TEST_IN_SIZE, 0, 0,
902 "continue selftest");
903
904 /* At least some prototype chips seem to give RC_TESTING error
905 * immediately. This is a workaround for that.
906 */
907 if (rc == TPM2_RC_TESTING) {
908 dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
909 rc = 0;

--- 34 unchanged lines hidden (view full) ---

944 cmd.header.in = tpm2_pcrread_header;
945 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
946 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
947 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
948 cmd.params.pcrread_in.pcr_select[0] = 0x01;
949 cmd.params.pcrread_in.pcr_select[1] = 0x00;
950 cmd.params.pcrread_in.pcr_select[2] = 0x00;
951
902 "continue selftest");
903
904 /* At least some prototype chips seem to give RC_TESTING error
905 * immediately. This is a workaround for that.
906 */
907 if (rc == TPM2_RC_TESTING) {
908 dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
909 rc = 0;

--- 34 unchanged lines hidden (view full) ---

944 cmd.header.in = tpm2_pcrread_header;
945 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
946 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
947 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
948 cmd.params.pcrread_in.pcr_select[0] = 0x01;
949 cmd.params.pcrread_in.pcr_select[1] = 0x00;
950 cmd.params.pcrread_in.pcr_select[2] = 0x00;
951
952 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0, NULL);
952 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
953 NULL);
953 if (rc < 0)
954 break;
955
956 rc = be32_to_cpu(cmd.header.out.return_code);
957 if (rc != TPM2_RC_TESTING)
958 break;
959
960 msleep(delay_msec);

--- 16 unchanged lines hidden (view full) ---

977 struct tpm2_cmd cmd;
978 int rc;
979
980 cmd.header.in = tpm2_get_tpm_pt_header;
981 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
982 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
983 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
984
954 if (rc < 0)
955 break;
956
957 rc = be32_to_cpu(cmd.header.out.return_code);
958 if (rc != TPM2_RC_TESTING)
959 break;
960
961 msleep(delay_msec);

--- 16 unchanged lines hidden (view full) ---

978 struct tpm2_cmd cmd;
979 int rc;
980
981 cmd.header.in = tpm2_get_tpm_pt_header;
982 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
983 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
984 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
985
985 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0, NULL);
986 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
986 if (rc < 0)
987 return rc;
988
989 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
990 chip->flags |= TPM_CHIP_FLAG_TPM2;
991
992 return 0;
993}

--- 21 unchanged lines hidden (view full) ---

1015 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1016 if (rc)
1017 return rc;
1018
1019 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
1020 tpm_buf_append_u32(&buf, 0);
1021 tpm_buf_append_u32(&buf, 1);
1022
987 if (rc < 0)
988 return rc;
989
990 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
991 chip->flags |= TPM_CHIP_FLAG_TPM2;
992
993 return 0;
994}

--- 21 unchanged lines hidden (view full) ---

1016 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1017 if (rc)
1018 return rc;
1019
1020 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
1021 tpm_buf_append_u32(&buf, 0);
1022 tpm_buf_append_u32(&buf, 1);
1023
1023 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 9, 0,
1024 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
1024 "get tpm pcr allocation");
1025 if (rc)
1026 goto out;
1027
1028 count = be32_to_cpup(
1029 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
1030
1031 if (count > ARRAY_SIZE(chip->active_banks)) {

--- 55 unchanged lines hidden (view full) ---

1087 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1088 if (rc)
1089 goto out;
1090
1091 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
1092 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
1093 tpm_buf_append_u32(&buf, nr_commands);
1094
1025 "get tpm pcr allocation");
1026 if (rc)
1027 goto out;
1028
1029 count = be32_to_cpup(
1030 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
1031
1032 if (count > ARRAY_SIZE(chip->active_banks)) {

--- 55 unchanged lines hidden (view full) ---

1088 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1089 if (rc)
1090 goto out;
1091
1092 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
1093 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
1094 tpm_buf_append_u32(&buf, nr_commands);
1095
1095 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 9 + 4 * nr_commands,
1096 0, NULL);
1096 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
1097 9 + 4 * nr_commands, 0, NULL);
1097 if (rc) {
1098 tpm_buf_destroy(&buf);
1099 goto out;
1100 }
1101
1102 if (nr_commands !=
1103 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
1104 tpm_buf_destroy(&buf);

--- 80 unchanged lines hidden ---
1098 if (rc) {
1099 tpm_buf_destroy(&buf);
1100 goto out;
1101 }
1102
1103 if (nr_commands !=
1104 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
1105 tpm_buf_destroy(&buf);

--- 80 unchanged lines hidden ---