TCG TIS 1.3 compliant TPM driver to use the TPM as hardware crypto library.
The TPM 2.0 architecture, commands and structures are defined in the set of 4 Trusted Platform Module Library Specification, Family "2.0" specifications that that can be found at http://www.trustedcomputinggroup.org/resources/tpm_library_specification
The "PC Client Specific TPM Interface Specification (TIS), Version 1.3" that was used for this implementation can be found at http://www.trustedcomputinggroup.org/resources/pc_client_work_group_pc_client_specific_tpm_interface_specification_tis
All the information to get going is in SPITIS_TPM20.h!
SPITIS_TPM20.h@3:4b9ad18eae02, 2015-04-11 (annotated)
- Committer:
- LordOfDorks
- Date:
- Sat Apr 11 04:01:54 2015 +0000
- Revision:
- 3:4b9ad18eae02
- Parent:
- 2:526bf792254d
- Child:
- 4:77fecfe49437
Added TPM marshal library to make working with TPM structures easy.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LordOfDorks | 1:fd0a59e55a85 | 1 | /* mbed TCG SPI TPM 2.0 TIS 1.3 driver, |
LordOfDorks | 0:b11c8971edd9 | 2 | * Copyright (c) 2015, Microsoft Coprporation Inc. |
LordOfDorks | 0:b11c8971edd9 | 3 | * by Stefan Thom (LordOfDorks) StefanTh@Microsoft.com, Stefan@ThomsR.Us |
LordOfDorks | 0:b11c8971edd9 | 4 | * |
LordOfDorks | 0:b11c8971edd9 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
LordOfDorks | 0:b11c8971edd9 | 6 | * of this software and associated documentation files (the "Software"), to deal |
LordOfDorks | 0:b11c8971edd9 | 7 | * in the Software without restriction, including without limitation the rights |
LordOfDorks | 0:b11c8971edd9 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
LordOfDorks | 0:b11c8971edd9 | 9 | * copies of the Software, and to permit persons to whom the Software is |
LordOfDorks | 0:b11c8971edd9 | 10 | * furnished to do so, subject to the following conditions: |
LordOfDorks | 0:b11c8971edd9 | 11 | * |
LordOfDorks | 0:b11c8971edd9 | 12 | * The above copyright notice and this permission notice shall be included in |
LordOfDorks | 0:b11c8971edd9 | 13 | * all copies or substantial portions of the Software. |
LordOfDorks | 0:b11c8971edd9 | 14 | * |
LordOfDorks | 0:b11c8971edd9 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
LordOfDorks | 0:b11c8971edd9 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
LordOfDorks | 0:b11c8971edd9 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
LordOfDorks | 0:b11c8971edd9 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
LordOfDorks | 0:b11c8971edd9 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
LordOfDorks | 0:b11c8971edd9 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
LordOfDorks | 0:b11c8971edd9 | 21 | * THE SOFTWARE. |
LordOfDorks | 0:b11c8971edd9 | 22 | * |
LordOfDorks | 1:fd0a59e55a85 | 23 | * This code was developped and tested with a STMicro Nucleo-L152RE with |
LordOfDorks | 1:fd0a59e55a85 | 24 | * the STMicro ST33TPMF20MSPI SPI parts (TSSOP28), connected as follows: |
LordOfDorks | 0:b11c8971edd9 | 25 | * |
LordOfDorks | 1:fd0a59e55a85 | 26 | * ------------- |
LordOfDorks | 1:fd0a59e55a85 | 27 | * NC + 01 28 + NC |
LordOfDorks | 1:fd0a59e55a85 | 28 | * NC + 02 S 27 + NC |
LordOfDorks | 1:fd0a59e55a85 | 29 | * NC + 03 T 26 + MISO -> D12/PA_6 |
LordOfDorks | 1:fd0a59e55a85 | 30 | * GND <- GND + 04 M 25 + NC |
LordOfDorks | 1:fd0a59e55a85 | 31 | * NC + 05 i 24 + VPS -> 3V3 |
LordOfDorks | 1:fd0a59e55a85 | 32 | * NC + 06 c 23 + MOSI -> D11/PA_7 |
LordOfDorks | 1:fd0a59e55a85 | 33 | * NC <- PP + 07 r 22 + #SPI_CS -> D10/PB_6 |
LordOfDorks | 1:fd0a59e55a85 | 34 | * NC + 08 o 21 + SPI_CLK -> D13/PA_5 |
LordOfDorks | 1:fd0a59e55a85 | 35 | * NC + 09 20 + #SPI_PIRQ -> D7/PA_8 |
LordOfDorks | 1:fd0a59e55a85 | 36 | * NC + 10 S 19 + NC |
LordOfDorks | 1:fd0a59e55a85 | 37 | * NC + 11 P 18 + NC |
LordOfDorks | 1:fd0a59e55a85 | 38 | * NC + 12 I 17 + NC |
LordOfDorks | 1:fd0a59e55a85 | 39 | * NC + 13 16 + #SPI_RST -> NRST |
LordOfDorks | 1:fd0a59e55a85 | 40 | * NC + 14 15 + NC |
LordOfDorks | 1:fd0a59e55a85 | 41 | * ------------- |
LordOfDorks | 0:b11c8971edd9 | 42 | * |
LordOfDorks | 1:fd0a59e55a85 | 43 | * TestCode main.cpp: |
LordOfDorks | 1:fd0a59e55a85 | 44 | |
LordOfDorks | 1:fd0a59e55a85 | 45 | #include "mbed.h" |
LordOfDorks | 1:fd0a59e55a85 | 46 | #include "SPITIS_TPM20.h" |
LordOfDorks | 1:fd0a59e55a85 | 47 | |
LordOfDorks | 1:fd0a59e55a85 | 48 | DigitalIn mybutton(USER_BUTTON); |
LordOfDorks | 1:fd0a59e55a85 | 49 | |
LordOfDorks | 1:fd0a59e55a85 | 50 | TIS_TPM20 tpm(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, D7); |
LordOfDorks | 1:fd0a59e55a85 | 51 | |
LordOfDorks | 1:fd0a59e55a85 | 52 | int main() |
LordOfDorks | 1:fd0a59e55a85 | 53 | { |
LordOfDorks | 1:fd0a59e55a85 | 54 | uint32_t result = 0; |
LordOfDorks | 3:4b9ad18eae02 | 55 | |
LordOfDorks | 1:fd0a59e55a85 | 56 | result = tpm.InitializeTis(); |
LordOfDorks | 3:4b9ad18eae02 | 57 | printf("TIS.InitializeTis= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 58 | if(((result = tpm.StartSession(TIS_TPM20::TIS_LOCALITY_0)) != TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) || |
LordOfDorks | 3:4b9ad18eae02 | 59 | ((result = tpm.TPM2_Startup(TPM_SU_CLEAR)) != TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) || |
LordOfDorks | 3:4b9ad18eae02 | 60 | ((result = tpm.EndSession()) != TIS_TPM20::TIS_SESSION_RESULT_COMPLETE)) |
LordOfDorks | 3:4b9ad18eae02 | 61 | { |
LordOfDorks | 3:4b9ad18eae02 | 62 | printf("TPM2_Startup= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 63 | } |
LordOfDorks | 3:4b9ad18eae02 | 64 | else |
LordOfDorks | 3:4b9ad18eae02 | 65 | { |
LordOfDorks | 3:4b9ad18eae02 | 66 | printf("TPM2_Startup= OK\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 67 | } |
LordOfDorks | 3:4b9ad18eae02 | 68 | |
LordOfDorks | 1:fd0a59e55a85 | 69 | while(1) |
LordOfDorks | 1:fd0a59e55a85 | 70 | { |
LordOfDorks | 1:fd0a59e55a85 | 71 | if(mybutton == 0) |
LordOfDorks | 1:fd0a59e55a85 | 72 | { |
LordOfDorks | 1:fd0a59e55a85 | 73 | printf("Button!\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 74 | |
LordOfDorks | 3:4b9ad18eae02 | 75 | if((result = tpm.StartSession(TIS_TPM20::TIS_LOCALITY_0)) == TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) |
LordOfDorks | 3:4b9ad18eae02 | 76 | { |
LordOfDorks | 3:4b9ad18eae02 | 77 | TPM2B_AUTH auth = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 78 | TPMI_DH_OBJECT hashHandle = 0; |
LordOfDorks | 3:4b9ad18eae02 | 79 | TPML_DIGEST_VALUES values = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 80 | |
LordOfDorks | 3:4b9ad18eae02 | 81 | result = tpm.TPM2_HashSequenceStart(&auth, TPM_ALG_NULL, &hashHandle); |
LordOfDorks | 3:4b9ad18eae02 | 82 | printf("TPM2_HashSequenceStart= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 83 | for(uint32_t n = 0; n < 0x80000; n += 1024) |
LordOfDorks | 3:4b9ad18eae02 | 84 | { |
LordOfDorks | 3:4b9ad18eae02 | 85 | if((result = tpm.TPM2_SequenceUpdate(hashHandle, &auth, (uint8_t*)(0x08000000 + n), 1024)) != TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) |
LordOfDorks | 3:4b9ad18eae02 | 86 | { |
LordOfDorks | 3:4b9ad18eae02 | 87 | printf("TPM2_SequenceUpdate(%d)= 0x%08x\n\r", n, result); |
LordOfDorks | 3:4b9ad18eae02 | 88 | } |
LordOfDorks | 3:4b9ad18eae02 | 89 | } |
LordOfDorks | 3:4b9ad18eae02 | 90 | result = tpm.TPM2_EventSequenceComplete((TPM_HC)(HR_PCR + 0), &auth, hashHandle, &auth, &values); |
LordOfDorks | 3:4b9ad18eae02 | 91 | printf("TPM2_EventSequenceComplete= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 92 | for(uint32_t n = 0; n < values.count; n++) |
LordOfDorks | 3:4b9ad18eae02 | 93 | { |
LordOfDorks | 3:4b9ad18eae02 | 94 | printf("Digest(0x%04x)= ", (uint16_t)values.digests[n].hashAlg); |
LordOfDorks | 3:4b9ad18eae02 | 95 | switch(values.digests[n].hashAlg) |
LordOfDorks | 3:4b9ad18eae02 | 96 | { |
LordOfDorks | 3:4b9ad18eae02 | 97 | case TPM_ALG_SHA1: |
LordOfDorks | 3:4b9ad18eae02 | 98 | for(uint16_t m = 0; m < sizeof(values.digests[n].digest.sha1); m++) printf("%02x", values.digests[n].digest.sha1[m]); |
LordOfDorks | 3:4b9ad18eae02 | 99 | break; |
LordOfDorks | 3:4b9ad18eae02 | 100 | case TPM_ALG_SHA256: |
LordOfDorks | 3:4b9ad18eae02 | 101 | for(uint16_t m = 0; m < sizeof(values.digests[n].digest.sha256); m++) printf("%02x", values.digests[n].digest.sha256[m]); |
LordOfDorks | 3:4b9ad18eae02 | 102 | break; |
LordOfDorks | 3:4b9ad18eae02 | 103 | case TPM_ALG_NULL: |
LordOfDorks | 3:4b9ad18eae02 | 104 | break; |
LordOfDorks | 3:4b9ad18eae02 | 105 | } |
LordOfDorks | 3:4b9ad18eae02 | 106 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 107 | } |
LordOfDorks | 3:4b9ad18eae02 | 108 | tpm.EndSession(); |
LordOfDorks | 3:4b9ad18eae02 | 109 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 110 | } |
LordOfDorks | 1:fd0a59e55a85 | 111 | |
LordOfDorks | 3:4b9ad18eae02 | 112 | if((result = tpm.StartSession(TIS_TPM20::TIS_LOCALITY_0)) == TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) |
LordOfDorks | 3:4b9ad18eae02 | 113 | { |
LordOfDorks | 3:4b9ad18eae02 | 114 | TPML_PCR_SELECTION pcrSelection = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 115 | uint32_t pcrUpdateCounter = 0; |
LordOfDorks | 3:4b9ad18eae02 | 116 | TPML_DIGEST pcrValues = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 117 | |
LordOfDorks | 3:4b9ad18eae02 | 118 | pcrSelection.count = 2; |
LordOfDorks | 3:4b9ad18eae02 | 119 | pcrSelection.pcrSelections[0].hash = TPM_ALG_SHA1; |
LordOfDorks | 3:4b9ad18eae02 | 120 | pcrSelection.pcrSelections[0].sizeofSelect = 3; |
LordOfDorks | 3:4b9ad18eae02 | 121 | pcrSelection.pcrSelections[0].pcrSelect[0] = 0x01; |
LordOfDorks | 3:4b9ad18eae02 | 122 | pcrSelection.pcrSelections[0].pcrSelect[1] = 0x00; |
LordOfDorks | 3:4b9ad18eae02 | 123 | pcrSelection.pcrSelections[0].pcrSelect[2] = 0x00; |
LordOfDorks | 3:4b9ad18eae02 | 124 | pcrSelection.pcrSelections[1].hash = TPM_ALG_SHA256; |
LordOfDorks | 3:4b9ad18eae02 | 125 | pcrSelection.pcrSelections[1].sizeofSelect = 3; |
LordOfDorks | 3:4b9ad18eae02 | 126 | pcrSelection.pcrSelections[1].pcrSelect[0] = 0x01; |
LordOfDorks | 3:4b9ad18eae02 | 127 | pcrSelection.pcrSelections[1].pcrSelect[1] = 0x00; |
LordOfDorks | 3:4b9ad18eae02 | 128 | pcrSelection.pcrSelections[1].pcrSelect[2] = 0x00; |
LordOfDorks | 3:4b9ad18eae02 | 129 | result = tpm.TPM2_PCR_Read(&pcrSelection, &pcrUpdateCounter, &pcrSelection, &pcrValues); |
LordOfDorks | 3:4b9ad18eae02 | 130 | printf("TPM2_PCR_Read= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 131 | for(uint32_t n = 0; n < pcrValues.count; n++) |
LordOfDorks | 3:4b9ad18eae02 | 132 | { |
LordOfDorks | 3:4b9ad18eae02 | 133 | printf("PCR[0](0x%04x)= ", (uint16_t)pcrSelection.pcrSelections[n].hash); |
LordOfDorks | 3:4b9ad18eae02 | 134 | for(uint16_t m = 0; m < pcrValues.digests[n].t.size; m++) printf("%02x", pcrValues.digests[n].t.buffer[m]); |
LordOfDorks | 3:4b9ad18eae02 | 135 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 136 | } |
LordOfDorks | 3:4b9ad18eae02 | 137 | tpm.EndSession(); |
LordOfDorks | 3:4b9ad18eae02 | 138 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 139 | } |
LordOfDorks | 3:4b9ad18eae02 | 140 | |
LordOfDorks | 3:4b9ad18eae02 | 141 | if((result = tpm.StartSession(TIS_TPM20::TIS_LOCALITY_0)) == TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) |
LordOfDorks | 3:4b9ad18eae02 | 142 | { |
LordOfDorks | 3:4b9ad18eae02 | 143 | TPM2B_AUTH auth = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 144 | TPMI_DH_OBJECT hashHandle = 0; |
LordOfDorks | 3:4b9ad18eae02 | 145 | TPM2B_DIGEST digest = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 146 | TPMT_TK_HASHCHECK validation = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 147 | |
LordOfDorks | 3:4b9ad18eae02 | 148 | uint8_t testVector[] = "The quick brown fox jumps over the lazy dog"; |
LordOfDorks | 3:4b9ad18eae02 | 149 | result = tpm.TPM2_HashSequenceStart(&auth, TPM_ALG_SHA256, &hashHandle); |
LordOfDorks | 3:4b9ad18eae02 | 150 | printf("TPM2_HashSequenceStart= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 151 | result = tpm.TPM2_SequenceUpdate(hashHandle, &auth, testVector, sizeof(testVector) - 1); |
LordOfDorks | 3:4b9ad18eae02 | 152 | printf("TPM2_SequenceUpdate= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 153 | result = tpm.TPM2_SequenceComplete(hashHandle, &auth, TPM_RH_NULL, &digest, &validation); |
LordOfDorks | 3:4b9ad18eae02 | 154 | printf("TPM2_HashSequenceComplete= 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 155 | printf("Digest= "); |
LordOfDorks | 3:4b9ad18eae02 | 156 | for(uint16_t n = 0; n < digest.t.size; n++) printf("%02x", digest.t.buffer[n]); |
LordOfDorks | 3:4b9ad18eae02 | 157 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 158 | printf("Ref= d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 159 | tpm.EndSession(); |
LordOfDorks | 3:4b9ad18eae02 | 160 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 161 | } |
LordOfDorks | 3:4b9ad18eae02 | 162 | |
LordOfDorks | 3:4b9ad18eae02 | 163 | if((result = tpm.StartSession(TIS_TPM20::TIS_LOCALITY_0)) == TIS_TPM20::TIS_SESSION_RESULT_COMPLETE) |
LordOfDorks | 3:4b9ad18eae02 | 164 | { |
LordOfDorks | 3:4b9ad18eae02 | 165 | TPM2B_DIGEST random = {0}; |
LordOfDorks | 3:4b9ad18eae02 | 166 | random.t.size = 32; |
LordOfDorks | 3:4b9ad18eae02 | 167 | result = tpm.TPM2_GetRandom(&random); |
LordOfDorks | 3:4b9ad18eae02 | 168 | printf("TPM2_GetRandom = 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 169 | printf("Random = "); |
LordOfDorks | 3:4b9ad18eae02 | 170 | for(uint16_t n = 0; n < random.t.size; n++) printf("%02x", random.t.buffer[n]); |
LordOfDorks | 3:4b9ad18eae02 | 171 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 172 | result = tpm.TPM2_StirRandom(&random); |
LordOfDorks | 3:4b9ad18eae02 | 173 | printf("TPM2_StirRandom = 0x%08x\n\r", result); |
LordOfDorks | 3:4b9ad18eae02 | 174 | tpm.EndSession(); |
LordOfDorks | 3:4b9ad18eae02 | 175 | printf("\n\r"); |
LordOfDorks | 3:4b9ad18eae02 | 176 | } |
LordOfDorks | 3:4b9ad18eae02 | 177 | |
LordOfDorks | 3:4b9ad18eae02 | 178 | wait(1); |
LordOfDorks | 1:fd0a59e55a85 | 179 | } |
LordOfDorks | 1:fd0a59e55a85 | 180 | } |
LordOfDorks | 1:fd0a59e55a85 | 181 | } |
LordOfDorks | 1:fd0a59e55a85 | 182 | |
LordOfDorks | 0:b11c8971edd9 | 183 | * |
LordOfDorks | 1:fd0a59e55a85 | 184 | * The TPM 2.0 architecture, commands and structures are defined in the set of 4 |
LordOfDorks | 1:fd0a59e55a85 | 185 | * Trusted Platform Module Library Specification, Family "2.0" specifications that |
LordOfDorks | 1:fd0a59e55a85 | 186 | * that can be found at |
LordOfDorks | 0:b11c8971edd9 | 187 | * http://www.trustedcomputinggroup.org/resources/tpm_library_specification |
LordOfDorks | 0:b11c8971edd9 | 188 | * |
LordOfDorks | 1:fd0a59e55a85 | 189 | * The "PC Client Specific TPM Interface Specification (TIS), Version 1.3" that was |
LordOfDorks | 1:fd0a59e55a85 | 190 | * used for this implementationcan be found at |
LordOfDorks | 1:fd0a59e55a85 | 191 | * http://www.trustedcomputinggroup.org/resources/pc_client_work_group_pc_client_specific_tpm_interface_specification_tis |
LordOfDorks | 1:fd0a59e55a85 | 192 | * |
LordOfDorks | 0:b11c8971edd9 | 193 | */ |
LordOfDorks | 0:b11c8971edd9 | 194 | |
LordOfDorks | 0:b11c8971edd9 | 195 | #include "mbed.h" |
LordOfDorks | 1:fd0a59e55a85 | 196 | #include "TPM20.h" |
LordOfDorks | 1:fd0a59e55a85 | 197 | #include "GizmosNGadgets.h" |
LordOfDorks | 3:4b9ad18eae02 | 198 | #include "Marshal.h" |
LordOfDorks | 0:b11c8971edd9 | 199 | |
LordOfDorks | 0:b11c8971edd9 | 200 | //#define TPM_TIS_DEBUG_OUTPUT 1 |
LordOfDorks | 1:fd0a59e55a85 | 201 | //#define TPM_TIS_INTERFACE_DEBUG_OUTPUT 1 |
LordOfDorks | 0:b11c8971edd9 | 202 | |
LordOfDorks | 2:526bf792254d | 203 | // If you do not need command filtering, you can save some space here |
LordOfDorks | 2:526bf792254d | 204 | //#define TPM_TIS_NO_COMMAND_FILTERING 1 |
LordOfDorks | 2:526bf792254d | 205 | |
LordOfDorks | 1:fd0a59e55a85 | 206 | class TIS_TPM20 |
LordOfDorks | 0:b11c8971edd9 | 207 | { |
LordOfDorks | 0:b11c8971edd9 | 208 | public: |
LordOfDorks | 1:fd0a59e55a85 | 209 | enum TIS_RESULT |
LordOfDorks | 1:fd0a59e55a85 | 210 | { |
LordOfDorks | 1:fd0a59e55a85 | 211 | TIS_SESSION_RESULT_COMPLETE = 0, |
LordOfDorks | 1:fd0a59e55a85 | 212 | TIS_SESSION_RESULT_PENDING, |
LordOfDorks | 1:fd0a59e55a85 | 213 | TIS_SESSION_RESULT_FAILED, |
LordOfDorks | 1:fd0a59e55a85 | 214 | TIS_SESSION_RESULT_FILTERED, |
LordOfDorks | 1:fd0a59e55a85 | 215 | TIS_SESSION_RESULT_PREEMPTED, |
LordOfDorks | 1:fd0a59e55a85 | 216 | TIS_SESSION_RESULT_TIMEOUT, |
LordOfDorks | 1:fd0a59e55a85 | 217 | TIS_SESSION_RESULT_OCCUPIED |
LordOfDorks | 1:fd0a59e55a85 | 218 | }; |
LordOfDorks | 1:fd0a59e55a85 | 219 | |
LordOfDorks | 1:fd0a59e55a85 | 220 | enum TIS_LOCALITY |
LordOfDorks | 1:fd0a59e55a85 | 221 | { |
LordOfDorks | 1:fd0a59e55a85 | 222 | TIS_LOCALITY_0 = 0, |
LordOfDorks | 1:fd0a59e55a85 | 223 | TIS_LOCALITY_1 = 1, |
LordOfDorks | 1:fd0a59e55a85 | 224 | TIS_LOCALITY_2 = 2, |
LordOfDorks | 1:fd0a59e55a85 | 225 | TIS_LOCALITY_3 = 3, |
LordOfDorks | 1:fd0a59e55a85 | 226 | TIS_LOCALITY_4 = 4, |
LordOfDorks | 1:fd0a59e55a85 | 227 | TIS_NOT_IN_USE = -1 |
LordOfDorks | 1:fd0a59e55a85 | 228 | }; |
LordOfDorks | 1:fd0a59e55a85 | 229 | |
LordOfDorks | 1:fd0a59e55a85 | 230 | TIS_TPM20(PinName miso, |
LordOfDorks | 1:fd0a59e55a85 | 231 | PinName Mosi, |
LordOfDorks | 1:fd0a59e55a85 | 232 | PinName clk, |
LordOfDorks | 1:fd0a59e55a85 | 233 | PinName cs, |
LordOfDorks | 1:fd0a59e55a85 | 234 | PinName irq) : m_spi(miso, Mosi, clk), m_chipSelect(cs), m_interrupt(irq) |
LordOfDorks | 1:fd0a59e55a85 | 235 | { |
LordOfDorks | 1:fd0a59e55a85 | 236 | m_spi.format(8, 00); |
LordOfDorks | 1:fd0a59e55a85 | 237 | m_spi.frequency(4000000); |
LordOfDorks | 1:fd0a59e55a85 | 238 | m_chipSelect = 1; |
LordOfDorks | 1:fd0a59e55a85 | 239 | m_locality = TIS_NOT_IN_USE; |
LordOfDorks | 1:fd0a59e55a85 | 240 | m_intfCabability = 0; |
LordOfDorks | 2:526bf792254d | 241 | m_fixedBurstCount = true; |
LordOfDorks | 2:526bf792254d | 242 | m_maxBurstCount = 0; |
LordOfDorks | 1:fd0a59e55a85 | 243 | #ifdef TPM_TIS_DEBUG_OUTPUT |
LordOfDorks | 1:fd0a59e55a85 | 244 | printf("TIS.Init: OK\n\r"); |
LordOfDorks | 1:fd0a59e55a85 | 245 | #endif |
LordOfDorks | 1:fd0a59e55a85 | 246 | } |
LordOfDorks | 0:b11c8971edd9 | 247 | |
LordOfDorks | 1:fd0a59e55a85 | 248 | ~TIS_TPM20() |
LordOfDorks | 1:fd0a59e55a85 | 249 | { |
LordOfDorks | 1:fd0a59e55a85 | 250 | #ifdef TPM_TIS_DEBUG_OUTPUT |
LordOfDorks | 1:fd0a59e55a85 | 251 | printf("TIS.Destroy: OK.\n\r"); |
LordOfDorks | 1:fd0a59e55a85 | 252 | #endif |
LordOfDorks | 1:fd0a59e55a85 | 253 | } |
LordOfDorks | 1:fd0a59e55a85 | 254 | |
LordOfDorks | 1:fd0a59e55a85 | 255 | TIS_RESULT InitializeTis(); |
LordOfDorks | 3:4b9ad18eae02 | 256 | |
LordOfDorks | 3:4b9ad18eae02 | 257 | // Session management |
LordOfDorks | 3:4b9ad18eae02 | 258 | TIS_RESULT StartSession(TIS_TPM20::TIS_LOCALITY locality = TIS_TPM20::TIS_LOCALITY_0); |
LordOfDorks | 3:4b9ad18eae02 | 259 | TIS_RESULT EndSession(); |
LordOfDorks | 1:fd0a59e55a85 | 260 | |
LordOfDorks | 1:fd0a59e55a85 | 261 | // Submit a command |
LordOfDorks | 1:fd0a59e55a85 | 262 | TIS_RESULT SendCommand(uint8_t* pbCmd, |
LordOfDorks | 1:fd0a59e55a85 | 263 | uint32_t cbCmd, |
LordOfDorks | 3:4b9ad18eae02 | 264 | uint8_t* pbTrailingData = NULL, |
LordOfDorks | 3:4b9ad18eae02 | 265 | uint32_t cbTrailingData = 0); |
LordOfDorks | 1:fd0a59e55a85 | 266 | |
LordOfDorks | 1:fd0a59e55a85 | 267 | // Retrieve the response |
LordOfDorks | 1:fd0a59e55a85 | 268 | TIS_RESULT RetrieveResponse(uint8_t* pbRsp, |
LordOfDorks | 1:fd0a59e55a85 | 269 | uint32_t cbRsp, |
LordOfDorks | 1:fd0a59e55a85 | 270 | uint32_t* pcbRsp); |
LordOfDorks | 1:fd0a59e55a85 | 271 | |
LordOfDorks | 1:fd0a59e55a85 | 272 | // Attempt to abort the current command |
LordOfDorks | 3:4b9ad18eae02 | 273 | TIS_TPM20::TIS_RESULT AbortCommand(); |
LordOfDorks | 1:fd0a59e55a85 | 274 | |
LordOfDorks | 3:4b9ad18eae02 | 275 | // TPM sample commands |
LordOfDorks | 3:4b9ad18eae02 | 276 | TPM_RC TPM2_Startup(uint16_t startupType); |
LordOfDorks | 3:4b9ad18eae02 | 277 | TPM_RC TPM2_Shutdown(uint16_t shutdownType); |
LordOfDorks | 3:4b9ad18eae02 | 278 | TPM_RC TPM2_GetRandom(TPM2B_DIGEST* random); |
LordOfDorks | 3:4b9ad18eae02 | 279 | TPM_RC TPM2_StirRandom(TPM2B_DIGEST* seed); |
LordOfDorks | 3:4b9ad18eae02 | 280 | TPM_RC TPM2_HMAC_Start(TPMI_DH_OBJECT handle, TPM2B_AUTH* handleAuth, TPM2B_AUTH* auth, TPMI_ALG_HASH hashAlg, TPMI_DH_OBJECT* sequenceHandle); |
LordOfDorks | 3:4b9ad18eae02 | 281 | TPM_RC TPM2_HashSequenceStart(TPM2B_AUTH* auth, TPMI_ALG_HASH hashAlg, TPMI_DH_OBJECT* sequenceHandle); |
LordOfDorks | 3:4b9ad18eae02 | 282 | TPM_RC TPM2_SequenceUpdate(TPMI_DH_OBJECT sequenceHandle, TPM2B_AUTH* sequenceHandleAuth, uint8_t* pbData, uint16_t cbData); |
LordOfDorks | 3:4b9ad18eae02 | 283 | TPM_RC TPM2_SequenceComplete(TPMI_DH_OBJECT sequenceHandle, TPM2B_AUTH* sequenceHandleAuth, TPMI_RH_HIERARCHY hierarchy, TPM2B_DIGEST* result, TPMT_TK_HASHCHECK* validation); |
LordOfDorks | 3:4b9ad18eae02 | 284 | TPM_RC TPM2_EventSequenceComplete(TPMI_DH_PCR pcrHandle, TPM2B_AUTH* pcrAuth, TPMI_DH_OBJECT sequenceHandle, TPM2B_AUTH* handleAuth, TPML_DIGEST_VALUES* results); |
LordOfDorks | 3:4b9ad18eae02 | 285 | TPM_RC TPM2_PCR_Read(TPML_PCR_SELECTION* pcrSelectionIn, uint32_t* pcrUpdateCounter, TPML_PCR_SELECTION* pcrSelectionOut, TPML_DIGEST* pcrValues); |
LordOfDorks | 1:fd0a59e55a85 | 286 | |
LordOfDorks | 1:fd0a59e55a85 | 287 | private: |
LordOfDorks | 1:fd0a59e55a85 | 288 | // TIS registers addresses |
LordOfDorks | 1:fd0a59e55a85 | 289 | const static uint16_t TIS_ACCESS_REGISTER = 0x0000; |
LordOfDorks | 1:fd0a59e55a85 | 290 | const static uint16_t TIS_INT_ENABLE_REGISTER = 0x0008; |
LordOfDorks | 1:fd0a59e55a85 | 291 | const static uint16_t TIS_INT_VECTOR_REGISTER = 0x000c; |
LordOfDorks | 1:fd0a59e55a85 | 292 | const static uint16_t TIS_INT_STATUS_REGISTER = 0x0010; |
LordOfDorks | 1:fd0a59e55a85 | 293 | const static uint16_t TIS_INTF_CAPABILITY_REGISTER = 0x0014; |
LordOfDorks | 1:fd0a59e55a85 | 294 | const static uint16_t TIS_STS_REGISTER = 0x0018; |
LordOfDorks | 1:fd0a59e55a85 | 295 | const static uint16_t TIS_STS_BURSTCOUNT_REGISTER = 0x0019; // 16bit segment in TIS_STS_REGISTER |
LordOfDorks | 1:fd0a59e55a85 | 296 | const static uint16_t TIS_DATA_FIFO = 0x0024; |
LordOfDorks | 1:fd0a59e55a85 | 297 | const static uint16_t TIS_XDATA_FIFO = 0x0080; |
LordOfDorks | 1:fd0a59e55a85 | 298 | const static uint16_t TIS_DID_VID = 0x0F00; |
LordOfDorks | 1:fd0a59e55a85 | 299 | const static uint16_t TIS_RID = 0x0F04; |
LordOfDorks | 1:fd0a59e55a85 | 300 | const static uint16_t TIS_VENDORSPECIFIC = 0x0F90; |
LordOfDorks | 0:b11c8971edd9 | 301 | const static uint8_t TIS_MAX_HW_FRAME_SIZE = 64; |
LordOfDorks | 0:b11c8971edd9 | 302 | |
LordOfDorks | 1:fd0a59e55a85 | 303 | // TIS Specification Table 14 |
LordOfDorks | 1:fd0a59e55a85 | 304 | enum TIS_ACCESS |
LordOfDorks | 1:fd0a59e55a85 | 305 | { |
LordOfDorks | 0:b11c8971edd9 | 306 | TIS_ACCESS_VALID = 0x80, |
LordOfDorks | 0:b11c8971edd9 | 307 | TIS_ACCESS_ACTIVE_LOCALITY = 0x20, |
LordOfDorks | 1:fd0a59e55a85 | 308 | TIS_ACCESS_BEEING_SEIZED = 0x10, |
LordOfDorks | 1:fd0a59e55a85 | 309 | TIS_ACCESS_SEIZE = 0x08, |
LordOfDorks | 1:fd0a59e55a85 | 310 | TIS_ACCESS_PENDING_REQUEST = 0x04, |
LordOfDorks | 0:b11c8971edd9 | 311 | TIS_ACCESS_REQUEST_USE = 0x02, |
LordOfDorks | 1:fd0a59e55a85 | 312 | TIS_ACCESS_TPM_ESTABLISHMENT = 0x01 |
LordOfDorks | 0:b11c8971edd9 | 313 | }; |
LordOfDorks | 1:fd0a59e55a85 | 314 | |
LordOfDorks | 1:fd0a59e55a85 | 315 | // TIS Specification Table 15 |
LordOfDorks | 1:fd0a59e55a85 | 316 | enum TIS_STS |
LordOfDorks | 1:fd0a59e55a85 | 317 | { |
LordOfDorks | 0:b11c8971edd9 | 318 | TIS_STS_VALID = 0x80, |
LordOfDorks | 0:b11c8971edd9 | 319 | TIS_STS_COMMAND_READY = 0x40, |
LordOfDorks | 0:b11c8971edd9 | 320 | TIS_STS_GO = 0x20, |
LordOfDorks | 0:b11c8971edd9 | 321 | TIS_STS_DATA_AVAIL = 0x10, |
LordOfDorks | 0:b11c8971edd9 | 322 | TIS_STS_DATA_EXPECT = 0x08, |
LordOfDorks | 0:b11c8971edd9 | 323 | TIS_STS_RESPONSERETRY = 0x02 |
LordOfDorks | 0:b11c8971edd9 | 324 | }; |
LordOfDorks | 0:b11c8971edd9 | 325 | |
LordOfDorks | 1:fd0a59e55a85 | 326 | // TIS Specification Table 17 |
LordOfDorks | 1:fd0a59e55a85 | 327 | enum TIS_INTF_CAPPABILITY |
LordOfDorks | 1:fd0a59e55a85 | 328 | { |
LordOfDorks | 1:fd0a59e55a85 | 329 | TIS_INTF_CAPPABILITY_INTERFACE_VERSION_12 = 0x00000000, |
LordOfDorks | 1:fd0a59e55a85 | 330 | TIS_INTF_CAPPABILITY_INTERFACE_VERSION_13 = 0x20000000, |
LordOfDorks | 1:fd0a59e55a85 | 331 | TIS_INTF_CAPPABILITY_DATA_TRANSFER_SIZE_SUPPORT_MASK = 0x00000600, |
LordOfDorks | 1:fd0a59e55a85 | 332 | TIS_INTF_CAPPABILITY_DATA_TRANSFER_SIZE_SUPPORT_64B = 0x00000600, |
LordOfDorks | 1:fd0a59e55a85 | 333 | TIS_INTF_CAPPABILITY_DATA_TRANSFER_SIZE_SUPPORT_32B = 0x00000400, |
LordOfDorks | 1:fd0a59e55a85 | 334 | TIS_INTF_CAPPABILITY_DATA_TRANSFER_SIZE_SUPPORT_8B = 0x00000200, |
LordOfDorks | 1:fd0a59e55a85 | 335 | TIS_INTF_CAPPABILITY_DATA_TRANSFER_SIZE_SUPPORT_LEGACY = 0x00000000, |
LordOfDorks | 1:fd0a59e55a85 | 336 | TIS_INTF_CAPPABILITY_BURST_COUNT_STATIC = 0x00000100, |
LordOfDorks | 1:fd0a59e55a85 | 337 | TIS_INTF_CAPPABILITY_COMMAND_READY_INT_SUPPORT = 0x00000080, |
LordOfDorks | 1:fd0a59e55a85 | 338 | TIS_INTF_CAPPABILITY_INTERRUPT_EDGE_FALLING = 0x00000040, |
LordOfDorks | 1:fd0a59e55a85 | 339 | TIS_INTF_CAPPABILITY_INTERRUPT_EDGE_RISING = 0x00000020, |
LordOfDorks | 1:fd0a59e55a85 | 340 | TIS_INTF_CAPPABILITY_INTERRUPT_LEVEL_LOW = 0x00000010, |
LordOfDorks | 1:fd0a59e55a85 | 341 | TIS_INTF_CAPPABILITY_INTERRUPT_LEVEL_HIGH = 0x00000008, |
LordOfDorks | 1:fd0a59e55a85 | 342 | TIS_INTF_CAPPABILITY_LOCALITY_CHANGE_INT_SUPPORT = 0x00000004, |
LordOfDorks | 1:fd0a59e55a85 | 343 | TIS_INTF_CAPPABILITY_STS_VALID_INT_SUPPORT = 0x00000002, |
LordOfDorks | 1:fd0a59e55a85 | 344 | TIS_INTF_CAPPABILITY_DATA_AVAILABLE_INT_SUPPORT = 0x00000001 |
LordOfDorks | 1:fd0a59e55a85 | 345 | }; |
LordOfDorks | 0:b11c8971edd9 | 346 | |
LordOfDorks | 1:fd0a59e55a85 | 347 | // TIS Spceification Table 19 |
LordOfDorks | 1:fd0a59e55a85 | 348 | enum TIS_INT_ENABLE |
LordOfDorks | 1:fd0a59e55a85 | 349 | { |
LordOfDorks | 1:fd0a59e55a85 | 350 | TIS_INT_ENABLE_DATA_AVAILABLE_INT_ENABLE = 0x00000001, |
LordOfDorks | 1:fd0a59e55a85 | 351 | TIS_INT_ENABLE_STS_VALID_INT_ENABLE = 0x00000002, |
LordOfDorks | 1:fd0a59e55a85 | 352 | TIS_INT_ENABLE_LOCALITY_CHANGE_INT_ENABLE = 0x00000004, |
LordOfDorks | 1:fd0a59e55a85 | 353 | TIS_INT_ENABLE_TYPE_POLARITY_HIGH = 0x00000000, |
LordOfDorks | 1:fd0a59e55a85 | 354 | TIS_INT_ENABLE_TYPE_POLARITY_LOW = 0x00000008, |
LordOfDorks | 1:fd0a59e55a85 | 355 | TIS_INT_ENABLE_TYPE_POLARITY_RISING = 0x00000010, |
LordOfDorks | 1:fd0a59e55a85 | 356 | TIS_INT_ENABLE_TYPE_POLARITY_FALLING = 0x00000018, |
LordOfDorks | 1:fd0a59e55a85 | 357 | TIS_INT_ENABLE_COMMAND_READY_INT_ENABLE = 0x00000080, |
LordOfDorks | 1:fd0a59e55a85 | 358 | TIS_INT_ENABLE_GLOBAL_INT_ENABLE = -2147483648 //0x80000000 |
LordOfDorks | 1:fd0a59e55a85 | 359 | }; |
LordOfDorks | 0:b11c8971edd9 | 360 | |
LordOfDorks | 1:fd0a59e55a85 | 361 | enum TIS_INT_STATUS |
LordOfDorks | 1:fd0a59e55a85 | 362 | { |
LordOfDorks | 1:fd0a59e55a85 | 363 | TIS_INT_STATUS_DATA_AVAILABLE_INT_OCCURED = 0x01, |
LordOfDorks | 1:fd0a59e55a85 | 364 | TIS_INT_STATUS_STS_VALID_INT_OCCURED = 0x02, |
LordOfDorks | 1:fd0a59e55a85 | 365 | TIS_INT_STATUS_LOCALITY_CHANGE_INT_OCCURED = 0x04, |
LordOfDorks | 1:fd0a59e55a85 | 366 | TIS_INT_STATUS_COMMAND_READY_INT_OCCURED = 0x80, |
LordOfDorks | 1:fd0a59e55a85 | 367 | TIS_INT_STATUS_RESET_ALL = TIS_INT_STATUS_DATA_AVAILABLE_INT_OCCURED | |
LordOfDorks | 1:fd0a59e55a85 | 368 | TIS_INT_STATUS_STS_VALID_INT_OCCURED | |
LordOfDorks | 1:fd0a59e55a85 | 369 | TIS_INT_STATUS_LOCALITY_CHANGE_INT_OCCURED | |
LordOfDorks | 1:fd0a59e55a85 | 370 | TIS_INT_STATUS_COMMAND_READY_INT_OCCURED |
LordOfDorks | 1:fd0a59e55a85 | 371 | }; |
LordOfDorks | 0:b11c8971edd9 | 372 | |
LordOfDorks | 1:fd0a59e55a85 | 373 | enum TIS_SESSION_STATUS |
LordOfDorks | 1:fd0a59e55a85 | 374 | { |
LordOfDorks | 1:fd0a59e55a85 | 375 | TIS_SESSION_STATUS_UNINITIALIZED = 0, |
LordOfDorks | 1:fd0a59e55a85 | 376 | TIS_SESSION_STATUS_NEW, |
LordOfDorks | 1:fd0a59e55a85 | 377 | TIS_SESSION_STATUS_GET_READY_FOR_COMMAND, |
LordOfDorks | 1:fd0a59e55a85 | 378 | TIS_SESSION_STATUS_READY_FOR_COMMAND, |
LordOfDorks | 1:fd0a59e55a85 | 379 | TIS_SESSION_STATUS_EXECUTING, |
LordOfDorks | 1:fd0a59e55a85 | 380 | TIS_SESSION_STATUS_DATA_AVAILABLE, |
LordOfDorks | 1:fd0a59e55a85 | 381 | TIS_SESSION_STATUS_COMPLETE, |
LordOfDorks | 1:fd0a59e55a85 | 382 | TIS_SESSION_STATUS_PREEMPTED, |
LordOfDorks | 1:fd0a59e55a85 | 383 | TIS_SESSION_STATUS_TIMEOUT |
LordOfDorks | 1:fd0a59e55a85 | 384 | }; |
LordOfDorks | 0:b11c8971edd9 | 385 | |
LordOfDorks | 1:fd0a59e55a85 | 386 | // Bottom interface to the TPM chip |
LordOfDorks | 1:fd0a59e55a85 | 387 | SPI m_spi; |
LordOfDorks | 1:fd0a59e55a85 | 388 | DigitalOut m_chipSelect; |
LordOfDorks | 1:fd0a59e55a85 | 389 | DigitalIn m_interrupt; |
LordOfDorks | 1:fd0a59e55a85 | 390 | uint32_t m_intfCabability; |
LordOfDorks | 2:526bf792254d | 391 | bool m_fixedBurstCount; |
LordOfDorks | 2:526bf792254d | 392 | uint16_t m_maxBurstCount; |
LordOfDorks | 1:fd0a59e55a85 | 393 | TIS_LOCALITY m_locality; |
LordOfDorks | 1:fd0a59e55a85 | 394 | |
LordOfDorks | 1:fd0a59e55a85 | 395 | // Bottom communication functions |
LordOfDorks | 1:fd0a59e55a85 | 396 | bool FullDuplex(bool readCycle, uint16_t reg, uint8_t* pbBuffer, uint16_t cbBuffer); |
LordOfDorks | 1:fd0a59e55a85 | 397 | bool ReadRegister(uint16_t reg, uint8_t* pbBuffer, uint16_t cbBuffer) |
LordOfDorks | 1:fd0a59e55a85 | 398 | { |
LordOfDorks | 1:fd0a59e55a85 | 399 | return FullDuplex(true, reg, pbBuffer, cbBuffer); |
LordOfDorks | 1:fd0a59e55a85 | 400 | } |
LordOfDorks | 1:fd0a59e55a85 | 401 | bool WriteRegister(uint16_t reg, uint8_t* pbBuffer, uint16_t cbBuffer) |
LordOfDorks | 1:fd0a59e55a85 | 402 | { |
LordOfDorks | 1:fd0a59e55a85 | 403 | return FullDuplex(false, reg, pbBuffer, cbBuffer); |
LordOfDorks | 1:fd0a59e55a85 | 404 | } |
LordOfDorks | 1:fd0a59e55a85 | 405 | uint16_t GetBurstCount(); |
LordOfDorks | 1:fd0a59e55a85 | 406 | bool TpmInteruptOn(uint32_t intEnable); |
LordOfDorks | 1:fd0a59e55a85 | 407 | bool RequestLocality(TIS_TPM20::TIS_LOCALITY locality); |
LordOfDorks | 0:b11c8971edd9 | 408 | bool ReleaseLocality(); |
LordOfDorks | 1:fd0a59e55a85 | 409 | |
LordOfDorks | 1:fd0a59e55a85 | 410 | // Top interface to caller |
LordOfDorks | 1:fd0a59e55a85 | 411 | #ifndef TPM_TIS_NO_COMMAND_FILTERING |
LordOfDorks | 1:fd0a59e55a85 | 412 | TIS_RESULT ApplyFilter(TIS_TPM20::TIS_LOCALITY locality, uint8_t* pbCmd, uint32_t cbCmd); |
LordOfDorks | 1:fd0a59e55a85 | 413 | #endif |
LordOfDorks | 3:4b9ad18eae02 | 414 | TPM_RC ParseResponseHeader(TPM_ST* tag, UINT32* rspSize, uint8_t** buffer, int32_t* size); |
LordOfDorks | 0:b11c8971edd9 | 415 | }; |
LordOfDorks | 3:4b9ad18eae02 | 416 |