| 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 |
// This file is a collection of miscellaneous macros. |
| 37 |
|
| 38 |
#ifndef GP_MACROS_H |
| 39 |
#define GP_MACROS_H |
| 40 |
|
| 41 |
#ifndef NULL |
| 42 |
#define NULL 0 |
| 43 |
#endif |
| 44 |
|
| 45 |
#include "swap.h" |
| 46 |
#include "VendorString.h" |
| 47 |
|
| 48 |
|
| 49 |
//** For Self-test |
| 50 |
// These macros are used in CryptUtil to invoke the incremental self test. |
| 51 |
#if SELF_TEST |
| 52 |
# define TEST(alg) if(TEST_BIT(alg, g_toTest)) CryptTestAlgorithm(alg, NULL) |
| 53 |
|
| 54 |
// Use of TPM_ALG_NULL is reserved for RSAEP/RSADP testing. If someone is wanting |
| 55 |
// to test a hash with that value, don't do it. |
| 56 |
# define TEST_HASH(alg) \ |
| 57 |
if(TEST_BIT(alg, g_toTest) \ |
| 58 |
&& (alg != ALG_NULL_VALUE)) \ |
| 59 |
CryptTestAlgorithm(alg, NULL) |
| 60 |
#else |
| 61 |
# define TEST(alg) |
| 62 |
# define TEST_HASH(alg) |
| 63 |
#endif // SELF_TEST |
| 64 |
|
| 65 |
//** For Failures |
| 66 |
#if defined _POSIX_ |
| 67 |
# define FUNCTION_NAME 0 |
| 68 |
#else |
| 69 |
# define FUNCTION_NAME __FUNCTION__ |
| 70 |
#endif |
| 71 |
|
| 72 |
#if !FAIL_TRACE |
| 73 |
# define FAIL(errorCode) (TpmFail(errorCode)) |
| 74 |
# define LOG_FAILURE(errorCode) (TpmLogFailure(errorCode)) |
| 75 |
#else |
| 76 |
# define FAIL(errorCode) TpmFail(FUNCTION_NAME, __LINE__, errorCode) |
| 77 |
# define LOG_FAILURE(errorCode) TpmLogFailure(FUNCTION_NAME, __LINE__, errorCode) |
| 78 |
#endif |
| 79 |
|
| 80 |
// If implementation is using longjmp, then the call to TpmFail() does not return |
| 81 |
// and the compiler will complain about unreachable code that comes after. To allow |
| 82 |
// for not having longjmp, TpmFail() will return and the subsequent code will be |
| 83 |
// executed. This macro accounts for the difference. |
| 84 |
#ifndef NO_LONGJMP |
| 85 |
# define FAIL_RETURN(returnCode) |
| 86 |
# define TPM_FAIL_RETURN NORETURN void |
| 87 |
#else |
| 88 |
# define FAIL_RETURN(returnCode) return (returnCode) |
| 89 |
# define TPM_FAIL_RETURN void |
| 90 |
#endif |
| 91 |
|
| 92 |
// This macro tests that a condition is TRUE and puts the TPM into failure mode |
| 93 |
// if it is not. If longjmp is being used, then the FAIL(FATAL_ERROR_) macro makes |
| 94 |
// a call from which there is no return. Otherwise, it returns and the function |
| 95 |
// will exit with the appropriate return code. |
| 96 |
#define REQUIRE(condition, errorCode, returnCode) \ |
| 97 |
{ \ |
| 98 |
if(!!(condition)) \ |
| 99 |
{ \ |
| 100 |
FAIL(FATAL_ERROR_errorCode); \ |
| 101 |
FAIL_RETURN(returnCode); \ |
| 102 |
} \ |
| 103 |
} |
| 104 |
|
| 105 |
#define PARAMETER_CHECK(condition, returnCode) \ |
| 106 |
REQUIRE((condition), PARAMETER, returnCode) |
| 107 |
|
| 108 |
#if (defined EMPTY_ASSERT) && (EMPTY_ASSERT != NO) |
| 109 |
# define pAssert(a) ((void)0) |
| 110 |
#else |
| 111 |
# define pAssert(a) {if(!(a)) FAIL(FATAL_ERROR_PARAMETER);} |
| 112 |
#endif |
| 113 |
|
| 114 |
//** Derived from Vendor-specific values |
| 115 |
// Values derived from vendor specific settings in TpmProfile.h |
| 116 |
#define PCR_SELECT_MIN ((PLATFORM_PCR+7)/8) |
| 117 |
#define PCR_SELECT_MAX ((IMPLEMENTATION_PCR+7)/8) |
| 118 |
#define MAX_ORDERLY_COUNT ((1 << ORDERLY_BITS) - 1) |
| 119 |
|
| 120 |
//** Compile-time Checks |
| 121 |
// In some cases, the relationship between two values may be dependent |
| 122 |
// on things that change based on various selections like the chosen cryptographic |
| 123 |
// libraries. It is possible that these selections will result in incompatible |
| 124 |
// settings. These are often detectable by the compiler but it isn't always |
| 125 |
// possible to do the check in the preprocessor code. For example, when the |
| 126 |
// check requires use of "sizeof" then the preprocessor can't do the comparison. |
| 127 |
// For these cases, we include a special macro that, depending on the compiler |
| 128 |
// will generate a warning to indicate if the check always passes or always fails |
| 129 |
// because it involves fixed constants. To run these checks, define COMPILER_CHECKS |
| 130 |
// in TpmBuildSwitches.h |
| 131 |
#if COMPILER_CHECKS |
| 132 |
# define cAssert pAssert |
| 133 |
#else |
| 134 |
# define cAssert(value) |
| 135 |
#endif |
| 136 |
|
| 137 |
// This is used commonly in the "Crypt" code as a way to keep listings from |
| 138 |
// getting too long. This is not to save paper but to allow one to see more |
| 139 |
// useful stuff on the screen at any given time. |
| 140 |
#define ERROR_RETURN(returnCode) \ |
| 141 |
{ \ |
| 142 |
retVal = returnCode; \ |
| 143 |
goto Exit; \ |
| 144 |
} |
| 145 |
|
| 146 |
#ifndef MAX |
| 147 |
# define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 148 |
#endif |
| 149 |
#ifndef MIN |
| 150 |
# define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 151 |
#endif |
| 152 |
#ifndef IsOdd |
| 153 |
# define IsOdd(a) (((a) & 1) != 0) |
| 154 |
#endif |
| 155 |
|
| 156 |
#ifndef BITS_TO_BYTES |
| 157 |
# define BITS_TO_BYTES(bits) (((bits) + 7) >> 3) |
| 158 |
#endif |
| 159 |
|
| 160 |
// These are defined for use when the size of the vector being checked is known |
| 161 |
// at compile time. |
| 162 |
#define TEST_BIT(bit, vector) TestBit((bit), (BYTE *)&(vector), sizeof(vector)) |
| 163 |
#define SET_BIT(bit, vector) SetBit((bit), (BYTE *)&(vector), sizeof(vector)) |
| 164 |
#define CLEAR_BIT(bit, vector) ClearBit((bit), (BYTE *)&(vector), sizeof(vector)) |
| 165 |
|
| 166 |
|
| 167 |
// The following definitions are used if they have not already been defined. The |
| 168 |
// defaults for these settings are compatible with ISO/IEC 9899:2011 (E) |
| 169 |
#ifndef LIB_EXPORT |
| 170 |
# define LIB_EXPORT |
| 171 |
# define LIB_IMPORT |
| 172 |
#endif |
| 173 |
#ifndef NORETURN |
| 174 |
# define NORETURN _Noreturn |
| 175 |
#endif |
| 176 |
#ifndef NOT_REFERENCED |
| 177 |
# define NOT_REFERENCED(x = x) ((void) (x)) |
| 178 |
#endif |
| 179 |
|
| 180 |
#define STD_RESPONSE_HEADER (sizeof(TPM_ST) + sizeof(UINT32) + sizeof(TPM_RC)) |
| 181 |
|
| 182 |
#define JOIN(x, y) x##y |
| 183 |
#define JOIN3(x, y, z) x##y##z |
| 184 |
#define CONCAT(x, y) JOIN(x, y) |
| 185 |
#define CONCAT3(x, y, z) JOIN3(x,y,z) |
| 186 |
|
| 187 |
// If CONTEXT_INTEGRITY_HASH_ALG is defined, then the vendor is using the old style |
| 188 |
// table. Otherwise, pick the "strongest" implemented hash algorithm as the context |
| 189 |
// hash. |
| 190 |
#ifndef CONTEXT_HASH_ALGORITHM |
| 191 |
# if defined ALG_SHA512 && ALG_SHA512 == YES |
| 192 |
# define CONTEXT_HASH_ALGORITHM SHA512 |
| 193 |
# elif defined ALG_SHA384 && ALG_SHA384 == YES |
| 194 |
# define CONTEXT_HASH_ALGORITHM SHA384 |
| 195 |
# elif defined ALG_SHA256 && ALG_SHA256 == YES |
| 196 |
# define CONTEXT_HASH_ALGORITHM SHA256 |
| 197 |
# elif defined ALG_SM3_256 && ALG_SM3_256 == YES |
| 198 |
# define CONTEXT_HASH_ALGORITHM SM3_256 |
| 199 |
# elif defined ALG_SHA1 && ALG_SHA1 == YES |
| 200 |
# define CONTEXT_HASH_ALGORITHM SHA1 |
| 201 |
# endif |
| 202 |
# define CONTEXT_INTEGRITY_HASH_ALG CONCAT(TPM_ALG_, CONTEXT_HASH_ALGORITHM) |
| 203 |
#endif |
| 204 |
|
| 205 |
#ifndef CONTEXT_INTEGRITY_HASH_SIZE |
| 206 |
#define CONTEXT_INTEGRITY_HASH_SIZE CONCAT(CONTEXT_HASH_ALGORITHM, _DIGEST_SIZE) |
| 207 |
#endif |
| 208 |
#if ALG_RSA |
| 209 |
#define RSA_SECURITY_STRENGTH (MAX_RSA_KEY_BITS >= 15360 ? 256 : \ |
| 210 |
(MAX_RSA_KEY_BITS >= 7680 ? 192 : \ |
| 211 |
(MAX_RSA_KEY_BITS >= 3072 ? 128 : \ |
| 212 |
(MAX_RSA_KEY_BITS >= 2048 ? 112 : \ |
| 213 |
(MAX_RSA_KEY_BITS >= 1024 ? 80 : 0))))) |
| 214 |
#else |
| 215 |
#define RSA_SECURITY_STRENGTH 0 |
| 216 |
#endif // ALG_RSA |
| 217 |
|
| 218 |
#if ALG_ECC |
| 219 |
#define ECC_SECURITY_STRENGTH (MAX_ECC_KEY_BITS >= 521 ? 256 : \ |
| 220 |
(MAX_ECC_KEY_BITS >= 384 ? 192 : \ |
| 221 |
(MAX_ECC_KEY_BITS >= 256 ? 128 : 0))) |
| 222 |
#else |
| 223 |
#define ECC_SECURITY_STRENGTH 0 |
| 224 |
#endif // ALG_ECC |
| 225 |
|
| 226 |
#define MAX_ASYM_SECURITY_STRENGTH \ |
| 227 |
MAX(RSA_SECURITY_STRENGTH, ECC_SECURITY_STRENGTH) |
| 228 |
|
| 229 |
#define MAX_HASH_SECURITY_STRENGTH ((CONTEXT_INTEGRITY_HASH_SIZE * 8) / 2) |
| 230 |
|
| 231 |
// Unless some algorithm is broken... |
| 232 |
#define MAX_SYM_SECURITY_STRENGTH MAX_SYM_KEY_BITS |
| 233 |
|
| 234 |
#define MAX_SECURITY_STRENGTH_BITS \ |
| 235 |
MAX(MAX_ASYM_SECURITY_STRENGTH, \ |
| 236 |
MAX(MAX_SYM_SECURITY_STRENGTH, \ |
| 237 |
MAX_HASH_SECURITY_STRENGTH)) |
| 238 |
|
| 239 |
// This is the size that was used before the 1.38 errata requiring that P1.14.4 be |
| 240 |
// followed |
| 241 |
#define PROOF_SIZE CONTEXT_INTEGRITY_HASH_SIZE |
| 242 |
|
| 243 |
// As required by P1.14.4 |
| 244 |
#define COMPLIANT_PROOF_SIZE \ |
| 245 |
(MAX(CONTEXT_INTEGRITY_HASH_SIZE, (2 * MAX_SYM_KEY_BYTES))) |
| 246 |
|
| 247 |
// As required by P1.14.3.1 |
| 248 |
#define COMPLIANT_PRIMARY_SEED_SIZE \ |
| 249 |
BITS_TO_BYTES(MAX_SECURITY_STRENGTH_BITS * 2) |
| 250 |
|
| 251 |
// This is the pre-errata version |
| 252 |
#ifndef PRIMARY_SEED_SIZE |
| 253 |
# define PRIMARY_SEED_SIZE PROOF_SIZE |
| 254 |
#endif |
| 255 |
|
| 256 |
#if USE_SPEC_COMPLIANT_PROOFS |
| 257 |
# undef PROOF_SIZE |
| 258 |
# define PROOF_SIZE COMPLIANT_PROOF_SIZE |
| 259 |
# undef PRIMARY_SEED_SIZE |
| 260 |
# define PRIMARY_SEED_SIZE COMPLIANT_PRIMARY_SEED_SIZE |
| 261 |
#endif // USE_SPEC_COMPLIANT_PROOFS |
| 262 |
|
| 263 |
#if !SKIP_PROOF_ERRORS |
| 264 |
# if PROOF_SIZE < COMPLIANT_PROOF_SIZE |
| 265 |
# error "PROOF_SIZE is not compliant with TPM specification" |
| 266 |
# endif |
| 267 |
# if PRIMARY_SEED_SIZE < COMPLIANT_PRIMARY_SEED_SIZE |
| 268 |
# error Non-compliant PRIMARY_SEED_SIZE |
| 269 |
# endif |
| 270 |
#endif // !SKIP_PROOF_ERRORS |
| 271 |
|
| 272 |
// If CONTEXT_ENCRYPT_ALG is defined, then the vendor is using the old style table |
| 273 |
#if defined CONTEXT_ENCRYPT_ALG |
| 274 |
# undef CONTEXT_ENCRYPT_ALGORITHM |
| 275 |
# if CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE |
| 276 |
# define CONTEXT_ENCRYPT_ALGORITHM AES |
| 277 |
# elif CONTEXT_ENCRYPT_ALG == ALG_SM4_VALUE |
| 278 |
# define CONTEXT_ENCRYPT_ALGORITHM SM4 |
| 279 |
# elif CONTEXT_ENCRYPT_ALG == ALG_CAMELLIA_VALUE |
| 280 |
# define CONTEXT_ENCRYPT_ALGORITHM CAMELLIA |
| 281 |
# elif CONTEXT_ENCRYPT_ALG == ALG_TDES_VALUE |
| 282 |
# error Are you kidding? |
| 283 |
# else |
| 284 |
# error Unknown value for CONTEXT_ENCRYPT_ALG |
| 285 |
# endif // CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE |
| 286 |
#else |
| 287 |
# define CONTEXT_ENCRYPT_ALG \ |
| 288 |
CONCAT3(ALG_, CONTEXT_ENCRYPT_ALGORITHM, _VALUE) |
| 289 |
#endif // CONTEXT_ENCRYPT_ALG |
| 290 |
#define CONTEXT_ENCRYPT_KEY_BITS \ |
| 291 |
CONCAT(CONTEXT_ENCRYPT_ALGORITHM, _MAX_KEY_SIZE_BITS) |
| 292 |
#define CONTEXT_ENCRYPT_KEY_BYTES ((CONTEXT_ENCRYPT_KEY_BITS+7)/8) |
| 293 |
|
| 294 |
// This is updated to follow the requirement of P2 that the label not be larger |
| 295 |
// than 32 bytes. |
| 296 |
#ifndef LABEL_MAX_BUFFER |
| 297 |
#define LABEL_MAX_BUFFER MIN(32, MAX(MAX_ECC_KEY_BYTES, MAX_DIGEST_SIZE)) |
| 298 |
#endif |
| 299 |
|
| 300 |
// This bit is used to indicate that an authorization ticket expires on TPM Reset |
| 301 |
// and TPM Restart. It is added to the timeout value returned by TPM2_PoliySigned() |
| 302 |
// and TPM2_PolicySecret() and used by TPM2_PolicyTicket(). The timeout value is |
| 303 |
// relative to Time (g_time). Time is reset whenever the TPM loses power and cannot |
| 304 |
// be moved forward by the user (as can Clock). 'g_time' is a 64-bit value expressing |
| 305 |
// time in ms. Stealing the MSb for a flag means that the TPM needs to be reset |
| 306 |
// at least once every 292,471,208 years rather than once every 584,942,417 years. |
| 307 |
#define EXPIRATION_BIT ((UINT64)1 << 63) |
| 308 |
|
| 309 |
// Check for consistency of the bit ordering of bit fields |
| 310 |
#if BIG_ENDIAN_TPM && MOST_SIGNIFICANT_BIT_0 && USE_BIT_FIELD_STRUCTURES |
| 311 |
# error "Settings not consistent" |
| 312 |
#endif |
| 313 |
|
| 314 |
// These macros are used to handle the variation in handling of bit fields. If |
| 315 |
#if USE_BIT_FIELD_STRUCTURES // The default, old version, with bit fields |
| 316 |
# define IS_ATTRIBUTE(a, type, b) ((a.b) != 0) |
| 317 |
# define SET_ATTRIBUTE(a, type, b) (a.b = SET) |
| 318 |
# define CLEAR_ATTRIBUTE(a, type, b) (a.b = CLEAR) |
| 319 |
# define GET_ATTRIBUTE(a, type, b) (a.b) |
| 320 |
# define TPMA_ZERO_INITIALIZER() {0} |
| 321 |
#else |
| 322 |
# define IS_ATTRIBUTE(a, type, b) ((a & type##_##b) != 0) |
| 323 |
# define SET_ATTRIBUTE(a, type, b) (a |= type##_##b) |
| 324 |
# define CLEAR_ATTRIBUTE(a, type, b) (a &= ~type##_##b) |
| 325 |
# define GET_ATTRIBUTE(a, type, b) \ |
| 326 |
(type)((a & type##_##b) >> type##_##b##_SHIFT) |
| 327 |
# define TPMA_ZERO_INITIALIZER() (0) |
| 328 |
#endif |
| 329 |
|
| 330 |
#define VERIFY(_X) if(!(_X)) goto Error |
| 331 |
|
| 332 |
#endif // GP_MACROS_H |