1
2 /*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2004-2006
8 * (C) Christian Kummer 2007
9 *
10 */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <inttypes.h>
16
17 #include "trousers/tss.h"
18 #include "trousers/trousers.h"
19 #include "trousers_types.h"
20 #include "spi_utils.h"
21 #include "capabilities.h"
22 #include "tsplog.h"
23 #include "obj.h"
24
25
26 TSS_RESULT
Tspi_TPM_PcrExtend(TSS_HTPM hTPM,UINT32 ulPcrIndex,UINT32 ulPcrDataLength,BYTE * pbPcrData,TSS_PCR_EVENT * pPcrEvent,UINT32 * pulPcrValueLength,BYTE ** prgbPcrValue)27 Tspi_TPM_PcrExtend(TSS_HTPM hTPM, /* in */
28 UINT32 ulPcrIndex, /* in */
29 UINT32 ulPcrDataLength, /* in */
30 BYTE *pbPcrData, /* in */
31 TSS_PCR_EVENT *pPcrEvent, /* in */
32 UINT32 * pulPcrValueLength, /* out */
33 BYTE ** prgbPcrValue) /* out */
34 {
35 TCPA_PCRVALUE outDigest;
36 TSS_RESULT result;
37 BYTE *extendData;
38 TPM_DIGEST digest;
39 UINT32 number;
40 TSS_HCONTEXT tspContext;
41 Trspi_HashCtx hashCtx;
42
43 if (pulPcrValueLength == NULL || prgbPcrValue == NULL)
44 return TSPERR(TSS_E_BAD_PARAMETER);
45
46 if (ulPcrDataLength > 0 && pbPcrData == NULL)
47 return TSPERR(TSS_E_BAD_PARAMETER);
48
49 if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
50 return result;
51
52 if (pPcrEvent) {
53 /* Create data to extend according to the TSS 1.2 spec section 2.6.2
54 * 'TSS_PCR_EVENT', in the 'rgbPcrValue' parameter description. */
55 result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
56 result |= Trspi_Hash_UINT32(&hashCtx, ulPcrIndex);
57 result |= Trspi_HashUpdate(&hashCtx, ulPcrDataLength, pbPcrData);
58 result |= Trspi_Hash_UINT32(&hashCtx, pPcrEvent->eventType);
59 result |= Trspi_HashUpdate(&hashCtx, pPcrEvent->ulEventLength, pPcrEvent->rgbEvent);
60 if ((result |= Trspi_HashFinal(&hashCtx, (BYTE *)&digest.digest)))
61 return result;
62
63 extendData = (BYTE *)&digest.digest;
64 } else {
65 if (ulPcrDataLength != TPM_SHA1_160_HASH_LEN)
66 return TSPERR(TSS_E_BAD_PARAMETER);
67
68 extendData = pbPcrData;
69 }
70
71 if ((result = TCS_API(tspContext)->Extend(tspContext, ulPcrIndex, *(TPM_DIGEST *)extendData,
72 &outDigest)))
73 return result;
74
75 /* log the event structure if its passed in */
76 if (pPcrEvent) {
77 /* Set the PCR index in the event struct */
78 pPcrEvent->ulPcrIndex = ulPcrIndex;
79
80 if ((pPcrEvent->rgbPcrValue = calloc_tspi(tspContext,
81 TPM_SHA1_160_HASH_LEN)) == NULL) {
82 LogError("malloc of %d bytes failed.", TPM_SHA1_160_HASH_LEN);
83 return TSPERR(TSS_E_OUTOFMEMORY);
84 }
85
86 memcpy(pPcrEvent->rgbPcrValue, (BYTE *)&digest.digest, TPM_SHA1_160_HASH_LEN);
87 pPcrEvent->ulPcrValueLength = TPM_SHA1_160_HASH_LEN;
88
89 /* Set the version info in the event struct */
90 memcpy(&pPcrEvent->versionInfo, &VERSION_1_1, sizeof(TCPA_VERSION));
91
92 if ((result = RPC_LogPcrEvent(tspContext, *pPcrEvent, &number)))
93 return result;
94 }
95
96 *prgbPcrValue = calloc_tspi(tspContext, sizeof(TPM_PCRVALUE));
97 if (*prgbPcrValue == NULL) {
98 LogError("malloc of %zd bytes failed.", sizeof(TPM_PCRVALUE));
99 return TSPERR(TSS_E_OUTOFMEMORY);
100 }
101
102 memcpy(*prgbPcrValue, &outDigest, sizeof(TPM_PCRVALUE));
103 *pulPcrValueLength = sizeof(TPM_PCRVALUE);
104
105 return result;
106 }
107
108 TSS_RESULT
Tspi_TPM_PcrRead(TSS_HTPM hTPM,UINT32 ulPcrIndex,UINT32 * pulPcrValueLength,BYTE ** prgbPcrValue)109 Tspi_TPM_PcrRead(TSS_HTPM hTPM, /* in */
110 UINT32 ulPcrIndex, /* in */
111 UINT32 *pulPcrValueLength, /* out */
112 BYTE **prgbPcrValue) /* out */
113 {
114 TCPA_PCRVALUE outDigest;
115 TSS_RESULT result;
116 TSS_HCONTEXT tspContext;
117
118 if (pulPcrValueLength == NULL || prgbPcrValue == NULL)
119 return TSPERR(TSS_E_BAD_PARAMETER);
120
121 if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
122 return result;
123
124 if ((result = TCS_API(tspContext)->PcrRead(tspContext, ulPcrIndex, &outDigest)))
125 return result;
126
127 *prgbPcrValue = calloc_tspi(tspContext, sizeof(TCPA_PCRVALUE));
128 if (*prgbPcrValue == NULL) {
129 LogError("malloc of %zd bytes failed.", sizeof(TCPA_PCRVALUE));
130 return TSPERR(TSS_E_OUTOFMEMORY);
131 }
132 memcpy(*prgbPcrValue, outDigest.digest, sizeof(TCPA_PCRVALUE));
133 *pulPcrValueLength = sizeof(TCPA_PCRVALUE);
134
135 return TSS_SUCCESS;
136 }
137
138 TSS_RESULT
Tspi_TPM_PcrReset(TSS_HTPM hTPM,TSS_HPCRS hPcrComposite)139 Tspi_TPM_PcrReset(TSS_HTPM hTPM, /* in */
140 TSS_HPCRS hPcrComposite) /* in */
141 {
142 TSS_RESULT result;
143 TSS_HCONTEXT tspContext;
144 UINT32 pcrDataSize;
145 BYTE pcrData[16];
146
147 if (!hPcrComposite)
148 return TSPERR(TSS_E_BAD_PARAMETER);
149
150 if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
151 return result;
152
153 if ((result = obj_pcrs_get_selection(hPcrComposite, &pcrDataSize, pcrData)))
154 return result;
155
156 return TCS_API(tspContext)->PcrReset(tspContext, pcrDataSize, pcrData);
157 }
158
159