| 1 |
/* Microsoft Reference Implementation for TPM 2.0 |
| 2 |
* |
| 3 |
* The copyright in this software is being made available under the BSD License, |
| 4 |
* included below. This software may be subject to other third party and |
| 5 |
* contributor rights, including patent rights, and no such rights are granted |
| 6 |
* under this license. |
| 7 |
* |
| 8 |
* Copyright (c) Microsoft Corporation |
| 9 |
* |
| 10 |
* All rights reserved. |
| 11 |
* |
| 12 |
* BSD License |
| 13 |
* |
| 14 |
* Redistribution and use in source and binary forms, with or without modification, |
| 15 |
* are permitted provided that the following conditions are met: |
| 16 |
* |
| 17 |
* Redistributions of source code must retain the above copyright notice, this list |
| 18 |
* of conditions and the following disclaimer. |
| 19 |
* |
| 20 |
* Redistributions in binary form must reproduce the above copyright notice, this |
| 21 |
* list of conditions and the following disclaimer in the documentation and/or |
| 22 |
* other materials provided with the distribution. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" |
| 25 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 28 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 31 |
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
//** Introduction |
| 36 |
// |
| 37 |
// This function contains the functions needed for PCR access and manipulation. |
| 38 |
// |
| 39 |
// This implementation uses a static allocation for the PCR. The amount of |
| 40 |
// memory is allocated based on the number of PCR in the implementation and |
| 41 |
// the number of implemented hash algorithms. This is not the expected |
| 42 |
// implementation. PCR SPACE DEFINITIONS. |
| 43 |
// |
| 44 |
// In the definitions below, the g_hashPcrMap is a bit array that indicates |
| 45 |
// which of the PCR are implemented. The g_hashPcr array is an array of digests. |
| 46 |
// In this implementation, the space is allocated whether the PCR is implemented |
| 47 |
// or not. |
| 48 |
|
| 49 |
//** Includes, Defines, and Data Definitions |
| 50 |
#define PCR_C |
| 51 |
#include "Tpm.h" |
| 52 |
|
| 53 |
// The initial value of PCR attributes. The value of these fields should be |
| 54 |
// consistent with PC Client specification |
| 55 |
// In this implementation, we assume the total number of implemented PCR is 24. |
| 56 |
static const PCR_Attributes s_initAttributes[] = |
| 57 |
{ |
| 58 |
// PCR 0 - 15, static RTM |
| 59 |
{1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, |
| 60 |
{1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, |
| 61 |
{1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, |
| 62 |
{1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, {1, 0, 0x1F}, |
| 63 |
|
| 64 |
{0, 0x0F, 0x1F}, // PCR 16, Debug |
| 65 |
{0, 0x10, 0x1C}, // PCR 17, Locality 4 |
| 66 |
{0, 0x10, 0x1C}, // PCR 18, Locality 3 |
| 67 |
{0, 0x10, 0x0C}, // PCR 19, Locality 2 |
| 68 |
{0, 0x14, 0x0E}, // PCR 20, Locality 1 |
| 69 |
{0, 0x14, 0x04}, // PCR 21, Dynamic OS |
| 70 |
{0, 0x14, 0x04}, // PCR 22, Dynamic OS |
| 71 |
{0, 0x0F, 0x1F}, // PCR 23, Application specific |
| 72 |
{0, 0x0F, 0x1F} // PCR 24, testing policy |
| 73 |
}; |
| 74 |
|
| 75 |
//** Functions |
| 76 |
|
| 77 |
//*** PCRBelongsAuthGroup() |
| 78 |
// This function indicates if a PCR belongs to a group that requires an authValue |
| 79 |
// in order to modify the PCR. If it does, 'groupIndex' is set to value of |
| 80 |
// the group index. This feature of PCR is decided by the platform specification. |
| 81 |
// Return Type: BOOL |
| 82 |
// TRUE(1) PCR belongs an authorization group |
| 83 |
// FALSE(0) PCR does not belong an authorization group |
| 84 |
BOOL |
| 85 |
PCRBelongsAuthGroup( |
| 86 |
TPMI_DH_PCR handle, // IN: handle of PCR |
| 87 |
UINT32 *groupIndex // OUT: group index if PCR belongs a |
| 88 |
// group that allows authValue. If PCR |
| 89 |
// does not belong to an authorization |
| 90 |
// group, the value in this parameter is |
| 91 |
// invalid |
| 92 |
) |
| 93 |
{ |
| 94 |
#if defined NUM_AUTHVALUE_PCR_GROUP && NUM_AUTHVALUE_PCR_GROUP > 0 |
| 95 |
// Platform specification determines to which authorization group a PCR belongs |
| 96 |
// (if any). In this implementation, we assume there is only |
| 97 |
// one authorization group which contains PCR[20-22]. If the platform |
| 98 |
// specification requires differently, the implementation should be changed |
| 99 |
// accordingly |
| 100 |
if(handle >= 20 && handle <= 22) |
| 101 |
{ |
| 102 |
*groupIndex = 0; |
| 103 |
return TRUE; |
| 104 |
} |
| 105 |
|
| 106 |
#endif |
| 107 |
return FALSE; |
| 108 |
} |
| 109 |
|
| 110 |
//*** PCRBelongsPolicyGroup() |
| 111 |
// This function indicates if a PCR belongs to a group that requires a policy |
| 112 |
// authorization in order to modify the PCR. If it does, 'groupIndex' is set |
| 113 |
// to value of the group index. This feature of PCR is decided by the platform |
| 114 |
// specification. |
| 115 |
// Return Type: BOOL |
| 116 |
// TRUE(1) PCR belongs a policy group |
| 117 |
// FALSE(0) PCR does not belong a policy group |
| 118 |
BOOL |
| 119 |
PCRBelongsPolicyGroup( |
| 120 |
TPMI_DH_PCR handle, // IN: handle of PCR |
| 121 |
UINT32 *groupIndex // OUT: group index if PCR belongs a group that |
| 122 |
// allows policy. If PCR does not belong to |
| 123 |
// a policy group, the value in this |
| 124 |
// parameter is invalid |
| 125 |
) |
| 126 |
{ |
| 127 |
#if defined NUM_POLICY_PCR_GROUP && NUM_POLICY_PCR_GROUP > 0 |
| 128 |
// Platform specification decides if a PCR belongs to a policy group and |
| 129 |
// belongs to which group. In this implementation, we assume there is only |
| 130 |
// one policy group which contains PCR20-22. If the platform specification |
| 131 |
// requires differently, the implementation should be changed accordingly |
| 132 |
if(handle >= 20 && handle <= 22) |
| 133 |
{ |
| 134 |
*groupIndex = 0; |
| 135 |
return TRUE; |
| 136 |
} |
| 137 |
#endif |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
|
| 141 |
//*** PCRBelongsTCBGroup() |
| 142 |
// This function indicates if a PCR belongs to the TCB group. |
| 143 |
// Return Type: BOOL |
| 144 |
// TRUE(1) PCR belongs to TCB group |
| 145 |
// FALSE(0) PCR does not belong to TCB group |
| 146 |
static BOOL |
| 147 |
PCRBelongsTCBGroup( |
| 148 |
TPMI_DH_PCR handle // IN: handle of PCR |
| 149 |
) |
| 150 |
{ |
| 151 |
#if ENABLE_PCR_NO_INCREMENT == YES |
| 152 |
// Platform specification decides if a PCR belongs to a TCB group. In this |
| 153 |
// implementation, we assume PCR[20-22] belong to TCB group. If the platform |
| 154 |
// specification requires differently, the implementation should be |
| 155 |
// changed accordingly |
| 156 |
if(handle >= 20 && handle <= 22) |
| 157 |
return TRUE; |
| 158 |
|
| 159 |
#endif |
| 160 |
return FALSE; |
| 161 |
} |
| 162 |
|
| 163 |
//*** PCRPolicyIsAvailable() |
| 164 |
// This function indicates if a policy is available for a PCR. |
| 165 |
// Return Type: BOOL |
| 166 |
// TRUE(1) the PCR should be authorized by policy |
| 167 |
// FALSE(0) the PCR does not allow policy |
| 168 |
BOOL |
| 169 |
PCRPolicyIsAvailable( |
| 170 |
TPMI_DH_PCR handle // IN: PCR handle |
| 171 |
) |
| 172 |
{ |
| 173 |
UINT32 groupIndex; |
| 174 |
|
| 175 |
return PCRBelongsPolicyGroup(handle, &groupIndex); |
| 176 |
} |
| 177 |
|
| 178 |
//*** PCRGetAuthValue() |
| 179 |
// This function is used to access the authValue of a PCR. If PCR does not |
| 180 |
// belong to an authValue group, an EmptyAuth will be returned. |
| 181 |
TPM2B_AUTH * |
| 182 |
PCRGetAuthValue( |
| 183 |
TPMI_DH_PCR handle // IN: PCR handle |
| 184 |
) |
| 185 |
{ |
| 186 |
UINT32 groupIndex; |
| 187 |
|
| 188 |
if(PCRBelongsAuthGroup(handle, &groupIndex)) |
| 189 |
{ |
| 190 |
return &gc.pcrAuthValues.auth[groupIndex]; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
return NULL; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
//*** PCRGetAuthPolicy() |
| 199 |
// This function is used to access the authorization policy of a PCR. It sets |
| 200 |
// 'policy' to the authorization policy and returns the hash algorithm for policy |
| 201 |
// If the PCR does not allow a policy, TPM_ALG_NULL is returned. |
| 202 |
TPMI_ALG_HASH |
| 203 |
PCRGetAuthPolicy( |
| 204 |
TPMI_DH_PCR handle, // IN: PCR handle |
| 205 |
TPM2B_DIGEST *policy // OUT: policy of PCR |
| 206 |
) |
| 207 |
{ |
| 208 |
UINT32 groupIndex; |
| 209 |
|
| 210 |
if(PCRBelongsPolicyGroup(handle, &groupIndex)) |
| 211 |
{ |
| 212 |
*policy = gp.pcrPolicies.policy[groupIndex]; |
| 213 |
return gp.pcrPolicies.hashAlg[groupIndex]; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
policy->t.size = 0; |
| 218 |
return TPM_ALG_NULL; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
//*** PCRSimStart() |
| 223 |
// This function is used to initialize the policies when a TPM is manufactured. |
| 224 |
// This function would only be called in a manufacturing environment or in |
| 225 |
// a TPM simulator. |
| 226 |
void |
| 227 |
PCRSimStart( |
| 228 |
void |
| 229 |
) |
| 230 |
{ |
| 231 |
UINT32 i; |
| 232 |
#if defined NUM_POLICY_PCR_GROUP && NUM_POLICY_PCR_GROUP > 0 |
| 233 |
for(i = 0; i < NUM_POLICY_PCR_GROUP; i++) |
| 234 |
{ |
| 235 |
gp.pcrPolicies.hashAlg[i] = TPM_ALG_NULL; |
| 236 |
gp.pcrPolicies.policy[i].t.size = 0; |
| 237 |
} |
| 238 |
#endif |
| 239 |
#if defined NUM_AUTHVALUE_PCR_GROUP && NUM_AUTHVALUE_PCR_GROUP > 0 |
| 240 |
for(i = 0; i < NUM_AUTHVALUE_PCR_GROUP; i++) |
| 241 |
{ |
| 242 |
gc.pcrAuthValues.auth[i].t.size = 0; |
| 243 |
} |
| 244 |
#endif |
| 245 |
// We need to give an initial configuration on allocated PCR before |
| 246 |
// receiving any TPM2_PCR_Allocate command to change this configuration |
| 247 |
// When the simulation environment starts, we allocate all the PCRs |
| 248 |
for(gp.pcrAllocated.count = 0; gp.pcrAllocated.count < HASH_COUNT; |
| 249 |
gp.pcrAllocated.count++) |
| 250 |
{ |
| 251 |
gp.pcrAllocated.pcrSelections[gp.pcrAllocated.count].hash |
| 252 |
= CryptHashGetAlgByIndex(gp.pcrAllocated.count); |
| 253 |
|
| 254 |
gp.pcrAllocated.pcrSelections[gp.pcrAllocated.count].sizeofSelect |
| 255 |
= PCR_SELECT_MAX; |
| 256 |
for(i = 0; i < PCR_SELECT_MAX; i++) |
| 257 |
gp.pcrAllocated.pcrSelections[gp.pcrAllocated.count].pcrSelect[i] |
| 258 |
= 0xFF; |
| 259 |
} |
| 260 |
|
| 261 |
// Store the initial configuration to NV |
| 262 |
NV_SYNC_PERSISTENT(pcrPolicies); |
| 263 |
NV_SYNC_PERSISTENT(pcrAllocated); |
| 264 |
|
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
//*** GetSavedPcrPointer() |
| 269 |
// This function returns the address of an array of state saved PCR based |
| 270 |
// on the hash algorithm. |
| 271 |
// Return Type: BYTE * |
| 272 |
// NULL no such algorithm |
| 273 |
// != NULL pointer to the 0th byte of the 0th PCR |
| 274 |
static BYTE * |
| 275 |
GetSavedPcrPointer( |
| 276 |
TPM_ALG_ID alg, // IN: algorithm for bank |
| 277 |
UINT32 pcrIndex // IN: PCR index in PCR_SAVE |
| 278 |
) |
| 279 |
{ |
| 280 |
BYTE *retVal; |
| 281 |
switch(alg) |
| 282 |
{ |
| 283 |
#if ALG_SHA1 |
| 284 |
case ALG_SHA1_VALUE: |
| 285 |
retVal = gc.pcrSave.sha1[pcrIndex]; |
| 286 |
break; |
| 287 |
#endif |
| 288 |
#if ALG_SHA256 |
| 289 |
case ALG_SHA256_VALUE: |
| 290 |
retVal = gc.pcrSave.sha256[pcrIndex]; |
| 291 |
break; |
| 292 |
#endif |
| 293 |
#if ALG_SHA384 |
| 294 |
case ALG_SHA384_VALUE: |
| 295 |
retVal = gc.pcrSave.sha384[pcrIndex]; |
| 296 |
break; |
| 297 |
#endif |
| 298 |
|
| 299 |
#if ALG_SHA512 |
| 300 |
case ALG_SHA512_VALUE: |
| 301 |
retVal = gc.pcrSave.sha512[pcrIndex]; |
| 302 |
break; |
| 303 |
#endif |
| 304 |
#if ALG_SM3_256 |
| 305 |
case ALG_SM3_256_VALUE: |
| 306 |
retVal = gc.pcrSave.sm3_256[pcrIndex]; |
| 307 |
break; |
| 308 |
#endif |
| 309 |
default: |
| 310 |
FAIL(FATAL_ERROR_INTERNAL); |
| 311 |
} |
| 312 |
return retVal; |
| 313 |
} |
| 314 |
|
| 315 |
//*** PcrIsAllocated() |
| 316 |
// This function indicates if a PCR number for the particular hash algorithm |
| 317 |
// is allocated. |
| 318 |
// Return Type: BOOL |
| 319 |
// TRUE(1) PCR is allocated |
| 320 |
// FALSE(0) PCR is not allocated |
| 321 |
BOOL |
| 322 |
PcrIsAllocated( |
| 323 |
UINT32 pcr, // IN: The number of the PCR |
| 324 |
TPMI_ALG_HASH hashAlg // IN: The PCR algorithm |
| 325 |
) |
| 326 |
{ |
| 327 |
UINT32 i; |
| 328 |
BOOL allocated = FALSE; |
| 329 |
|
| 330 |
if(pcr < IMPLEMENTATION_PCR) |
| 331 |
{ |
| 332 |
for(i = 0; i < gp.pcrAllocated.count; i++) |
| 333 |
{ |
| 334 |
if(gp.pcrAllocated.pcrSelections[i].hash == hashAlg) |
| 335 |
{ |
| 336 |
if(((gp.pcrAllocated.pcrSelections[i].pcrSelect[pcr / 8]) |
| 337 |
& (1 << (pcr % 8))) != 0) |
| 338 |
allocated = TRUE; |
| 339 |
else |
| 340 |
allocated = FALSE; |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
return allocated; |
| 346 |
} |
| 347 |
|
| 348 |
//*** GetPcrPointer() |
| 349 |
// This function returns the address of an array of PCR based on the |
| 350 |
// hash algorithm. |
| 351 |
// Return Type: BYTE * |
| 352 |
// NULL no such algorithm |
| 353 |
// != NULL pointer to the 0th byte of the 0th PCR |
| 354 |
static BYTE * |
| 355 |
GetPcrPointer( |
| 356 |
TPM_ALG_ID alg, // IN: algorithm for bank |
| 357 |
UINT32 pcrNumber // IN: PCR number |
| 358 |
) |
| 359 |
{ |
| 360 |
static BYTE *pcr = NULL; |
| 361 |
|
| 362 |
if(!PcrIsAllocated(pcrNumber, alg)) |
| 363 |
return NULL; |
| 364 |
|
| 365 |
switch(alg) |
| 366 |
{ |
| 367 |
#if ALG_SHA1 |
| 368 |
case ALG_SHA1_VALUE: |
| 369 |
pcr = s_pcrs[pcrNumber].sha1Pcr; |
| 370 |
break; |
| 371 |
#endif |
| 372 |
#if ALG_SHA256 |
| 373 |
case ALG_SHA256_VALUE: |
| 374 |
pcr = s_pcrs[pcrNumber].sha256Pcr; |
| 375 |
break; |
| 376 |
#endif |
| 377 |
#if ALG_SHA384 |
| 378 |
case ALG_SHA384_VALUE: |
| 379 |
pcr = s_pcrs[pcrNumber].sha384Pcr; |
| 380 |
break; |
| 381 |
#endif |
| 382 |
#if ALG_SHA512 |
| 383 |
case ALG_SHA512_VALUE: |
| 384 |
pcr = s_pcrs[pcrNumber].sha512Pcr; |
| 385 |
break; |
| 386 |
#endif |
| 387 |
#if ALG_SM3_256 |
| 388 |
case ALG_SM3_256_VALUE: |
| 389 |
pcr = s_pcrs[pcrNumber].sm3_256Pcr; |
| 390 |
break; |
| 391 |
#endif |
| 392 |
default: |
| 393 |
FAIL(FATAL_ERROR_INTERNAL); |
| 394 |
break; |
| 395 |
} |
| 396 |
return pcr; |
| 397 |
} |
| 398 |
|
| 399 |
//*** IsPcrSelected() |
| 400 |
// This function indicates if an indicated PCR number is selected by the bit map in |
| 401 |
// 'selection'. |
| 402 |
// Return Type: BOOL |
| 403 |
// TRUE(1) PCR is selected |
| 404 |
// FALSE(0) PCR is not selected |
| 405 |
static BOOL |
| 406 |
IsPcrSelected( |
| 407 |
UINT32 pcr, // IN: The number of the PCR |
| 408 |
TPMS_PCR_SELECTION *selection // IN: The selection structure |
| 409 |
) |
| 410 |
{ |
| 411 |
BOOL selected; |
| 412 |
selected = (pcr < IMPLEMENTATION_PCR |
| 413 |
&& ((selection->pcrSelect[pcr / 8]) & (1 << (pcr % 8))) != 0); |
| 414 |
return selected; |
| 415 |
} |
| 416 |
|
| 417 |
//*** FilterPcr() |
| 418 |
// This function modifies a PCR selection array based on the implemented |
| 419 |
// PCR. |
| 420 |
static void |
| 421 |
FilterPcr( |
| 422 |
TPMS_PCR_SELECTION *selection // IN: input PCR selection |
| 423 |
) |
| 424 |
{ |
| 425 |
UINT32 i; |
| 426 |
TPMS_PCR_SELECTION *allocated = NULL; |
| 427 |
|
| 428 |
// If size of select is less than PCR_SELECT_MAX, zero the unspecified PCR |
| 429 |
for(i = selection->sizeofSelect; i < PCR_SELECT_MAX; i++) |
| 430 |
selection->pcrSelect[i] = 0; |
| 431 |
|
| 432 |
// Find the internal configuration for the bank |
| 433 |
for(i = 0; i < gp.pcrAllocated.count; i++) |
| 434 |
{ |
| 435 |
if(gp.pcrAllocated.pcrSelections[i].hash == selection->hash) |
| 436 |
{ |
| 437 |
allocated = &gp.pcrAllocated.pcrSelections[i]; |
| 438 |
break; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
for(i = 0; i < selection->sizeofSelect; i++) |
| 443 |
{ |
| 444 |
if(allocated == NULL) |
| 445 |
{ |
| 446 |
// If the required bank does not exist, clear input selection |
| 447 |
selection->pcrSelect[i] = 0; |
| 448 |
} |
| 449 |
else |
| 450 |
selection->pcrSelect[i] &= allocated->pcrSelect[i]; |
| 451 |
} |
| 452 |
|
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
//*** PcrDrtm() |
| 457 |
// This function does the DRTM and H-CRTM processing it is called from |
| 458 |
// _TPM_Hash_End. |
| 459 |
void |
| 460 |
PcrDrtm( |
| 461 |
const TPMI_DH_PCR pcrHandle, // IN: the index of the PCR to be |
| 462 |
// modified |
| 463 |
const TPMI_ALG_HASH hash, // IN: the bank identifier |
| 464 |
const TPM2B_DIGEST *digest // IN: the digest to modify the PCR |
| 465 |
) |
| 466 |
{ |
| 467 |
BYTE *pcrData = GetPcrPointer(hash, pcrHandle); |
| 468 |
|
| 469 |
if(pcrData != NULL) |
| 470 |
{ |
| 471 |
// Rest the PCR to zeros |
| 472 |
MemorySet(pcrData, 0, digest->t.size); |
| 473 |
|
| 474 |
// if the TPM has not started, then set the PCR to 0...04 and then extend |
| 475 |
if(!TPMIsStarted()) |
| 476 |
{ |
| 477 |
pcrData[digest->t.size - 1] = 4; |
| 478 |
} |
| 479 |
// Now, extend the value |
| 480 |
PCRExtend(pcrHandle, hash, digest->t.size, (BYTE *)digest->t.buffer); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
//*** PCR_ClearAuth() |
| 485 |
// This function is used to reset the PCR authorization values. It is called |
| 486 |
// on TPM2_Startup(CLEAR) and TPM2_Clear(). |
| 487 |
void |
| 488 |
PCR_ClearAuth( |
| 489 |
void |
| 490 |
) |
| 491 |
{ |
| 492 |
#if defined NUM_AUTHVALUE_PCR_GROUP && NUM_AUTHVALUE_PCR_GROUP > 0 |
| 493 |
int j; |
| 494 |
for(j = 0; j < NUM_AUTHVALUE_PCR_GROUP; j++) |
| 495 |
{ |
| 496 |
gc.pcrAuthValues.auth[j].t.size = 0; |
| 497 |
} |
| 498 |
#endif |
| 499 |
} |
| 500 |
|
| 501 |
//*** PCRStartup() |
| 502 |
// This function initializes the PCR subsystem at TPM2_Startup(). |
| 503 |
BOOL |
| 504 |
PCRStartup( |
| 505 |
STARTUP_TYPE type, // IN: startup type |
| 506 |
BYTE locality // IN: startup locality |
| 507 |
) |
| 508 |
{ |
| 509 |
UINT32 pcr, j; |
| 510 |
UINT32 saveIndex = 0; |
| 511 |
|
| 512 |
g_pcrReConfig = FALSE; |
| 513 |
|
| 514 |
// Don't test for SU_RESET because that should be the default when nothing |
| 515 |
// else is selected |
| 516 |
if(type != SU_RESUME && type != SU_RESTART) |
| 517 |
{ |
| 518 |
// PCR generation counter is cleared at TPM_RESET |
| 519 |
gr.pcrCounter = 0; |
| 520 |
} |
| 521 |
|
| 522 |
// Initialize/Restore PCR values |
| 523 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 524 |
{ |
| 525 |
// On resume, need to know if this PCR had its state saved or not |
| 526 |
UINT32 stateSaved; |
| 527 |
|
| 528 |
if(type == SU_RESUME |
| 529 |
&& s_initAttributes[pcr].stateSave == SET) |
| 530 |
{ |
| 531 |
stateSaved = 1; |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
stateSaved = 0; |
| 536 |
PCRChanged(pcr); |
| 537 |
} |
| 538 |
|
| 539 |
// If this is the H-CRTM PCR and we are not doing a resume and we |
| 540 |
// had an H-CRTM event, then we don't change this PCR |
| 541 |
if(pcr == HCRTM_PCR && type != SU_RESUME && g_DrtmPreStartup == TRUE) |
| 542 |
continue; |
| 543 |
|
| 544 |
// Iterate each hash algorithm bank |
| 545 |
for(j = 0; j < gp.pcrAllocated.count; j++) |
| 546 |
{ |
| 547 |
TPMI_ALG_HASH hash = gp.pcrAllocated.pcrSelections[j].hash; |
| 548 |
BYTE *pcrData = GetPcrPointer(hash, pcr); |
| 549 |
UINT16 pcrSize = CryptHashGetDigestSize(hash); |
| 550 |
|
| 551 |
if(pcrData != NULL) |
| 552 |
{ |
| 553 |
// if state was saved |
| 554 |
if(stateSaved == 1) |
| 555 |
{ |
| 556 |
// Restore saved PCR value |
| 557 |
BYTE *pcrSavedData; |
| 558 |
pcrSavedData = GetSavedPcrPointer( |
| 559 |
gp.pcrAllocated.pcrSelections[j].hash, |
| 560 |
saveIndex); |
| 561 |
if(pcrSavedData == NULL) |
| 562 |
return FALSE; |
| 563 |
MemoryCopy(pcrData, pcrSavedData, pcrSize); |
| 564 |
} |
| 565 |
else |
| 566 |
// PCR was not restored by state save |
| 567 |
{ |
| 568 |
// If the reset locality of the PCR is 4, then |
| 569 |
// the reset value is all one's, otherwise it is |
| 570 |
// all zero. |
| 571 |
if((s_initAttributes[pcr].resetLocality & 0x10) != 0) |
| 572 |
MemorySet(pcrData, 0xFF, pcrSize); |
| 573 |
else |
| 574 |
{ |
| 575 |
MemorySet(pcrData, 0, pcrSize); |
| 576 |
if(pcr == HCRTM_PCR) |
| 577 |
pcrData[pcrSize - 1] = locality; |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
saveIndex += stateSaved; |
| 583 |
} |
| 584 |
// Reset authValues on TPM2_Startup(CLEAR) |
| 585 |
if(type != SU_RESUME) |
| 586 |
PCR_ClearAuth(); |
| 587 |
return TRUE; |
| 588 |
} |
| 589 |
|
| 590 |
//*** PCRStateSave() |
| 591 |
// This function is used to save the PCR values that will be restored on TPM Resume. |
| 592 |
void |
| 593 |
PCRStateSave( |
| 594 |
TPM_SU type // IN: startup type |
| 595 |
) |
| 596 |
{ |
| 597 |
UINT32 pcr, j; |
| 598 |
UINT32 saveIndex = 0; |
| 599 |
|
| 600 |
// if state save CLEAR, nothing to be done. Return here |
| 601 |
if(type == TPM_SU_CLEAR) |
| 602 |
return; |
| 603 |
|
| 604 |
// Copy PCR values to the structure that should be saved to NV |
| 605 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 606 |
{ |
| 607 |
UINT32 stateSaved = (s_initAttributes[pcr].stateSave == SET) ? 1 : 0; |
| 608 |
|
| 609 |
// Iterate each hash algorithm bank |
| 610 |
for(j = 0; j < gp.pcrAllocated.count; j++) |
| 611 |
{ |
| 612 |
BYTE *pcrData; |
| 613 |
UINT32 pcrSize; |
| 614 |
|
| 615 |
pcrData = GetPcrPointer(gp.pcrAllocated.pcrSelections[j].hash, pcr); |
| 616 |
|
| 617 |
if(pcrData != NULL) |
| 618 |
{ |
| 619 |
pcrSize |
| 620 |
= CryptHashGetDigestSize(gp.pcrAllocated.pcrSelections[j].hash); |
| 621 |
|
| 622 |
if(stateSaved == 1) |
| 623 |
{ |
| 624 |
// Restore saved PCR value |
| 625 |
BYTE *pcrSavedData; |
| 626 |
pcrSavedData |
| 627 |
= GetSavedPcrPointer(gp.pcrAllocated.pcrSelections[j].hash, |
| 628 |
saveIndex); |
| 629 |
MemoryCopy(pcrSavedData, pcrData, pcrSize); |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
saveIndex += stateSaved; |
| 634 |
} |
| 635 |
|
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
//*** PCRIsStateSaved() |
| 640 |
// This function indicates if the selected PCR is a PCR that is state saved |
| 641 |
// on TPM2_Shutdown(STATE). The return value is based on PCR attributes. |
| 642 |
// Return Type: BOOL |
| 643 |
// TRUE(1) PCR is state saved |
| 644 |
// FALSE(0) PCR is not state saved |
| 645 |
BOOL |
| 646 |
PCRIsStateSaved( |
| 647 |
TPMI_DH_PCR handle // IN: PCR handle to be extended |
| 648 |
) |
| 649 |
{ |
| 650 |
UINT32 pcr = handle - PCR_FIRST; |
| 651 |
|
| 652 |
if(s_initAttributes[pcr].stateSave == SET) |
| 653 |
return TRUE; |
| 654 |
else |
| 655 |
return FALSE; |
| 656 |
} |
| 657 |
|
| 658 |
//*** PCRIsResetAllowed() |
| 659 |
// This function indicates if a PCR may be reset by the current command locality. |
| 660 |
// The return value is based on PCR attributes, and not the PCR allocation. |
| 661 |
// Return Type: BOOL |
| 662 |
// TRUE(1) TPM2_PCR_Reset is allowed |
| 663 |
// FALSE(0) TPM2_PCR_Reset is not allowed |
| 664 |
BOOL |
| 665 |
PCRIsResetAllowed( |
| 666 |
TPMI_DH_PCR handle // IN: PCR handle to be extended |
| 667 |
) |
| 668 |
{ |
| 669 |
UINT8 commandLocality; |
| 670 |
UINT8 localityBits = 1; |
| 671 |
UINT32 pcr = handle - PCR_FIRST; |
| 672 |
|
| 673 |
// Check for the locality |
| 674 |
commandLocality = _plat__LocalityGet(); |
| 675 |
|
| 676 |
#ifdef DRTM_PCR |
| 677 |
// For a TPM that does DRTM, Reset is not allowed at locality 4 |
| 678 |
if(commandLocality == 4) |
| 679 |
return FALSE; |
| 680 |
#endif |
| 681 |
|
| 682 |
localityBits = localityBits << commandLocality; |
| 683 |
if((localityBits & s_initAttributes[pcr].resetLocality) == 0) |
| 684 |
return FALSE; |
| 685 |
else |
| 686 |
return TRUE; |
| 687 |
} |
| 688 |
|
| 689 |
//*** PCRChanged() |
| 690 |
// This function checks a PCR handle to see if the attributes for the PCR are set |
| 691 |
// so that any change to the PCR causes an increment of the pcrCounter. If it does, |
| 692 |
// then the function increments the counter. Will also bump the counter if the |
| 693 |
// handle is zero which means that PCR 0 can not be in the TCB group. Bump on zero |
| 694 |
// is used by TPM2_Clear(). |
| 695 |
void |
| 696 |
PCRChanged( |
| 697 |
TPM_HANDLE pcrHandle // IN: the handle of the PCR that changed. |
| 698 |
) |
| 699 |
{ |
| 700 |
// For the reference implementation, the only change that does not cause |
| 701 |
// increment is a change to a PCR in the TCB group. |
| 702 |
if((pcrHandle == 0) || !PCRBelongsTCBGroup(pcrHandle)) |
| 703 |
{ |
| 704 |
gr.pcrCounter++; |
| 705 |
if(gr.pcrCounter == 0) |
| 706 |
FAIL(FATAL_ERROR_COUNTER_OVERFLOW); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
//*** PCRIsExtendAllowed() |
| 711 |
// This function indicates a PCR may be extended at the current command locality. |
| 712 |
// The return value is based on PCR attributes, and not the PCR allocation. |
| 713 |
// Return Type: BOOL |
| 714 |
// TRUE(1) extend is allowed |
| 715 |
// FALSE(0) extend is not allowed |
| 716 |
BOOL |
| 717 |
PCRIsExtendAllowed( |
| 718 |
TPMI_DH_PCR handle // IN: PCR handle to be extended |
| 719 |
) |
| 720 |
{ |
| 721 |
UINT8 commandLocality; |
| 722 |
UINT8 localityBits = 1; |
| 723 |
UINT32 pcr = handle - PCR_FIRST; |
| 724 |
|
| 725 |
// Check for the locality |
| 726 |
commandLocality = _plat__LocalityGet(); |
| 727 |
localityBits = localityBits << commandLocality; |
| 728 |
if((localityBits & s_initAttributes[pcr].extendLocality) == 0) |
| 729 |
return FALSE; |
| 730 |
else |
| 731 |
return TRUE; |
| 732 |
} |
| 733 |
|
| 734 |
//*** PCRExtend() |
| 735 |
// This function is used to extend a PCR in a specific bank. |
| 736 |
void |
| 737 |
PCRExtend( |
| 738 |
TPMI_DH_PCR handle, // IN: PCR handle to be extended |
| 739 |
TPMI_ALG_HASH hash, // IN: hash algorithm of PCR |
| 740 |
UINT32 size, // IN: size of data to be extended |
| 741 |
BYTE *data // IN: data to be extended |
| 742 |
) |
| 743 |
{ |
| 744 |
BYTE *pcrData; |
| 745 |
HASH_STATE hashState; |
| 746 |
UINT16 pcrSize; |
| 747 |
|
| 748 |
pcrData = GetPcrPointer(hash, handle - PCR_FIRST); |
| 749 |
|
| 750 |
// Extend PCR if it is allocated |
| 751 |
if(pcrData != NULL) |
| 752 |
{ |
| 753 |
pcrSize = CryptHashGetDigestSize(hash); |
| 754 |
CryptHashStart(&hashState, hash); |
| 755 |
CryptDigestUpdate(&hashState, pcrSize, pcrData); |
| 756 |
CryptDigestUpdate(&hashState, size, data); |
| 757 |
CryptHashEnd(&hashState, pcrSize, pcrData); |
| 758 |
|
| 759 |
// PCR has changed so update the pcrCounter if necessary |
| 760 |
PCRChanged(handle); |
| 761 |
} |
| 762 |
|
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
//*** PCRComputeCurrentDigest() |
| 767 |
// This function computes the digest of the selected PCR. |
| 768 |
// |
| 769 |
// As a side-effect, 'selection' is modified so that only the implemented PCR |
| 770 |
// will have their bits still set. |
| 771 |
void |
| 772 |
PCRComputeCurrentDigest( |
| 773 |
TPMI_ALG_HASH hashAlg, // IN: hash algorithm to compute digest |
| 774 |
TPML_PCR_SELECTION *selection, // IN/OUT: PCR selection (filtered on |
| 775 |
// output) |
| 776 |
TPM2B_DIGEST *digest // OUT: digest |
| 777 |
) |
| 778 |
{ |
| 779 |
HASH_STATE hashState; |
| 780 |
TPMS_PCR_SELECTION *select; |
| 781 |
BYTE *pcrData; // will point to a digest |
| 782 |
UINT32 pcrSize; |
| 783 |
UINT32 pcr; |
| 784 |
UINT32 i; |
| 785 |
|
| 786 |
// Initialize the hash |
| 787 |
digest->t.size = CryptHashStart(&hashState, hashAlg); |
| 788 |
pAssert(digest->t.size > 0 && digest->t.size < UINT16_MAX); |
| 789 |
|
| 790 |
// Iterate through the list of PCR selection structures |
| 791 |
for(i = 0; i < selection->count; i++) |
| 792 |
{ |
| 793 |
// Point to the current selection |
| 794 |
select = &selection->pcrSelections[i]; // Point to the current selection |
| 795 |
FilterPcr(select); // Clear out the bits for unimplemented PCR |
| 796 |
|
| 797 |
// Need the size of each digest |
| 798 |
pcrSize = CryptHashGetDigestSize(selection->pcrSelections[i].hash); |
| 799 |
|
| 800 |
// Iterate through the selection |
| 801 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 802 |
{ |
| 803 |
if(IsPcrSelected(pcr, select)) // Is this PCR selected |
| 804 |
{ |
| 805 |
// Get pointer to the digest data for the bank |
| 806 |
pcrData = GetPcrPointer(selection->pcrSelections[i].hash, pcr); |
| 807 |
pAssert(pcrData != NULL); |
| 808 |
CryptDigestUpdate(&hashState, pcrSize, pcrData); // add to digest |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
// Complete hash stack |
| 813 |
CryptHashEnd2B(&hashState, &digest->b); |
| 814 |
|
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
//*** PCRRead() |
| 819 |
// This function is used to read a list of selected PCR. If the requested PCR |
| 820 |
// number exceeds the maximum number that can be output, the 'selection' is |
| 821 |
// adjusted to reflect the actual output PCR. |
| 822 |
void |
| 823 |
PCRRead( |
| 824 |
TPML_PCR_SELECTION *selection, // IN/OUT: PCR selection (filtered on |
| 825 |
// output) |
| 826 |
TPML_DIGEST *digest, // OUT: digest |
| 827 |
UINT32 *pcrCounter // OUT: the current value of PCR generation |
| 828 |
// number |
| 829 |
) |
| 830 |
{ |
| 831 |
TPMS_PCR_SELECTION *select; |
| 832 |
BYTE *pcrData; // will point to a digest |
| 833 |
UINT32 pcr; |
| 834 |
UINT32 i; |
| 835 |
|
| 836 |
digest->count = 0; |
| 837 |
|
| 838 |
// Iterate through the list of PCR selection structures |
| 839 |
for(i = 0; i < selection->count; i++) |
| 840 |
{ |
| 841 |
// Point to the current selection |
| 842 |
select = &selection->pcrSelections[i]; // Point to the current selection |
| 843 |
FilterPcr(select); // Clear out the bits for unimplemented PCR |
| 844 |
|
| 845 |
// Iterate through the selection |
| 846 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 847 |
{ |
| 848 |
if(IsPcrSelected(pcr, select)) // Is this PCR selected |
| 849 |
{ |
| 850 |
// Check if number of digest exceed upper bound |
| 851 |
if(digest->count > 7) |
| 852 |
{ |
| 853 |
// Clear rest of the current select bitmap |
| 854 |
while(pcr < IMPLEMENTATION_PCR |
| 855 |
// do not round up! |
| 856 |
&& (pcr / 8) < select->sizeofSelect) |
| 857 |
{ |
| 858 |
// do not round up! |
| 859 |
select->pcrSelect[pcr / 8] &= (BYTE)~(1 << (pcr % 8)); |
| 860 |
pcr++; |
| 861 |
} |
| 862 |
// Exit inner loop |
| 863 |
break; |
| 864 |
} |
| 865 |
// Need the size of each digest |
| 866 |
digest->digests[digest->count].t.size = |
| 867 |
CryptHashGetDigestSize(selection->pcrSelections[i].hash); |
| 868 |
|
| 869 |
// Get pointer to the digest data for the bank |
| 870 |
pcrData = GetPcrPointer(selection->pcrSelections[i].hash, pcr); |
| 871 |
pAssert(pcrData != NULL); |
| 872 |
// Add to the data to digest |
| 873 |
MemoryCopy(digest->digests[digest->count].t.buffer, |
| 874 |
pcrData, |
| 875 |
digest->digests[digest->count].t.size); |
| 876 |
digest->count++; |
| 877 |
} |
| 878 |
} |
| 879 |
// If we exit inner loop because we have exceed the output upper bound |
| 880 |
if(digest->count > 7 && pcr < IMPLEMENTATION_PCR) |
| 881 |
{ |
| 882 |
// Clear rest of the selection |
| 883 |
while(i < selection->count) |
| 884 |
{ |
| 885 |
MemorySet(selection->pcrSelections[i].pcrSelect, 0, |
| 886 |
selection->pcrSelections[i].sizeofSelect); |
| 887 |
i++; |
| 888 |
} |
| 889 |
// exit outer loop |
| 890 |
break; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
*pcrCounter = gr.pcrCounter; |
| 895 |
|
| 896 |
return; |
| 897 |
} |
| 898 |
|
| 899 |
//*** PCRAllocate() |
| 900 |
// This function is used to change the PCR allocation. |
| 901 |
// Return Type: TPM_RC |
| 902 |
// TPM_RC_NO_RESULT allocate failed |
| 903 |
// TPM_RC_PCR improper allocation |
| 904 |
TPM_RC |
| 905 |
PCRAllocate( |
| 906 |
TPML_PCR_SELECTION *allocate, // IN: required allocation |
| 907 |
UINT32 *maxPCR, // OUT: Maximum number of PCR |
| 908 |
UINT32 *sizeNeeded, // OUT: required space |
| 909 |
UINT32 *sizeAvailable // OUT: available space |
| 910 |
) |
| 911 |
{ |
| 912 |
UINT32 i, j, k; |
| 913 |
TPML_PCR_SELECTION newAllocate; |
| 914 |
// Initialize the flags to indicate if HCRTM PCR and DRTM PCR are allocated. |
| 915 |
BOOL pcrHcrtm = FALSE; |
| 916 |
BOOL pcrDrtm = FALSE; |
| 917 |
|
| 918 |
// Create the expected new PCR allocation based on the existing allocation |
| 919 |
// and the new input: |
| 920 |
// 1. if a PCR bank does not appear in the new allocation, the existing |
| 921 |
// allocation of this PCR bank will be preserved. |
| 922 |
// 2. if a PCR bank appears multiple times in the new allocation, only the |
| 923 |
// last one will be in effect. |
| 924 |
newAllocate = gp.pcrAllocated; |
| 925 |
for(i = 0; i < allocate->count; i++) |
| 926 |
{ |
| 927 |
for(j = 0; j < newAllocate.count; j++) |
| 928 |
{ |
| 929 |
// If hash matches, the new allocation covers the old allocation |
| 930 |
// for this particular bank. |
| 931 |
// The assumption is the initial PCR allocation (from manufacture) |
| 932 |
// has all the supported hash algorithms with an assigned bank |
| 933 |
// (possibly empty). So there must be a match for any new bank |
| 934 |
// allocation from the input. |
| 935 |
if(newAllocate.pcrSelections[j].hash == |
| 936 |
allocate->pcrSelections[i].hash) |
| 937 |
{ |
| 938 |
newAllocate.pcrSelections[j] = allocate->pcrSelections[i]; |
| 939 |
break; |
| 940 |
} |
| 941 |
} |
| 942 |
// The j loop must exit with a match. |
| 943 |
pAssert(j < newAllocate.count); |
| 944 |
} |
| 945 |
|
| 946 |
// Max PCR in a bank is MIN(implemented PCR, PCR with attributes defined) |
| 947 |
*maxPCR = sizeof(s_initAttributes) / sizeof(PCR_Attributes); |
| 948 |
if(*maxPCR > IMPLEMENTATION_PCR) |
| 949 |
*maxPCR = IMPLEMENTATION_PCR; |
| 950 |
|
| 951 |
// Compute required size for allocation |
| 952 |
*sizeNeeded = 0; |
| 953 |
for(i = 0; i < newAllocate.count; i++) |
| 954 |
{ |
| 955 |
UINT32 digestSize |
| 956 |
= CryptHashGetDigestSize(newAllocate.pcrSelections[i].hash); |
| 957 |
#if defined(DRTM_PCR) |
| 958 |
// Make sure that we end up with at least one DRTM PCR |
| 959 |
pcrDrtm = pcrDrtm || TestBit(DRTM_PCR, |
| 960 |
newAllocate.pcrSelections[i].pcrSelect, |
| 961 |
newAllocate.pcrSelections[i].sizeofSelect); |
| 962 |
|
| 963 |
#else // if DRTM PCR is not required, indicate that the allocation is OK |
| 964 |
pcrDrtm = TRUE; |
| 965 |
#endif |
| 966 |
|
| 967 |
#if defined(HCRTM_PCR) |
| 968 |
// and one HCRTM PCR (since this is usually PCR 0...) |
| 969 |
pcrHcrtm = pcrHcrtm || TestBit(HCRTM_PCR, |
| 970 |
newAllocate.pcrSelections[i].pcrSelect, |
| 971 |
newAllocate.pcrSelections[i].sizeofSelect); |
| 972 |
#else |
| 973 |
pcrHcrtm = TRUE; |
| 974 |
#endif |
| 975 |
for(j = 0; j < newAllocate.pcrSelections[i].sizeofSelect; j++) |
| 976 |
{ |
| 977 |
BYTE mask = 1; |
| 978 |
for(k = 0; k < 8; k++) |
| 979 |
{ |
| 980 |
if((newAllocate.pcrSelections[i].pcrSelect[j] & mask) != 0) |
| 981 |
*sizeNeeded += digestSize; |
| 982 |
mask = mask << 1; |
| 983 |
} |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
if(!pcrDrtm || !pcrHcrtm) |
| 988 |
return TPM_RC_PCR; |
| 989 |
|
| 990 |
// In this particular implementation, we always have enough space to |
| 991 |
// allocate PCR. Different implementation may return a sizeAvailable less |
| 992 |
// than the sizeNeed. |
| 993 |
*sizeAvailable = sizeof(s_pcrs); |
| 994 |
|
| 995 |
// Save the required allocation to NV. Note that after NV is written, the |
| 996 |
// PCR allocation in NV is no longer consistent with the RAM data |
| 997 |
// gp.pcrAllocated. The NV version reflect the allocate after next |
| 998 |
// TPM_RESET, while the RAM version reflects the current allocation |
| 999 |
NV_WRITE_PERSISTENT(pcrAllocated, newAllocate); |
| 1000 |
|
| 1001 |
return TPM_RC_SUCCESS; |
| 1002 |
} |
| 1003 |
|
| 1004 |
//*** PCRSetValue() |
| 1005 |
// This function is used to set the designated PCR in all banks to an initial value. |
| 1006 |
// The initial value is signed and will be sign extended into the entire PCR. |
| 1007 |
// |
| 1008 |
void |
| 1009 |
PCRSetValue( |
| 1010 |
TPM_HANDLE handle, // IN: the handle of the PCR to set |
| 1011 |
INT8 initialValue // IN: the value to set |
| 1012 |
) |
| 1013 |
{ |
| 1014 |
int i; |
| 1015 |
UINT32 pcr = handle - PCR_FIRST; |
| 1016 |
TPMI_ALG_HASH hash; |
| 1017 |
UINT16 digestSize; |
| 1018 |
BYTE *pcrData; |
| 1019 |
|
| 1020 |
// Iterate supported PCR bank algorithms to reset |
| 1021 |
for(i = 0; i < HASH_COUNT; i++) |
| 1022 |
{ |
| 1023 |
hash = CryptHashGetAlgByIndex(i); |
| 1024 |
// Prevent runaway |
| 1025 |
if(hash == TPM_ALG_NULL) |
| 1026 |
break; |
| 1027 |
|
| 1028 |
// Get a pointer to the data |
| 1029 |
pcrData = GetPcrPointer(gp.pcrAllocated.pcrSelections[i].hash, pcr); |
| 1030 |
|
| 1031 |
// If the PCR is allocated |
| 1032 |
if(pcrData != NULL) |
| 1033 |
{ |
| 1034 |
// And the size of the digest |
| 1035 |
digestSize = CryptHashGetDigestSize(hash); |
| 1036 |
|
| 1037 |
// Set the LSO to the input value |
| 1038 |
pcrData[digestSize - 1] = initialValue; |
| 1039 |
|
| 1040 |
// Sign extend |
| 1041 |
if(initialValue >= 0) |
| 1042 |
MemorySet(pcrData, 0, digestSize - 1); |
| 1043 |
else |
| 1044 |
MemorySet(pcrData, -1, digestSize - 1); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
//*** PCRResetDynamics |
| 1050 |
// This function is used to reset a dynamic PCR to 0. This function is used in |
| 1051 |
// DRTM sequence. |
| 1052 |
void |
| 1053 |
PCRResetDynamics( |
| 1054 |
void |
| 1055 |
) |
| 1056 |
{ |
| 1057 |
UINT32 pcr, i; |
| 1058 |
|
| 1059 |
// Initialize PCR values |
| 1060 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 1061 |
{ |
| 1062 |
// Iterate each hash algorithm bank |
| 1063 |
for(i = 0; i < gp.pcrAllocated.count; i++) |
| 1064 |
{ |
| 1065 |
BYTE *pcrData; |
| 1066 |
UINT32 pcrSize; |
| 1067 |
|
| 1068 |
pcrData = GetPcrPointer(gp.pcrAllocated.pcrSelections[i].hash, pcr); |
| 1069 |
|
| 1070 |
if(pcrData != NULL) |
| 1071 |
{ |
| 1072 |
pcrSize = |
| 1073 |
CryptHashGetDigestSize(gp.pcrAllocated.pcrSelections[i].hash); |
| 1074 |
|
| 1075 |
// Reset PCR |
| 1076 |
// Any PCR can be reset by locality 4 should be reset to 0 |
| 1077 |
if((s_initAttributes[pcr].resetLocality & 0x10) != 0) |
| 1078 |
MemorySet(pcrData, 0, pcrSize); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
} |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
|
| 1085 |
//*** PCRCapGetAllocation() |
| 1086 |
// This function is used to get the current allocation of PCR banks. |
| 1087 |
// Return Type: TPMI_YES_NO |
| 1088 |
// YES if the return count is 0 |
| 1089 |
// NO if the return count is not 0 |
| 1090 |
TPMI_YES_NO |
| 1091 |
PCRCapGetAllocation( |
| 1092 |
UINT32 count, // IN: count of return |
| 1093 |
TPML_PCR_SELECTION *pcrSelection // OUT: PCR allocation list |
| 1094 |
) |
| 1095 |
{ |
| 1096 |
if(count == 0) |
| 1097 |
{ |
| 1098 |
pcrSelection->count = 0; |
| 1099 |
return YES; |
| 1100 |
} |
| 1101 |
else |
| 1102 |
{ |
| 1103 |
*pcrSelection = gp.pcrAllocated; |
| 1104 |
return NO; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
//*** PCRSetSelectBit() |
| 1109 |
// This function sets a bit in a bitmap array. |
| 1110 |
static void |
| 1111 |
PCRSetSelectBit( |
| 1112 |
UINT32 pcr, // IN: PCR number |
| 1113 |
BYTE *bitmap // OUT: bit map to be set |
| 1114 |
) |
| 1115 |
{ |
| 1116 |
bitmap[pcr / 8] |= (1 << (pcr % 8)); |
| 1117 |
return; |
| 1118 |
} |
| 1119 |
|
| 1120 |
//*** PCRGetProperty() |
| 1121 |
// This function returns the selected PCR property. |
| 1122 |
// Return Type: BOOL |
| 1123 |
// TRUE(1) the property type is implemented |
| 1124 |
// FALSE(0) the property type is not implemented |
| 1125 |
static BOOL |
| 1126 |
PCRGetProperty( |
| 1127 |
TPM_PT_PCR property, |
| 1128 |
TPMS_TAGGED_PCR_SELECT *select |
| 1129 |
) |
| 1130 |
{ |
| 1131 |
UINT32 pcr; |
| 1132 |
UINT32 groupIndex; |
| 1133 |
|
| 1134 |
select->tag = property; |
| 1135 |
// Always set the bitmap to be the size of all PCR |
| 1136 |
select->sizeofSelect = (IMPLEMENTATION_PCR + 7) / 8; |
| 1137 |
|
| 1138 |
// Initialize bitmap |
| 1139 |
MemorySet(select->pcrSelect, 0, select->sizeofSelect); |
| 1140 |
|
| 1141 |
// Collecting properties |
| 1142 |
for(pcr = 0; pcr < IMPLEMENTATION_PCR; pcr++) |
| 1143 |
{ |
| 1144 |
switch(property) |
| 1145 |
{ |
| 1146 |
case TPM_PT_PCR_SAVE: |
| 1147 |
if(s_initAttributes[pcr].stateSave == SET) |
| 1148 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1149 |
break; |
| 1150 |
case TPM_PT_PCR_EXTEND_L0: |
| 1151 |
if((s_initAttributes[pcr].extendLocality & 0x01) != 0) |
| 1152 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1153 |
break; |
| 1154 |
case TPM_PT_PCR_RESET_L0: |
| 1155 |
if((s_initAttributes[pcr].resetLocality & 0x01) != 0) |
| 1156 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1157 |
break; |
| 1158 |
case TPM_PT_PCR_EXTEND_L1: |
| 1159 |
if((s_initAttributes[pcr].extendLocality & 0x02) != 0) |
| 1160 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1161 |
break; |
| 1162 |
case TPM_PT_PCR_RESET_L1: |
| 1163 |
if((s_initAttributes[pcr].resetLocality & 0x02) != 0) |
| 1164 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1165 |
break; |
| 1166 |
case TPM_PT_PCR_EXTEND_L2: |
| 1167 |
if((s_initAttributes[pcr].extendLocality & 0x04) != 0) |
| 1168 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1169 |
break; |
| 1170 |
case TPM_PT_PCR_RESET_L2: |
| 1171 |
if((s_initAttributes[pcr].resetLocality & 0x04) != 0) |
| 1172 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1173 |
break; |
| 1174 |
case TPM_PT_PCR_EXTEND_L3: |
| 1175 |
if((s_initAttributes[pcr].extendLocality & 0x08) != 0) |
| 1176 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1177 |
break; |
| 1178 |
case TPM_PT_PCR_RESET_L3: |
| 1179 |
if((s_initAttributes[pcr].resetLocality & 0x08) != 0) |
| 1180 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1181 |
break; |
| 1182 |
case TPM_PT_PCR_EXTEND_L4: |
| 1183 |
if((s_initAttributes[pcr].extendLocality & 0x10) != 0) |
| 1184 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1185 |
break; |
| 1186 |
case TPM_PT_PCR_RESET_L4: |
| 1187 |
if((s_initAttributes[pcr].resetLocality & 0x10) != 0) |
| 1188 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1189 |
break; |
| 1190 |
case TPM_PT_PCR_DRTM_RESET: |
| 1191 |
// DRTM reset PCRs are the PCR reset by locality 4 |
| 1192 |
if((s_initAttributes[pcr].resetLocality & 0x10) != 0) |
| 1193 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1194 |
break; |
| 1195 |
#if defined NUM_POLICY_PCR_GROUP && NUM_POLICY_PCR_GROUP > 0 |
| 1196 |
case TPM_PT_PCR_POLICY: |
| 1197 |
if(PCRBelongsPolicyGroup(pcr + PCR_FIRST, &groupIndex)) |
| 1198 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1199 |
break; |
| 1200 |
#endif |
| 1201 |
#if defined NUM_AUTHVALUE_PCR_GROUP && NUM_AUTHVALUE_PCR_GROUP > 0 |
| 1202 |
case TPM_PT_PCR_AUTH: |
| 1203 |
if(PCRBelongsAuthGroup(pcr + PCR_FIRST, &groupIndex)) |
| 1204 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1205 |
break; |
| 1206 |
#endif |
| 1207 |
#if ENABLE_PCR_NO_INCREMENT == YES |
| 1208 |
case TPM_PT_PCR_NO_INCREMENT: |
| 1209 |
if(PCRBelongsTCBGroup(pcr + PCR_FIRST)) |
| 1210 |
PCRSetSelectBit(pcr, select->pcrSelect); |
| 1211 |
break; |
| 1212 |
#endif |
| 1213 |
default: |
| 1214 |
// If property is not supported, stop scanning PCR attributes |
| 1215 |
// and return. |
| 1216 |
return FALSE; |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
return TRUE; |
| 1221 |
} |
| 1222 |
|
| 1223 |
//*** PCRCapGetProperties() |
| 1224 |
// This function returns a list of PCR properties starting at 'property'. |
| 1225 |
// Return Type: TPMI_YES_NO |
| 1226 |
// YES if no more property is available |
| 1227 |
// NO if there are more properties not reported |
| 1228 |
TPMI_YES_NO |
| 1229 |
PCRCapGetProperties( |
| 1230 |
TPM_PT_PCR property, // IN: the starting PCR property |
| 1231 |
UINT32 count, // IN: count of returned properties |
| 1232 |
TPML_TAGGED_PCR_PROPERTY *select // OUT: PCR select |
| 1233 |
) |
| 1234 |
{ |
| 1235 |
TPMI_YES_NO more = NO; |
| 1236 |
UINT32 i; |
| 1237 |
|
| 1238 |
// Initialize output property list |
| 1239 |
select->count = 0; |
| 1240 |
|
| 1241 |
// The maximum count of properties we may return is MAX_PCR_PROPERTIES |
| 1242 |
if(count > MAX_PCR_PROPERTIES) count = MAX_PCR_PROPERTIES; |
| 1243 |
|
| 1244 |
// TPM_PT_PCR_FIRST is defined as 0 in spec. It ensures that property |
| 1245 |
// value would never be less than TPM_PT_PCR_FIRST |
| 1246 |
cAssert(TPM_PT_PCR_FIRST == 0); |
| 1247 |
|
| 1248 |
// Iterate PCR properties. TPM_PT_PCR_LAST is the index of the last property |
| 1249 |
// implemented on the TPM. |
| 1250 |
for(i = property; i <= TPM_PT_PCR_LAST; i++) |
| 1251 |
{ |
| 1252 |
if(select->count < count) |
| 1253 |
{ |
| 1254 |
// If we have not filled up the return list, add more properties to it |
| 1255 |
if(PCRGetProperty(i, &select->pcrProperty[select->count])) |
| 1256 |
// only increment if the property is implemented |
| 1257 |
select->count++; |
| 1258 |
} |
| 1259 |
else |
| 1260 |
{ |
| 1261 |
// If the return list is full but we still have properties |
| 1262 |
// available, report this and stop iterating. |
| 1263 |
more = YES; |
| 1264 |
break; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
return more; |
| 1268 |
} |
| 1269 |
|
| 1270 |
//*** PCRCapGetHandles() |
| 1271 |
// This function is used to get a list of handles of PCR, started from 'handle'. |
| 1272 |
// If 'handle' exceeds the maximum PCR handle range, an empty list will be |
| 1273 |
// returned and the return value will be NO. |
| 1274 |
// Return Type: TPMI_YES_NO |
| 1275 |
// YES if there are more handles available |
| 1276 |
// NO all the available handles has been returned |
| 1277 |
TPMI_YES_NO |
| 1278 |
PCRCapGetHandles( |
| 1279 |
TPMI_DH_PCR handle, // IN: start handle |
| 1280 |
UINT32 count, // IN: count of returned handles |
| 1281 |
TPML_HANDLE *handleList // OUT: list of handle |
| 1282 |
) |
| 1283 |
{ |
| 1284 |
TPMI_YES_NO more = NO; |
| 1285 |
UINT32 i; |
| 1286 |
|
| 1287 |
pAssert(HandleGetType(handle) == TPM_HT_PCR); |
| 1288 |
|
| 1289 |
// Initialize output handle list |
| 1290 |
handleList->count = 0; |
| 1291 |
|
| 1292 |
// The maximum count of handles we may return is MAX_CAP_HANDLES |
| 1293 |
if(count > MAX_CAP_HANDLES) count = MAX_CAP_HANDLES; |
| 1294 |
|
| 1295 |
// Iterate PCR handle range |
| 1296 |
for(i = handle & HR_HANDLE_MASK; i <= PCR_LAST; i++) |
| 1297 |
{ |
| 1298 |
if(handleList->count < count) |
| 1299 |
{ |
| 1300 |
// If we have not filled up the return list, add this PCR |
| 1301 |
// handle to it |
| 1302 |
handleList->handle[handleList->count] = i + PCR_FIRST; |
| 1303 |
handleList->count++; |
| 1304 |
} |
| 1305 |
else |
| 1306 |
{ |
| 1307 |
// If the return list is full but we still have PCR handle |
| 1308 |
// available, report this and stop iterating |
| 1309 |
more = YES; |
| 1310 |
break; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
return more; |
| 1314 |
} |