| 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 |
//** Includes, Defines, and Types |
| 36 |
#include "Tpm.h" |
| 37 |
#include <assert.h> |
| 38 |
|
| 39 |
#if RUNTIME_SIZE_CHECKS |
| 40 |
|
| 41 |
static int once = 0; |
| 42 |
|
| 43 |
//** TpmSizeChecks() |
| 44 |
// This function is used during the development process to make sure that the |
| 45 |
// vendor-specific values result in a consistent implementation. When possible, |
| 46 |
// the code contains #if to do compile-time checks. However, in some cases, the |
| 47 |
// values require the use of "sizeof()" and that can't be used in an #if. |
| 48 |
void |
| 49 |
TpmSizeChecks( |
| 50 |
void |
| 51 |
) |
| 52 |
{ |
| 53 |
#if DEBUG |
| 54 |
if(once++ != 0) return; |
| 55 |
{ |
| 56 |
BOOL PASS = TRUE; |
| 57 |
UINT32 maxAsymSecurityStrength = MAX_ASYM_SECURITY_STRENGTH; |
| 58 |
UINT32 maxHashSecurityStrength = MAX_HASH_SECURITY_STRENGTH; |
| 59 |
UINT32 maxSymSecurityStrength = MAX_SYM_SECURITY_STRENGTH; |
| 60 |
UINT32 maxSecurityStrengthBits = MAX_SECURITY_STRENGTH_BITS; |
| 61 |
UINT32 proofSize = PROOF_SIZE; |
| 62 |
UINT32 compliantProofSize = COMPLIANT_PROOF_SIZE; |
| 63 |
UINT32 compliantPrimarySeedSize = COMPLIANT_PRIMARY_SEED_SIZE; |
| 64 |
UINT32 primarySeedSize = PRIMARY_SEED_SIZE; |
| 65 |
|
| 66 |
UINT32 cmacState = sizeof(tpmCmacState_t); |
| 67 |
UINT32 hashState = sizeof(HASH_STATE); |
| 68 |
UINT32 keyScheduleSize = sizeof(tpmCryptKeySchedule_t); |
| 69 |
// |
| 70 |
NOT_REFERENCED(cmacState); |
| 71 |
NOT_REFERENCED(hashState); |
| 72 |
NOT_REFERENCED(keyScheduleSize); |
| 73 |
NOT_REFERENCED(maxAsymSecurityStrength); |
| 74 |
NOT_REFERENCED(maxHashSecurityStrength); |
| 75 |
NOT_REFERENCED(maxSymSecurityStrength); |
| 76 |
NOT_REFERENCED(maxSecurityStrengthBits); |
| 77 |
NOT_REFERENCED(proofSize); |
| 78 |
NOT_REFERENCED(compliantProofSize); |
| 79 |
NOT_REFERENCED(compliantPrimarySeedSize); |
| 80 |
NOT_REFERENCED(primarySeedSize); |
| 81 |
|
| 82 |
|
| 83 |
{ |
| 84 |
TPMT_SENSITIVE *p; |
| 85 |
// This assignment keeps compiler from complaining about a conditional |
| 86 |
// comparison being between two constants |
| 87 |
UINT16 max_rsa_key_bytes = MAX_RSA_KEY_BYTES; |
| 88 |
if((max_rsa_key_bytes / 2) != (sizeof(p->sensitive.rsa.t.buffer) / 5)) |
| 89 |
{ |
| 90 |
printf("Sensitive part of TPMT_SENSITIVE is undersized. May be caused by" |
| 91 |
"use of wrong version of Part 2.\n"); |
| 92 |
PASS = FALSE; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#if 0 |
| 97 |
printf("Size of OBJECT = %d\n", sizeof(OBJECT)); |
| 98 |
printf("Size of components in TPMT_SENSITIVE = %d\n", sizeof(TPMT_SENSITIVE)); |
| 99 |
printf(" TPMI_ALG_PUBLIC %d\n", sizeof(TPMI_ALG_PUBLIC)); |
| 100 |
printf(" TPM2B_AUTH %d\n", sizeof(TPM2B_AUTH)); |
| 101 |
printf(" TPM2B_DIGEST %d\n", sizeof(TPM2B_DIGEST)); |
| 102 |
printf(" TPMU_SENSITIVE_COMPOSITE %d\n", |
| 103 |
sizeof(TPMU_SENSITIVE_COMPOSITE)); |
| 104 |
#endif |
| 105 |
// Make sure that the size of the context blob is large enough for the largest |
| 106 |
// context |
| 107 |
// TPMS_CONTEXT_DATA contains two TPM2B values. That is not how this is |
| 108 |
// implemented. Rather, the size field of the TPM2B_CONTEXT_DATA is used to |
| 109 |
// determine the amount of data in the encrypted data. That part is not |
| 110 |
// independently sized. This makes the actual size 2 bytes smaller than |
| 111 |
// calculated using Part 2. Since this is opaque to the caller, it is not |
| 112 |
// necessary to fix. The actual size is returned by TPM2_GetCapabilties(). |
| 113 |
|
| 114 |
// Initialize output handle. At the end of command action, the output |
| 115 |
// handle of an object will be replaced, while the output handle |
| 116 |
// for a session will be the same as input |
| 117 |
|
| 118 |
// Get the size of fingerprint in context blob. The sequence value in |
| 119 |
// TPMS_CONTEXT structure is used as the fingerprint |
| 120 |
{ |
| 121 |
UINT32 fingerprintSize = sizeof(UINT64); |
| 122 |
UINT32 integritySize = sizeof(UINT16) |
| 123 |
+ CryptHashGetDigestSize(CONTEXT_INTEGRITY_HASH_ALG); |
| 124 |
UINT32 biggestObject = MAX(MAX(sizeof(HASH_OBJECT), sizeof(OBJECT)), |
| 125 |
sizeof(SESSION)); |
| 126 |
UINT32 biggestContext = fingerprintSize + integritySize + biggestObject; |
| 127 |
|
| 128 |
// round required size up to nearest 8 byte boundary. |
| 129 |
biggestContext = 8 * ((biggestContext + 7) / 8); |
| 130 |
|
| 131 |
if(MAX_CONTEXT_SIZE != biggestContext) |
| 132 |
{ |
| 133 |
printf("MAX_CONTEXT_SIZE should be changed to %d (%d)\n", biggestContext, MAX_CONTEXT_SIZE); |
| 134 |
PASS = FALSE; |
| 135 |
} |
| 136 |
} |
| 137 |
{ |
| 138 |
union u |
| 139 |
{ |
| 140 |
TPMA_OBJECT attributes; |
| 141 |
UINT32 uint32Value; |
| 142 |
} u; |
| 143 |
// these are defined so that compiler doesn't complain about conditional |
| 144 |
// expressions comparing two constants. |
| 145 |
int aSize = sizeof(u.attributes); |
| 146 |
int uSize = sizeof(u.uint32Value); |
| 147 |
u.uint32Value = 0; |
| 148 |
SET_ATTRIBUTE(u.attributes, TPMA_OBJECT, Reserved_bit_at_0); |
| 149 |
if(u.uint32Value != 1) |
| 150 |
{ |
| 151 |
printf("The bit allocation in a TPMA_OBJECT is not as expected"); |
| 152 |
PASS = FALSE; |
| 153 |
} |
| 154 |
if(aSize != uSize) // comparison of two sizeof() values annoys compiler |
| 155 |
{ |
| 156 |
printf("A TPMA_OBJECT is not the expected size."); |
| 157 |
PASS = FALSE; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Make sure that the size of the Capability buffer can hold the largest |
| 162 |
// TPML_PCR_SELECTION. The list length is nominally set by the number of hash |
| 163 |
// algorithms implemented on the TPM. A requirement of this implementation is |
| 164 |
// that a list of all allowed TPMS_PCR_SELECTIONS fits in MAX_CAP_DATA. |
| 165 |
// TBD |
| 166 |
pAssert(PASS); |
| 167 |
} |
| 168 |
#endif // DEBUG |
| 169 |
} |
| 170 |
|
| 171 |
#endif // RUNTIME_SIZE_CHECKS |