Changeset 108933 in vbox
- Timestamp:
- Apr 10, 2025 12:55:45 PM (4 weeks ago)
- svn:sync-xref-src-repo-rev:
- 168442
- Location:
- trunk/src/libs/libtpms-0.10.0
- Files:
-
- 1 deleted
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/src/libs/libtpms-0.10.0/Makefile.kmk ¶
r108932 r108933 29 29 include $(KBUILD_PATH)/subheader.kmk 30 30 31 VBOX_PATH_LIBTPMS = $(PATH_ROOT)/src/libs/libtpms-0. 9.631 VBOX_PATH_LIBTPMS = $(PATH_ROOT)/src/libs/libtpms-0.10.0 32 32 33 33 LIBRARIES += VBox-libtpms_tpm12 VBox-libtpms_tpm2 VBox-libtpms -
TabularUnified trunk/src/libs/libtpms-0.10.0/config.h ¶
r108932 r108933 17 17 #define HAVE_LIBCRYPTO 1 18 18 19 /* Define to 1 if you have the <memory.h> header file. */ 20 #define HAVE_MEMORY_H 1 21 19 22 /* Define to 1 if you have the <openssl/aes.h> header file. */ 20 #define HAVE_OPENSSL_AES_H 1 23 /* #undef HAVE_OPENSSL_AES_H */ 21 24 22 25 /* Define to 1 if you have the <plbase64.h> header file. */ … … 28 31 /* Define to 1 if you have the <stdint.h> header file. */ 29 32 #define HAVE_STDINT_H 1 30 31 /* Define to 1 if you have the <stdio.h> header file. */32 #define HAVE_STDIO_H 133 33 34 34 /* Define to 1 if you have the <stdlib.h> header file. */ … … 63 63 64 64 /* Define to the full name and version of this package. */ 65 #define PACKAGE_STRING "libtpms 0. 9.0"65 #define PACKAGE_STRING "libtpms 0.10.0" 66 66 67 67 /* Define to the one symbol short name of this package. */ … … 72 72 73 73 /* Define to the version of this package. */ 74 #define PACKAGE_VERSION "0. 9.0"74 #define PACKAGE_VERSION "0.10.0" 75 75 76 /* Define to 1 if all of the C90 standard headers exist (not just the ones 77 required in a freestanding environment). This macro is provided for 78 backward compatibility; new code need not use it. */ 76 /* Define to 1 if you have the ANSI C header files. */ 79 77 #define STDC_HEADERS 1 80 78 … … 86 84 87 85 /* Version number of package */ 88 #define VERSION "0. 9.0"86 #define VERSION "0.10.0" 89 87 90 88 /* With TPM 1.2 support */ -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/compiler.h ¶
r108932 r108933 40 40 41 41 #ifndef VBOX 42 #ifndef __clang__ 43 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) \ 44 __attribute__((format (printf, STRING_IDX, FIRST_TO_CHECK))) 42 #ifndef __clang__ 43 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) \ 44 __attribute__((format (printf, STRING_IDX, FIRST_TO_CHECK))) 45 #else 46 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) \ 47 __attribute__((__format__ (__printf__, STRING_IDX, FIRST_TO_CHECK))) 48 #endif 49 #ifdef __GNUC__ /* gcc and clang */ 50 # define LIBTPMS_ATTR_UNUSED __attribute__((unused)) 51 #endif 45 52 #else 46 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) \ 47 __attribute__((__format__ (__printf__, STRING_IDX, FIRST_TO_CHECK))) 48 #endif 49 #else 50 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) 53 # define ATTRIBUTE_FORMAT(STRING_IDX, FIRST_TO_CHECK) 51 54 #endif 52 55 -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm12/tpm_crypto.c ¶
r108932 r108933 51 51 #include <openssl/evp.h> 52 52 #include <openssl/rsa.h> 53 53 54 #ifdef VBOX 54 #include <openssl/rsa.h>55 #include <openssl/err.h>56 #include <openssl/obj_mac.h>55 #include <openssl/rsa.h> 56 #include <openssl/err.h> 57 #include <openssl/obj_mac.h> 57 58 #endif 58 59 -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm12/tpm_time.c ¶
r108932 r108933 1 /********************************************************************************/ 2 /* */ 3 /* Time Utilities */ 4 /* Written by Ken Goldman */ 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: tpm_time.c 4648 2011-10-25 19:22:18Z kgoldman $ */ 7 /* */ 8 /* (c) Copyright IBM Corporation 2006, 2010. */ 9 /* */ 10 /* All rights reserved. */ 11 /* */ 12 /* Redistribution and use in source and binary forms, with or without */ 1 13 /********************************************************************************/ 2 14 /* */ … … 76 88 return rc; 77 89 } 90 78 91 #endif 79 92 #else 80 93 # include <iprt/time.h> 81 94 82 95 TPM_RESULT TPM_GetTimeOfDay(uint32_t *tv_sec, uint32_t *tv_usec) 83 96 { … … 85 98 RTTIMESPEC Timespec; 86 99 int64_t i64Us; 87 100 88 101 RTTimeNow(&Timespec); 89 102 i64Us = RTTimeSpecGetMicro(&Timespec); -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/BnMath.c ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: BnMath.c 1529 2019-11-21 23:29:01Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 20 19*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 /* 10.2.3 BnMath.c */ 63 64 /* 10.2.3.1 Introduction */ 65 /* The simulator code uses the canonical form whenever possible in order to make the code in Part 3 66 more accessible. The canonical data formats are simple and not well suited for complex big number 67 computations. When operating on big numbers, the data format is changed for easier 68 manipulation. The format is native words in little-endian format. As the magnitude of the number 69 decreases, the length of the array containing the number decreases but the starting address 70 doesn't change. */ 71 /* The functions in this file perform simple operations on these big numbers. Only the more complex 72 operations are passed to the underlying support library. Although the support library would have 73 most of these functions, the interface code to convert the format for the values is greater than 74 the size of the code to implement the functions here. So, rather than incur the overhead of 75 conversion, they are done here. */ 76 /* If an implementer would prefer, the underlying library can be used simply by making code 77 substitutions here. */ 78 /* NOTE: There is an intention to continue to augment these functions so that there would be no need 79 to use an external big number library. */ 80 /* Many of these functions have no error returns and will always return TRUE. This is to allow them 81 to be used in guarded sequences. That is: OK = OK || BnSomething(s); where the BnSomething() 82 function should not be called if OK isn't true. */ 83 84 /* 10.2.3.2 Includes */ 85 #include "Tpm.h" 86 /* A constant value of zero as a stand in for NULL bigNum values */ 87 const bignum_t BnConstZero = {1, 0, {0}}; 88 /* 10.2.3.3 Functions */ 89 /* 10.2.3.3.1 AddSame() */ 90 /* Adds two values that are the same size. This function allows result to be the same as either of 91 the addends. This is a nice function to put into assembly because handling the carry for 92 multi-precision stuff is not as easy in C (unless there is a REALLY smart compiler). It would be 93 nice if there were idioms in a language that a compiler could recognize what is going on and 94 optimize loops like this. */ 95 /* Return Values Meaning */ 96 /* 0 no carry out */ 97 /* 1 carry out */ 98 static BOOL 99 AddSame( 100 crypt_uword_t *result, 101 const crypt_uword_t *op1, 102 const crypt_uword_t *op2, 103 int count 104 ) 105 { 106 int carry = 0; 107 int i; 61 //** Introduction 62 // The simulator code uses the canonical form whenever possible in order to make 63 // the code in Part 3 more accessible. The canonical data formats are simple and 64 // not well suited for complex big number computations. When operating on big 65 // numbers, the data format is changed for easier manipulation. The format is native 66 // words in little-endian format. As the magnitude of the number decreases, the 67 // length of the array containing the number decreases but the starting address 68 // doesn't change. 69 // 70 // The functions in this file perform simple operations on these big numbers. Only 71 // the more complex operations are passed to the underlying support library. 72 // Although the support library would have most of these functions, the interface 73 // code to convert the format for the values is greater than the size of the 74 // code to implement the functions here. So, rather than incur the overhead of 75 // conversion, they are done here. 76 // 77 // If an implementer would prefer, the underlying library can be used simply by 78 // making code substitutions here. 79 // 80 // NOTE: There is an intention to continue to augment these functions so that there 81 // would be no need to use an external big number library. 82 // 83 // Many of these functions have no error returns and will always return TRUE. This 84 // is to allow them to be used in "guarded" sequences. That is: 85 // OK = OK || BnSomething(s); 86 // where the BnSomething() function should not be called if OK isn't true. 87 88 //** Includes 89 #include "Tpm.h" // libtpms: for CryptRand.h 90 #include "TpmMath_Util_fp.h" 91 #include "TpmBigNum.h" 92 extern BOOL g_inFailureMode; // can't use global.h because we can't use tpm.h 93 94 // A constant value of zero as a stand in for NULL bigNum values 95 const bignum_t BnConstZero = {1, 0, {0}}; 96 97 //** Functions 98 99 //*** AddSame() 100 // Adds two values that are the same size. This function allows 'result' to be 101 // the same as either of the addends. This is a nice function to put into assembly 102 // because handling the carry for multi-precision stuff is not as easy in C 103 // (unless there is a REALLY smart compiler). It would be nice if there were idioms 104 // in a language that a compiler could recognize what is going on and optimize 105 // loops like this. 106 // Return Type: int 107 // 0 no carry out 108 // 1 carry out 109 static BOOL AddSame(crypt_uword_t* result, 110 const crypt_uword_t* op1, 111 const crypt_uword_t* op2, 112 int count) 113 { 114 int carry = 0; 115 int i; 116 108 117 for(i = 0; i < count; i++) 109 118 { 110 crypt_uword_t a= op1[i];111 crypt_uword_t 112 result[i] = sum + carry;119 crypt_uword_t a = op1[i]; 120 crypt_uword_t sum = a + op2[i]; 121 result[i] = sum + carry; 113 122 // generate a carry if the sum is less than either of the inputs 114 123 // propagate a carry if there was a carry and the sum + carry is zero … … 120 129 return carry; 121 130 } 122 /* 10.2.3.3.2 CarryProp() */ 123 /* Propagate a carry */ 124 static int 125 CarryProp( 126 crypt_uword_t *result, 127 const crypt_uword_t *op, 128 int count, 129 int carry 130 ) 131 132 //*** CarryProp() 133 // Propagate a carry 134 static int CarryProp( 135 crypt_uword_t* result, const crypt_uword_t* op, int count, int carry) 131 136 { 132 137 for(; count; count--) … … 134 139 return carry; 135 140 } 136 static void 137 CarryResolve( 138 bigNum result, 139 int stop, 140 int carry 141 ) 141 142 static void CarryResolve(bigNum result, int stop, int carry) 142 143 { 143 144 if(carry) … … 148 149 BnSetTop(result, stop); 149 150 } 150 /* 10.2.3.3.3 BnAdd() */ 151 /* This function adds two bigNum values. Always returns TRUE */ 152 LIB_EXPORT BOOL 153 BnAdd( 154 bigNum result, 155 bigConst op1, 156 bigConst op2 157 ) 158 { 159 crypt_uword_t stop; 160 int carry; 161 const bignum_t *n1 = op1; 162 const bignum_t *n2 = op2; 151 152 //*** BnAdd() 153 // This function adds two bigNum values. This function always returns TRUE. 154 LIB_EXPORT BOOL BnAdd(bigNum result, bigConst op1, bigConst op2) 155 { 156 crypt_uword_t stop; 157 int carry; 158 const bignum_t* n1 = op1; 159 const bignum_t* n2 = op2; 160 163 161 // 164 162 if(n2->size > n1->size) … … 168 166 } 169 167 pAssert(result->allocated >= n1->size); 170 stop = MIN(n1->size, n2->allocated);168 stop = MIN(n1->size, n2->allocated); 171 169 carry = (int)AddSame(result->d, n1->d, n2->d, (int)stop); 172 170 if(n1->size > stop) 173 carry = CarryProp(&result->d[stop], &n1->d[stop], (int)(n1->size - stop), carry); 171 carry = 172 CarryProp(&result->d[stop], &n1->d[stop], (int)(n1->size - stop), carry); 174 173 CarryResolve(result, (int)n1->size, carry); 175 174 return TRUE; 176 175 } 177 /* 10.2.3.3.4 BnAddWord() */ 178 /* Adds a word value to a bigNum. */ 179 LIB_EXPORT BOOL 180 BnAddWord( 181 bigNum result, 182 bigConst op, 183 crypt_uword_t word 184 ) 185 { 186 int carry; 176 177 //*** BnAddWord() 178 // This function adds a word value to a bigNum. This function always returns TRUE. 179 LIB_EXPORT BOOL BnAddWord(bigNum result, bigConst op, crypt_uword_t word) 180 { 181 int carry; 187 182 // 188 183 carry = (result->d[0] = op->d[0] + word) < word; … … 191 186 return TRUE; 192 187 } 193 /* 10.2.3.3.5 SubSame() */ 194 /* Subtract two values that have the same size. */ 195 static int 196 SubSame( 197 crypt_uword_t *result, 198 const crypt_uword_t *op1, 199 const crypt_uword_t *op2, 200 int count 201 ) 202 { 203 int borrow = 0; 204 int i; 188 189 //*** SubSame() 190 // This function subtracts two values that have the same size. 191 static int SubSame(crypt_uword_t* result, 192 const crypt_uword_t* op1, 193 const crypt_uword_t* op2, 194 int count) 195 { 196 int borrow = 0; 197 int i; 205 198 for(i = 0; i < count; i++) 206 199 { 207 crypt_uword_t a= op1[i];208 crypt_uword_t 209 result[i] = diff - borrow;200 crypt_uword_t a = op1[i]; 201 crypt_uword_t diff = a - op2[i]; 202 result[i] = diff - borrow; 210 203 // generate | propagate 211 204 borrow = (diff > a) | ((diff == 0) & borrow); … … 213 206 return borrow; 214 207 } 215 /* 10.2.3.3.6 BorrowProp() */ 216 /* This propagates a borrow. If borrow is true when the end of the array is reached, then it means 217 that op2 was larger than op1 and we don't handle that case so an assert is generated. This design 218 choice was made because our only bigNum computations are on large positive numbers (primes) or on 219 fields. Propagate a borrow. */ 220 static int 221 BorrowProp( 222 crypt_uword_t *result, 223 const crypt_uword_t *op, 224 int size, 225 int borrow 226 ) 208 209 //*** BorrowProp() 210 // This propagates a borrow. If borrow is true when the end 211 // of the array is reached, then it means that op2 was larger than 212 // op1 and we don't handle that case so an assert is generated. 213 // This design choice was made because our only bigNum computations 214 // are on large positive numbers (primes) or on fields. 215 // Propagate a borrow. 216 static int BorrowProp( 217 crypt_uword_t* result, const crypt_uword_t* op, int size, int borrow) 227 218 { 228 219 for(; size > 0; size--) … … 230 221 return borrow; 231 222 } 232 /* 10.2.3.3.7 BnSub() */ 233 /* This function does subtraction of two bigNum values and returns result = op1 - op2 when op1 is 234 greater than op2. If op2 is greater than op1, then a fault is generated. This function always 235 returns TRUE. */ 236 237 LIB_EXPORT BOOL 238 BnSub( 239 bigNum result, 240 bigConst op1, 241 bigConst op2 242 ) 243 { 244 int borrow; 245 int stop = (int)MIN(op1->size, op2->allocated); 223 224 //*** BnSub() 225 // This function does subtraction of two bigNum values and returns result = op1 - op2 226 // when op1 is greater than op2. If op2 is greater than op1, then a fault is 227 // generated. This function always returns TRUE. 228 LIB_EXPORT BOOL BnSub(bigNum result, bigConst op1, bigConst op2) 229 { 230 int borrow; 231 int stop = (int)MIN(op1->size, op2->allocated); 246 232 // 247 233 // Make sure that op2 is not obviously larger than op1 … … 249 235 borrow = SubSame(result->d, op1->d, op2->d, stop); 250 236 if(op1->size > (crypt_uword_t)stop) 251 borrow = BorrowProp( &result->d[stop], &op1->d[stop], (int)(op1->size - stop),252 borrow);237 borrow = BorrowProp( 238 &result->d[stop], &op1->d[stop], (int)(op1->size - stop), borrow); 253 239 pAssert(!borrow); 254 240 BnSetTop(result, op1->size); 255 241 return TRUE; 256 242 } 257 /* 10.2.3.3.8 BnSubWord() */ 258 /* This function subtracts a word value from a bigNum. This function always returns TRUE. */ 259 LIB_EXPORT BOOL 260 BnSubWord( 261 bigNum result, 262 bigConst op, 263 crypt_uword_t word 264 ) 265 { 266 int borrow; 243 244 //*** BnSubWord() 245 // This function subtracts a word value from a bigNum. This function always 246 // returns TRUE. 247 LIB_EXPORT BOOL BnSubWord(bigNum result, bigConst op, crypt_uword_t word) 248 { 249 int borrow; 267 250 // 268 251 pAssert(op->size > 1 || word <= op->d[0]); 269 borrow = word > op->d[0];252 borrow = word > op->d[0]; 270 253 result->d[0] = op->d[0] - word; 271 borrow = BorrowProp(&result->d[1], &op->d[1], (int)(op->size - 1), borrow);254 borrow = BorrowProp(&result->d[1], &op->d[1], (int)(op->size - 1), borrow); 272 255 pAssert(!borrow); 273 256 BnSetTop(result, op->size); 274 257 return TRUE; 275 258 } 276 /* 10.2.3.3.9 BnUnsignedCmp() */ 277 /* This function performs a comparison of op1 to op2. The compare is approximately constant time if 278 the size of the values used in the compare is consistent across calls (from the same line in the 279 calling code). */ 280 /* Return Values Meaning */ 281 /* < 0 op1 is less than op2 */ 282 /* 0 op1 is equal to op2 */ 283 /* > 0 op1 is greater than op2 */ 284 LIB_EXPORT int 285 BnUnsignedCmp( 286 bigConst op1, 287 bigConst op2 288 ) 289 { 290 int retVal; 291 int diff; 292 int i; 259 260 //*** BnUnsignedCmp() 261 // This function performs a comparison of op1 to op2. The compare is approximately 262 // constant time if the size of the values used in the compare is consistent 263 // across calls (from the same line in the calling code). 264 // Return Type: int 265 // < 0 op1 is less than op2 266 // 0 op1 is equal to op2 267 // > 0 op1 is greater than op2 268 LIB_EXPORT int BnUnsignedCmp(bigConst op1, bigConst op2) 269 { 270 int retVal; 271 int diff; 272 int i; 293 273 // 294 274 pAssert((op1 != NULL) && (op2 != NULL)); … … 298 278 for(i = (int)(op1->size - 1); i >= 0; i--) 299 279 { 300 diff = (op1->d[i] < op2->d[i]) ? -1 : (op1->d[i] != op2->d[i]);280 diff = (op1->d[i] < op2->d[i]) ? -1 : (op1->d[i] != op2->d[i]); 301 281 retVal = retVal == 0 ? diff : retVal; 302 282 } … … 306 286 return retVal; 307 287 } 308 /* 10.2.3.3.10 BnUnsignedCmpWord() */ 309 /* Compare a bigNum to a crypt_uword_t. */ 310 /* Return Value Meaning */ 311 /* -1 op1 is less that word */ 312 /* 0 op1 is equal to word */ 313 /* 1 op1 is greater than word */ 314 LIB_EXPORT int 315 BnUnsignedCmpWord( 316 bigConst op1, 317 crypt_uword_t word 318 ) 288 289 //*** BnUnsignedCmpWord() 290 // Compare a bigNum to a crypt_uword_t. 291 // Return Type: int 292 // -1 op1 is less that word 293 // 0 op1 is equal to word 294 // 1 op1 is greater than word 295 LIB_EXPORT int BnUnsignedCmpWord(bigConst op1, crypt_uword_t word) 319 296 { 320 297 if(op1->size > 1) … … 322 299 else if(op1->size == 1) 323 300 return (op1->d[0] < word) ? -1 : (op1->d[0] > word); 324 else // op1 is zero301 else // op1 is zero 325 302 // equal if word is zero 326 303 return (word == 0) ? 0 : -1; 327 304 } 328 /* 10.2.3.3.11 BnModWord() */ 329 /* This function does modular division of a big number when the modulus is a word value. */ 330 LIB_EXPORT crypt_word_t 331 BnModWord( 332 bigConst numerator, 333 crypt_word_t modulus 334 ) 305 306 //*** BnModWord() 307 // This function does modular division of a big number when the modulus is a 308 // word value. 309 LIB_EXPORT crypt_word_t BnModWord(bigConst numerator, crypt_word_t modulus) 335 310 { 336 311 BN_MAX(remainder); … … 342 317 return remainder->d[0]; 343 318 } 344 /* 10.2.3.3.12 Msb() */ 345 /* Returns the bit number of the most significant bit of a crypt_uword_t. The number for the least 346 significant bit of any bigNum value is 0. The maximum return value is RADIX_BITS - 1, */ 347 /* Return Values Meaning */ 348 /* -1 the word was zero */ 349 /* n the bit number of the most significant bit in the word */ 350 LIB_EXPORT int 351 Msb( 352 crypt_uword_t word 353 ) 354 { 355 int retVal = -1; 319 320 //*** Msb() 321 // This function returns the bit number of the most significant bit of a 322 // crypt_uword_t. The number for the least significant bit of any bigNum value is 0. 323 // The maximum return value is RADIX_BITS - 1, 324 // Return Type: int 325 // -1 the word was zero 326 // n the bit number of the most significant bit in the word 327 static int Msb(crypt_uword_t word) 328 { 329 int retVal = -1; 356 330 // 357 331 #if RADIX_BITS == 64 358 if(word & 0xffffffff00000000) { retVal += 32; word >>= 32; } 332 if(word & 0xffffffff00000000) 333 { 334 retVal += 32; 335 word >>= 32; 336 } 359 337 #endif 360 if(word & 0xffff0000) { retVal += 16; word >>= 16; } 361 if(word & 0x0000ff00) { retVal += 8; word >>= 8; } 362 if(word & 0x000000f0) { retVal += 4; word >>= 4; } 363 if(word & 0x0000000c) { retVal += 2; word >>= 2; } 364 if(word & 0x00000002) { retVal += 1; word >>= 1; } 338 if(word & 0xffff0000) 339 { 340 retVal += 16; 341 word >>= 16; 342 } 343 if(word & 0x0000ff00) 344 { 345 retVal += 8; 346 word >>= 8; 347 } 348 if(word & 0x000000f0) 349 { 350 retVal += 4; 351 word >>= 4; 352 } 353 if(word & 0x0000000c) 354 { 355 retVal += 2; 356 word >>= 2; 357 } 358 if(word & 0x00000002) 359 { 360 retVal += 1; 361 word >>= 1; 362 } 365 363 return retVal + (int)word; 366 364 } 367 /* 10.2.3.3.13 BnMsb() */ 368 /* This function returns the number of the MSb() of a bigNum value. */ 369 /* Return Value Meaning */ 370 /* -1 the word was zero or bn was NULL */ 371 /* n the bit number of the most significant bit in the word */ 372 373 LIB_EXPORT int 374 BnMsb( 375 bigConst bn 376 ) 365 366 //*** BnMsb() 367 // This function returns the number of the MSb of a bigNum value. 368 // Return Type: int 369 // -1 the word was zero or 'bn' was NULL 370 // n the bit number of the most significant bit in the word 371 LIB_EXPORT int BnMsb(bigConst bn) 377 372 { 378 373 // If the value is NULL, or the size is zero then treat as zero and return -1 379 374 if(bn != NULL && bn->size > 0) 380 375 { 381 int 376 int retVal = Msb(bn->d[bn->size - 1]); 382 377 retVal += (int)(bn->size - 1) * RADIX_BITS; 383 378 return retVal; … … 386 381 return -1; 387 382 } 388 /* 10.2.3.3.14 BnSizeInBits() */ 389 / * Returns the number of bits required to hold a number. It is one greater than the Msb. */390 LIB_EXPORT unsigned 391 BnSizeInBits( 392 bigConst n 393 394 { 395 int 383 384 //*** BnSizeInBits() 385 // This function returns the number of bits required to hold a number. It is one 386 // greater than the Msb. 387 // 388 LIB_EXPORT unsigned BnSizeInBits(bigConst n) 389 { 390 int bits = BnMsb(n) + 1; 396 391 // 397 392 return bits < 0 ? 0 : (unsigned)bits; 398 393 } 399 /* 10.2.3.3.15 BnSetWord() */ 400 /* Change the value of a bignum_t to a word value. */ 401 LIB_EXPORT bigNum 402 BnSetWord( 403 bigNum n, 404 crypt_uword_t w 405 ) 394 395 //*** BnSetWord() 396 // Change the value of a bignum_t to a word value. 397 LIB_EXPORT bigNum BnSetWord(bigNum n, crypt_uword_t w) 406 398 { 407 399 if(n != NULL) … … 413 405 return n; 414 406 } 415 /* 10.2.3.3.16 BnSetBit() */ 416 /* SET a bit in a bigNum. Bit 0 is the least-significant bit in the 0th digit_t. The function always 417 return TRUE */ 418 LIB_EXPORT BOOL 419 BnSetBit( 420 bigNum bn, // IN/OUT: big number to modify 421 unsigned int bitNum // IN: Bit number to SET 422 ) 423 { 424 crypt_uword_t offset = bitNum / RADIX_BITS; 425 pAssert(bn->allocated * RADIX_BITS >= bitNum); 407 408 //*** BnSetBit() 409 // This function will SET a bit in a bigNum. Bit 0 is the least-significant bit in 410 // the 0th digit_t. The function will return FALSE if the bitNum is invalid, else TRUE. 411 LIB_EXPORT BOOL BnSetBit(bigNum bn, // IN/OUT: big number to modify 412 unsigned int bitNum // IN: Bit number to SET 413 ) 414 { 415 crypt_uword_t offset = bitNum / RADIX_BITS; 416 if(bitNum > bn->allocated * RADIX_BITS) 417 { 418 // out of range 419 return FALSE; 420 } 426 421 // Grow the number if necessary to set the bit. 427 422 while(bn->size <= offset) … … 430 425 return TRUE; 431 426 } 432 /* 10.2.3.3.17 BnTestBit() */ 433 / * Check to see if a bit is SET in a bignum_t. The 0th bit is the LSb() of d[0]. */434 / * Return Values Meaning */435 / * TRUE the bit is set */436 / * FALSE the bit is not set or the number is out of range */437 LIB_EXPORT BOOL 438 BnTestBit( 439 bigNum bn,// IN: number to check440 unsigned int bitNum// IN: bit to test441 )442 { 443 crypt_uword_t 427 428 //*** BnTestBit() 429 // This function is used to check to see if a bit is SET in a bignum_t. The 0th bit 430 // is the LSb of d[0]. 431 // Return Type: BOOL 432 // TRUE(1) the bit is set 433 // FALSE(0) the bit is not set or the number is out of range 434 LIB_EXPORT BOOL BnTestBit(bigNum bn, // IN: number to check 435 unsigned int bitNum // IN: bit to test 436 ) 437 { 438 crypt_uword_t offset = RADIX_DIV(bitNum); 444 439 // 445 440 if(bn->size > offset) … … 448 443 return FALSE; 449 444 } 450 /* 10.2.3.3.18 BnMaskBits() */ 451 /* Function to mask off high order bits of a big number. The returned value will have no more than 452 maskBit bits set. */ 453 /* NOTE: There is a requirement that unused words of a bignum_t are set to zero. */ 454 /* Return Values Meaning */ 455 /* TRUE result masked */ 456 /* FALSE the input was not as large as the mask */ 457 LIB_EXPORT BOOL 458 BnMaskBits( 459 bigNum bn, // IN/OUT: number to mask 460 crypt_uword_t maskBit // IN: the bit number for the mask. 461 ) 462 { 463 crypt_uword_t finalSize; 464 BOOL retVal; 445 446 //***BnMaskBits() 447 // This function is used to mask off high order bits of a big number. 448 // The returned value will have no more than 'maskBit' bits 449 // set. 450 // Note: There is a requirement that unused words of a bignum_t are set to zero. 451 // Return Type: BOOL 452 // TRUE(1) result masked 453 // FALSE(0) the input was not as large as the mask 454 LIB_EXPORT BOOL BnMaskBits(bigNum bn, // IN/OUT: number to mask 455 crypt_uword_t maskBit // IN: the bit number for the mask. 456 ) 457 { 458 crypt_uword_t finalSize; 459 BOOL retVal; 460 465 461 finalSize = BITS_TO_CRYPT_WORDS(maskBit); 466 retVal = (finalSize <= bn->allocated);462 retVal = (finalSize <= bn->allocated); 467 463 if(retVal && (finalSize > 0)) 468 464 { 469 crypt_uword_t 465 crypt_uword_t mask; 470 466 mask = ~((crypt_uword_t)0) >> RADIX_MOD(maskBit); 471 467 bn->d[finalSize - 1] &= mask; … … 474 470 return retVal; 475 471 } 476 /* 10.2.3.3.19 BnShiftRight() */ 477 /* Function will shift a bigNum to the right by the shiftAmount. This function always returns 478 TRUE. */ 479 LIB_EXPORT BOOL 480 BnShiftRight( 481 bigNum result, 482 bigConst toShift, 483 uint32_t shiftAmount 484 ) 485 { 486 uint32_t offset = (shiftAmount >> RADIX_LOG2); 487 uint32_t i; 488 uint32_t shiftIn; 489 crypt_uword_t finalSize; 472 473 //*** BnShiftRight() 474 // This function will shift a bigNum to the right by the shiftAmount. 475 // This function always returns TRUE. 476 LIB_EXPORT BOOL BnShiftRight(bigNum result, bigConst toShift, uint32_t shiftAmount) 477 { 478 uint32_t offset = (shiftAmount >> RADIX_LOG2); 479 uint32_t i; 480 uint32_t shiftIn; 481 crypt_uword_t finalSize; 490 482 // 491 483 shiftAmount = shiftAmount & RADIX_MASK; 492 shiftIn = RADIX_BITS - shiftAmount; 484 shiftIn = RADIX_BITS - shiftAmount; 485 493 486 // The end size is toShift->size - offset less one additional 494 487 // word if the shiftAmount would make the upper word == 0 … … 500 493 else 501 494 finalSize = 0; 495 502 496 pAssert(finalSize <= result->allocated); 503 497 if(finalSize != 0) … … 514 508 return TRUE; 515 509 } 516 /* 10.2.3.3.20 BnGetRandomBits() */ 517 /* Return Value Meaning */ 518 /* TRUE(1) success */ 519 /* FALSE(0) failure */ 520 LIB_EXPORT BOOL 521 BnGetRandomBits( 522 bigNum n, 523 size_t bits, 524 RAND_STATE *rand 525 ) 526 { 527 // Since this could be used for ECC key generation using the extra bits method, 528 // make sure that the value is large enough 529 TPM2B_TYPE(LARGEST, LARGEST_NUMBER + 8); 530 TPM2B_LARGEST large; 531 // 532 large.b.size = (UINT16)BITS_TO_BYTES(bits); 533 if(DRBG_Generate(rand, large.t.buffer, large.t.size) == large.t.size) 534 { 535 if(BnFrom2B(n, &large.b) != NULL) 536 { 537 if(BnMaskBits(n, (crypt_uword_t)bits)) 538 return TRUE; 539 } 540 } 541 return FALSE; 542 } 543 /* 10.2.3.3.21 BnGenerateRandomInRange() */ 544 /* Function to generate a random number r in the range 1 <= r < limit. The function gets a random 545 number of bits that is the size of limit. There is some some probability that the returned number 546 is going to be greater than or equal to the limit. If it is, try again. There is no more than 50% 547 chance that the next number is also greater, so try again. We keep trying until we get a value 548 that meets the criteria. Since limit is very often a number with a LOT of high order ones, this 549 rarely would need a second try. */ 550 /* Return Value Meaning */ 551 /* TRUE(1) success */ 552 /* FALSE(0) failure */ 553 LIB_EXPORT BOOL 554 BnGenerateRandomInRange( 555 bigNum dest, 556 bigConst limit, 557 RAND_STATE *rand 558 ) 559 { 560 size_t bits = BnSizeInBits(limit); 561 // 562 if(bits < 2) 563 { 564 BnSetWord(dest, 0); 565 return FALSE; 566 } 510 511 //*** BnIsPointOnCurve() 512 // This function checks if a point is on the curve. 513 BOOL BnIsPointOnCurve(pointConst Q, const TPMBN_ECC_CURVE_CONSTANTS* C) 514 { 515 BN_VAR(right, (MAX_ECC_KEY_BITS * 3)); 516 BN_VAR(left, (MAX_ECC_KEY_BITS * 2)); 517 bigConst prime = BnCurveGetPrime(C); 518 // 519 // Show that point is on the curve y^2 = x^3 + ax + b; 520 // Or y^2 = x(x^2 + a) + b 521 // y^2 522 BnMult(left, Q->y, Q->y); 523 524 BnMod(left, prime); 525 // x^2 526 BnMult(right, Q->x, Q->x); 527 528 // x^2 + a 529 BnAdd(right, right, BnCurveGet_a(C)); 530 531 // ExtMath_Mod(right, CurveGetPrime(C)); 532 // x(x^2 + a) 533 BnMult(right, right, Q->x); 534 535 // x(x^2 + a) + b 536 BnAdd(right, right, BnCurveGet_b(C)); 537 538 BnMod(right, prime); 539 if(BnUnsignedCmp(left, right) == 0) 540 return TRUE; 567 541 else 568 { 569 while(BnGetRandomBits(dest, bits, rand) 570 && (BnEqualZero(dest) || (BnUnsignedCmp(dest, limit) >= 0))); 571 } 572 return !g_inFailureMode; 542 return FALSE; 573 543 } 574 544 … … 578 548 // and thus calculates the bits that OpenSSL will work with after truncating 579 549 // the leading zeros 580 static /*VBOX: LIB_EXPORT*/ unsigned 581 BnSizeInBitsSkipLeadingZeros( 582 bigConst n 583 ) 550 static unsigned BnSizeInBitsSkipLeadingZeros(bigConst n) 584 551 { 585 552 int firstByte; … … 609 576 will not have a timing-sidechannel 610 577 */ 611 LIB_EXPORT BOOL 612 BnGenerateRandomInRangeAllBytes( 613 bigNum dest, 614 bigConst limit, 615 RAND_STATE *rand 616 ) 578 LIB_EXPORT BOOL BnGenerateRandomInRangeAllBytes(bigNum dest, 579 bigConst limit, 580 RAND_STATE* rand 581 ) 617 582 { 618 583 BOOL OK; … … 624 589 625 590 if (rand) 626 return BnGenerateRandomInRange(dest,limit, rand);591 return TpmMath_GetRandomInRange((Crypt_Int*)dest, (const Crypt_Int*)limit, rand); 627 592 628 593 // a 'limit' like 'BN_P638_n' has leading zeros and we only need 73 bytes not 80 … … 634 599 635 600 while (true) { 636 OK = BnGenerateRandomInRange(dest,limit, rand);601 OK = TpmMath_GetRandomInRange((Crypt_Int*)dest, (const Crypt_Int*)limit, rand); 637 602 if (!OK) 638 603 break; -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/Clock.c ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: Clock.c 1529 2019-11-21 23:29:01Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 20 19*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 /* added for portability because Linux clock is 32 bits */ 63 64 #include <stdint.h> 65 #include <stdio.h> 66 #ifndef VBOX 67 #include <time.h> 68 #else 69 # include <iprt/time.h> 70 #endif 71 72 /* C.3 Clock.c */ 73 /* C.3.1. Description */ 74 /* This file contains the routines that are used by the simulator to mimic a hardware clock on a 75 TPM. In this implementation, all the time values are measured in millisecond. However, the 76 precision of the clock functions may be implementation dependent. */ 77 /* C.3.2. Includes and Data Definitions */ 61 //** Description 62 // 63 // This file contains the routines that are used by the simulator to mimic 64 // a hardware clock on a TPM. 65 // 66 // In this implementation, all the time values are measured in millisecond. 67 // However, the precision of the clock functions may be implementation dependent. 68 69 //** Includes and Data Definitions 78 70 #include <assert.h> 79 71 #include "Platform.h" 80 #include "TpmFail_fp.h" 72 73 #ifndef VBOX 74 # include <time.h> 75 #else 76 # include <iprt/time.h> 77 #endif 78 79 // CLOCK_NOMINAL is the number of hardware ticks per ms. A value of 30000 means 80 // that the nominal clock rate used to drive the hardware clock is 30 MHz. The 81 // adjustment rates are used to determine the conversion of the hardware ticks to 82 // internal hardware clock value. In practice, we would expect that there would be 83 // a hardware register will accumulated mS. It would be incremented by the output 84 // of a pre-scaler. The pre-scaler would divide the ticks from the clock by some 85 // value that would compensate for the difference between clock time and real time. 86 // The code in Clock does the emulation of this function. 87 #define CLOCK_NOMINAL 30000 88 // A 1% change in rate is 300 counts 89 #define CLOCK_ADJUST_COARSE 300 90 // A 0.1% change in rate is 30 counts 91 #define CLOCK_ADJUST_MEDIUM 30 92 // A minimum change in rate is 1 count 93 #define CLOCK_ADJUST_FINE 1 94 // The clock tolerance is +/-15% (4500 counts) 95 // Allow some guard band (16.7%) 96 #define CLOCK_ADJUST_LIMIT 5000 81 97 82 98 /* libtpms added begin */ … … 85 101 ClockGetTime( 86 102 #ifndef VBOX 87 88 #else 89 103 clockid_t clk_id 104 #else 105 TPM_CLOCK_ID clk_id 90 106 #endif 91 107 ) … … 103 119 104 120 return time; 105 #else 121 #else /* VBOX */ 106 122 if (clk_id == CLOCK_REALTIME) 107 123 { … … 115 131 116 132 return 0; 117 #endif 133 #endif /* !VBOX */ 118 134 } 119 135 … … 133 149 #else 134 150 s_hostMonotonicAdjustTime = -(int64_t)ClockGetTime(CLOCK_MONOTONIC); 135 #endif 151 #endif /* !VBOX */ 136 152 137 153 /* s_lastSystemTime & s_lastReportTime need to be set as well */ … … 144 160 /* libtpms added end */ 145 161 146 /* C.3.3. Simulator Functions */ 147 /* C.3.3.1. Introduction */ 148 /* This set of functions is intended to be called by the simulator environment in order to simulate 149 hardware events. */ 150 /* C.3.3.2. _plat__TimerReset() */ 151 /* This function sets current system clock time as t0 for counting TPM time. This function is called 152 at a power on event to reset the clock. When the clock is reset, the indication that the clock 153 was stopped is also set. */ 154 LIB_EXPORT void 155 _plat__TimerReset( 156 void 157 ) 162 //** Simulator Functions 163 //*** Introduction 164 // This set of functions is intended to be called by the simulator environment in 165 // order to simulate hardware events. 166 167 //***_plat__TimerReset() 168 // This function sets current system clock time as t0 for counting TPM time. 169 // This function is called at a power on event to reset the clock. When the clock 170 // is reset, the indication that the clock was stopped is also set. 171 LIB_EXPORT void _plat__TimerReset(void) 158 172 { 159 173 s_lastSystemTime = 0; 160 s_tpmTime = 0;161 s_adjustRate = CLOCK_NOMINAL;162 s_timerReset = TRUE;163 s_timerStopped = TRUE;174 s_tpmTime = 0; 175 s_adjustRate = CLOCK_NOMINAL; 176 s_timerReset = TRUE; 177 s_timerStopped = TRUE; 164 178 s_hostMonotonicAdjustTime = 0; /* libtpms added */ 165 179 s_suspendedElapsedTime = 0; /* libtpms added */ 166 180 return; 167 181 } 168 /* C.3.3.3. _plat__TimerRestart() */ 169 /* This function should be called in order to simulate the restart of the timer should it be stopped 170 while power is still applied. */ 171 LIB_EXPORT void 172 _plat__TimerRestart( 173 void 174 ) 182 183 //*** _plat__TimerRestart() 184 // This function should be called in order to simulate the restart of the timer 185 // should it be stopped while power is still applied. 186 LIB_EXPORT void _plat__TimerRestart(void) 175 187 { 176 188 s_timerStopped = TRUE; … … 178 190 } 179 191 180 /* C.3.4. Functions Used by TPM */ 181 /* C.3.4.1. Introduction */ 182 /* These functions are called by the TPM code. They should be replaced by appropriated hardware 183 functions. */ 184 185 clock_t debugTime; 186 /* C.3.4.2. _plat__Time() */ 187 /* This is another, probably futile, attempt to define a portable function that will return a 64-bit 188 clock value that has mSec resolution. */ 189 LIB_EXPORT uint64_t 190 _plat__RealTime( 191 void 192 ) 193 { 194 clock64_t time; 192 //** Functions Used by TPM 193 //*** Introduction 194 // These functions are called by the TPM code. They should be replaced by 195 // appropriated hardware functions. 196 197 clock_t debugTime; 198 199 //*** _plat__RealTime() 200 // This is another, probably futile, attempt to define a portable function 201 // that will return a 64-bit clock value that has mSec resolution. 202 LIB_EXPORT uint64_t _plat__RealTime(void) 203 { 204 clock64_t time; 195 205 //#ifdef _MSC_VER kgold 196 #ifndef VBOX197 206 #ifdef TPM_WINDOWS 198 207 #include <sys/timeb.h> 199 struct _timeb 208 struct _timeb sysTime; 200 209 // 201 210 _ftime(&sysTime); /* kgold, mingw doesn't have _ftime_s */ … … 206 215 #else 207 216 // hopefully, this will work with most UNIX systems 208 struct timespec 217 struct timespec systime; 209 218 // 210 219 clock_gettime(CLOCK_MONOTONIC, &systime); 211 220 time = (clock64_t)systime.tv_sec * 1000 + (systime.tv_nsec / 1000000); 212 #endif213 #else214 time = (clock64_t)RTTimeMilliTS();215 221 #endif 216 222 /* libtpms added begin */ … … 229 235 } 230 236 231 232 233 /* C.3.4.3. _plat__TimerRead() */ 234 /* This function provides access to the tick timer of the platform. The TPM code uses this value to 235 drive the TPM Clock. */ 236 /* The tick timer is supposed to run when power is applied to the device. This timer should not be 237 reset by time events including _TPM_Init(). It should only be reset when TPM power is 238 re-applied. */ 239 /* If the TPM is run in a protected environment, that environment may provide the tick time to the 240 TPM as long as the time provided by the environment is not allowed to go backwards. If the time 241 provided by the system can go backwards during a power discontinuity, then the 242 _plat__Signal_PowerOn() should call _plat__TimerReset(). */ 243 LIB_EXPORT uint64_t 244 _plat__TimerRead( 245 void 246 ) 237 //***_plat__TimerRead() 238 // This function provides access to the tick timer of the platform. The TPM code 239 // uses this value to drive the TPM Clock. 240 // 241 // The tick timer is supposed to run when power is applied to the device. This timer 242 // should not be reset by time events including _TPM_Init. It should only be reset 243 // when TPM power is re-applied. 244 // 245 // If the TPM is run in a protected environment, that environment may provide the 246 // tick time to the TPM as long as the time provided by the environment is not 247 // allowed to go backwards. If the time provided by the system can go backwards 248 // during a power discontinuity, then the _plat__Signal_PowerOn should call 249 // _plat__TimerReset(). 250 LIB_EXPORT uint64_t _plat__TimerRead(void) 247 251 { 248 252 #ifdef HARDWARE_CLOCK 249 # error "need a definition for reading the hardware clock"253 # error "need a defintion for reading the hardware clock" 250 254 return HARDWARE_CLOCK 251 255 #else 252 clock64_t timeDiff; 253 clock64_t adjustedTimeDiff; 254 clock64_t timeNow; 255 clock64_t readjustedTimeDiff; 256 clock64_t timeDiff; 257 clock64_t adjustedTimeDiff; 258 clock64_t timeNow; 259 clock64_t readjustedTimeDiff; 260 256 261 // This produces a timeNow that is basically locked to the system clock. 257 262 timeNow = _plat__RealTime(); 263 258 264 // if this hasn't been initialized, initialize it 259 265 if(s_lastSystemTime == 0) 260 266 { 261 s_lastSystemTime = timeNow;262 debugTime = clock();267 s_lastSystemTime = timeNow; 268 debugTime = clock(); 263 269 s_lastReportedTime = 0; 264 270 s_realTimePrevious = 0; … … 270 276 s_lastSystemTime = timeNow; 271 277 s_lastReportedTime = s_lastReportedTime + timeNow - s_lastSystemTime; 272 s_lastSystemTime = timeNow; 273 timeNow = s_lastReportedTime; 278 s_lastSystemTime = timeNow; 279 timeNow = s_lastReportedTime; 280 274 281 // The code above produces a timeNow that is similar to the value returned 275 282 // by Clock(). The difference is that timeNow does not max out, and it is … … 281 288 // Compute the amount of time since the last update of the system clock 282 289 timeDiff = timeNow - s_realTimePrevious; 290 283 291 // Do the time rate adjustment and conversion from CLOCKS_PER_SEC to mSec 284 292 adjustedTimeDiff = (timeDiff * CLOCK_NOMINAL) / ((uint64_t)s_adjustRate); 293 285 294 // update the TPM time with the adjusted timeDiff 286 295 s_tpmTime += (clock64_t)adjustedTimeDiff; 296 287 297 // Might have some rounding error that would loose CLOCKS. See what is not 288 298 // being used. As mentioned above, this could result in putting back more than 289 299 // is taken out. Here, we are trying to recreate timeDiff. 290 readjustedTimeDiff = (adjustedTimeDiff * (uint64_t)s_adjustRate )291 / CLOCK_NOMINAL; 300 readjustedTimeDiff = (adjustedTimeDiff * (uint64_t)s_adjustRate) / CLOCK_NOMINAL; 301 292 302 // adjusted is now converted back to being the amount we should advance the 293 303 // previous sampled time. It should always be less than or equal to timeDiff. 294 304 // That is, we could not have use more time than we started with. 295 305 s_realTimePrevious = s_realTimePrevious + readjustedTimeDiff; 296 #ifdef DEBUGGING_TIME 306 307 # ifdef DEBUGGING_TIME 297 308 // Put this in so that TPM time will pass much faster than real time when 298 309 // doing debug. … … 300 311 // A good value might be 100 301 312 return (s_tpmTime * DEBUG_TIME_MULTIPLIER); 302 # endif313 # endif 303 314 return s_tpmTime; 304 315 #endif 305 316 } 306 317 307 308 /* C.3.4.3. _plat__TimerWasReset() */ 309 /* This function is used to interrogate the flag indicating if the tick timer has been reset. */ 310 /* If the resetFlag parameter is SET, then the flag will be CLEAR before the function returns. */ 311 LIB_EXPORT int 312 _plat__TimerWasReset( 313 void 314 ) 315 { 316 int retVal = s_timerReset; 318 //*** _plat__TimerWasReset() 319 // This function is used to interrogate the flag indicating if the tick timer has 320 // been reset. 321 // 322 // If the resetFlag parameter is SET, then the flag will be CLEAR before the 323 // function returns. 324 LIB_EXPORT int _plat__TimerWasReset(void) 325 { 326 int retVal = s_timerReset; 317 327 s_timerReset = FALSE; 318 328 return retVal; 319 329 } 320 /* C.3.4.4. _plat__TimerWasStopped() */ 321 /* This function is used to interrogate the flag indicating if the tick timer has been stopped. If 322 so, this is typically a reason to roll the nonce. */ 323 /* This function will CLEAR the s_timerStopped flag before returning. This provides functionality 324 that is similar to status register that is cleared when read. This is the model used here because 325 it is the one that has the most impact on the TPM code as the flag can only be accessed by one 326 entity in the TPM. Any other implementation of the hardware can be made to look like a read-once 327 register. */ 328 LIB_EXPORT int 329 _plat__TimerWasStopped( 330 void 331 ) 332 { 333 BOOL retVal = s_timerStopped; 330 331 //*** _plat__TimerWasStopped() 332 // This function is used to interrogate the flag indicating if the tick timer has 333 // been stopped. If so, this is typically a reason to roll the nonce. 334 // 335 // This function will CLEAR the s_timerStopped flag before returning. This provides 336 // functionality that is similar to status register that is cleared when read. This 337 // is the model used here because it is the one that has the most impact on the TPM 338 // code as the flag can only be accessed by one entity in the TPM. Any other 339 // implementation of the hardware can be made to look like a read-once register. 340 LIB_EXPORT int _plat__TimerWasStopped(void) 341 { 342 int retVal = s_timerStopped; 334 343 s_timerStopped = FALSE; 335 344 return retVal; 336 345 } 337 /* C.3.4.5. _plat__ClockAdjustRate() */ 338 /* Adjust the clock rate */ 339 LIB_EXPORT void 340 _plat__ClockAdjustRate( 341 int adjust // IN: the adjust number. It could be positive 342 // or negative 343 ) 346 347 //***_plat__ClockAdjustRate() 348 // Adjust the clock rate 349 LIB_EXPORT void _plat__ClockRateAdjust(_plat__ClockAdjustStep adjust) 344 350 { 345 351 // We expect the caller should only use a fixed set of constant values to … … 347 353 switch(adjust) 348 354 { 349 case CLOCK_ADJUST_COARSE: 355 // slower increases the divisor 356 case PLAT_TPM_CLOCK_ADJUST_COARSE_SLOWER: 350 357 s_adjustRate += CLOCK_ADJUST_COARSE; 351 358 break; 352 case -CLOCK_ADJUST_COARSE: 359 case PLAT_TPM_CLOCK_ADJUST_MEDIUM_SLOWER: 360 s_adjustRate += CLOCK_ADJUST_MEDIUM; 361 break; 362 case PLAT_TPM_CLOCK_ADJUST_FINE_SLOWER: 363 s_adjustRate += CLOCK_ADJUST_FINE; 364 break; 365 // faster decreases the divisor 366 case PLAT_TPM_CLOCK_ADJUST_FINE_FASTER: 367 s_adjustRate -= CLOCK_ADJUST_FINE; 368 break; 369 case PLAT_TPM_CLOCK_ADJUST_MEDIUM_FASTER: 370 s_adjustRate -= CLOCK_ADJUST_MEDIUM; 371 break; 372 case PLAT_TPM_CLOCK_ADJUST_COARSE_FASTER: 353 373 s_adjustRate -= CLOCK_ADJUST_COARSE; 354 374 break; 355 case CLOCK_ADJUST_MEDIUM:356 s_adjustRate += CLOCK_ADJUST_MEDIUM;357 break;358 case -CLOCK_ADJUST_MEDIUM:359 s_adjustRate -= CLOCK_ADJUST_MEDIUM;360 break;361 case CLOCK_ADJUST_FINE:362 s_adjustRate += CLOCK_ADJUST_FINE;363 break;364 case -CLOCK_ADJUST_FINE:365 s_adjustRate -= CLOCK_ADJUST_FINE;366 break;367 default:368 // ignore any other values;369 break;370 375 } 376 371 377 if(s_adjustRate > (CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT)) 372 378 s_adjustRate = CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT; 373 379 if(s_adjustRate < (CLOCK_NOMINAL - CLOCK_ADJUST_LIMIT)) 374 380 s_adjustRate = CLOCK_NOMINAL - CLOCK_ADJUST_LIMIT; 381 375 382 return; 376 383 } 384 385 #if 0 386 387 /* added for portability because Linux clock is 32 bits */ 388 389 #include <stdint.h> 390 #include <stdio.h> 391 #include <time.h> 392 393 #include "TpmFail_fp.h" 394 395 LIB_EXPORT uint64_t 396 _plat__RealTime( 397 void 398 ) 399 { 400 clock64_t time; 401 //#ifdef _MSC_VER kgold 402 #ifndef VBOX 403 #ifdef TPM_WINDOWS 404 #include <sys/timeb.h> 405 struct _timeb sysTime; 406 // 407 _ftime(&sysTime); /* kgold, mingw doesn't have _ftime_s */ 408 time = (clock64_t)(sysTime.time) * 1000 + sysTime.millitm; 409 // set the time back by one hour if daylight savings 410 if(sysTime.dstflag) 411 time -= 1000 * 60 * 60; // mSec/sec * sec/min * min/hour = ms/hour 412 #else 413 // hopefully, this will work with most UNIX systems 414 struct timespec systime; 415 // 416 clock_gettime(CLOCK_MONOTONIC, &systime); 417 time = (clock64_t)systime.tv_sec * 1000 + (systime.tv_nsec / 1000000); 418 #endif 419 #else 420 time = (clock64_t)RTTimeMilliTS(); 421 #endif 422 return time; 423 } 424 425 #endif -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/ContextCommands.c ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: ContextCommands.c 1658 2021-01-22 23:14:01Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 202 1*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 61 #include "Tpm.h" 63 #include "ContextSave_fp.h" 64 #include "NVMarshal.h" // libtpms added 62 65 63 #if CC_ContextSave // Conditional expansion of this file 66 #include "Context_spt_fp.h" 67 /* Error Returns Meaning */ 68 /* TPM_RC_CONTEXT_GAP a contextID could not be assigned for a session context save */ 69 /* TPM_RC_TOO_MANY_CONTEXTS no more contexts can be saved as the counter has maxed out */ 64 65 # include "ContextSave_fp.h" 66 # include "Marshal.h" 67 # include "Context_spt_fp.h" 68 # include "NVMarshal.h" // libtpms added 69 70 /*(See part 3 specification) 71 Save context 72 */ 73 // Return Type: TPM_RC 74 // TPM_RC_CONTEXT_GAP a contextID could not be assigned for a session 75 // context save 76 // TPM_RC_TOO_MANY_CONTEXTS no more contexts can be saved as the counter has 77 // maxed out 70 78 TPM_RC 71 TPM2_ContextSave( 72 ContextSave_In *in, // IN: input parameter list 73 ContextSave_Out *out // OUT: output parameter list 79 TPM2_ContextSave(ContextSave_In* in, // IN: input parameter list 80 ContextSave_Out* out // OUT: output parameter list 74 81 ) 75 82 { 76 TPM_RC 77 UINT16 fingerprintSize;// The size of fingerprint in context83 TPM_RC result = TPM_RC_SUCCESS; 84 UINT16 fingerprintSize; // The size of fingerprint in context 78 85 // blob. 79 UINT64 contextID = 0; // session context ID 80 TPM2B_SYM_KEY symKey; 81 TPM2B_IV iv; 82 TPM2B_DIGEST integrity; 83 UINT16 integritySize; 84 BYTE *buffer; 86 UINT64 contextID = 0; // session context ID 87 TPM2B_SYM_KEY symKey; 88 TPM2B_IV iv; 89 90 TPM2B_DIGEST integrity; 91 UINT16 integritySize; 92 BYTE* buffer; 85 93 // This command may cause the orderlyState to be cleared due to 86 94 // the update of state reset data. If the state is orderly and 87 95 // cannot be changed, exit early. 88 96 RETURN_IF_ORDERLY; 89 97 90 98 // Internal Data Update 91 99 92 100 // This implementation does not do things in quite the same way as described in 93 // Part 2 of the specification. In Part 2, it indicates that the 94 // TPMS_CONTEXT_DATA contains two TPM2B values. That is not how this is 95 // implemented. Rather, the size field of the TPM2B_CONTEXT_DATA is used to 96 // determine the amount of data in the encrypted data. That part is not 97 // independently sized. This makes the actual size 2 bytes smaller than 98 // calculated using Part 2. Since this is opaque to the caller, it is not 101 // Part 2 of the specification. In Part 2, it indicates that the 102 // TPMS_CONTEXT_DATA contains two TPM2B values. That is not how this is 103 // implemented. Rather, the size field of the TPM2B_CONTEXT_DATA is used to 104 // determine the amount of data in the encrypted data. That part is not 105 // independently sized. This makes the actual size 2 bytes smaller than 106 // calculated using Part 2. Since this is opaque to the caller, it is not 99 107 // necessary to fix. The actual size is returned by TPM2_GetCapabilties(). 100 108 101 109 // Initialize output handle. At the end of command action, the output 102 110 // handle of an object will be replaced, while the output handle 103 111 // for a session will be the same as input 104 112 out->context.savedHandle = in->saveHandle; 113 105 114 // Get the size of fingerprint in context blob. The sequence value in 106 115 // TPMS_CONTEXT structure is used as the fingerprint 107 116 fingerprintSize = sizeof(out->context.sequence); 117 108 118 // Compute the integrity size at the beginning of context blob 109 integritySize = sizeof(integrity.t.size) 110 + CryptHashGetDigestSize(CONTEXT_INTEGRITY_HASH_ALG); 119 integritySize = 120 sizeof(integrity.t.size) + CryptHashGetDigestSize(CONTEXT_INTEGRITY_HASH_ALG); 121 111 122 // Perform object or session specific context save 112 123 switch(HandleGetType(in->saveHandle)) … … 114 125 case TPM_HT_TRANSIENT: 115 126 { 116 OBJECT *object = HandleToObject(in->saveHandle);117 ANY_OBJECT_BUFFER *outObject;127 OBJECT* object = HandleToObject(in->saveHandle); 128 ANY_OBJECT_BUFFER* outObject; 118 129 #ifndef VBOX 119 unsigned char buffer[sizeof(OBJECT) * 2]; // libtpms changed begin120 BYTE *bufptr = &buffer[0];121 INT32 size = sizeof(buffer);130 unsigned char objbuf[sizeof(OBJECT) * 2]; // libtpms changed begin 131 BYTE* bufptr = &objbuf[0]; 132 INT32 size = sizeof(objbuf); 122 133 #else 123 134 unsigned char abBuf[sizeof(OBJECT) * 2]; // libtpms changed begin 124 135 BYTE *bufptr = &abBuf[0]; 125 136 INT32 size = sizeof(abBuf); 126 #endif 127 UINT16 written = ANY_OBJECT_Marshal(object, &bufptr, &size);128 UINT16 objectSize = written;// libtpms changed end129 outObject = (ANY_OBJECT_BUFFER*)(out->context.contextBlob.t.buffer130 137 #endif /* VBOX */ 138 139 UINT16 objectSize; // libtpms changed end 140 outObject = (ANY_OBJECT_BUFFER*)(out->context.contextBlob.t.buffer 141 + integritySize + fingerprintSize); 131 142 // Set size of the context data. The contents of context blob is vendor 132 143 // defined. In this implementation, the size is size of integrity 133 144 // plus fingerprint plus the whole internal OBJECT structure 134 out->context.contextBlob.t.size = integritySize + 135 fingerprintSize + objectSize; 145 # if 0 // libtpms: added 146 out->context.contextBlob.t.size = 147 integritySize + fingerprintSize + objectSize; 148 # endif // libtpms: added 149 # if ALG_RSA 150 // For an RSA key, make sure that the key has had the private exponent 151 // computed before saving. 152 if(object->publicArea.type == TPM_ALG_RSA 153 && !(object->attributes.publicOnly)) 154 CryptRsaLoadPrivateExponent(&object->publicArea, &object->sensitive, object); // libtpms: added object param 155 # endif 156 objectSize = ANY_OBJECT_Marshal(object, &bufptr, &size, &g_RuntimeProfile); // libtpms: added begin 157 158 out->context.contextBlob.t.size = 159 integritySize + fingerprintSize + objectSize; 136 160 // Make sure things fit 137 161 pAssert(out->context.contextBlob.t.size … … 139 163 // Copy the whole internal OBJECT structure to context blob 140 164 #ifndef VBOX 141 MemoryCopy(outObject, buffer, written); // libtpms changed165 MemoryCopy(outObject, objbuf, objectSize); // libtpms changed 142 166 #else 143 MemoryCopy(outObject, &abBuf, written); // libtpms changed144 #endif 167 MemoryCopy(outObject, &abBuf, objectSize); // libtpms changed 168 #endif /* VBOX */ 145 169 // Increment object context ID 146 170 gr.objectContextID++; … … 148 172 if(gr.objectContextID == 0) 149 173 FAIL(FATAL_ERROR_INTERNAL); 174 150 175 // Fill in other return values for an object. 151 176 out->context.sequence = gr.objectContextID; … … 157 182 out->context.savedHandle = 0x80000001; 158 183 /* ANY_OBJECT_Marshal already wrote it // libtpms changed begin 159 SequenceDataExport((HASH_OBJECT 160 (HASH_OBJECT_BUFFER 184 SequenceDataExport((HASH_OBJECT*)object, 185 (HASH_OBJECT_BUFFER*)outObject); 161 186 */ // libtpms changed end 162 187 } 163 188 else 164 out->context.savedHandle = (object->attributes.stClear == SET)165 189 out->context.savedHandle = 190 (object->attributes.stClear == SET) ? 0x80000002 : 0x80000000; 166 191 // Get object hierarchy 167 out->context.hierarchy = ObjectGetHierarchy(object); 192 out->context.hierarchy = object->hierarchy; 193 168 194 break; 169 195 } … … 171 197 case TPM_HT_POLICY_SESSION: 172 198 { 173 SESSION *session = SessionGet(in->saveHandle); 199 SESSION* session = SessionGet(in->saveHandle); 200 174 201 // Set size of the context data. The contents of context blob is vendor 175 202 // defined. In this implementation, the size of context blob is the 176 203 // size of a internal session structure plus the size of 177 204 // fingerprint plus the size of integrity 178 out->context.contextBlob.t.size = integritySize + 179 fingerprintSize + sizeof(*session); 205 out->context.contextBlob.t.size = 206 integritySize + fingerprintSize + sizeof(*session); 207 180 208 // Make sure things fit 181 209 pAssert(out->context.contextBlob.t.size 182 210 < sizeof(out->context.contextBlob.t.buffer)); 211 183 212 // Copy the whole internal SESSION structure to context blob. 184 213 // Save space for fingerprint at the beginning of the buffer … … 187 216 pAssert(sizeof(*session) <= sizeof(out->context.contextBlob.t.buffer) 188 217 - integritySize - fingerprintSize); 189 MemoryCopy(out->context.contextBlob.t.buffer + integritySize 190 + fingerprintSize, session, sizeof(*session)); 218 MemoryCopy( 219 out->context.contextBlob.t.buffer + integritySize + fingerprintSize, 220 session, 221 sizeof(*session)); 191 222 // Fill in the other return parameters for a session 192 223 // Get a context ID and set the session tracking values appropriately … … 199 230 // sequence number is the current session contextID 200 231 out->context.sequence = contextID; 232 201 233 // use TPM_RH_NULL as hierarchy for session context 202 234 out->context.hierarchy = TPM_RH_NULL; 235 203 236 break; 204 237 } … … 209 242 break; 210 243 } 244 211 245 // Save fingerprint at the beginning of encrypted area of context blob. 212 246 // Reserve the integrity space 213 pAssert(sizeof(out->context.sequence) <=214 sizeof(out->context.contextBlob.t.buffer) - integritySize);247 pAssert(sizeof(out->context.sequence) 248 <= sizeof(out->context.contextBlob.t.buffer) - integritySize); 215 249 MemoryCopy(out->context.contextBlob.t.buffer + integritySize, 216 &out->context.sequence, sizeof(out->context.sequence)); 250 &out->context.sequence, 251 sizeof(out->context.sequence)); 252 217 253 // Compute context encryption key 218 ComputeContextProtectionKey(&out->context, &symKey, &iv); 254 result = ComputeContextProtectionKey(&out->context, &symKey, &iv); 255 if(result != TPM_RC_SUCCESS) 256 return result; 257 219 258 // Encrypt context blob 220 259 CryptSymmetricEncrypt(out->context.contextBlob.t.buffer + integritySize, 221 CONTEXT_ENCRYPT_ALG, CONTEXT_ENCRYPT_KEY_BITS, 222 symKey.t.buffer, &iv, TPM_ALG_CFB, 260 CONTEXT_ENCRYPT_ALG, 261 CONTEXT_ENCRYPT_KEY_BITS, 262 symKey.t.buffer, 263 &iv, 264 TPM_ALG_CFB, 223 265 out->context.contextBlob.t.size - integritySize, 224 266 out->context.contextBlob.t.buffer + integritySize); 267 225 268 // Compute integrity hash for the object 226 269 // In this implementation, the same routine is used for both sessions 227 270 // and objects. 228 ComputeContextIntegrity(&out->context, &integrity); 271 result = ComputeContextIntegrity(&out->context, &integrity); 272 if(result != TPM_RC_SUCCESS) 273 return result; 274 229 275 // add integrity at the beginning of context blob 230 276 buffer = out->context.contextBlob.t.buffer; 231 277 TPM2B_DIGEST_Marshal(&integrity, &buffer, NULL); 278 232 279 // orderly state should be cleared because of the update of state reset and 233 280 // state clear data 234 281 g_clearOrderly = TRUE; 282 235 283 return result; 236 284 } 237 #endif // CC_ContextSave 285 286 #endif // CC_ContextSave 287 238 288 #include "Tpm.h" 239 #include "ContextLoad_fp.h" 289 240 290 #if CC_ContextLoad // Conditional expansion of this file 241 #include "Context_spt_fp.h" 291 292 # include "ContextLoad_fp.h" 293 # include "Marshal.h" 294 # include "Context_spt_fp.h" 295 296 /*(See part 3 specification) 297 // Load context 298 */ 299 300 // Return Type: TPM_RC 301 // TPM_RC_CONTEXT_GAP there is only one available slot and this is not 302 // the oldest saved session context 303 // TPM_RC_HANDLE 'context.savedHandle' does not reference a saved 304 // session 305 // TPM_RC_HIERARCHY 'context.hierarchy' is disabled 306 // TPM_RC_INTEGRITY 'context' integrity check fail 307 // TPM_RC_OBJECT_MEMORY no free slot for an object 308 // TPM_RC_SESSION_MEMORY no free session slots 309 // TPM_RC_SIZE incorrect context blob size 242 310 TPM_RC 243 TPM2_ContextLoad( 244 ContextLoad_In *in, // IN: input parameter list 245 ContextLoad_Out *out // OUT: output parameter list 311 TPM2_ContextLoad(ContextLoad_In* in, // IN: input parameter list 312 ContextLoad_Out* out // OUT: output parameter list 246 313 ) 247 314 { 248 TPM_RC result; 249 TPM2B_DIGEST integrityToCompare; 250 TPM2B_DIGEST integrity; 251 BYTE *buffer; // defined to save some typing 252 INT32 size; // defined to save some typing 253 TPM_HT handleType; 254 TPM2B_SYM_KEY symKey; 255 TPM2B_IV iv; 315 TPM_RC result; 316 TPM2B_DIGEST integrityToCompare; 317 TPM2B_DIGEST integrity; 318 BYTE* buffer; // defined to save some typing 319 INT32 size; // defined to save some typing 320 TPM_HT handleType; 321 TPM2B_SYM_KEY symKey; 322 TPM2B_IV iv; 323 256 324 // Input Validation 257 325 … … 260 328 // IF this is a session context, make sure that the sequence number is 261 329 // consistent with the version in the slot 330 262 331 // Check context blob size 263 332 handleType = HandleGetType(in->context.savedHandle); 333 264 334 // Get integrity from context blob 265 335 buffer = in->context.contextBlob.t.buffer; 266 size = (INT32)in->context.contextBlob.t.size;336 size = (INT32)in->context.contextBlob.t.size; 267 337 result = TPM2B_DIGEST_Unmarshal(&integrity, &buffer, &size); 268 338 if(result != TPM_RC_SUCCESS) 269 339 return result; 340 270 341 // the size of the integrity value has to match the size of digest produced 271 342 // by the integrity hash 272 343 if(integrity.t.size != CryptHashGetDigestSize(CONTEXT_INTEGRITY_HASH_ALG)) 273 344 return TPM_RCS_SIZE + RC_ContextLoad_context; 345 274 346 // Make sure that the context blob has enough space for the fingerprint. This 275 347 // is elastic pants to go with the belt and suspenders we already have to make … … 277 349 if((unsigned)size < sizeof(in->context.sequence)) 278 350 return TPM_RCS_SIZE + RC_ContextLoad_context; 351 279 352 // After unmarshaling the integrity value, 'buffer' is pointing at the first 280 353 // byte of the integrity protected and encrypted buffer and 'size' is the number 281 354 // of integrity protected and encrypted bytes. 355 282 356 // Compute context integrity 283 ComputeContextIntegrity(&in->context, &integrityToCompare); 357 result = ComputeContextIntegrity(&in->context, &integrityToCompare); 358 if(result != TPM_RC_SUCCESS) 359 return result; 360 284 361 // Compare integrity 285 362 if(!MemoryEqual2B(&integrity.b, &integrityToCompare.b)) 286 363 return TPM_RCS_INTEGRITY + RC_ContextLoad_context; 287 364 // Compute context encryption key 288 ComputeContextProtectionKey(&in->context, &symKey, &iv); 365 result = ComputeContextProtectionKey(&in->context, &symKey, &iv); 366 if(result != TPM_RC_SUCCESS) 367 return result; 368 289 369 // Decrypt context data in place 290 CryptSymmetricDecrypt(buffer, CONTEXT_ENCRYPT_ALG, CONTEXT_ENCRYPT_KEY_BITS, 291 symKey.t.buffer, &iv, TPM_ALG_CFB, size, buffer); 370 CryptSymmetricDecrypt(buffer, 371 CONTEXT_ENCRYPT_ALG, 372 CONTEXT_ENCRYPT_KEY_BITS, 373 symKey.t.buffer, 374 &iv, 375 TPM_ALG_CFB, 376 size, 377 buffer); 292 378 // See if the fingerprint value matches. If not, it is symptomatic of either 293 379 // a broken TPM or that the TPM is under attack so go into failure mode. 294 380 if(!MemoryEqual(buffer, &in->context.sequence, sizeof(in->context.sequence))) 295 381 FAIL(FATAL_ERROR_INTERNAL); 382 296 383 // step over fingerprint 297 384 buffer += sizeof(in->context.sequence); 385 298 386 // set the remaining size of the context 299 387 size -= sizeof(in->context.sequence); 388 300 389 // Perform object or session specific input check 301 390 switch(handleType) … … 303 392 case TPM_HT_TRANSIENT: 304 393 { 305 OBJECT *outObject; 394 OBJECT* outObject; 395 306 396 if(size > (INT32)sizeof(OBJECT)) 307 397 FAIL(FATAL_ERROR_INTERNAL); 398 308 399 // Discard any changes to the handle that the TRM might have made 309 400 in->context.savedHandle = TRANSIENT_FIRST; 401 310 402 // If hierarchy is disabled, no object context can be loaded in this 311 403 // hierarchy 312 404 if(!HierarchyIsEnabled(in->context.hierarchy)) 313 405 return TPM_RCS_HIERARCHY + RC_ContextLoad_context; 406 314 407 // Restore object. If there is no empty space, indicate as much 315 408 outObject = ObjectContextLoadLibtpms(buffer, size, // libtpms changed … … 317 410 if(outObject == NULL) 318 411 return TPM_RC_OBJECT_MEMORY; 412 319 413 break; 320 414 } … … 324 418 if(size != sizeof(SESSION)) 325 419 FAIL(FATAL_ERROR_INTERNAL); 420 326 421 // This command may cause the orderlyState to be cleared due to 327 422 // the update of state reset data. If this is the case, check if NV is 328 423 // available first 329 424 RETURN_IF_ORDERLY; 425 330 426 // Check if input handle points to a valid saved session and that the 331 427 // sequence number makes sense 332 428 if(!SequenceNumberForSavedContextIsValid(&in->context)) 333 429 return TPM_RCS_HANDLE + RC_ContextLoad_context; 430 334 431 // Restore session. A TPM_RC_SESSION_MEMORY, TPM_RC_CONTEXT_GAP error 335 432 // may be returned at this point 336 result = SessionContextLoad((SESSION_BUF *)buffer,337 433 result = 434 SessionContextLoad((SESSION_BUF*)buffer, &in->context.savedHandle); 338 435 if(result != TPM_RC_SUCCESS) 339 436 return result; 437 340 438 out->loadedHandle = in->context.savedHandle; 439 341 440 // orderly state should be cleared because of the update of state 342 441 // reset and state clear data 343 442 g_clearOrderly = TRUE; 443 344 444 break; 345 445 } … … 352 452 return TPM_RC_SUCCESS; 353 453 } 454 354 455 #endif // CC_ContextLoad 456 355 457 #include "Tpm.h" 356 458 #include "FlushContext_fp.h" … … 393 495 } 394 496 #endif // CC_FlushContext 497 395 498 #include "Tpm.h" 396 499 #include "EvictControl_fp.h" 500 397 501 #if CC_EvictControl // Conditional expansion of this file 502 503 /*(See part 3 specification) 504 // Make a transient object persistent or evict a persistent object 505 */ 506 // Return Type: TPM_RC 507 // TPM_RC_ATTRIBUTES an object with 'temporary', 'stClear' or 'publicOnly' 508 // attribute SET cannot be made persistent 509 // TPM_RC_HIERARCHY 'auth' cannot authorize the operation in the hierarchy 510 // of 'evictObject'; 511 // an object in a firmware-bound or SVN-bound hierarchy 512 // cannot be made persistent. 513 // TPM_RC_HANDLE 'evictHandle' of the persistent object to be evicted is 514 // not the same as the 'persistentHandle' argument 515 // TPM_RC_NV_HANDLE 'persistentHandle' is unavailable 516 // TPM_RC_NV_SPACE no space in NV to make 'evictHandle' persistent 517 // TPM_RC_RANGE 'persistentHandle' is not in the range corresponding to 518 // the hierarchy of 'evictObject' 398 519 TPM_RC 399 TPM2_EvictControl( 400 EvictControl_In *in // IN: input parameter list 520 TPM2_EvictControl(EvictControl_In* in // IN: input parameter list 401 521 ) 402 522 { 403 TPM_RC 404 OBJECT *evictObject;523 TPM_RC result; 524 OBJECT* evictObject; 405 525 // Input Validation 526 406 527 // Get internal object pointer 407 528 evictObject = HandleToObject(in->objectHandle); 529 530 // Objects in a firmware-limited or SVN-limited hierarchy cannot be made 531 // persistent. 532 if(HierarchyIsFirmwareLimited(evictObject->hierarchy) 533 || HierarchyIsSvnLimited(evictObject->hierarchy)) 534 return TPM_RCS_HIERARCHY + RC_EvictControl_objectHandle; 535 408 536 // Temporary, stClear or public only objects can not be made persistent 409 537 if(evictObject->attributes.temporary == SET … … 411 539 || evictObject->attributes.publicOnly == SET) 412 540 return TPM_RCS_ATTRIBUTES + RC_EvictControl_objectHandle; 541 413 542 // If objectHandle refers to a persistent object, it should be the same as 414 543 // input persistentHandle … … 416 545 && evictObject->evictHandle != in->persistentHandle) 417 546 return TPM_RCS_HANDLE + RC_EvictControl_objectHandle; 547 418 548 // Additional authorization validation 419 549 if(in->auth == TPM_RH_PLATFORM) … … 421 551 // To make persistent 422 552 if(evictObject->attributes.evict == CLEAR) 423 424 425 426 427 428 429 430 431 553 { 554 // PlatformAuth can not set evict object in storage or endorsement 555 // hierarchy 556 if(evictObject->attributes.ppsHierarchy == CLEAR) 557 return TPM_RCS_HIERARCHY + RC_EvictControl_objectHandle; 558 // Platform cannot use a handle outside of platform persistent range. 559 if(!NvIsPlatformPersistentHandle(in->persistentHandle)) 560 return TPM_RCS_RANGE + RC_EvictControl_persistentHandle; 561 } 432 562 // PlatformAuth can delete any persistent object 433 563 } … … 437 567 if(evictObject->attributes.ppsHierarchy == SET) 438 568 return TPM_RCS_HIERARCHY + RC_EvictControl_objectHandle; 569 439 570 // Owner cannot use a handle outside of owner persistent range. 440 571 if(evictObject->attributes.evict == CLEAR … … 466 597 return result; 467 598 } 468 #endif // CC_EvictControl 599 600 #endif // CC_EvictControl -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/GpMacros.h ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: GpMacros.h 1658 2021-01-22 23:14:01Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 202 1*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2024 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 #ifndef GPMACROS_H 63 #define GPMACROS_H 64 65 /* 5.10.1 Introduction */ 66 /* This file is a collection of miscellaneous macros. */ 61 //** Introduction 62 // This file is a collection of miscellaneous macros. 63 64 #ifndef GP_MACROS_H 65 #define GP_MACROS_H 66 67 67 #ifndef NULL 68 #define NULL 0 69 #endif 70 #include "swap.h" 71 #include "VendorString.h" 72 /* 5.10.2 For Self-test */ 73 /* These macros are used in CryptUtil() to invoke the incremental self test. */ 74 #if SELF_TEST 75 # define TEST(alg) if(TEST_BIT(alg, g_toTest)) CryptTestAlgorithm(alg, NULL) 76 /* Use of TPM_ALG_NULL is reserved for RSAEP/RSADP testing. If someone is wanting to test a hash 77 with that value, don't do it. */ 78 # define TEST_HASH(alg) \ 79 if(TEST_BIT(alg, g_toTest) \ 80 && (alg != TPM_ALG_NULL)) \ 81 CryptTestAlgorithm(alg, NULL) 68 # define NULL 0 69 #endif 70 71 #include "endian_swap.h" 72 #include "VendorInfo.h" 73 74 //** For Self-test 75 // These macros are used in CryptUtil to invoke the incremental self test. 76 #if ENABLE_SELF_TESTS 77 # define TPM_DO_SELF_TEST(alg) \ 78 do \ 79 { \ 80 if(TEST_BIT(alg, g_toTest)) \ 81 CryptTestAlgorithm(alg, NULL); \ 82 } while(0) 82 83 #else 83 # defineTEST(alg)84 # define TEST_HASH(alg)85 #endif // SELF_TEST 86 / * 5.10.3 For Failures */84 # define TPM_DO_SELF_TEST(alg) 85 #endif // ENABLE_SELF_TESTS 86 87 //** For Failures 87 88 #if defined _POSIX_ 88 # define FUNCTION_NAME__func__ /* libtpms changed */89 # define FUNCTION_NAME __func__ /* libtpms changed */ 89 90 #else 90 # define FUNCTION_NAME __FUNCTION__ 91 #endif 92 #if !FAIL_TRACE 93 # define FAIL(errorCode) (TpmFail(errorCode)) 94 # define LOG_FAILURE(errorCode) (TpmLogFailure(errorCode)) 91 # define FUNCTION_NAME __FUNCTION__ 92 #endif 93 94 #if defined(FAIL_TRACE) && FAIL_TRACE != 0 95 # define CODELOCATOR() FUNCTION_NAME, __LINE__ 96 #else // !FAIL_TRACE 97 // if provided, use the definition of CODELOCATOR from TpmConfiguration so 98 // implementor can customize this. 99 # ifndef CODELOCATOR 100 # define CODELOCATOR() 0 101 # endif 102 #endif // FAIL_TRACE 103 104 // SETFAILED calls TpmFail. It may or may not return based on the NO_LONGJMP flag. 105 // CODELOCATOR is a macro that expands to either one 64-bit value that encodes the 106 // location, or two parameters: Function Name and Line Number. 107 #define SETFAILED(errorCode) (TpmFail(CODELOCATOR(), errorCode)) 108 109 // If implementation is using longjmp, then calls to TpmFail() will never 110 // return. However, without longjmp facility, TpmFail will return while most of 111 // the code currently expects FAIL() calls to immediately abort the current 112 // command. If they don't, some commands return success instead of failure. The 113 // family of macros below are provided to allow the code to be modified to 114 // correctly propagate errors correctly, based on the context. 115 // 116 // * Some functions, particularly the ECC crypto have state cleanup at the end 117 // of the function and need to use the goto Exit pattern. 118 // * Other functions return TPM_RC values, which should return TPM_RC_FAILURE 119 // * Still other functions return an isOK boolean and need to return FALSE. 120 // 121 // if longjmp is available, all these macros just call SETFAILED and immediately 122 // abort. Note any of these approaches could leak memory if the crypto adapter 123 // libraries are using dynamic memory. 124 // 125 // FAIL vs. FAIL_NORET 126 // =================== 127 // Be cautious with these macros. FAIL_NORET is intended as an affirmation 128 // that the upstream code calling the function using this macro has been 129 // investigated to confirm that upstream functions correctly handle this 130 // function putting the TPM into failure mode without returning an error. 131 // 132 // The TPM library was originally written with a lot of error checking omitted, 133 // which means code occurring after a FAIL macro may not expect to be called 134 // when the TPM is in failure mode. When NO_LONGJMP is false (the system has a 135 // longjmp API), then none of that code is executed because the sample platform 136 // sets up longjmp before calling ExecuteCommand. However, in the NO_LONGJMP 137 // case, code following a FAIL or FAIL_NORET macro will get run. The 138 // conservative assumption is that code is untested and may be unsafe in such a 139 // situation. FAIL_NORET can replace FAIL when the code has been reviewed to 140 // ensure the post-FAIL code is safe. Of course, this is a point-in-time 141 // assertion that is only true when the FAIL_NORET macro is first inserted; 142 // hence it is better to use one of the early-exit macros to immediately return. 143 // However, the necessary return-code plumbing may be large and FAIL/FAIL_NORET 144 // are provided to support gradual improvement over time. 145 146 #ifndef NO_LONGJMP 147 // has longjmp 148 // necesary to reference Exit, even though the code is no-return 149 # define TPM_FAIL_RETURN NORETURN void 150 151 // see discussion above about FAIL/FAIL_NORET 152 # define FAIL(failCode) SETFAILED(failCode) 153 # define FAIL_NORET(failCode) SETFAILED(failCode) 154 # define FAIL_IMMEDIATE(failCode, retval) SETFAILED(failCode) 155 # define FAIL_BOOL(failCode) SETFAILED(failCode) 156 # define FAIL_RC(failCode) SETFAILED(failCode) 157 # define FAIL_VOID(failCode) SETFAILED(failCode) 158 # define FAIL_NULL(failCode) SETFAILED(failCode) 159 # define FAIL_EXIT(failCode, returnVar, returnCode) \ 160 do \ 161 { \ 162 SETFAILED(failCode); \ 163 goto Exit; \ 164 } while(0) 165 166 #else // NO_LONGJMP 167 // no longjmp service is available 168 # define TPM_FAIL_RETURN void 169 170 // This macro is provided for existing code and should not be used in new code. 171 // see discussion above. 172 # define FAIL(failCode) FAIL_NORET(failCode) 173 174 // Be cautious with this macro, see discussion above. 175 # define FAIL_NORET(failCode) SETFAILED(failCode) 176 177 // fail and immediately return void 178 # define FAIL_VOID(failCode) \ 179 do \ 180 { \ 181 SETFAILED(failCode); \ 182 return; \ 183 } while(0) 184 185 // fail and immediately return a value 186 # define FAIL_IMMEDIATE(failCode, retval) \ 187 do \ 188 { \ 189 SETFAILED(failCode); \ 190 return retval; \ 191 } while(0) 192 193 // fail and return FALSE 194 # define FAIL_BOOL(failCode) FAIL_IMMEDIATE(failCode, FALSE) 195 196 // fail and return TPM_RC_FAILURE 197 # define FAIL_RC(failCode) FAIL_IMMEDIATE(failCode, TPM_RC_FAILURE) 198 199 // fail and return NULL 200 # define FAIL_NULL(failCode) FAIL_IMMEDIATE(failCode, NULL) 201 202 // fail and return using the goto exit pattern 203 # define FAIL_EXIT(failCode, returnVar, returnCode) \ 204 do \ 205 { \ 206 SETFAILED(failCode); \ 207 returnVar = returnCode; \ 208 goto Exit; \ 209 } while(0) 210 211 #endif 212 213 // This macro tests that a condition is TRUE and puts the TPM into failure mode 214 // if it is not. If longjmp is being used, then the macro makes a call from 215 // which there is no return. Otherwise, the function will return the given 216 // return code. 217 #define VERIFY(condition, failCode, returnCode) \ 218 do \ 219 { \ 220 if(!(condition)) \ 221 { \ 222 FAIL_IMMEDIATE(failCode, returnCode); \ 223 } \ 224 } while(0) 225 226 // this function also verifies a condition and enters failure mode, but sets a 227 // return value and jumps to Exit on failure - allowing for cleanup. 228 #define VERIFY_OR_EXIT(condition, failCode, returnVar, returnCode) \ 229 do \ 230 { \ 231 if(!(condition)) \ 232 { \ 233 FAIL_EXIT(failCode, returnVar, returnCode); \ 234 } \ 235 } while(0) 236 237 // verify the given TPM_RC is success and we are not in 238 // failure mode. Otherwise, return immediately with TPM_RC_FAILURE. 239 // note that failure mode is checked first so that an existing FATAL_* error code 240 // is not overwritten with the default from this macro. 241 #define VERIFY_RC(rc) \ 242 do \ 243 { \ 244 if(g_inFailureMode) \ 245 { \ 246 return TPM_RC_FAILURE; \ 247 } \ 248 if(rc != TPM_RC_SUCCESS) \ 249 { \ 250 FAIL_IMMEDIATE(FATAL_ERROR_ASSERT, TPM_RC_FAILURE); \ 251 } \ 252 } while(0) 253 254 // verify the TPM is not in failure mode or return failure 255 #define VERIFY_NOT_FAILED() \ 256 do \ 257 { \ 258 if(g_inFailureMode) \ 259 { \ 260 return TPM_RC_FAILURE; \ 261 } \ 262 } while(0) 263 264 // Enter failure mode if the given TPM_RC is not success, return void. 265 #define VERIFY_RC_VOID(rc) \ 266 do \ 267 { \ 268 if(g_inFailureMode) \ 269 { \ 270 return; \ 271 } \ 272 if(rc != TPM_RC_SUCCESS) \ 273 { \ 274 FAIL_VOID(FATAL_ERROR_ASSERT); \ 275 } \ 276 } while(0) 277 278 // These VERIFY_CRYPTO macros all set failure mode to FATAL_ERROR_CRYPTO 279 // and immediately return. The general way to parse the names is: 280 // VERIFY_CRYPTO_[conditionType]_[OR_EXIT]_[retValType] 281 // if conditionType is omitted, it is taken as BOOL. 282 // Without OR_EXIT, implies an immediate return. Thus VERIFY_CRYPTO_BOOL: 283 // 1. check fn against TRUE 284 // 2. if false, set failure mode to FATAL_ERROR_CRYPTO 285 // 3. immediately return FALSE. 286 // and, VERIFY_CRYPTO_OR_EXIT_RC translates to: 287 // 1. Check a BOOL 288 // 2. If false, set failure mode with FATAL_ERROR_CRYPTO, 289 // 3. assume retVal is type TPM_RC, set it to TPM_RC_FAILURE 290 // 4. Goto Exit 291 // while VERIFY_CRYPTO_RC_OR_EXIT translates to: 292 // 1. Check fn result against TPM_RC_SUCCESS 293 // 2. if not equal, set failure mode to FATAL_ERROR_CRYPTO 294 // 3. assume retVal is type TPM_RC, set it to TPM_RC_FAILURE 295 // 4. Goto Exit. 296 #define VERIFY_CRYPTO(fn) VERIFY((fn), FATAL_ERROR_CRYPTO, TPM_RC_FAILURE) 297 298 #define VERIFY_CRYPTO_BOOL(fn) VERIFY((fn), FATAL_ERROR_CRYPTO, FALSE) 299 300 #define VERIFY_CRYPTO_OR_NULL(fn) VERIFY((fn), FATAL_ERROR_CRYPTO, NULL) 301 302 // these VERIFY_CRYPTO macros all set a result value and goto Exit 303 #define VERIFY_CRYPTO_OR_EXIT(fn, returnVar, returnCode) \ 304 VERIFY_OR_EXIT(fn, FATAL_ERROR_CRYPTO, returnVar, returnCode); 305 306 // these VERIFY_CRYPTO_OR_EXIT functions assume the return value variable is 307 // named retVal 308 #define VERIFY_CRYPTO_OR_EXIT_RC(fn) \ 309 VERIFY_CRYPTO_OR_EXIT_GENERIC(fn, retVal, TPM_RC_FAILURE) 310 311 #define VERIFY_CRYPTO_OR_EXIT_FALSE(fn) \ 312 VERIFY_CRYPTO_OR_EXIT_GENERIC(fn, retVal, FALSE) 313 314 #define VERIFY_CRYPTO_RC_OR_EXIT(fn) \ 315 do \ 316 { \ 317 TPM_RC rc = fn; \ 318 if(rc != TPM_RC_SUCCESS) \ 319 { \ 320 FAIL_EXIT(FATAL_ERROR_CRYPTO, retVal, rc); \ 321 } \ 322 } while(0) 323 324 #if(defined EMPTY_ASSERT) && (EMPTY_ASSERT != NO) 325 # define pAssert(a) ((void)0) 95 326 #else 96 # define FAIL(errorCode) TpmFail(FUNCTION_NAME, __LINE__, errorCode) 97 # define LOG_FAILURE(errorCode) TpmLogFailure(FUNCTION_NAME, __LINE__, errorCode) 98 #endif 99 /* If implementation is using longjmp, then the call to TpmFail() does not return and the compiler 100 will complain about unreachable code that comes after. To allow for not having longjmp, TpmFail() 101 will return and the subsequent code will be executed. This macro accounts for the difference. */ 102 #ifndef NO_LONGJMP 103 # define FAIL_RETURN(returnCode) 104 # define TPM_FAIL_RETURN NORETURN void 105 #else 106 # define FAIL_RETURN(returnCode) return (returnCode) 107 # define TPM_FAIL_RETURN void 108 #endif 109 /* This macro tests that a condition is TRUE and puts the TPM into failure mode if it is not. If 110 longjmp is being used, then the FAIL(FATAL_ERROR_) macro makes a call from which there is no 111 return. Otherwise, it returns and the function will exit with the appropriate return code. */ 112 #define REQUIRE(condition, errorCode, returnCode) \ 113 { \ 114 if(!!(condition)) \ 115 { \ 116 FAIL(FATAL_ERROR_errorCode); \ 117 FAIL_RETURN(returnCode); \ 118 } \ 327 # define pAssert(a) \ 328 do \ 329 { \ 330 if(!(a)) \ 331 FAIL(FATAL_ERROR_PARAMETER); \ 332 } while(0) 333 334 # define pAssert_ZERO(a) \ 335 do \ 336 { \ 337 if(!(a)) \ 338 FAIL_IMMEDIATE(FATAL_ERROR_ASSERT, 0); \ 339 } while(0); 340 341 # define pAssert_RC(a) \ 342 do \ 343 { \ 344 if(!(a)) \ 345 FAIL_RC(FATAL_ERROR_ASSERT); \ 346 } while(0); 347 348 # define pAssert_BOOL(a) \ 349 do \ 350 { \ 351 if(!(a)) \ 352 FAIL_BOOL(FATAL_ERROR_ASSERT); \ 353 } while(0); 354 355 # define pAssert_NULL(a) \ 356 do \ 357 { \ 358 if(!(a)) \ 359 FAIL_NULL(FATAL_ERROR_ASSERT); \ 360 } while(0); 361 362 // using FAIL_NORET isn't optimium but is available in limited cases that 363 // result in wrong calculated values, and can be checked later 364 // but should have no vulnerability implications. 365 # define pAssert_NORET(a) \ 366 { \ 367 if(!(a)) \ 368 FAIL_NORET(FATAL_ERROR_ASSERT); \ 119 369 } 120 #define PARAMETER_CHECK(condition, returnCode) \ 121 REQUIRE((condition), PARAMETER, returnCode) 122 #if defined EMPTY_ASSERT && (EMPTY_ASSERT != NO) 123 # define pAssert(a) ((void)0) 124 #else 125 # define pAssert(a) {if(!(a)) FAIL(FATAL_ERROR_PARAMETER);} 126 #endif 127 /* 5.10.4 Derived from Vendor-specific values */ 128 /* Values derived from vendor specific settings in TpmProfile.h */ 129 #define PCR_SELECT_MIN ((PLATFORM_PCR+7)/8) 130 #define PCR_SELECT_MAX ((IMPLEMENTATION_PCR+7)/8) 131 #define MAX_ORDERLY_COUNT ((1 << ORDERLY_BITS) - 1) 132 #define RSA_MAX_PRIME (MAX_RSA_KEY_BYTES / 2) 133 #define RSA_PRIVATE_SIZE (RSA_MAX_PRIME * 5) 134 135 /* 5.10.5 Compile-time Checks */ 136 /* In some cases, the relationship between two values may be dependent on things that change based 137 on various selections like the chosen cryptographic libraries. It is possible that these 138 selections will result in incompatible settings. These are often detectable by the compiler but 139 it isn't always possible to do the check in the preprocessor code. For example, when the check 140 requires use of sizeof then the preprocessor can't do the comparison. For these cases, we include 141 a special macro that, depending on the compiler will generate a warning to indicate if the check 142 always passes or always fails because it involves fixed constants. To run these checks, define 143 COMPILER_CHECKS in TpmBuildSwitches.h */ 144 #if COMPILER_CHECKS 145 # define cAssert pAssert 146 #else 147 # define cAssert(value) 148 #endif 149 /* This is used commonly in the Crypt code as a way to keep listings from getting too long. This is 150 not to save paper but to allow one to see more useful stuff on the screen at any given time. */ 151 #define ERROR_RETURN(returnCode) \ 152 { \ 153 retVal = returnCode; \ 154 goto Exit; \ 370 371 // this macro is used where a calling code has been verified to function correctly 372 // when the failing assert immediately returns without an error code. 373 // this can be because either the caller checks the fatal error flag, or 374 // the state is safe and a higher-level check will catch it. 375 # define pAssert_VOID_OK(a) \ 376 { \ 377 if(!(a)) \ 378 FAIL_VOID(FATAL_ERROR_ASSERT); \ 155 379 } 156 #ifndef MAX 157 # define MAX(a, b) ((a) > (b) ? (a) : (b)) 158 #endif 159 #ifndef MIN 160 # define MIN(a, b) ((a) < (b) ? (a) : (b)) 161 #endif 380 381 #endif 382 383 // These macros are commonly used in the "Crypt" code as a way to keep listings from 384 // getting too long. This is not to save paper but to allow one to see more 385 // useful stuff on the screen at any given time. Neither macro sets failure mode. 386 #define ERROR_EXIT(returnCode) \ 387 do \ 388 { \ 389 retVal = returnCode; \ 390 goto Exit; \ 391 } while(0) 392 393 // braces are necessary for this usage: 394 // if (y) 395 // GOTO_ERROR_UNLESS(x) 396 // else ... 397 // without braces the else would attach to the GOTO macro instead of the 398 // outer if statement; given the amount of TPM code that doesn't use braces on 399 // if statements, this is a live risk. 400 #define GOTO_ERROR_UNLESS(_X) \ 401 do \ 402 { \ 403 if(!(_X)) \ 404 goto Error; \ 405 } while(0) 406 407 #include "MinMax.h" 408 162 409 #ifndef IsOdd 163 # define IsOdd(a) (((a) & 1) != 0) 164 #endif 410 # define IsOdd(a) (((a)&1) != 0) 411 #endif 412 165 413 #ifndef BITS_TO_BYTES 166 414 # define BITS_TO_BYTES(bits) (((bits) + 7) >> 3) 167 415 #endif 168 #ifndef DIV_UP 169 # define DIV_UP(var, div) ((var + div - 1) / (div)) 170 #endif 171 /* These are defined for use when the size of the vector being checked is known at compile time. */ 172 #define TEST_BIT(bit, vector) TestBit((bit), (BYTE*)&(vector), sizeof(vector))173 #define SET_BIT(bit, vector) SetBit((bit), (BYTE*)&(vector), sizeof(vector))174 #define CLEAR_BIT(bit, vector) ClearBit((bit), (BYTE *)&(vector), sizeof(vector)) 175 / * The following definitions are used if they have not already been defined. The defaults for these176 settings are compatible with ISO/IEC 9899:2011 (E) */ 416 417 // These are defined for use when the size of the vector being checked is known 418 // at compile time. 419 #define TEST_BIT(bit, vector) TestBit((bit), (BYTE*)&(vector), sizeof(vector)) 420 #define SET_BIT(bit, vector) SetBit((bit), (BYTE*)&(vector), sizeof(vector)) 421 #define CLEAR_BIT(bit, vector) ClearBit((bit), (BYTE*)&(vector), sizeof(vector)) 422 423 // The following definitions are used if they have not already been defined. The 424 // defaults for these settings are compatible with ISO/IEC 9899:2011 (E) 177 425 #ifndef LIB_EXPORT 178 # 179 # 426 # define LIB_EXPORT 427 # define LIB_IMPORT 180 428 #endif 181 429 #ifndef NORETURN 182 # 430 # define NORETURN _Noreturn 183 431 #endif 184 432 #ifndef NOT_REFERENCED 185 # define NOT_REFERENCED(x = x) ((void) (x)) 186 #endif 433 # define NOT_REFERENCED(x = x) ((void)(x)) 434 #endif 435 187 436 #define STD_RESPONSE_HEADER (sizeof(TPM_ST) + sizeof(UINT32) + sizeof(TPM_RC)) 188 #define JOIN(x,y) x##y 189 #define JOIN3(x, y, z) x##y##z 190 #define CONCAT(x,y) JOIN(x, y) 191 #define CONCAT3(x, y, z) JOIN3(x,y,z) 192 193 /* If CONTEXT_INTEGRITY_HASH_ALG is defined, then the vendor is using the old style 194 table. Otherwise, pick the strongest implemented hash algorithm as the context hash. */ 195 196 #if defined(VBOX) 197 /* 198 * Our openssl symbol mangling clashes with teh defines below prefixing them with VBox_ 199 * which of course breaks everything else... Lets just hope libtpms doesn't call SHA512(), etc. 200 */ 201 # undef SHA1 202 # undef SHA256 203 # undef SHA384 204 # undef SHA512 205 #endif 206 207 #ifndef CONTEXT_HASH_ALGORITHM 208 # if defined ALG_SHA3_512 && ALG_SHA3_512 == YES 209 # define CONTEXT_HASH_ALGORITHM SHA3_512 210 # elif defined ALG_SHA512 && ALG_SHA512 == YES 211 # define CONTEXT_HASH_ALGORITHM SHA512 212 # elif defined ALG_SHA3_384 && ALG_SHA3_384 == YES 213 # define CONTEXT_HASH_ALGORITHM SHA3_384 214 # elif defined ALG_SHA384 && ALG_SHA384 == YES 215 # define CONTEXT_HASH_ALGORITHM SHA384 216 # elif defined ALG_SHA3_256 && ALG_SHA3_256 == YES 217 # define CONTEXT_HASH_ALGORITHM SHA3_256 218 # elif defined ALG_SHA256 && ALG_SHA256 == YES 219 # define CONTEXT_HASH_ALGORITHM SHA256 220 # elif defined ALG_SM3_256 && ALG_SM3_256 == YES 221 # define CONTEXT_HASH_ALGORITHM SM3_256 222 # elif defined ALG_SHA1 && ALG_SHA1 == YES 223 # define CONTEXT_HASH_ALGORITHM SHA1 224 # endif 225 # define CONTEXT_INTEGRITY_HASH_ALG CONCAT(TPM_ALG_, CONTEXT_HASH_ALGORITHM) 226 #if CONTEXT_HASH_ALGORITHM != SHA512 // libtpms added begin 227 #error CONTEXT_HASH_ALGORITHM must remain SHA512 228 #endif // libtpms added end 229 #endif 230 231 #ifndef CONTEXT_INTEGRITY_HASH_SIZE 232 #define CONTEXT_INTEGRITY_HASH_SIZE CONCAT(CONTEXT_HASH_ALGORITHM, _DIGEST_SIZE) 233 #endif 234 #if ALG_RSA 235 #define RSA_SECURITY_STRENGTH (MAX_RSA_KEY_BITS >= 15360 ? 256 : \ 236 (MAX_RSA_KEY_BITS >= 7680 ? 192 : \ 237 (MAX_RSA_KEY_BITS >= 3072 ? 128 : \ 238 (MAX_RSA_KEY_BITS >= 2048 ? 112 : \ 239 (MAX_RSA_KEY_BITS >= 1024 ? 80 : 0))))) 437 438 // This bit is used to indicate that an authorization ticket expires on TPM Reset 439 // and TPM Restart. It is added to the timeout value returned by TPM2_PoliySigned() 440 // and TPM2_PolicySecret() and used by TPM2_PolicyTicket(). The timeout value is 441 // relative to Time (g_time). Time is reset whenever the TPM loses power and cannot 442 // be moved forward by the user (as can Clock). 'g_time' is a 64-bit value expressing 443 // time in ms. Stealing the MSb for a flag means that the TPM needs to be reset 444 // at least once every 292,471,208 years rather than once every 584,942,417 years. 445 #define EXPIRATION_BIT ((UINT64)1 << 63) 446 447 // Check for consistency of the bit ordering of bit fields 448 #if BIG_ENDIAN_TPM && MOST_SIGNIFICANT_BIT_0 && USE_BIT_FIELD_STRUCTURES 449 # error "Settings not consistent" 450 #endif 451 452 // These macros are used to handle the variation in handling of bit fields. If 453 #if USE_BIT_FIELD_STRUCTURES // The default, old version, with bit fields 454 # define IS_ATTRIBUTE(a, type, b) ((a.b) != 0) 455 # define SET_ATTRIBUTE(a, type, b) (a.b = SET) 456 # define CLEAR_ATTRIBUTE(a, type, b) (a.b = CLEAR) 457 # define GET_ATTRIBUTE(a, type, b) (a.b) 458 # define TPMA_ZERO_INITIALIZER() \ 459 { \ 460 0 \ 461 } 240 462 #else 241 #define RSA_SECURITY_STRENGTH 0 242 #endif // ALG_RSA 243 #if ALG_ECC 244 #define ECC_SECURITY_STRENGTH (MAX_ECC_KEY_BITS >= 521 ? 256 : \ 245 (MAX_ECC_KEY_BITS >= 384 ? 192 : \ 246 (MAX_ECC_KEY_BITS >= 256 ? 128 : 0))) 247 #else 248 #define ECC_SECURITY_STRENGTH 0 249 #endif // ALG_ECC 250 #define MAX_ASYM_SECURITY_STRENGTH \ 251 MAX(RSA_SECURITY_STRENGTH, ECC_SECURITY_STRENGTH) 252 #define MAX_HASH_SECURITY_STRENGTH ((CONTEXT_INTEGRITY_HASH_SIZE * 8) / 2) 253 254 /* Unless some algorithm is broken... */ 255 256 #define MAX_SYM_SECURITY_STRENGTH MAX_SYM_KEY_BITS 257 #define MAX_SECURITY_STRENGTH_BITS \ 258 MAX(MAX_ASYM_SECURITY_STRENGTH, \ 259 MAX(MAX_SYM_SECURITY_STRENGTH, \ 260 MAX_HASH_SECURITY_STRENGTH)) 261 /* This is the size that was used before the 1.38 errata requiring that P1.14.4 be followed */ 262 #define PROOF_SIZE CONTEXT_INTEGRITY_HASH_SIZE 263 /* As required by P1.14.4 */ 264 #define COMPLIANT_PROOF_SIZE \ 265 (MAX(CONTEXT_INTEGRITY_HASH_SIZE, (2 * MAX_SYM_KEY_BYTES))) 266 /* As required by P1.14.3.1 */ 267 #define COMPLIANT_PRIMARY_SEED_SIZE \ 268 BITS_TO_BYTES(MAX_SECURITY_STRENGTH_BITS * 2) 269 /* This is the pre-errata version */ 270 #ifndef PRIMARY_SEED_SIZE 271 # define PRIMARY_SEED_SIZE PROOF_SIZE 272 #endif 273 #if USE_SPEC_COMPLIANT_PROOFS 274 # undef PROOF_SIZE 275 # define PROOF_SIZE COMPLIANT_PROOF_SIZE 276 # undef PRIMARY_SEED_SIZE 277 # define PRIMARY_SEED_SIZE COMPLIANT_PRIMARY_SEED_SIZE 278 #endif // USE_SPEC_COMPLIANT_PROOFS 279 #if !SKIP_PROOF_ERRORS 280 # if PROOF_SIZE < COMPLIANT_PROOF_SIZE 281 # error "PROOF_SIZE is not compliant with TPM specification" 282 # endif 283 # if PRIMARY_SEED_SIZE < COMPLIANT_PRIMARY_SEED_SIZE 284 # error "Non-compliant PRIMARY_SEED_SIZE" 285 # endif 286 #endif // !SKIP_PROOF_ERRORS 287 288 /* If CONTEXT_ENCRYPT_ALG is defined, then the vendor is using the old style table */ 289 #if defined CONTEXT_ENCRYPT_ALG 290 # undef CONTEXT_ENCRYPT_ALGORITHM 291 # if CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE 292 # define CONTEXT_ENCRYPT_ALGORITHM AES 293 # elif CONTEXT_ENCRYPT_ALG == ALG_SM4_VALUE 294 # define CONTEXT_ENCRYPT_ALGORITHM SM4 295 # elif CONTEXT_ENCRYPT_ALG == ALG_CAMELLIA_VALUE 296 # define CONTEXT_ENCRYPT_ALGORITHM CAMELLIA 297 # elif CONTEXT_ENCRYPT_ALG == ALG_TDES_VALUE 298 # error Are you kidding? 299 # else 300 # error Unknown value for CONTEXT_ENCRYPT_ALG 301 # endif // CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE 302 #else 303 # define CONTEXT_ENCRYPT_ALG \ 304 CONCAT3(ALG_, CONTEXT_ENCRYPT_ALGORITHM, _VALUE) 305 #endif // CONTEXT_ENCRYPT_ALG 306 #define CONTEXT_ENCRYPT_KEY_BITS \ 307 CONCAT(CONTEXT_ENCRYPT_ALGORITHM, _MAX_KEY_SIZE_BITS) 308 #define CONTEXT_ENCRYPT_KEY_BYTES ((CONTEXT_ENCRYPT_KEY_BITS+7)/8) 309 310 /* This is updated to follow the requirement of P2 that the label not be larger than 32 bytes. */ 311 #ifndef LABEL_MAX_BUFFER 312 #define LABEL_MAX_BUFFER MIN(32, MAX(MAX_ECC_KEY_BYTES, MAX_DIGEST_SIZE)) 313 #endif 314 /* This bit is used to indicate that an authorization ticket expires on TPM Reset and TPM Restart.It 315 is added to the timeout value returned by TPM2_PoliySigned() and TPM2_PolicySecret() and used by 316 TPM2_PolicyTicket(). The timeout value is relative to Time (g_time). Time is reset whenever the 317 TPM loses power and cannot be moved forward by the user (as can Clock). g_time is a 64-bit value 318 expressing time in ms. Stealing the MSb() for a flag means that the TPM needs to be reset at least 319 once every 292,471,208 years rather than once every 584,942,417 years. */ 320 #define EXPIRATION_BIT ((UINT64)1 << 63) 321 /* Check for consistency of the bit ordering of bit fields */ 322 #if BIG_ENDIAN_TPM && MOST_SIGNIFICANT_BIT_0 && USE_BIT_FIELD_STRUCTURES 323 # error "Settings not consistent" 324 #endif 325 /* These macros are used to handle the variation in handling of bit fields. If */ 326 #if USE_BIT_FIELD_STRUCTURES // The default, old version, with bit fields 327 # define IS_ATTRIBUTE(a, type, b) ((a.b) != 0) 328 # define SET_ATTRIBUTE(a, type, b) (a.b = SET) 329 # define CLEAR_ATTRIBUTE(a, type, b) (a.b = CLEAR) 330 # define GET_ATTRIBUTE(a, type, b) (a.b) 331 # define TPMA_ZERO_INITIALIZER() {0} 332 #else 333 # define IS_ATTRIBUTE(a, type, b) ((a & type##_##b) != 0) 334 # define SET_ATTRIBUTE(a, type, b) (a |= type##_##b) 335 # define CLEAR_ATTRIBUTE(a, type, b) (a &= ~type##_##b) 336 # define GET_ATTRIBUTE(a, type, b) \ 337 (type)((a & type##_##b) >> type##_##b##_SHIFT) 338 # define TPMA_ZERO_INITIALIZER() (0) 339 #endif 340 #define VERIFY(_X) if(!(_X)) goto Error 463 # define IS_ATTRIBUTE(a, type, b) ((a & type##_##b) != 0) 464 # define SET_ATTRIBUTE(a, type, b) (a |= type##_##b) 465 # define CLEAR_ATTRIBUTE(a, type, b) (a &= ~type##_##b) 466 # define GET_ATTRIBUTE(a, type, b) (type)((a & type##_##b) >> type##_##b##_SHIFT) 467 # define TPMA_ZERO_INITIALIZER() (0) 468 #endif 469 341 470 // These macros determine if the values in this file are referenced or instanced. 342 471 // Global.c defines GLOBAL_C so all the values in this file will be instanced in 343 472 // Global.obj. For all other files that include this file, the values will simply 344 473 // be external references. For constants, there can be an initializer. 474 #ifndef EXTERN 475 # ifdef GLOBAL_C 476 # define EXTERN 477 # else 478 # define EXTERN extern 479 # endif 480 #endif // EXTERN 345 481 346 482 #ifdef GLOBAL_C 347 #define EXTERN 348 #define INITIALIZER(_value_) = _value_ 483 # define INITIALIZER(_value_) = _value_ 349 484 #else 350 #define EXTERN extern 351 #define INITIALIZER(_value_) 485 # define INITIALIZER(_value_) 352 486 #endif 353 487 … … 355 489 // 0x06 indicating an OID fallowed by an octet indicating the number of octets in the 356 490 // rest of the OID. This allows a user of this OID to know how much/little to copy. 357 #define MAKE_OID(NAME) \ 358 EXTERN const BYTE OID##NAME[] INITIALIZER({OID##NAME##_VALUE}) 359 360 /* This definition is moved from TpmProfile.h because it is not actually vendor- specific. It has to 361 be the same size as the sequence parameter of a TPMS_CONTEXT and that is a UINT64. So, this is an 362 invariant value */ 363 #define CONTEXT_COUNTER UINT64 364 365 #endif // GP_MACROS_H 491 #define MAKE_OID(NAME) EXTERN const BYTE OID##NAME[] INITIALIZER({OID##NAME##_VALUE}) 492 493 // This definition is moved from TpmProfile.h because it is not actually vendor- 494 // specific. It has to be the same size as the 'sequence' parameter of a TPMS_CONTEXT 495 // and that is a UINT64. So, this is an invariant value 496 #define CONTEXT_COUNTER UINT64 497 498 #include "TpmCalculatedAttributes.h" 499 500 #endif // GP_MACROS_H -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/HashTestData.h ¶
r108932 r108933 163 163 { 164 164 .key = &cmac_aeskey.b, 165 .data = { 0 }, 165 .data = { 0 }, /* VBOX */ 166 166 .datalen = 0, 167 167 .out = { 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/NVMarshal.c ¶
r108932 r108933 55 55 #include "TpmTcpProtocol.h" 56 56 #include "Simulator_fp.h" 57 #include "BackwardsCompatibilityBitArray.h" 58 #include "BackwardsCompatibilityObject.h" 57 59 58 60 #define TPM_HAVE_TPM2_DECLARATIONS 59 61 #include "tpm_library_intern.h" 62 63 MUST_BE(CONTEXT_INTEGRITY_HASH_ALG == TPM_ALG_SHA512); 60 64 61 65 /* … … 83 87 84 88 AssertCompile(NV_USER_DYNAMIC_END >= NV_USER_DYNAMIC); 85 #endif 86 89 #endif /* VBOX */ 90 87 91 typedef struct 88 92 { … … 109 113 INT32 size; 110 114 } pos[5]; /* more only needed for nested compile-time #ifdef's */ 111 } block_skip ;115 } block_skip_t; 112 116 113 117 /* … … 119 123 */ 120 124 static UINT16 121 block_skip_write_push(block_skip *bs, BOOL has_block,125 block_skip_write_push(block_skip_t *bs, BOOL has_block, 122 126 BYTE **buffer, INT32 *size) { 123 127 UINT16 written , w; … … 144 148 */ 145 149 static void 146 block_skip_write_pop(block_skip *bs, INT32 *size) {150 block_skip_write_pop(block_skip_t *bs, INT32 *size) { 147 151 UINT16 skip; 148 152 unsigned i = --bs->idx; … … 184 188 *size -= blocksize; 185 189 *skip_code = TRUE; 190 } else if (!has_block && !needs_block) { 191 /* no block but also none needed */ 192 *skip_code = TRUE; 186 193 } 187 194 } … … 190 197 191 198 #define BLOCK_SKIP_INIT \ 192 block_skip block_skip = { \199 block_skip_t block_skip = { \ 193 200 .idx = 0, \ 194 201 .sz = ARRAY_SIZE(block_skip.pos), \ … … 302 309 } 303 310 311 /* Marshal a string including its terminating NUL byte. A NULL pointer will be 312 * marshalled with 0 bytes and will be unmarshalled as a NULL pointer again. 313 */ 314 static UINT16 315 String_Marshal(const char *source, BYTE **buffer, INT32 *size) 316 { 317 UINT16 written = 0; 318 UINT16 len = 0; 319 320 if (source) 321 len = (UINT16)strlen(source) + 1; 322 written += UINT16_Marshal(&len, buffer, size); 323 324 if (len > 0 && *size >= len) { 325 memcpy(*buffer, source, len); 326 *buffer += len; 327 *size -= len; 328 329 written += len; 330 } 331 332 return written; 333 } 334 335 static TPM_RC 336 String_Unmarshal(char **target, BYTE **buffer, INT32 *size) 337 { 338 TPM_RC rc = TPM_RC_SUCCESS; 339 UINT16 len; 340 341 *target = NULL; 342 343 if (rc == TPM_RC_SUCCESS) { 344 rc = UINT16_Unmarshal(&len, buffer, size); 345 } 346 if (rc == TPM_RC_SUCCESS) { 347 if (*size < len) { 348 rc = TPM_RC_INSUFFICIENT; 349 } else if (len > 0) { 350 *target = malloc(len); 351 if (!*target) { 352 rc = TPM_RC_MEMORY; 353 } 354 } 355 } 356 if (rc == TPM_RC_SUCCESS) { 357 if (len > 0) { 358 memcpy(*target, *buffer, len); 359 *buffer += len; 360 *size -= len; 361 } 362 } 363 364 return rc; 365 } 366 304 367 static TPM_RC 305 368 UINT32_Unmarshal_Check(UINT32 *data, UINT32 exp, BYTE **buffer, INT32 *size, … … 313 376 if (rc == TPM_RC_SUCCESS && exp != *data) { 314 377 TPMLIB_LogTPM2Error("%s: Expected value: 0x%08x, found: 0x%08x\n", 315 __func__, exp, *data);378 msg, exp, *data); 316 379 rc = TPM_RC_BAD_TAG; 317 380 } … … 815 878 for (j = 0; j < pcrAllocated->pcrSelections[i].sizeofSelect; j++) { 816 879 if (pcrAllocated->pcrSelections[i].pcrSelect[j]) { 880 if (pcrAllocated->pcrSelections[i].hash >= 64) { 881 TPMLIB_LogTPM2Error("pcrbanks_algs_active: invalid hash alg id: %d\n", 882 pcrAllocated->pcrSelections[i].hash); 883 } else { 817 884 #ifndef VBOX 818 algs_active |= 1 << pcrAllocated->pcrSelections[i].hash; 819 #else 820 algs_active |= RT_BIT_64(pcrAllocated->pcrSelections[i].hash); 821 #endif 885 algs_active |= ((UINT64)1 << pcrAllocated->pcrSelections[i].hash); 886 #else 887 algs_active |= RT_BIT_64(pcrAllocated->pcrSelections[i].hash); 888 #endif /* VBOX */ 889 } 822 890 break; 823 891 } … … 866 934 needed_size = sizeof(data->Sha1); 867 935 t = (BYTE *)&data->Sha1; 868 break;936 break; 869 937 #endif 870 938 #if ALG_SHA256 … … 872 940 needed_size = sizeof(data->Sha256); 873 941 t = (BYTE *)&data->Sha256; 874 break;942 break; 875 943 #endif 876 944 #if ALG_SHA384 … … 878 946 needed_size = sizeof(data->Sha384); 879 947 t = (BYTE *)&data->Sha384; 880 break;948 break; 881 949 #endif 882 950 #if ALG_SHA512 … … 884 952 needed_size = sizeof(data->Sha512); 885 953 t = (BYTE *)&data->Sha512; 886 break;954 break; 887 955 #endif 888 956 #if ALG_SM3_256 … … 890 958 needed_size = sizeof(data->Sm3_256); 891 959 t = (BYTE *)&data->Sm3_256; 892 break;960 break; 893 961 #endif 894 962 #if ALG_SHA3_256 || ALG_SHA3_384 || ALG_SHA3_512 || ALG_SM3_256 … … 899 967 end = TRUE; 900 968 t = NULL; 901 break;969 break; 902 970 default: 903 971 TPMLIB_LogTPM2Error("PCR_SAVE: Unsupported algid %d.", … … 1048 1116 needed_size = sizeof(data->Sha1Pcr); 1049 1117 t = (BYTE *)&data->Sha1Pcr; 1050 break;1118 break; 1051 1119 #endif 1052 1120 #if ALG_SHA256 … … 1054 1122 needed_size = sizeof(data->Sha256Pcr); 1055 1123 t = (BYTE *)&data->Sha256Pcr; 1056 break;1124 break; 1057 1125 #endif 1058 1126 #if ALG_SHA384 … … 1060 1128 needed_size = sizeof(data->Sha384Pcr); 1061 1129 t = (BYTE *)&data->Sha384Pcr; 1062 break;1130 break; 1063 1131 #endif 1064 1132 #if ALG_SHA512 … … 1066 1134 needed_size = sizeof(data->Sha512Pcr); 1067 1135 t = (BYTE *)&data->Sha512Pcr; 1068 break;1136 break; 1069 1137 #endif 1070 1138 #if ALG_SM3_256 … … 1072 1140 needed_size = sizeof(data->Sm3_256Pcr); 1073 1141 t = (BYTE *)&data->Sm3_256Pcr; 1074 break;1142 break; 1075 1143 #endif 1076 1144 #if ALG_SHA3_256 || ALG_SHA3_384 || ALG_SHA3_512 || ALG_SM3_256 … … 1081 1149 end = TRUE; 1082 1150 t = NULL; 1083 break;1151 break; 1084 1152 default: 1085 1153 TPMLIB_LogTPM2Error("PCR: Unsupported algid %d.", … … 1319 1387 /* version <= 3 was writing an array of UINT8 */ 1320 1388 UINT8 element; 1321 for (i = 0; i < array_size && rc == TPM_RC_SUCCESS; i++) {1389 for (i = 0; i < array_size; i++) { 1322 1390 rc = UINT8_Unmarshal(&element, buffer, size); 1391 if (rc != TPM_RC_SUCCESS) 1392 break; 1393 1323 1394 data->contextArray[i] = element; 1324 1395 } … … 1483 1554 #define BN_PRIME_T_VERSION 2 1484 1555 static UINT16 1485 bn_prime_t_Marshal(bn_prime_t *data, BYTE **buffer, INT32 *size)1556 ci_prime_t_Marshal(ci_prime_t *data, BYTE **buffer, INT32 *size) 1486 1557 { 1487 1558 UINT16 written, numbytes; … … 1519 1590 1520 1591 static TPM_RC 1521 bn_prime_t_Unmarshal(bn_prime_t *data, BYTE **buffer, INT32 *size)1592 ci_prime_t_Unmarshal(ci_prime_t *data, BYTE **buffer, INT32 *size) 1522 1593 { 1523 1594 TPM_RC rc = TPM_RC_SUCCESS; … … 1542 1613 data->size = (numbytes + sizeof(crypt_uword_t) - 1) / sizeof(crypt_word_t); 1543 1614 if (data->size > data->allocated) { 1544 TPMLIB_LogTPM2Error(" bn_prime_t: Require size larger %zu than "1615 TPMLIB_LogTPM2Error("ci_prime_t: Require size larger %zu than " 1545 1616 "allocated %zu\n", 1546 1617 (size_t)data->size, (size_t)data->allocated); … … 1598 1669 #error Missing code 1599 1670 #else 1600 written += bn_prime_t_Marshal(&data->Q, buffer, size);1601 written += bn_prime_t_Marshal(&data->dP, buffer, size);1602 written += bn_prime_t_Marshal(&data->dQ, buffer, size);1603 written += bn_prime_t_Marshal(&data->qInv, buffer, size);1671 written += ci_prime_t_Marshal(&data->Q, buffer, size); 1672 written += ci_prime_t_Marshal(&data->dP, buffer, size); 1673 written += ci_prime_t_Marshal(&data->dQ, buffer, size); 1674 written += ci_prime_t_Marshal(&data->qInv, buffer, size); 1604 1675 #endif 1605 1676 … … 1630 1701 #else 1631 1702 if (rc == TPM_RC_SUCCESS) { 1632 rc = bn_prime_t_Unmarshal(&data->Q, buffer, size);1633 } 1634 if (rc == TPM_RC_SUCCESS) { 1635 rc = bn_prime_t_Unmarshal(&data->dP, buffer, size);1636 } 1637 if (rc == TPM_RC_SUCCESS) { 1638 rc = bn_prime_t_Unmarshal(&data->dQ, buffer, size);1639 } 1640 if (rc == TPM_RC_SUCCESS) { 1641 rc = bn_prime_t_Unmarshal(&data->qInv, buffer, size);1703 rc = ci_prime_t_Unmarshal(&data->Q, buffer, size); 1704 } 1705 if (rc == TPM_RC_SUCCESS) { 1706 rc = ci_prime_t_Unmarshal(&data->dP, buffer, size); 1707 } 1708 if (rc == TPM_RC_SUCCESS) { 1709 rc = ci_prime_t_Unmarshal(&data->dQ, buffer, size); 1710 } 1711 if (rc == TPM_RC_SUCCESS) { 1712 rc = ci_prime_t_Unmarshal(&data->qInv, buffer, size); 1642 1713 } 1643 1714 #endif … … 2386 2457 pAssert(source->authValue.t.size == 0); 2387 2458 pAssert(source->seedValue.t.size == 0); 2388 pAssert(source->sensitiveType == TPM_ALG_ERROR) 2459 pAssert(source->sensitiveType == TPM_ALG_ERROR); 2389 2460 /* public keys */ 2390 2461 } … … 2419 2490 pAssert(target->authValue.t.size == 0); 2420 2491 pAssert(target->seedValue.t.size == 0); 2421 pAssert(target->sensitiveType == TPM_ALG_ERROR) 2492 pAssert(target->sensitiveType == TPM_ALG_ERROR); 2422 2493 /* nothing do to do */ 2423 2494 } … … 2427 2498 2428 2499 #define OBJECT_MAGIC 0x75be73af 2429 #define OBJECT_VERSION 32500 #define OBJECT_VERSION 4 2430 2501 2431 2502 static UINT16 2432 OBJECT_Marshal(OBJECT *data, BYTE **buffer, INT32 *size) 2503 OBJECT_Marshal(OBJECT *data, BYTE **buffer, INT32 *size, 2504 struct RuntimeProfile *RuntimeProfile) 2433 2505 { 2434 2506 UINT16 written; 2435 2507 BOOL has_block; 2436 2508 BLOCK_SKIP_INIT; 2509 UINT16 blob_version; 2510 2511 switch (RuntimeProfile->stateFormatLevel) { 2512 case 0: 2513 pAssert(FALSE); 2514 break; 2515 case 1 ... 5: 2516 blob_version = 3; 2517 break; 2518 default: // since StateFormatLevel 6 2519 blob_version = 4; 2520 break; 2521 } 2437 2522 2438 2523 written = NV_HEADER_Marshal(buffer, size, 2439 OBJECT_VERSION, OBJECT_MAGIC, 3);2524 blob_version, OBJECT_MAGIC, blob_version); 2440 2525 2441 2526 /* … … 2445 2530 written += NV_TPMT_SENSITIVE_Marshal(&data->sensitive, buffer, size); 2446 2531 2447 #if ALG_RSA 2532 /* before v4: private exponent was always written 2533 * since v4: private exponent only written for RSA keys 2534 */ 2448 2535 has_block = TRUE; 2449 #else 2450 has_block = FALSE; 2451 #endif 2536 if (blob_version >= 4 && 2537 data->sensitive.sensitiveType != TPM_ALG_RSA) 2538 has_block = FALSE; 2539 2452 2540 written += BLOCK_SKIP_WRITE_PUSH(has_block, buffer, size); 2453 #if ALG_RSA 2454 written += privateExponent_t_Marshal(&data->privateExponent, 2455 buffer, size); 2456 #endif 2541 if (has_block) 2542 written += privateExponent_t_Marshal(&data->privateExponent, 2543 buffer, size); 2457 2544 BLOCK_SKIP_WRITE_POP(size); 2458 2545 … … 2468 2555 /* future versions append below this line */ 2469 2556 2557 /* since v4: hierarchy is written */ 2558 if (blob_version >= 4) 2559 written += TPMI_RH_HIERARCHY_Marshal(&data->hierarchy, buffer, size); 2560 2470 2561 BLOCK_SKIP_WRITE_POP(size); 2471 2562 BLOCK_SKIP_WRITE_POP(size); … … 2498 2589 } 2499 2590 2500 #if ALG_RSA 2591 /* before v4: private exponent was always written 2592 * since v4: private exponent only written for RSA keys 2593 */ 2501 2594 needs_block = TRUE; 2502 #else 2503 needs_block = FALSE; 2504 #endif 2595 if (hdr.version >= 4 && 2596 data->sensitive.sensitiveType != TPM_ALG_RSA) 2597 needs_block = FALSE; 2598 2505 2599 if (rc == TPM_RC_SUCCESS) { 2506 2600 BLOCK_SKIP_READ(skip_alg_rsa, needs_block, buffer, size, 2507 2601 "OBJECT", "privateExponent"); 2508 2602 } 2509 #if ALG_RSA2510 2603 if (rc == TPM_RC_SUCCESS) { 2511 2604 rc = privateExponent_t_Unmarshal(&data->privateExponent, 2512 2605 buffer, size); 2513 2606 } 2514 #endif2515 2607 skip_alg_rsa: 2516 2608 … … 2527 2619 /* default values before conditional block */ 2528 2620 data->seedCompatLevel = SEED_COMPAT_LEVEL_ORIGINAL; 2621 data->hierarchy = ObjectGetHierarchyFromAttributes(data); 2529 2622 2530 2623 /* version 2 starts having indicator for next versions that we can skip; … … 2540 2633 2541 2634 if (rc == TPM_RC_SUCCESS) { 2542 BLOCK_SKIP_READ(skip_future_versions, FALSE, buffer, size,2635 BLOCK_SKIP_READ(skip_future_versions, hdr.version >= 4, buffer, size, 2543 2636 "OBJECT", "version 4 or later"); 2544 2637 } 2545 /* future versions nest-append here */ 2638 2639 if (rc == TPM_RC_SUCCESS) { 2640 rc = TPMI_RH_HIERARCHY_Unmarshal(&data->hierarchy, buffer, size, TRUE); 2641 } 2546 2642 } 2547 2643 … … 2554 2650 2555 2651 UINT16 2556 ANY_OBJECT_Marshal(OBJECT *data, BYTE **buffer, INT32 *size) 2652 ANY_OBJECT_Marshal(OBJECT *data, BYTE **buffer, INT32 *size, 2653 struct RuntimeProfile *RuntimeProfile) 2557 2654 { 2558 2655 UINT16 written; … … 2569 2666 written += HASH_OBJECT_Marshal((HASH_OBJECT *)data, buffer, size); 2570 2667 else 2571 written += OBJECT_Marshal(data, buffer, size );2668 written += OBJECT_Marshal(data, buffer, size, RuntimeProfile); 2572 2669 } 2573 2670 … … 2664 2761 written += TPM2B_NONCE_Marshal(&data->nonceTPM, buffer, size); 2665 2762 // TPM2B_NAME or TPM2B_DIGEST could be used for marshalling 2763 MUST_BE(sizeof(data->u1.boundEntity) == sizeof(data->u1)); 2666 2764 written += TPM2B_NAME_Marshal(&data->u1.boundEntity, buffer, size); 2765 MUST_BE(sizeof(data->u2.auditDigest) == sizeof(data->u2)); 2667 2766 written += TPM2B_DIGEST_Marshal(&data->u2.auditDigest, buffer, size); 2668 2767 … … 2828 2927 2829 2928 UINT16 2830 VolatileState_Marshal(BYTE **buffer, INT32 *size )2929 VolatileState_Marshal(BYTE **buffer, INT32 *size, struct RuntimeProfile *RuntimeProfile) 2831 2930 { 2832 2931 UINT16 written; … … 2839 2938 BLOCK_SKIP_INIT; 2840 2939 PERSISTENT_DATA pd; 2940 TPM2B_AUTH unused = { 2941 .b.size = 0, 2942 }; 2841 2943 2842 2944 written = NV_HEADER_Marshal(buffer, size, … … 2863 2965 has_block = TRUE; 2864 2966 #else 2967 # error Unsupport #define value(s) 2865 2968 has_block = FALSE; 2866 2969 #endif … … 2870 2973 /* g_daUsed: must write */ 2871 2974 written += BOOL_Marshal(&g_daUsed, buffer, size); /* line 484 */ 2975 #else 2976 # error Unsupport #define value(s) 2872 2977 #endif 2873 2978 BLOCK_SKIP_WRITE_POP(size); … … 2886 2991 written += TPM2B_AUTH_Marshal(&g_platformUniqueAuthorities, buffer, size); /* line 535 */ 2887 2992 #endif 2888 written += TPM2B_AUTH_Marshal(& g_platformUniqueDetails, buffer, size); /* line 536*/2993 written += TPM2B_AUTH_Marshal(&unused, buffer, size); /* was g_platformUniqueDetails; unused since v0.10 */ 2889 2994 2890 2995 /* gp (persistent_data): skip; we assume its latest states in the persistent data file */ … … 2903 3008 has_block = TRUE; 2904 3009 #else 3010 # error Unsupport #define value(s) 2905 3011 has_block = FALSE; 2906 3012 #endif … … 2930 3036 has_block = TRUE; 2931 3037 #else 3038 # error Unsupport #define value(s) 2932 3039 has_block = FALSE; 2933 3040 #endif … … 2937 3044 /* s_cpHashForCommandAudit: seems not used; better to write it */ 2938 3045 written += TPM2B_DIGEST_Marshal(&s_cpHashForCommandAudit, buffer, size); 3046 #else 3047 # error Unsupport #define value(s) 2939 3048 #endif 2940 3049 BLOCK_SKIP_WRITE_POP(size); … … 2945 3054 BLOCK_SKIP_WRITE_POP(size); 2946 3055 2947 #if defined DA_C || defined GLOBAL_C || defined MANUFACTURE_C3056 #if defined DA_C || defined GLOBAL_C_UNSUPPORTED || defined MANUFACTURE_C 2948 3057 has_block = TRUE; 3058 #error Unsupport #define value(s) 2949 3059 #else 2950 3060 has_block = FALSE; … … 2952 3062 written += BLOCK_SKIP_WRITE_PUSH(has_block, buffer, size); 2953 3063 2954 #if defined DA_C || defined GLOBAL_C || defined MANUFACTURE_C2955 3064 #if defined DA_C || defined GLOBAL_C_UNSUPPORTED || defined MANUFACTURE_C 3065 #error Unsupport #define value(s) 2956 3066 #if !ACCUMULATE_SELF_HEAL_TIMER 2957 3067 has_block = TRUE; … … 2972 3082 has_block = TRUE; 2973 3083 #else 3084 # error Unsupport #define value(s) 2974 3085 has_block = FALSE; 2975 3086 #endif … … 2995 3106 * - s_cachedNvRamRef 2996 3107 */ 3108 #else 3109 # error Unsupport #define value(s) 2997 3110 #endif 2998 3111 BLOCK_SKIP_WRITE_POP(size); … … 3001 3114 has_block = TRUE; 3002 3115 #else 3116 # error Unsupport #define value(s) 3003 3117 has_block = FALSE; 3004 3118 #endif … … 3013 3127 3014 3128 for (i = 0; i < array_size; i++) { 3015 written += ANY_OBJECT_Marshal(&s_objects[i], buffer, size); 3016 } 3129 written += ANY_OBJECT_Marshal(&s_objects[i], buffer, size, RuntimeProfile); 3130 } 3131 #else 3132 # error Unsupport #define value(s) 3017 3133 #endif 3018 3134 BLOCK_SKIP_WRITE_POP(size); … … 3021 3137 has_block = TRUE; 3022 3138 #else 3139 # error Unsupport #define value(s) 3023 3140 has_block = FALSE; 3024 3141 #endif … … 3033 3150 written += PCR_Marshal(&s_pcrs[i], buffer, size); 3034 3151 } 3152 #else 3153 # error Unsupport #define value(s) 3035 3154 #endif 3036 3155 BLOCK_SKIP_WRITE_POP(size); … … 3039 3158 has_block = TRUE; 3040 3159 #else 3160 # error Unsupport #define value(s) 3041 3161 has_block = FALSE; 3042 3162 #endif … … 3055 3175 /* s_freeSessionSlots: */ 3056 3176 written += UINT32_Marshal((UINT32 *)&s_freeSessionSlots, buffer, size); 3177 #else 3178 # error Unsupport #define value(s) 3057 3179 #endif 3058 3180 BLOCK_SKIP_WRITE_POP(size); 3059 3181 3060 #if defined IO_BUFFER_C || defined GLOBAL_C 3182 #if defined IO_BUFFER_C || defined GLOBAL_C_UNSUPPORTED 3183 # error Unsupport #define value(s) 3061 3184 /* s_actionInputBuffer: skip; only used during a single command */ 3062 3185 /* s_actionOutputBuffer: skip; only used during a single command */ … … 3071 3194 has_block = TRUE; 3072 3195 #else 3196 # error Unsupport #define value(s) 3073 3197 has_block = FALSE; 3074 3198 #endif … … 3079 3203 written += UINT32_Marshal(&s_failLine, buffer, size); 3080 3204 written += UINT32_Marshal(&s_failCode, buffer, size); 3205 #else 3206 # error Unsupport #define value(s) 3081 3207 #endif // TPM_FAIL_C 3082 3208 BLOCK_SKIP_WRITE_POP(size); … … 3086 3212 #else 3087 3213 has_block = FALSE; 3214 # error Unsupport #define value(s) 3088 3215 #endif 3089 3216 written += BLOCK_SKIP_WRITE_PUSH(has_block, buffer, size); … … 3094 3221 tmp_uint64 = s_tpmTime; 3095 3222 written += UINT64_Marshal(&tmp_uint64, buffer, size); 3223 #else 3224 # error Unsupport #define value(s) 3096 3225 #endif 3097 3226 BLOCK_SKIP_WRITE_POP(size); … … 3228 3357 UINT16 array_size = 0; 3229 3358 UINT64 backthen; 3359 TPM2B_AUTH unused = { 3360 .b.size = 0, 3361 }; 3230 3362 3231 3363 if (rc == TPM_RC_SUCCESS) { … … 3258 3390 needs_block = TRUE; 3259 3391 #else 3392 # error Unsupport #define value(s) 3260 3393 needs_block = FALSE; 3261 3394 #endif … … 3268 3401 rc = BOOL_Unmarshal(&g_daUsed, buffer, size); /* line 484 */ 3269 3402 } 3403 #else 3404 # error Unsupport #define value(s) 3270 3405 #endif 3271 3406 skip_da: … … 3286 3421 #endif 3287 3422 if (rc == TPM_RC_SUCCESS) { 3288 rc = TPM2B_AUTH_Unmarshal(& g_platformUniqueDetails, buffer, size); /* line 536*/3423 rc = TPM2B_AUTH_Unmarshal(&unused, buffer, size); /* was g_platformUniqueDetails; unused since v0.10 */ 3289 3424 } 3290 3425 … … 3309 3444 needs_block = TRUE; 3310 3445 #else 3446 # error Unsupport #define value(s) 3311 3447 needs_block = FALSE; 3312 3448 #endif … … 3357 3493 needs_block = TRUE; 3358 3494 #else 3495 # error Unsupport #define value(s) 3359 3496 needs_block = FALSE; 3360 3497 #endif … … 3373 3510 rc = BOOL_Unmarshal(&s_DAPendingOnNV, buffer, size); 3374 3511 } 3512 #else 3513 # error Unsupport #define value(s) 3375 3514 #endif /* SESSION_PROCESS_C */ 3376 3515 skip_session_process: 3377 3516 3378 #if defined DA_C || defined GLOBAL_C || defined MANUFACTURE_C 3517 #if defined DA_C || defined GLOBAL_C_UNSUPPORTED || defined MANUFACTURE_C 3518 # error Unsupport #define value(s) 3379 3519 needs_block = TRUE; 3380 3520 #else … … 3386 3526 } 3387 3527 3388 #if defined DA_C || defined GLOBAL_C || defined MANUFACTURE_C 3528 #if defined DA_C || defined GLOBAL_C_UNSUPPORTED || defined MANUFACTURE_C 3529 # error Unsupport #define value(s) 3389 3530 #if !ACCUMULATE_SELF_HEAL_TIMER 3390 3531 needs_block = TRUE; … … 3411 3552 needs_block = TRUE; 3412 3553 #else 3554 # error Unsupport #define value(s) 3413 3555 needs_block = FALSE; 3414 3556 #endif … … 3444 3586 * - s_cachedNvRamRef 3445 3587 */ 3588 #else 3589 # error Unsupport #define value(s) 3446 3590 #endif 3447 3591 skip_nv: … … 3450 3594 needs_block = TRUE; 3451 3595 #else 3596 # error Unsupport #define value(s) 3452 3597 needs_block = FALSE; 3453 3598 #endif … … 3470 3615 rc = ANY_OBJECT_Unmarshal(&s_objects[i], buffer, size, true); 3471 3616 } 3617 #else 3618 # error Unsupport #define value(s) 3472 3619 #endif 3473 3620 skip_object: … … 3476 3623 needs_block = TRUE; 3477 3624 #else 3625 # error Unsupport #define value(s) 3478 3626 needs_block = FALSE; 3479 3627 #endif … … 3496 3644 rc = PCR_Unmarshal(&s_pcrs[i], buffer, size, &shadow.pcrAllocated); 3497 3645 } 3646 #else 3647 # error Unsupport #define value(s) 3498 3648 #endif 3499 3649 skip_pcr: … … 3502 3652 needs_block = TRUE; 3503 3653 #else 3654 # error Unsupport #define value(s) 3504 3655 needs_block = FALSE; 3505 3656 #endif … … 3531 3682 rc = UINT32_Unmarshal((UINT32 *)&s_freeSessionSlots, buffer, size); 3532 3683 } 3684 #else 3685 # error Unsupport #define value(s) 3533 3686 #endif 3534 3687 skip_session: … … 3553 3706 needs_block = TRUE; 3554 3707 #else 3708 # error Unsupport #define value(s) 3555 3709 needs_block = FALSE; 3556 3710 #endif … … 3571 3725 rc = UINT32_Unmarshal(&s_failCode, buffer, size); 3572 3726 } 3727 #else 3728 # error Unsupport #define value(s) 3573 3729 #endif 3574 3730 skip_fail: … … 3577 3733 needs_block = TRUE; 3578 3734 #else 3735 # error Unsupport #define value(s) 3579 3736 needs_block = FALSE; 3580 3737 #endif … … 3593 3750 s_tpmTime = tmp_uint64; 3594 3751 } 3752 #else 3753 # error Unsupport #define value(s) 3595 3754 #endif 3596 3755 skip_hardware_clock: … … 3685 3844 static const struct _entry { 3686 3845 UINT32 constant; 3687 c har *name;3846 const char *name; 3688 3847 enum CompareOp { EQ, LE, GE, DONTCARE } cmp; 3689 3848 } pa_compile_constants[] = { … … 3763 3922 { COMPILE_CONSTANT(MAX_ALG_LIST_SIZE, EQ) }, 3764 3923 { COMPILE_CONSTANT(PRIMARY_SEED_SIZE, EQ) }, 3765 #if CONTEXT_ENCRYPT_ALG ORITHM == AES3924 #if CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE 3766 3925 #define CONTEXT_ENCRYPT_ALGORITHM_ TPM_ALG_AES 3767 3926 #endif … … 3784 3943 /* added for PA_COMPILE_CONSTANTS_VERSION == 3 */ 3785 3944 { COMPILE_CONSTANT(AES_128, LE) }, 3786 { COMPILE_CONSTANT( AES_192, LE) },3945 { COMPILE_CONSTANT(0, LE) }, /* was: AES_192; now handled via profile */ 3787 3946 { COMPILE_CONSTANT(AES_256, LE) }, 3788 3947 { COMPILE_CONSTANT(SM4_128, LE) }, 3789 3948 { COMPILE_CONSTANT(ALG_CAMELLIA, LE) }, 3790 3949 { COMPILE_CONSTANT(CAMELLIA_128, LE) }, 3791 { COMPILE_CONSTANT( CAMELLIA_192, LE) },3950 { COMPILE_CONSTANT(0, LE) }, /* was: CAMELLIA_192; now handled via profile */ 3792 3951 { COMPILE_CONSTANT(CAMELLIA_256, LE) }, 3793 3952 { COMPILE_CONSTANT(ALG_SHA3_256, LE) }, … … 3816 3975 { COMPILE_CONSTANT(RH_ACT_F, LE) }, 3817 3976 }; 3977 MUST_BE(CONTEXT_ENCRYPT_ALG == ALG_AES_VALUE); 3818 3978 3819 3979 static TPM_RC … … 3917 4077 break; 3918 4078 default: 3919 /* we don't sup ort anything newer - no downgrade */4079 /* we don't support anything newer - no downgrade */ 3920 4080 TPMLIB_LogTPM2Error("Unsupported PA_COMPILE_CONSTANTS version %d. " 3921 4081 "Supporting up to version %d.\n", … … 3957 4117 } 3958 4118 4119 static UINT16 4120 PERSISTENT_DATA_PPList_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size, 4121 UINT16 blob_version, UINT32 commandCount) 4122 { 4123 UINT8 ppList[(110 + 7) / 8]; 4124 UINT16 array_size; 4125 UINT16 written; 4126 UINT8 *ptr; 4127 4128 assert(!COMPRESSED_LISTS); 4129 if (blob_version <= 4) { 4130 /* Custom profile can get here; v0.9 had 110 commands enabled and 4131 * was using a COMPRESSED_LIST. 4132 */ 4133 assert(commandCount <= 110); 4134 array_size = (commandCount + 7) / 8; 4135 assert(sizeof(ppList) >= array_size); 4136 ConvertToCompressedBitArray(data->ppList, sizeof(data->ppList), 4137 ppList, array_size); 4138 ptr = ppList; 4139 } else { 4140 /* write the array as it is */ 4141 array_size = sizeof(data->ppList); 4142 ptr = data->ppList; 4143 } 4144 written = UINT16_Marshal(&array_size, buffer, size); 4145 written += Array_Marshal(ptr, array_size, buffer, size); 4146 4147 return written; 4148 } 4149 4150 static TPM_RC 4151 PERSISTENT_DATA_PPList_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size, 4152 UINT16 blob_version) 4153 { 4154 TPM_RC rc = TPM_RC_SUCCESS; 4155 UINT16 array_size; 4156 4157 assert(!COMPRESSED_LISTS); 4158 4159 if (rc == TPM_RC_SUCCESS) { 4160 rc = UINT16_Unmarshal(&array_size, buffer, size); 4161 } 4162 if (rc == TPM_RC_SUCCESS) { 4163 #ifndef VBOX 4164 BYTE buf[array_size]; 4165 rc = Array_Unmarshal(buf, array_size, buffer, size); 4166 if (rc == TPM_RC_SUCCESS) { 4167 if (blob_version <= 4) { 4168 /* version <= 4 used COMPRESSED_LISTS and needs to be converted */ 4169 rc = ConvertFromCompressedBitArray(buf, array_size, 4170 data->ppList, sizeof(data->ppList)); 4171 } else { 4172 memset(data->ppList, 0, sizeof(data->ppList)); 4173 assert(array_size <= sizeof(data->ppList)); 4174 memcpy(data->ppList, buf, array_size); 4175 } 4176 } 4177 #else 4178 BYTE *pBuf = (BYTE *)RTMemTmpAlloc(array_size); 4179 if (RT_LIKELY(pBuf)) 4180 { 4181 rc = Array_Unmarshal(pBuf, array_size, buffer, size); 4182 memcpy(data->ppList, pBuf, MIN(array_size, sizeof(data->ppList))); 4183 RTMemTmpFree(pBuf); 4184 } 4185 else 4186 rc = TPM_RC_SIZE; 4187 #endif /* VBOX */ 4188 } 4189 return rc; 4190 } 4191 4192 static UINT16 4193 PERSISTENT_DATA_AuditCommands_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size, 4194 UINT16 blob_version, UINT32 commandCount) 4195 { 4196 UINT8 auditCommands[(110 + 1 + 7) / 8]; 4197 UINT16 array_size; 4198 UINT16 written; 4199 UINT8 *ptr; 4200 4201 assert(!COMPRESSED_LISTS); 4202 4203 if (blob_version <= 4) { 4204 /* Custom profile can get here; v0.9 had 110 commands enabled and 4205 * was using a COMPRESSED_LIST. 4206 */ 4207 assert(commandCount <= 110); 4208 array_size = ((commandCount + 1) + 7) / 8; /* same as in Global.h PERSISTENT_DATA */ 4209 assert(sizeof(auditCommands) >= array_size); 4210 ConvertToCompressedBitArray(data->auditCommands, sizeof(data->auditCommands), 4211 auditCommands, array_size); 4212 ptr = auditCommands; 4213 } else { 4214 /* write the array as it is */ 4215 array_size = sizeof(data->auditCommands); 4216 ptr = data->auditCommands; 4217 } 4218 written = UINT16_Marshal(&array_size, buffer, size); 4219 written += Array_Marshal(ptr, array_size, buffer, size); 4220 4221 return written; 4222 } 4223 4224 static TPM_RC 4225 PERSISTENT_DATA_AuditCommands_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size, 4226 UINT16 blob_version) 4227 { 4228 TPM_RC rc = TPM_RC_SUCCESS; 4229 UINT16 array_size; 4230 4231 assert(!COMPRESSED_LISTS); 4232 4233 if (rc == TPM_RC_SUCCESS) { 4234 rc = UINT16_Unmarshal(&array_size, buffer, size); 4235 } 4236 if (rc == TPM_RC_SUCCESS) { 4237 #ifndef VBOX 4238 BYTE buf[array_size]; 4239 rc = Array_Unmarshal(buf, array_size, buffer, size); 4240 if (rc == TPM_RC_SUCCESS) { 4241 if (blob_version <= 4) { 4242 /* version <= 4 used COMPRESSED_LISTS and needs to be converted */ 4243 rc = ConvertFromCompressedBitArray(buf, array_size, 4244 data->auditCommands, sizeof(data->auditCommands)); 4245 } else { 4246 memset(data->auditCommands, 0, sizeof(data->auditCommands)); 4247 assert(array_size <= sizeof(data->auditCommands)); 4248 memcpy(data->auditCommands, buf, array_size); 4249 } 4250 } 4251 #else 4252 BYTE *pBuf = (BYTE *)RTMemTmpAlloc(array_size); 4253 if (RT_LIKELY(pBuf)) 4254 { 4255 rc = Array_Unmarshal(pBuf, array_size, buffer, size); 4256 memcpy(data->auditCommands, pBuf, MIN(array_size, sizeof(data->auditCommands))); 4257 RTMemTmpFree(pBuf); 4258 } 4259 else 4260 rc = TPM_RC_SIZE; 4261 #endif 4262 } 4263 return rc; 4264 } 4265 4266 3959 4267 #define PERSISTENT_DATA_MAGIC 0x12213443 3960 #define PERSISTENT_DATA_VERSION 44268 #define PERSISTENT_DATA_VERSION 5 3961 4269 3962 4270 static UINT16 3963 PERSISTENT_DATA_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size) 3964 { 4271 PERSISTENT_DATA_Marshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *size, 4272 struct RuntimeProfile *RuntimeProfile) 4273 { 4274 UINT32 commandCount = RuntimeCommandsCountEnabled(&RuntimeProfile->RuntimeCommands); 3965 4275 UINT16 written; 3966 UINT16 array_size;3967 4276 UINT8 clocksize; 3968 4277 BOOL has_block; 3969 4278 BLOCK_SKIP_INIT; 4279 UINT16 blob_version; 4280 4281 switch (RuntimeProfile->stateFormatLevel) { 4282 case 0: 4283 pAssert(FALSE); 4284 break; 4285 case 1 ... 2: 4286 blob_version = 4; 4287 break; 4288 default: 4289 blob_version = 5; /* since stateFormatLevel 3 */ 4290 break; 4291 } 3970 4292 3971 4293 written = NV_HEADER_Marshal(buffer, size, 3972 PERSISTENT_DATA_VERSION, 3973 PERSISTENT_DATA_MAGIC, 4); 4294 blob_version, 4295 PERSISTENT_DATA_MAGIC, blob_version); 4296 // platformReserved (added in v0.10) is not persisted 3974 4297 written += BOOL_Marshal(&data->disableClear, buffer, size); 3975 4298 written += TPM_ALG_ID_Marshal(&data->ownerAlg, buffer, size); … … 4005 4328 written += TPML_PCR_SELECTION_Marshal(&data->pcrAllocated, buffer, size); 4006 4329 4007 /* ppList may grow */ 4008 array_size = sizeof(data->ppList); 4009 written += UINT16_Marshal(&array_size, buffer, size); 4010 written += Array_Marshal(&data->ppList[0], array_size, buffer, size); 4011 4330 written += PERSISTENT_DATA_PPList_Marshal(data, buffer, size, blob_version, commandCount); 4012 4331 written += UINT32_Marshal(&data->failedTries, buffer, size); 4013 4332 written += UINT32_Marshal(&data->maxTries, buffer, size); … … 4017 4336 written += UINT16_Marshal(&data->orderlyState, buffer, size); 4018 4337 4019 /* auditCommands may grow */ 4020 array_size = sizeof(data->auditCommands); 4021 written += UINT16_Marshal(&array_size, buffer, size); 4022 written += Array_Marshal(&data->auditCommands[0], array_size, 4023 buffer, size); 4024 4338 written += PERSISTENT_DATA_AuditCommands_Marshal(data, buffer, size, blob_version, commandCount); 4025 4339 written += TPM_ALG_ID_Marshal(&data->auditHashAlg, buffer, size); 4026 4340 written += UINT64_Marshal(&data->auditCounter, buffer, size); … … 4068 4382 TPM_RC rc = TPM_RC_SUCCESS; 4069 4383 NV_HEADER hdr; 4070 UINT16 array_size;4071 4384 UINT8 clocksize; 4072 4385 BOOL needs_block; … … 4156 4469 } 4157 4470 4158 /* ppList array may not be our size */ 4159 if (rc == TPM_RC_SUCCESS) { 4160 rc = UINT16_Unmarshal(&array_size, buffer, size); 4161 } 4162 if (rc == TPM_RC_SUCCESS) { 4163 #ifndef VBOX 4164 BYTE buf[array_size]; 4165 rc = Array_Unmarshal(buf, array_size, buffer, size); 4166 memcpy(data->ppList, buf, MIN(array_size, sizeof(data->ppList))); 4167 #else 4168 BYTE *pBuf = (BYTE *)RTMemTmpAlloc(array_size); 4169 if (RT_LIKELY(pBuf)) 4170 { 4171 rc = Array_Unmarshal(pBuf, array_size, buffer, size); 4172 memcpy(data->ppList, pBuf, MIN(array_size, sizeof(data->ppList))); 4173 RTMemTmpFree(pBuf); 4174 } 4175 else 4176 rc = TPM_RC_SIZE; 4177 #endif 4471 if (rc == TPM_RC_SUCCESS) { 4472 rc = PERSISTENT_DATA_PPList_Unmarshal(data, buffer, size, hdr.version); 4178 4473 } 4179 4474 … … 4200 4495 /* auditCommands array may not be our size */ 4201 4496 if (rc == TPM_RC_SUCCESS) { 4202 rc = UINT16_Unmarshal(&array_size, buffer, size); 4203 } 4204 if (rc == TPM_RC_SUCCESS) { 4205 #ifndef VBOX 4206 BYTE buf[array_size]; 4207 rc = Array_Unmarshal(buf, array_size, buffer, size); 4208 memcpy(data->auditCommands, buf, 4209 MIN(array_size, sizeof(data->auditCommands))); 4210 #else 4211 BYTE *pBuf = (BYTE *)RTMemTmpAlloc(array_size); 4212 if (RT_LIKELY(pBuf)) 4213 { 4214 rc = Array_Unmarshal(pBuf, array_size, buffer, size); 4215 memcpy(data->auditCommands, pBuf, MIN(array_size, sizeof(data->auditCommands))); 4216 RTMemTmpFree(pBuf); 4217 } 4218 else 4219 rc = TPM_RC_SIZE; 4220 #endif 4221 } 4222 4497 rc = PERSISTENT_DATA_AuditCommands_Unmarshal(data, buffer, size, hdr.version); 4498 } 4223 4499 if (rc == TPM_RC_SUCCESS) { 4224 4500 rc = TPM_ALG_ID_Unmarshal(&data->auditHashAlg, buffer, size); … … 4334 4610 while (TRUE) { 4335 4611 #ifndef VBOX 4336 nrhp = array + offset;4612 nrhp = (NV_RAM_HEADER *)((BYTE *)array + offset); 4337 4613 #else 4338 4614 nrhp = (NV_RAM_HEADER *)((uint8_t *)array + offset); 4339 4615 #endif 4616 4340 4617 /* nrhp may point to misaligned address (ubsan), so use 'nrh'; first access only 'size' */ 4341 4618 memcpy(&nrh, nrhp, sizeof(nrh.size)); … … 4370 4647 /* append the data */ 4371 4648 #ifndef VBOX 4372 written += Array_Marshal( array + offset + sizeof(NV_RAM_HEADER),4649 written += Array_Marshal((BYTE *)array + offset + sizeof(NV_RAM_HEADER), 4373 4650 datasize, buffer, size); 4374 4651 #else … … 4425 4702 */ 4426 4703 #ifndef VBOX 4427 nrhp = array + offset;4704 nrhp = (NV_RAM_HEADER *)((BYTE *)array + offset); 4428 4705 #else 4429 4706 nrhp = (NV_RAM_HEADER *)((uint8_t *)array + offset); … … 4475 4752 /* append the data */ 4476 4753 #ifndef VBOX 4477 rc = Array_Unmarshal( array + offset + sizeof(NV_RAM_HEADER),4754 rc = Array_Unmarshal((BYTE *)array + offset + sizeof(NV_RAM_HEADER), 4478 4755 datasize, buffer, size); 4479 4756 #else … … 4543 4820 /* NV_INDEX has the index again at offset 0! */ 4544 4821 NvReadNvIndexInfo(entryRef + offset, &nvi); 4545 offset += sizeof(nvi);4546 4822 datasize = entrysize - sizeof(UINT32) - sizeof(nvi); 4547 fprintf(stderr, " datasize: %u\n", datasize);4823 fprintf(stderr, " datasize: %u\n", datasize); 4548 4824 break; 4549 break;4550 4825 case TPM_HT_PERSISTENT: 4551 4826 fprintf(stderr, " (PERSISTENT)"); 4552 offset += sizeof(handle); 4553 4554 NvRead(&obj, entryRef + offset, sizeof(obj)); 4555 offset += sizeof(obj); 4556 fprintf(stderr, " sizeof(obj): %zu\n", sizeof(obj)); 4557 break; 4827 4828 NvReadObject(entryRef + offset, &obj); 4829 fprintf(stderr, " sizeof(obj): %zu entrysize: %u\n", sizeof(obj), entrysize); 4830 break; 4831 #if CC_NV_DefineSpace2 4832 # error Missing support for TPM_HT_PERMANENT_NV 4833 #endif 4558 4834 default: 4559 4835 TPMLIB_LogTPM2Error("USER_NVRAM: Corrupted handle: %08x\n", handle); … … 4572 4848 #define USER_NVRAM_MAGIC 0x094f22c3 4573 4849 static UINT32 4574 USER_NVRAM_Marshal(BYTE **buffer, INT32 *size )4850 USER_NVRAM_Marshal(BYTE **buffer, INT32 *size, struct RuntimeProfile *RuntimeProfile) 4575 4851 { 4576 4852 UINT32 written; … … 4632 4908 RTMemTmpFree(pBuf); 4633 4909 } 4634 #endif 4910 #endif /* VBOX */ 4635 4911 } 4636 break;4912 break; 4637 4913 case TPM_HT_PERSISTENT: 4638 offset += sizeof(handle);4639 4640 NvRead(&obj, entryRef + offset, sizeof(obj));4641 offset += sizeof(obj); 4642 written += ANY_OBJECT_Marshal(&obj, buffer, size); 4643 break; 4914 NvReadObject(entryRef + offset, &obj); 4915 written += ANY_OBJECT_Marshal(&obj, buffer, size, RuntimeProfile); 4916 break; 4917 #if CC_NV_DefineSpace2 4918 # error Missing support for TPM_HT_PERMANENT_NV 4919 #endif 4644 4920 default: 4645 4921 TPMLIB_LogTPM2Error("USER_NVRAM: Corrupted handle: %08x\n", handle); … … 4678 4954 UINT32 entrysize; 4679 4955 UINT64 offset, o = 0; 4680 NV_INDEX nvi; 4956 NV_INDEX nvi = { 4957 .publicArea.attributes = 0, // Coverity 4958 }; 4681 4959 UINT64 maxCount; 4682 4960 TPM_HANDLE handle; … … 4704 4982 if (rc == TPM_RC_SUCCESS) { 4705 4983 rc = UINT32_Unmarshal(&entrysize, buffer, size); 4706 4984 } 4985 if (rc == TPM_RC_SUCCESS) { 4707 4986 /* the entrysize also depends on the sizeof(nvi); we may have to 4708 4987 update it if sizeof(nvi) changed between versions */ … … 4712 4991 if (entrysize == 0) 4713 4992 break; 4714 } 4715 /* 2nd: handle */ 4716 if (rc == TPM_RC_SUCCESS) { 4993 4994 /* 2nd: handle */ 4717 4995 rc = TPM_HANDLE_Unmarshal(&handle, buffer, size); 4718 4996 } … … 4729 5007 if (rc == TPM_RC_SUCCESS) { 4730 5008 rc = NV_INDEX_Unmarshal(&nvi, buffer, size); 4731 NvWrite(entryRef + o + offset, sizeof(nvi), &nvi); 4732 offset += sizeof(nvi); 5009 if (rc == TPM_RC_SUCCESS) { 5010 NvWrite(entryRef + o + offset, sizeof(nvi), &nvi); 5011 offset += sizeof(nvi); 5012 } 4733 5013 } 4734 5014 if (rc == TPM_RC_SUCCESS) { … … 4769 5049 entrysize = sizeof(UINT32) + sizeof(nvi) + datasize; 4770 5050 } 4771 break;5051 break; 4772 5052 case TPM_HT_PERSISTENT: 4773 5053 if (rc == TPM_RC_SUCCESS && … … 4779 5059 4780 5060 if (rc == TPM_RC_SUCCESS) { 5061 BYTE objBuffer[MAX_MARSHALLED_OBJECT_SIZE]; 5062 UINT32 marshalledObjectSize; 5063 4781 5064 NvWrite(entryRef + o + offset, sizeof(handle), &handle); 4782 5065 offset += sizeof(TPM_HANDLE); … … 4784 5067 memset(&obj, 0, sizeof(obj)); 4785 5068 rc = ANY_OBJECT_Unmarshal(&obj, buffer, size, true); 4786 NvWrite(entryRef + o + offset, sizeof(obj), &obj); 4787 offset += sizeof(obj); 5069 pAssert(rc == TPM_RC_SUCCESS); 5070 // convert the OBJECT into a buffer to copy into NVRAM 5071 marshalledObjectSize = NvObjectToBuffer(&obj, objBuffer, sizeof(objBuffer)); 5072 NvWrite(entryRef + o + offset, marshalledObjectSize, objBuffer); 5073 offset += marshalledObjectSize; 5074 5075 entrysize = sizeof(UINT32) + sizeof(TPM_HANDLE) + marshalledObjectSize; 4788 5076 } 4789 entrysize = sizeof(UINT32) + sizeof(TPM_HANDLE) + sizeof(obj); 4790 break; 5077 break; 4791 5078 default: 4792 5079 TPMLIB_LogTPM2Error("USER_NVRAM: " … … 4849 5136 * - NVRAM locations (NV_USER_DYNAMIC) 4850 5137 */ 4851 #define PERSISTENT_ALL_VERSION 35138 #define PERSISTENT_ALL_VERSION 4 4852 5139 #define PERSISTENT_ALL_MAGIC 0xab364723 4853 5140 UINT32 4854 5141 PERSISTENT_ALL_Marshal(BYTE **buffer, INT32 *size) 4855 5142 { 5143 struct RuntimeProfile *RuntimeProfile = &g_RuntimeProfile; 5144 const char *profileJSON; 4856 5145 UINT32 magic; 4857 5146 PERSISTENT_DATA pd; … … 4863 5152 BLOCK_SKIP_INIT; 4864 5153 BOOL writeSuState; 5154 UINT16 blob_version; 4865 5155 4866 5156 NvRead(&pd, NV_PERSISTENT_DATA, sizeof(pd)); … … 4872 5162 NvRead(indexOrderlyRam, NV_INDEX_RAM_DATA, sizeof(indexOrderlyRam)); 4873 5163 5164 /* If we previously read state that did not contain a profile then 5165 * we don't want to upgrade the state unnecessarily but still write 5166 * it as v3. 5167 */ 5168 switch (RuntimeProfile->stateFormatLevel) { 5169 case 1: 5170 blob_version = 3; 5171 break; 5172 default: 5173 blob_version = 4; /* since stateFormatLevel 2 */ 5174 break; 5175 } 5176 5177 if (RuntimeProfileWasNullProfile(RuntimeProfile) && blob_version != 3) 5178 assert(false); 5179 else if (!RuntimeProfileWasNullProfile(RuntimeProfile) && blob_version == 3) 5180 assert(false); 5181 4874 5182 written = NV_HEADER_Marshal(buffer, size, 4875 PERSISTENT_ALL_VERSION, 4876 PERSISTENT_ALL_MAGIC, 3); 5183 blob_version, 5184 PERSISTENT_ALL_MAGIC, blob_version); 5185 if (blob_version >= 4) { 5186 profileJSON = RuntimeProfileGetJSON(RuntimeProfile); 5187 assert(profileJSON); 5188 written += String_Marshal(profileJSON, buffer, size); // since v4 5189 } 4877 5190 written += PACompileConstants_Marshal(buffer, size); 4878 written += PERSISTENT_DATA_Marshal(&pd, buffer, size );5191 written += PERSISTENT_DATA_Marshal(&pd, buffer, size, RuntimeProfile); 4879 5192 written += ORDERLY_DATA_Marshal(&od, buffer, size); 4880 5193 writeSuState = (pd.orderlyState & TPM_SU_STATE_MASK) == TPM_SU_STATE; … … 4886 5199 written += INDEX_ORDERLY_RAM_Marshal(indexOrderlyRam, sizeof(indexOrderlyRam), 4887 5200 buffer, size); 4888 written += USER_NVRAM_Marshal(buffer, size );5201 written += USER_NVRAM_Marshal(buffer, size, RuntimeProfile); 4889 5202 4890 5203 written += BLOCK_SKIP_WRITE_PUSH(TRUE, buffer, size); … … 4911 5224 STATE_CLEAR_DATA scd; 4912 5225 BYTE indexOrderlyRam[sizeof(s_indexOrderlyRam)]; 5226 unsigned int stateFormatLevel = 0; // ignored 4913 5227 BOOL readSuState = false; 5228 char *profileJSON = NULL; 4914 5229 4915 5230 memset(&pd, 0, sizeof(pd)); … … 4924 5239 PERSISTENT_ALL_MAGIC); 4925 5240 } 4926 5241 if (rc == TPM_RC_SUCCESS) { 5242 if (hdr.version >= 4) { 5243 rc = String_Unmarshal(&profileJSON, buffer, size); 5244 } 5245 } 5246 if (rc == TPM_RC_SUCCESS) { 5247 /* set the profile read from the state */ 5248 rc = RuntimeProfileSet(&g_RuntimeProfile, profileJSON, false); 5249 } 5250 if (rc == TPM_RC_SUCCESS) { 5251 /* allow all algorithms to be unmarshalled */ 5252 rc = RuntimeAlgorithmSetProfile(&g_RuntimeProfile.RuntimeAlgorithm, NULL, &stateFormatLevel, ~0); 5253 } 4927 5254 if (rc == TPM_RC_SUCCESS) { 4928 5255 rc = PACompileConstants_Unmarshal(buffer, size); … … 4961 5288 if (rc == TPM_RC_SUCCESS && hdr.version >= 2) { 4962 5289 BLOCK_SKIP_READ(skip_future_versions, FALSE, buffer, size, 4963 " USER NVRAM", "version 3 or later");5290 "PERSISTENT_ALL", "version 3 or later"); 4964 5291 /* future versions nest-append here */ 4965 5292 } … … 4978 5305 NvWrite(NV_STATE_CLEAR_DATA, sizeof(scd), &scd); 4979 5306 NvWrite(NV_INDEX_RAM_DATA, sizeof(indexOrderlyRam), indexOrderlyRam); 4980 } 5307 /* Activate a profile read from the state of the TPM 2 */ 5308 rc = RuntimeProfileSet(&g_RuntimeProfile, profileJSON, false); 5309 } 5310 5311 free(profileJSON); 4981 5312 4982 5313 return rc; -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/Platform_fp.h ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: Platform_fp.h 1594 2020-03-26 22:15:48Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 202 0*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 /* C.8 Platform_fp.h */ 63 #ifndef _PLATFORM_FP_H_ 64 #define _PLATFORM_FP_H_ 65 66 #include "BaseTypes.h" 67 68 /* C.8.1. From Cancel.c */ 69 /* C.8.1.1. _plat__IsCanceled() */ 70 /* Check if the cancel flag is set */ 71 /* Return Values Meaning */ 72 /* TRUE(1) if cancel flag is set */ 73 /* FALSE(0) if cancel flag is not set */ 74 LIB_EXPORT int 75 _plat__IsCanceled( 76 void 77 ); 78 /* Set cancel flag. */ 79 LIB_EXPORT void 80 _plat__SetCancel( 81 void 82 ); 83 /* C.8.1.2. _plat__ClearCancel() */ 84 /* Clear cancel flag */ 85 LIB_EXPORT void 86 _plat__ClearCancel( 87 void 88 ); 89 /* C.8.2. From Clock.c */ 90 /* C.8.2.1. _plat__TimerReset() */ 91 /* This function sets current system clock time as t0 for counting TPM time. This function is called 92 at a power on event to reset the clock. When the clock is reset, the indication that the clock 93 was stopped is also set. */ 94 LIB_EXPORT void 95 _plat__TimerReset( 96 void 97 ); 98 /* C.8.2.2. _plat__TimerRestart() */ 99 /* This function should be called in order to simulate the restart of the timer should it be stopped 100 while power is still applied. */ 101 LIB_EXPORT void 102 _plat__TimerRestart( 103 void 104 ); 105 // C.8.2.3. _plat__Time() This is another, probably futile, attempt to define a portable function 106 // that will return a 64-bit clock value that has mSec resolution. 107 LIB_EXPORT uint64_t 108 _plat__RealTime( 109 void 110 ); 111 /* C.8.2.4. _plat__TimerRead() */ 112 /* This function provides access to the tick timer of the platform. The TPM code uses this value to 113 drive the TPM Clock. */ 114 /* The tick timer is supposed to run when power is applied to the device. This timer should not be 115 reset by time events including _TPM_Init(). It should only be reset when TPM power is 116 re-applied. */ 117 /* If the TPM is run in a protected environment, that environment may provide the tick time to the 118 TPM as long as the time provided by the environment is not allowed to go backwards. If the time 119 provided by the system can go backwards during a power discontinuity, then the 120 _plat__Signal_PowerOn() should call _plat__TimerReset(). */ 121 LIB_EXPORT uint64_t 122 _plat__TimerRead( 123 void 124 ); 125 /* C.8.2.5. _plat__TimerWasReset() */ 126 /* This function is used to interrogate the flag indicating if the tick timer has been reset. */ 127 /* If the resetFlag parameter is SET, then the flag will be CLEAR before the function returns. */ 128 LIB_EXPORT int 129 _plat__TimerWasReset( 130 void 131 ); 132 /* C.8.2.6. _plat__TimerWasStopped() */ 133 /* This function is used to interrogate the flag indicating if the tick timer has been stopped. If 134 so, this is typically a reason to roll the nonce. */ 135 /* This function will CLEAR the s_timerStopped flag before returning. This provides functionality 136 that is similar to status register that is cleared when read. This is the model used here because 137 it is the one that has the most impact on the TPM code as the flag can only be accessed by one 138 entity in the TPM. Any other implementation of the hardware can be made to look like a read-once 139 register. */ 140 LIB_EXPORT int 141 _plat__TimerWasStopped( 142 void 143 ); 144 /* C.8.2.7. _plat__ClockAdjustRate() */ 145 /* Adjust the clock rate */ 146 LIB_EXPORT void 147 _plat__ClockAdjustRate( 148 int adjust // IN: the adjust number. It could be positive 149 // or negative 150 ); 151 /* C.8.3. From Entropy.c */ 152 // C.8.4. _plat__GetEntropy() 153 // This function is used to get available hardware entropy. In a hardware implementation of this 154 // function, there would be no call to the system to get entropy. 155 /* Return Values Meaning */ 156 /* < 0 hardware failure of the entropy generator, this is sticky */ 157 /* >= 0 the returned amount of entropy (bytes) */ 158 LIB_EXPORT int32_t 159 _plat__GetEntropy( 160 unsigned char *entropy, // output buffer 161 uint32_t amount // amount requested 162 ); 163 /* C.8.4. From LocalityPlat.c */ 164 /* C.8.4.1. _plat__LocalityGet() */ 165 /* Get the most recent command locality in locality value form. This is an integer value for 166 locality and not a locality structure The locality can be 0-4 or 32-255. 5-31 is not allowed. */ 167 LIB_EXPORT unsigned char 168 _plat__LocalityGet( 169 void 170 ); 171 /* C.8.4.2. _plat__LocalitySet() */ 172 /* Set the most recent command locality in locality value form */ 173 LIB_EXPORT void 174 _plat__LocalitySet( 175 unsigned char locality 176 ); 177 /* C.8.5. From NVMem.c */ 178 #if 0 /* libtpms added */ 179 /* C.8.5.1. _plat__NvErrors() */ 180 /* This function is used by the simulator to set the error flags in the NV subsystem to simulate an error in the NV loading process */ 181 LIB_EXPORT void 182 _plat__NvErrors( 183 int recoverable, 184 int unrecoverable 185 ); 186 #endif /* libtpms added */ 187 /* C.8.5.2. _plat__NVEnable() */ 188 /* Enable NV memory. */ 189 /* This version just pulls in data from a file. In a real TPM, with NV on chip, this function would 190 verify the integrity of the saved context. If the NV memory was not on chip but was in something 191 like RPMB, the NV state would be read in, decrypted and integrity checked. */ 192 /* The recovery from an integrity failure depends on where the error occurred. It it was in the 193 state that is discarded by TPM Reset, then the error is recoverable if the TPM is 194 reset. Otherwise, the TPM must go into failure mode. */ 195 /* Return Values Meaning */ 196 /* 0 if success */ 197 /* > 0 if receive recoverable error */ 198 /* <0 if unrecoverable error */ 199 LIB_EXPORT int 200 _plat__NVEnable( 201 void *platParameter // IN: platform specific parameters 202 ); 203 /* libtpms added begin */ 204 LIB_EXPORT int 205 _plat__NVEnable_NVChipFile( 206 void *platParameter // IN: platform specific parameters 207 ); 208 /* libtpms added end */ 209 /* C.8.5.3. _plat__NVDisable() */ 210 /* Disable NV memory */ 211 LIB_EXPORT void 212 _plat__NVDisable( 213 int delete // IN: If TRUE, delete the NV contents. 214 ); 215 /* C.8.6.4. _plat__IsNvAvailable() */ 216 /* Check if NV is available */ 217 /* Return Values Meaning */ 218 /* 0 NV is available */ 219 /* 1 NV is not available due to write failure */ 220 /* 2 NV is not available due to rate limit */ 221 LIB_EXPORT int 222 _plat__IsNvAvailable( 223 void 224 ); 225 /* C.8.5.5. _plat__NvMemoryRead() */ 226 /* Function: Read a chunk of NV memory */ 227 LIB_EXPORT void 228 _plat__NvMemoryRead( 229 unsigned int startOffset, // IN: read start 230 unsigned int size, // IN: size of bytes to read 231 void *data // OUT: data buffer 232 ); 233 /* C.8.5.6. _plat__NvIsDifferent() */ 234 /* This function checks to see if the NV is different from the test value. This is so that NV will 235 not be written if it has not changed. */ 236 /* Return Values Meaning */ 237 /* TRUE(1) the NV location is different from the test value */ 238 /* FALSE(0) the NV location is the same as the test value */ 239 LIB_EXPORT int 240 _plat__NvIsDifferent( 241 unsigned int startOffset, // IN: read start 242 unsigned int size, // IN: size of bytes to read 243 void *data // IN: data buffer 244 ); 245 /* C.8.5.7. _plat__NvMemoryWrite() */ 246 /* This function is used to update NV memory. The write is to a memory copy of NV. At the end of the 247 current command, any changes are written to the actual NV memory. */ 248 /* NOTE: A useful optimization would be for this code to compare the current contents of NV with the 249 local copy and note the blocks that have changed. Then only write those blocks when 250 _plat__NvCommit() is called. */ 251 LIB_EXPORT int 252 _plat__NvMemoryWrite( 253 unsigned int startOffset, // IN: write start 254 unsigned int size, // IN: size of bytes to write 255 void *data // OUT: data buffer 256 ); 257 /* C.8.6.8. _plat__NvMemoryClear() */ 258 /* Function is used to set a range of NV memory bytes to an implementation-dependent value. The 259 value represents the erase state of the memory. */ 260 LIB_EXPORT void 261 _plat__NvMemoryClear( 262 unsigned int start, // IN: clear start 263 unsigned int size // IN: number of bytes to clear 264 ); 265 /* C.8.5.9. _plat__NvMemoryMove() */ 266 /* Function: Move a chunk of NV memory from source to destination This function should ensure that 267 if there overlap, the original data is copied before it is written */ 268 LIB_EXPORT void 269 _plat__NvMemoryMove( 270 unsigned int sourceOffset, // IN: source offset 271 unsigned int destOffset, // IN: destination offset 272 unsigned int size // IN: size of data being moved 273 ); 274 /* C.8.5.10. _plat__NvCommit() */ 275 // This function writes the local copy of NV to NV for permanent store. It will write NV_MEMORY_SIZE 276 // bytes to NV. If a file is use, the entire file is written. 277 /* Return Values Meaning */ 278 /* 0 NV write success */ 279 /* non-0 NV write fail */ 280 LIB_EXPORT int 281 _plat__NvCommit( 282 void 283 ); 284 /* C.8.5.11. _plat__SetNvAvail() */ 285 /* Set the current NV state to available. This function is for testing purpose only. It is not 286 part of the platform NV logic */ 287 LIB_EXPORT void 288 _plat__SetNvAvail( 289 void 290 ); 291 /* C.8.5.12. _plat__ClearNvAvail() */ 292 /* Set the current NV state to unavailable. This function is for testing purpose only. It is not 293 part of the platform NV logic */ 294 LIB_EXPORT void 295 _plat__ClearNvAvail( 296 void 297 ); 298 299 /* C.6.2.15. _plat__NVNeedsManufacture() */ 300 /* This function is used by the simulator to determine when the TPM's NV state needs to be manufactured. */ 301 302 LIB_EXPORT int 303 _plat__NVNeedsManufacture( 304 void 305 ); 306 307 /* C.8.6. From PowerPlat.c */ 308 /* C.8.6.1. _plat__Signal_PowerOn() */ 309 /* Signal platform power on */ 310 LIB_EXPORT int 311 _plat__Signal_PowerOn( 312 void 313 ); 314 /* C.8.6.2. _plat__WasPowerLost() */ 315 /* Test whether power was lost before a _TPM_Init(). */ 316 /* This function will clear the hardware indication of power loss before return. This means that 317 there can only be one spot in the TPM code where this value gets read. This method is used here 318 as it is the most difficult to manage in the TPM code and, if the hardware actually works this 319 way, it is hard to make it look like anything else. So, the burden is placed on the TPM code 320 rather than the platform code */ 321 /* Return Values Meaning */ 322 /* TRUE(1) power was lost */ 323 /* FALSE(0) power was not lost */ 324 LIB_EXPORT int 325 _plat__WasPowerLost( 326 void 327 ); 328 /* C.8.6.3. _plat_Signal_Reset() */ 329 /* This a TPM reset without a power loss. */ 330 LIB_EXPORT int 331 _plat__Signal_Reset( 332 void 333 ); 334 /* C.8.6.4. _plat__Signal_PowerOff() */ 335 /* Signal platform power off */ 336 LIB_EXPORT void 337 _plat__Signal_PowerOff( 338 void 339 ); 340 341 /* C.8.7. From PPPlat.c */ 342 /* C.8.7.1. _plat__PhysicalPresenceAsserted() */ 343 /* Check if physical presence is signaled */ 344 /* Return Values Meaning */ 345 /* TRUE(1) if physical presence is signaled */ 346 /* FALSE(0) if physical presence is not signaled */ 347 LIB_EXPORT int 348 _plat__PhysicalPresenceAsserted( 349 void 350 ); 351 #if 0 /* libtpms added */ 352 /* C.8.7.2. _plat__Signal_PhysicalPresenceOn() */ 353 /* Signal physical presence on */ 354 LIB_EXPORT void 355 _plat__Signal_PhysicalPresenceOn( 356 void 357 ); 358 /* C.8.7.3. _plat__Signal_PhysicalPresenceOff() */ 359 /* Signal physical presence off */ 360 LIB_EXPORT void 361 _plat__Signal_PhysicalPresenceOff( 362 void 363 ); 364 #endif /* libtpms added */ 365 366 /* C.8.8. From RunCommand.c */ 367 /* C.8.8.1. _plat__RunCommand() */ 368 /* This version of RunCommand() will set up a jum_buf and call ExecuteCommand(). If the command 369 executes without failing, it will return and RunCommand() will return. If there is a failure in 370 the command, then _plat__Fail() is called and it will longjump back to RunCommand() which will 371 call ExecuteCommand() again. However, this time, the TPM will be in failure mode so 372 ExecuteCommand() will simply build a failure response and return. */ 373 LIB_EXPORT void 374 _plat__RunCommand( 375 uint32_t requestSize, // IN: command buffer size 376 unsigned char *request, // IN: command buffer 377 uint32_t *responseSize, // IN/OUT: response buffer size 378 unsigned char **response // IN/OUT: response buffer 379 ); 380 /* C.8.8.2. _plat__Fail() */ 381 /* This is the platform depended failure exit for the TPM. */ 382 LIB_EXPORT NORETURN void 383 _plat__Fail( 384 void 385 ); 386 387 /* C.8.9. From Unique.c */ 388 /* C.8.9.1 _plat__GetUnique() */ 389 /* This function is used to access the platform-specific unique value. This function places the 390 unique value in the provided buffer (b) and returns the number of bytes transferred. The function 391 will not copy more data than bSize. */ 392 /* NOTE: If a platform unique value has unequal distribution of uniqueness and bSize is smaller than 393 the size of the unique value, the bSize portion with the most uniqueness should be returned. */ 394 LIB_EXPORT uint32_t 395 _plat__GetUnique( 396 uint32_t which, // authorities (0) or details 397 uint32_t bSize, // size of the buffer 398 unsigned char *b // output buffer 399 ); 400 401 /* libtpms added begin */ 402 #ifndef VBOX 403 #include <time.h> 404 #else 405 #undef CLOCK_REALTIME 406 #undef CLOCK_MONOTONIC 407 typedef enum 408 { 409 CLOCK_REALTIME = 0, 410 CLOCK_MONOTONIC 411 } TPM_CLOCK_ID; 412 #endif 413 void ClockAdjustPostResume(UINT64 backthen, BOOL timesAreRealtime); 414 #ifndef VBOX 415 uint64_t ClockGetTime(clockid_t clk_id); 416 #else 417 uint64_t ClockGetTime(TPM_CLOCK_ID clk_id); 418 #endif 419 /* libtpms added end */ 420 421 #endif // _PLATFORM_FP_H_ 61 #include "platform_public_interface.h" // libtpms added -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/TPMCmdp.c ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: TPMCmdp.c 1658 2021-01-22 23:14:01Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2016 - 202 1*/57 /* (c) Copyright IBM Corp. and others, 2016 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 61 //** Description 62 // This file contains the functions that process the commands received on the 63 // control port or the command port of the simulator. The control port is used 64 // to allow simulation of hardware events (such as, _TPM_Hash_Start) to test 65 // the simulated TPM's reaction to those events. This improves code coverage 66 // of the testing. 67 68 //** Includes and Data Definitions 69 #include "simulatorPrivate.h" 70 71 static bool s_isPowerOn = false; 72 73 //** Functions 74 75 //*** Signal_PowerOn() 76 // This function processes a power-on indication. Among other things, it 77 // calls the _TPM_Init() handler. 78 void _rpc__Signal_PowerOn(bool isReset) 79 { 80 // if power is on and this is not a call to do TPM reset then return 81 if(s_isPowerOn && !isReset) 82 return; 83 // If this is a reset but power is not on, then return 84 if(isReset && !s_isPowerOn) 85 return; 86 // Unless this is just a reset, pass power on signal to platform 87 if(!isReset) 88 _plat__Signal_PowerOn(); 89 // Power on and reset both lead to _TPM_Init() 90 _plat__Signal_Reset(); 91 92 // Set state as power on 93 s_isPowerOn = true; 94 } 95 96 #if 0 /* libtpms added */ 97 //*** Signal_Restart() 98 // This function processes the clock restart indication. All it does is call 99 // the platform function. 100 void _rpc__Signal_Restart(void) 101 { 102 _plat__TimerRestart(); 103 } 104 #endif /* libtpms added */ 105 106 //***Signal_PowerOff() 107 // This function processes the power off indication. Its primary function is 108 // to set a flag indicating that the next power on indication should cause 109 // _TPM_Init() to be called. 110 void _rpc__Signal_PowerOff(void) 111 { 112 if(s_isPowerOn) 113 // Pass power off signal to platform 114 _plat__Signal_PowerOff(); 115 // This could be redundant, but... 116 s_isPowerOn = false; 117 118 return; 119 } 120 #if 0 /* libtpms added */ 121 122 //*** _rpc__ForceFailureMode() 123 // This function is used to debug the Failure Mode logic of the TPM. It will set 124 // a flag in the TPM code such that the next call to TPM2_SelfTest() will result 125 // in a failure, putting the TPM into Failure Mode. 126 void _rpc__ForceFailureMode(void) 127 { 128 #if SIMULATION 129 SetForceFailureMode(); 130 #endif 131 return; 132 } 133 134 //*** _rpc__Signal_PhysicalPresenceOn() 135 // This function is called to simulate activation of the physical presence "pin". 136 void _rpc__Signal_PhysicalPresenceOn(void) 137 { 138 // If TPM power is on... 139 if(s_isPowerOn) 140 // ... pass physical presence on to platform 141 _plat__Signal_PhysicalPresenceOn(); 142 return; 143 } 144 145 //*** _rpc__Signal_PhysicalPresenceOff() 146 // This function is called to simulate deactivation of the physical presence "pin". 147 void _rpc__Signal_PhysicalPresenceOff(void) 148 { 149 // If TPM is power on... 150 if(s_isPowerOn) 151 // ... pass physical presence off to platform 152 _plat__Signal_PhysicalPresenceOff(); 153 return; 154 } 155 156 //*** _rpc__Signal_Hash_Start() 157 // This function is called to simulate a _TPM_Hash_Start event. It will call 158 // 159 void _rpc__Signal_Hash_Start(void) 160 { 161 // If TPM power is on... 162 if(s_isPowerOn) 163 // ... pass _TPM_Hash_Start signal to TPM 164 _TPM_Hash_Start(); 165 return; 166 } 167 168 //*** _rpc__Signal_Hash_Data() 169 // This function is called to simulate a _TPM_Hash_Data event. 170 void _rpc__Signal_Hash_Data(_IN_BUFFER input) 171 { 172 // If TPM power is on... 173 if(s_isPowerOn) 174 // ... pass _TPM_Hash_Data signal to TPM 175 _TPM_Hash_Data(input.BufferSize, input.Buffer); 176 return; 177 } 178 179 //*** _rpc__Signal_HashEnd() 180 // This function is called to simulate a _TPM_Hash_End event. 181 void _rpc__Signal_HashEnd(void) 182 { 183 // If TPM power is on... 184 if(s_isPowerOn) 185 // ... pass _TPM_HashEnd signal to TPM 186 _TPM_Hash_End(); 187 return; 188 } 189 #endif /* libtpms added */ 190 191 //*** _rpc__Send_Command() 192 // This is the interface to the TPM code. 193 // Return Type: void 194 void _rpc__Send_Command( 195 unsigned char locality, _IN_BUFFER request, _OUT_BUFFER* response) 196 { 197 // If TPM is power off, reject any commands. 198 if(!s_isPowerOn) 199 { 200 response->BufferSize = 0; 201 return; 202 } 203 // Set the locality of the command so that it doesn't change during the command 204 _plat__LocalitySet(locality); 205 // Do implementation-specific command dispatch 206 _plat__RunCommand( 207 request.BufferSize, request.Buffer, &response->BufferSize, &response->Buffer); 208 return; 209 } 210 211 //*** _rpc__Signal_CancelOn() 212 // This function is used to turn on the indication to cancel a command in process. 213 // An executing command is not interrupted. The command code may periodically check 214 // this indication to see if it should abort the current command processing and 215 // returned TPM_RC_CANCELLED. 216 void _rpc__Signal_CancelOn(void) 217 { 218 // If TPM power is on... 219 if(s_isPowerOn) 220 // ... set the platform canceling flag. 221 _plat__SetCancel(); 222 return; 223 } 224 225 //*** _rpc__Signal_CancelOff() 226 // This function is used to turn off the indication to cancel a command in process. 227 void _rpc__Signal_CancelOff(void) 228 { 229 // If TPM power is on... 230 if(s_isPowerOn) 231 // ... set the platform canceling flag. 232 _plat__ClearCancel(); 233 return; 234 } 235 236 //*** _rpc__Signal_NvOn() 237 // In a system where the NV memory used by the TPM is not within the TPM, the 238 // NV may not always be available. This function turns on the indicator that 239 // indicates that NV is available. 240 void _rpc__Signal_NvOn(void) 241 { 242 // If TPM power is on... 243 if(s_isPowerOn) 244 // ... make the NV available 245 _plat__SetNvAvail(); 246 return; 247 } 248 #if 0 /* libtpms added */ 249 250 //*** _rpc__Signal_NvOff() 251 // This function is used to set the indication that NV memory is no 252 // longer available. 253 void _rpc__Signal_NvOff(void) 254 { 255 // If TPM power is on... 256 if(s_isPowerOn) 257 // ... make NV not available 258 _plat__ClearNvAvail(); 259 return; 260 } 261 262 void RsaKeyCacheControl(int state); 263 264 //*** _rpc__RsaKeyCacheControl() 265 // This function is used to enable/disable the use of the RSA key cache during 266 // simulation. 267 void _rpc__RsaKeyCacheControl(int state) 268 { 269 #if USE_RSA_KEY_CACHE 270 RsaKeyCacheControl(state); 271 #else 272 NOT_REFERENCED(state); 273 #endif 274 return; 275 } 276 277 //*** _rpc__ACT_GetSignaled() 278 // This function is used to count the ACT second tick. 279 bool _rpc__ACT_GetSignaled(uint32_t actHandle) 280 { 281 #if ACT_SUPPORT 282 // If TPM power is on... 283 if(s_isPowerOn) 284 // ... query the platform 285 return _plat__ACT_GetSignaled(actHandle - TPM_RH_ACT_0); 286 #else // ACT_SUPPORT 287 NOT_REFERENCED(actHandle); 288 #endif // ACT_SUPPORT 289 return false; 290 } 291 292 //*** _rpc__SetTpmFirmwareHash() 293 // This function is used to modify the firmware's hash during simulation. 294 void _rpc__SetTpmFirmwareHash(uint32_t hash) 295 { 296 #if SIMULATION 297 _plat__SetTpmFirmwareHash(hash); 298 #endif 299 } 300 301 //*** _rpc__SetTpmFirmwareSvn() 302 // This function is used to modify the firmware's SVN during simulation. 303 void _rpc__SetTpmFirmwareSvn(uint16_t svn) 304 { 305 #if SIMULATION 306 _plat__SetTpmFirmwareSvn(svn); 307 #endif 308 } 309 #endif /* libtpms added */ 310 311 /* libtpms added begin */ 312 static bool tpmEstablished; 313 314 void _rpc__Signal_SetTPMEstablished(void) 315 { 316 tpmEstablished = TRUE; 317 } 318 319 void _rpc__Signal_ResetTPMEstablished(void) 320 { 321 /* check for locality 3 or 4 already done by caller */ 322 tpmEstablished = FALSE; 323 } 324 325 bool _rpc__Signal_GetTPMEstablished(void) 326 { 327 return tpmEstablished; 328 } 329 330 bool _rpc__Signal_IsPowerOn(void) 331 { 332 return s_isPowerOn; 333 } 334 /* libtpms added end */ 335 336 #if 0 62 337 /* D.4 TPMCmdp.c */ 63 338 /* D.4.1. Description */ … … 77 352 #include <winsock.h> 78 353 #endif 79 #endif 80 #include "Platform_fp.h"354 #endif /* VBOX*/ 355 //#include "Platform_fp.h" 81 356 #include "PlatformACT_fp.h" 82 357 #include "ExecCommand_fp.h" … … 96 371 #include "TcpServerPosix_fp.h" /* kgold */ 97 372 #endif 98 #endif 373 #endif /* VBOX */ 99 374 #include "TpmProfile.h" /* kgold */ 100 375 101 static bool s_isPowerOn = false; 102 /* D.4.3. Functions */ 103 /* D.4.3.1. Signal_PowerOn() */ 104 /* This function processes a power-on indication. Among other things, it calls the _TPM_Init() 105 handler. */ 106 void 107 _rpc__Signal_PowerOn( 108 bool isReset 109 ) 110 { 111 // if power is on and this is not a call to do TPM reset then return 112 if(s_isPowerOn && !isReset) 113 return; 114 // If this is a reset but power is not on, then return 115 if(isReset && !s_isPowerOn) 116 return; 117 // Unless this is just a reset, pass power on signal to platform 118 if(!isReset) 119 _plat__Signal_PowerOn(); 120 // Power on and reset both lead to _TPM_Init() 121 _plat__Signal_Reset(); 122 // Set state as power on 123 s_isPowerOn = true; 124 } 125 #if 0 /* libtpms added */ 126 /* D.4.3.2. Signal_Restart() */ 127 /* This function processes the clock restart indication. All it does is call the platform 128 function. */ 129 void 130 _rpc__Signal_Restart( 131 void 132 ) 133 { 134 _plat__TimerRestart(); 135 } 136 #endif /* libtpms added */ 137 /* D.4.3.3. Signal_PowerOff() */ 138 /* This function processes the power off indication. Its primary function is to set a flag 139 indicating that the next power on indication should cause _TPM_Init() to be called. */ 140 void 141 _rpc__Signal_PowerOff( 142 void 143 ) 144 { 145 if(s_isPowerOn) 146 // Pass power off signal to platform 147 _plat__Signal_PowerOff(); 148 // This could be redundant, but... 149 s_isPowerOn = false; 150 return; 151 } 152 #if 0 /* libtpms added */ 153 /* D.4.3.4. _rpc__ForceFailureMode() */ 154 /* This function is used to debug the Failure Mode logic of the TPM. It will set a flag in the TPM 155 code such that the next call to TPM2_SelfTest() will result in a failure, putting the TPM into 156 Failure Mode. */ 157 void 158 _rpc__ForceFailureMode( 159 void 160 ) 161 { 162 SetForceFailureMode(); 163 return; 164 } 165 /* D.4.3.5. _rpc__Signal_PhysicalPresenceOn() */ 166 /* This function is called to simulate activation of the physical presence pin. */ 167 void 168 _rpc__Signal_PhysicalPresenceOn( 169 void 170 ) 171 { 172 // If TPM power is on 173 if(s_isPowerOn) 174 // Pass physical presence on to platform 175 _plat__Signal_PhysicalPresenceOn(); 176 return; 177 } 178 /* D.4.3.6. _rpc__Signal_PhysicalPresenceOff() */ 179 /* This function is called to simulate deactivation of the physical presence pin. */ 180 void 181 _rpc__Signal_PhysicalPresenceOff( 182 void 183 ) 184 { 185 // If TPM power is on 186 if(s_isPowerOn) 187 // Pass physical presence off to platform 188 _plat__Signal_PhysicalPresenceOff(); 189 return; 190 } 191 /* D.4.3.7. _rpc__Signal_Hash_Start() */ 192 /* This function is called to simulate a _TPM_Hash_Start() event. It will call */ 193 void 194 _rpc__Signal_Hash_Start( 195 void 196 ) 197 { 198 // If TPM power is on 199 if(s_isPowerOn) 200 // Pass _TPM_Hash_Start signal to TPM 201 _TPM_Hash_Start(); 202 return; 203 } 204 /* D.4.3.8. _rpc__Signal_Hash_Data() */ 205 /* This function is called to simulate a _TPM_Hash_Data() event. */ 206 void 207 _rpc__Signal_Hash_Data( 208 _IN_BUFFER input 209 ) 210 { 211 // If TPM power is on 212 if(s_isPowerOn) 213 // Pass _TPM_Hash_Data signal to TPM 214 _TPM_Hash_Data(input.BufferSize, input.Buffer); 215 return; 216 } 217 /* D.4.3.9. _rpc__Signal_HashEnd() */ 218 /* This function is called to simulate a _TPM_Hash_End() event. */ 219 void 220 _rpc__Signal_HashEnd( 221 void 222 ) 223 { 224 // If TPM power is on 225 if(s_isPowerOn) 226 // Pass _TPM_HashEnd signal to TPM 227 _TPM_Hash_End(); 228 return; 229 } 230 #endif /* libtpms added */ 231 /* D.4.3.10. rpc_Send_Command() */ 232 /* This is the interface to the TPM code. */ 233 void 234 _rpc__Send_Command( 235 unsigned char locality, 236 _IN_BUFFER request, 237 _OUT_BUFFER *response 238 ) 239 { 240 // If TPM is power off, reject any commands. 241 if(!s_isPowerOn) 242 { 243 response->BufferSize = 0; 244 return; 245 } 246 // Set the locality of the command so that it doesn't change during the command 247 _plat__LocalitySet(locality); 248 // Do implementation-specific command dispatch 249 _plat__RunCommand(request.BufferSize, request.Buffer, 250 &response->BufferSize, &response->Buffer); 251 return; 252 } 253 /* D.4.3.10. _rpc__Signal_CancelOn() */ 254 /* This function is used to turn on the indication to cancel a command in process. An executing 255 command is not interrupted. The command code may periodically check this indication to see if it 256 should abort the current command processing and returned TPM_RC_CANCELLED. */ 257 void 258 _rpc__Signal_CancelOn( 259 void 260 ) 261 { 262 // If TPM is power off, reject this signal 263 if(s_isPowerOn) 264 // Set the platform canceling flag. 265 _plat__SetCancel(); 266 return; 267 } 268 /* D.4.3.11. _rpc__Signal_CancelOff() */ 269 /* This function is used to turn off the indication to cancel a command in process. */ 270 void 271 _rpc__Signal_CancelOff( 272 void 273 ) 274 { 275 // If TPM power is n 276 if(s_isPowerOn) 277 // Set the platform canceling flag. 278 _plat__ClearCancel(); 279 return; 280 } 281 /* D.4.3.12. _rpc__Signal_NvOn() */ 282 /* In a system where the NV memory used by the TPM is not within the TPM, the NV may not always be 283 available. This function turns on the indicator that indicates that NV is available. */ 284 void 285 _rpc__Signal_NvOn( 286 void 287 ) 288 { 289 // If TPM power is on 290 if(s_isPowerOn) 291 // Make the NV available 292 _plat__SetNvAvail(); 293 return; 294 } 295 #if 0 /* libtpms added */ 296 /* D.4.3.13. _rpc__Signal_NvOff() */ 297 /* This function is used to set the indication that NV memory is no longer available. */ 298 void 299 _rpc__Signal_NvOff( 300 void 301 ) 302 { 303 // If TPM power is on 304 if(s_isPowerOn) 305 // Make NV not available 306 _plat__ClearNvAvail(); 307 return; 308 } 309 void RsaKeyCacheControl(int state); 310 /* D.4.3.14. _rpc__RsaKeyCacheControl() */ 311 /* This function is used to enable/disable the use of the RSA key cache during simulation. */ 312 void 313 _rpc__RsaKeyCacheControl( 314 int state 315 ) 316 { 317 #if USE_RSA_KEY_CACHE 318 RsaKeyCacheControl(state); 319 #else 320 NOT_REFERENCED(state); 321 #endif 322 } 376 377 323 378 324 379 #define TPM_RH_ACT_0 0x40000110 325 380 326 /* D.4.2.15. _rpc__ACT_GetSignaled() */ 327 /* This function is used to count the ACT second tick. */ 328 bool 329 _rpc__ACT_GetSignaled( 330 uint32_t actHandle 331 ) 332 { 333 // If TPM power is on 334 if (s_isPowerOn) 335 // Query the platform 336 return _plat__ACT_GetSignaled(actHandle - TPM_RH_ACT_0); 337 return false; 338 } 339 #endif /* libtpms added */ 340 341 /* libtpms added begin */ 342 static bool tpmEstablished; 343 344 void 345 _rpc__Signal_SetTPMEstablished(void) 346 { 347 tpmEstablished = TRUE; 348 } 349 350 void 351 _rpc__Signal_ResetTPMEstablished(void) 352 { 353 /* check for locality 3 or 4 already done by caller */ 354 tpmEstablished = FALSE; 355 } 356 357 bool 358 _rpc__Signal_GetTPMEstablished(void) 359 { 360 return tpmEstablished; 361 } 362 363 bool 364 _rpc__Signal_IsPowerOn(void) 365 { 366 return s_isPowerOn; 367 } 368 /* libtpms added end */ 381 382 #endif -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/TpmProfile.h ¶
r108932 r108933 4 4 /* Written by Ken Goldman */ 5 5 /* IBM Thomas J. Watson Research Center */ 6 /* $Id: TpmProfile.h 1629 2020-06-01 20:50:13Z kgoldman $ */7 6 /* */ 8 7 /* Licenses and Notices */ … … 56 55 /* information herein. */ 57 56 /* */ 58 /* (c) Copyright IBM Corp. and others, 2019 - 202 0*/57 /* (c) Copyright IBM Corp. and others, 2019 - 2023 */ 59 58 /* */ 60 59 /********************************************************************************/ 61 60 62 // FOR LIBTPMS: DO NOT EDIT THIS FILE!61 // FOR LIBTPMS: DO NOT EDIT THIS or INCLUDED FILES! 63 62 // ANY MODIFICATION WILL LEAD TO AN UNSUPPORTED CONFIGURATION 64 63 65 // A.2 TpmProfile.h 64 // The primary configuration file that collects all configuration options for a 65 // TPM build. 66 66 #ifndef _TPM_PROFILE_H_ 67 67 #define _TPM_PROFILE_H_ 68 // Table 2:4 - Defines for Logic Values69 #undef TRUE70 #define TRUE 171 #undef FALSE72 #define FALSE 073 #undef YES74 #define YES 175 #undef NO76 #define NO 077 #undef SET78 #define SET 179 #undef CLEAR80 #define CLEAR 081 // Table 0:1 - Defines for Processor Values82 #ifndef VBOX83 #if defined __FreeBSD__ || defined __DragonFly__ /* libtpms added begin */84 # include <sys/endian.h>85 #elif defined __APPLE__86 # include <libkern/OSByteOrder.h>87 #else88 # include <endian.h>89 #endif90 #if defined __linux__ || defined __CYGWIN__91 #if __BYTE_ORDER == __LITTLE_ENDIAN92 #define BIG_ENDIAN_TPM NO93 #endif94 #if __BYTE_ORDER == __BIG_ENDIAN95 #define BIG_ENDIAN_TPM YES96 #endif97 #elif defined __OpenBSD__ || defined __FreeBSD__ || defined __NetBSD__ \98 || defined __DragonFly__99 #if _BYTE_ORDER == _LITTLE_ENDIAN100 #define BIG_ENDIAN_TPM NO101 #endif102 #if _BYTE_ORDER == _BIG_ENDIAN103 #define BIG_ENDIAN_TPM YES104 #endif105 #elif defined __APPLE__106 #define BIG_ENDIAN_TPM NO107 #else108 #error Unsupported OS109 #endif /* libtpms added end */110 #else /* !VBOX */111 # include <iprt/cdefs.h>112 # ifdef RT_LITTLE_ENDIAN113 # define BIG_ENDIAN_TPM NO114 # else115 # define BIG_ENDIAN_TPM YES116 # endif117 #endif118 #ifndef BIG_ENDIAN_TPM119 #define BIG_ENDIAN_TPM NO120 #endif121 #ifndef LITTLE_ENDIAN_TPM122 #define LITTLE_ENDIAN_TPM !BIG_ENDIAN_TPM123 #endif124 #ifndef MOST_SIGNIFICANT_BIT_0125 #define MOST_SIGNIFICANT_BIT_0 NO126 #endif127 #ifndef LEAST_SIGNIFICANT_BIT_0128 #define LEAST_SIGNIFICANT_BIT_0 !MOST_SIGNIFICANT_BIT_0129 #endif130 #ifndef AUTO_ALIGN131 #define AUTO_ALIGN NO132 #endif133 // Table 0:4 - Defines for Implemented Curves134 #ifndef ECC_NIST_P192135 #define ECC_NIST_P192 YES /* libtpms enabled */136 #endif137 #ifndef ECC_NIST_P224138 #define ECC_NIST_P224 YES /* libtpms enabled */139 #endif140 #ifndef ECC_NIST_P256141 #define ECC_NIST_P256 YES142 #endif143 #ifndef ECC_NIST_P384144 #define ECC_NIST_P384 YES145 #endif146 #ifndef ECC_NIST_P521147 #define ECC_NIST_P521 YES /* libtpms enabled */148 #endif149 #ifndef ECC_BN_P256150 #define ECC_BN_P256 YES151 #endif152 #ifndef ECC_BN_P638153 #define ECC_BN_P638 YES /* libtpms enabled */154 #endif155 #ifndef ECC_SM2_P256156 #define ECC_SM2_P256 YES /* libtpms enabled */157 #endif158 68 159 /* Table 0:6 - Defines for Implemented ACT */ 69 #include "TpmBuildSwitches.h" 70 #include "TpmProfile_Common.h" 71 #include "TpmProfile_CommandList.h" 72 #include "TpmProfile_Misc.h" 73 #include "TpmProfile_ErrorCodes.h" 74 #include "VendorInfo.h" 160 75 161 #ifndef RH_ACT_0 162 #define RH_ACT_0 NO /* libtpms: no */ 163 #endif 164 #ifndef RH_ACT_1 165 #define RH_ACT_1 NO 166 #endif 167 #ifndef RH_ACT_A 168 #define RH_ACT_A NO /* libtpms: no */ 169 #endif 170 // libtpms added begin 171 #if RH_ACT_0 + RH_ACT_1 + RH_ACT_2 + RH_ACT_3 + RH_ACT_4 + \ 172 RH_ACT_1 + RH_ACT_5 + RH_ACT_6 + RH_ACT_7 + RH_ACT_8 + \ 173 RH_ACT_9 + RH_ACT_A + RH_ACT_B + RH_ACT_C + RH_ACT_D + \ 174 RH_ACT_E + RH_ACT_F == 0 175 #define __ACT_DISABLED 176 #endif 177 // libtpms added end 178 179 // Table 0:7 - Defines for Implementation Values 180 #ifndef FIELD_UPGRADE_IMPLEMENTED 181 #define FIELD_UPGRADE_IMPLEMENTED NO 182 #endif 183 184 #ifndef VBOX 185 #ifdef TPM_POSIX // libtpms added begin 186 # include <openssl/bn.h> 187 # ifdef THIRTY_TWO_BIT 188 # define RADIX_BITS 32 189 # endif 190 # ifdef SIXTY_FOUR_BIT_LONG 191 # define RADIX_BITS 64 192 # endif 193 # ifndef RADIX_BITS 194 # error Need to determine RADIX_BITS value 195 # endif 196 #endif 197 #ifdef TPM_WINDOWS 198 #define RADIX_BITS 32 199 #endif // libtpms added end 200 #else 201 # include <iprt/cdefs.h> 202 # if defined(RT_ARCH_AMD64) || defined(RT_ARCH_SPARC64) || defined(RT_ARCH_ARM64) 203 # define RADIX_BITS 64 204 # elif defined(RT_ARCH_X86) || defined(RT_ARCH_SPARC) || defined(RT_ARCH_ARM32) 205 # define RADIX_BITS 32 206 # else 207 # error "Unknown/missing RT_ARCH_*." /* vbox: 64-bit (cannot safely use ARCH_BITS without including iprt/cdefs.h) */ 208 # endif 209 #endif 210 76 // libtpms: added begin 211 77 #ifndef HASH_LIB 212 78 #define HASH_LIB Ossl … … 216 82 #endif 217 83 #ifndef MATH_LIB 218 #define MATH_LIB Ossl84 #define MATH_LIB TpmBigNum 219 85 #endif 220 #ifndef IMPLEMENTATION_PCR221 #define IMPLEMENTATION_PCR 2486 #ifndef BN_MATH_LIB 87 #define BN_MATH_LIB Ossl 222 88 #endif 223 #ifndef PLATFORM_PCR 224 #define PLATFORM_PCR 24 225 #endif 226 #ifndef DRTM_PCR 227 #define DRTM_PCR 17 228 #endif 229 #ifndef HCRTM_PCR 230 #define HCRTM_PCR 0 231 #endif 232 #ifndef NUM_LOCALITIES 233 #define NUM_LOCALITIES 5 234 #endif 235 #ifndef MAX_HANDLE_NUM 236 #define MAX_HANDLE_NUM 3 237 #endif 238 #ifndef MAX_ACTIVE_SESSIONS 239 #define MAX_ACTIVE_SESSIONS 64 240 #endif 241 #ifndef CONTEXT_SLOT 242 #define CONTEXT_SLOT UINT16 /* libtpms: changed from UINT8 in v0.9.0 */ 243 #endif 244 #ifndef MAX_LOADED_SESSIONS 245 #define MAX_LOADED_SESSIONS 3 246 #endif 247 #ifndef MAX_SESSION_NUM 248 #define MAX_SESSION_NUM 3 249 #endif 250 #ifndef MAX_LOADED_OBJECTS 251 #define MAX_LOADED_OBJECTS 3 252 #endif 253 #ifndef MIN_EVICT_OBJECTS 254 #define MIN_EVICT_OBJECTS 7 /* libtpms changed -- for PC profile */ 255 #endif 256 #ifndef NUM_POLICY_PCR_GROUP 257 #define NUM_POLICY_PCR_GROUP 1 258 #endif 259 #ifndef NUM_AUTHVALUE_PCR_GROUP 260 #define NUM_AUTHVALUE_PCR_GROUP 1 261 #endif 262 #ifndef MAX_CONTEXT_SIZE 263 #define MAX_CONTEXT_SIZE 2680 /* libtpms changed */ 264 #endif 265 #ifndef MAX_DIGEST_BUFFER 266 #define MAX_DIGEST_BUFFER 1024 267 #endif 268 #ifndef MAX_NV_INDEX_SIZE 269 #define MAX_NV_INDEX_SIZE 2048 270 #endif 271 #ifndef MAX_NV_BUFFER_SIZE 272 #define MAX_NV_BUFFER_SIZE 1024 273 #endif 274 #ifndef MAX_CAP_BUFFER 275 #define MAX_CAP_BUFFER 1024 276 #endif 89 // libtpms: added end 277 90 278 /* for PC client, permits 279 280 1300 bytes reserved 281 7 * 2600 persistent objects 282 4000 NV indexes 283 60 * 68 nv index metadata 284 */ 285 286 #ifndef NV_MEMORY_SIZE 287 /* libtmps: 65 OBJECTs in USER NVRAM expanded by 704 bytes due to size 288 * increase of OBJECT from 2048 bit RSA keys to 3072 bit by 704 bytes*/ 289 #define NV_MEMORY_SIZE (128 * 1024 + 65 * 704) /* libtpms changed */ 290 #endif 291 #ifndef MIN_COUNTER_INDICES 292 #define MIN_COUNTER_INDICES 8 293 #endif 294 #ifndef NUM_STATIC_PCR 295 #define NUM_STATIC_PCR 16 296 #endif 297 #ifndef MAX_ALG_LIST_SIZE 298 #define MAX_ALG_LIST_SIZE 64 299 #endif 300 #ifndef PRIMARY_SEED_SIZE 301 #define PRIMARY_SEED_SIZE 64 /* libtpms: 64 per define USE_SPEC_COMPLIANT_PROOFS */ 302 #endif 303 #ifndef CONTEXT_ENCRYPT_ALGORITHM 304 #define CONTEXT_ENCRYPT_ALGORITHM AES 305 #endif 306 #ifndef NV_CLOCK_UPDATE_INTERVAL 307 #define NV_CLOCK_UPDATE_INTERVAL 12 308 #endif 309 #ifndef NUM_POLICY_PCR 310 #define NUM_POLICY_PCR 1 311 #endif 312 #ifndef MAX_COMMAND_SIZE 313 #define MAX_COMMAND_SIZE TPM2_GetBufferSize() /* libtpms changed */ 314 #endif 315 #ifndef MAX_RESPONSE_SIZE 316 #define MAX_RESPONSE_SIZE TPM2_GetBufferSize() /* libtpms changed */ 317 #endif 318 #ifndef ORDERLY_BITS 319 #define ORDERLY_BITS 8 320 #endif 321 #ifndef MAX_SYM_DATA 322 #define MAX_SYM_DATA 128 323 #endif 324 #ifndef MAX_RNG_ENTROPY_SIZE 325 #define MAX_RNG_ENTROPY_SIZE 64 326 #endif 327 #ifndef RAM_INDEX_SPACE 328 #define RAM_INDEX_SPACE 512 329 #endif 330 #ifndef RSA_DEFAULT_PUBLIC_EXPONENT 331 #define RSA_DEFAULT_PUBLIC_EXPONENT 0x00010001 332 #endif 333 #ifndef ENABLE_PCR_NO_INCREMENT 334 #define ENABLE_PCR_NO_INCREMENT YES 335 #endif 336 #ifndef CRT_FORMAT_RSA 337 #define CRT_FORMAT_RSA YES 338 #endif 339 #ifndef VENDOR_COMMAND_COUNT 340 #define VENDOR_COMMAND_COUNT 0 341 #endif 342 #ifndef MAX_VENDOR_BUFFER_SIZE 343 #define MAX_VENDOR_BUFFER_SIZE 1024 344 #endif 345 #ifndef SIZE_OF_X509_SERIAL_NUMBER 346 #define SIZE_OF_X509_SERIAL_NUMBER 20 347 #endif 348 #ifndef PRIVATE_VENDOR_SPECIFIC_BYTES 349 #define PRIVATE_VENDOR_SPECIFIC_BYTES \ 350 ((MAX_RSA_KEY_BYTES/2) * (3 + CRT_FORMAT_RSA * 2)) /* libtpms: keep as was in rev 150 */ 351 #endif 352 // Table 0:2 - Defines for Implemented Algorithms 353 #ifndef ALG_AES 354 #define ALG_AES ALG_YES 355 #endif 356 #ifndef ALG_CAMELLIA 357 #define ALG_CAMELLIA ALG_YES /* libtpms: YES since v0.9 */ 358 #endif 359 #ifndef ALG_CBC 360 #define ALG_CBC ALG_YES 361 #endif 362 #ifndef ALG_CFB 363 #define ALG_CFB ALG_YES 364 #endif 365 #ifndef ALG_CMAC 366 #define ALG_CMAC ALG_YES 367 #endif 368 #ifndef ALG_CTR 369 #define ALG_CTR ALG_YES 370 #endif 371 #ifndef ALG_ECB 372 #define ALG_ECB ALG_YES 373 #endif 374 #ifndef ALG_ECC 375 #define ALG_ECC ALG_YES 376 #endif 377 #ifndef ALG_ECDAA 378 #define ALG_ECDAA (ALG_YES && ALG_ECC) 379 #endif 380 #ifndef ALG_ECDH 381 #define ALG_ECDH (ALG_YES && ALG_ECC) 382 #endif 383 #ifndef ALG_ECDSA 384 #define ALG_ECDSA (ALG_YES && ALG_ECC) 385 #endif 386 #ifndef ALG_ECMQV 387 #define ALG_ECMQV (ALG_YES && ALG_ECC) /* libtpms enabled */ 388 #endif 389 #ifndef ALG_ECSCHNORR 390 #define ALG_ECSCHNORR (ALG_YES && ALG_ECC) 391 #endif 392 #ifndef ALG_HMAC 393 #define ALG_HMAC ALG_YES 394 #endif 395 #ifndef ALG_KDF1_SP800_108 396 #define ALG_KDF1_SP800_108 ALG_YES 397 #endif 398 #ifndef ALG_KDF1_SP800_56A 399 #define ALG_KDF1_SP800_56A (ALG_YES && ALG_ECC) 400 #endif 401 #ifndef ALG_KDF2 402 #define ALG_KDF2 ALG_YES 403 #endif 404 #ifndef ALG_KEYEDHASH 405 #define ALG_KEYEDHASH ALG_YES 406 #endif 407 #ifndef ALG_MGF1 408 #define ALG_MGF1 ALG_YES 409 #endif 410 #ifndef ALG_OAEP 411 #define ALG_OAEP (ALG_YES && ALG_RSA) 412 #endif 413 #ifndef ALG_OFB 414 #define ALG_OFB ALG_YES 415 #endif 416 #ifndef ALG_RSA 417 #define ALG_RSA ALG_YES 418 #endif 419 #ifndef ALG_RSAES 420 #define ALG_RSAES (ALG_YES && ALG_RSA) 421 #endif 422 #ifndef ALG_RSAPSS 423 #define ALG_RSAPSS (ALG_YES && ALG_RSA) 424 #endif 425 #ifndef ALG_RSASSA 426 #define ALG_RSASSA (ALG_YES && ALG_RSA) 427 #endif 428 #ifndef ALG_SHA 429 #define ALG_SHA ALG_NO /* Not specified by vendor */ 430 #endif 431 #ifndef ALG_SHA1 432 #define ALG_SHA1 ALG_YES 433 #endif 434 #ifndef ALG_SHA256 435 #define ALG_SHA256 ALG_YES 436 #endif 437 #ifndef ALG_SHA384 438 #define ALG_SHA384 ALG_YES 439 #endif 440 #ifndef ALG_SHA3_256 441 #define ALG_SHA3_256 ALG_NO /* Not specified by vendor */ 442 #endif 443 #ifndef ALG_SHA3_384 444 #define ALG_SHA3_384 ALG_NO /* Not specified by vendor */ 445 #endif 446 #ifndef ALG_SHA3_512 447 #define ALG_SHA3_512 ALG_NO /* Not specified by vendor */ 448 #endif 449 #ifndef ALG_SHA512 450 #define ALG_SHA512 ALG_YES 451 #endif 452 #ifndef ALG_SM2 453 #define ALG_SM2 (ALG_YES && ALG_ECC) /* libtpms enabled */ 454 #endif 455 #ifndef ALG_SM3_256 456 #define ALG_SM3_256 ALG_NO /* libtpms: NO */ 457 #endif 458 #ifndef ALG_SM4 459 #define ALG_SM4 ALG_NO /* libtpms: NO */ 460 #endif 461 #ifndef ALG_SYMCIPHER 462 #define ALG_SYMCIPHER ALG_YES 463 #endif 464 #ifndef ALG_TDES 465 #define ALG_TDES ALG_YES /* libtpms enabled */ 466 #endif 467 #ifndef ALG_XOR 468 #define ALG_XOR ALG_YES 469 #endif 470 // Table 1:00 - Defines for RSA Asymmetric Cipher Algorithm Constants 471 #ifndef RSA_1024 472 #define RSA_1024 (ALG_RSA && YES) 473 #endif 474 #ifndef RSA_2048 475 #define RSA_2048 (ALG_RSA && YES) 476 #endif 477 #ifndef RSA_3072 478 #define RSA_3072 (ALG_RSA && YES) 479 #endif 480 #ifndef RSA_4096 481 #define RSA_4096 (ALG_RSA && NO) 482 #endif 483 #ifndef RSA_16384 484 #define RSA_16384 (ALG_RSA && NO) 485 #endif 486 487 // Table 1:17 - Defines for AES Symmetric Cipher Algorithm Constants 488 #ifndef AES_128 489 #define AES_128 (ALG_AES && YES) 490 #endif 491 #ifndef AES_192 492 #define AES_192 (ALG_AES && NO) 493 #endif 494 #ifndef AES_256 495 #define AES_256 (ALG_AES && YES) 496 #endif 497 // Table 1:18 - Defines for SM4 Symmetric Cipher Algorithm Constants 498 #ifndef SM4_128 499 #define SM4_128 (ALG_SM4 && YES) 500 #endif 501 // Table 1:19 - Defines for CAMELLIA Symmetric Cipher Algorithm Constants 502 #ifndef CAMELLIA_128 503 #define CAMELLIA_128 (ALG_CAMELLIA && YES) 504 #endif 505 #ifndef CAMELLIA_192 506 #define CAMELLIA_192 (ALG_CAMELLIA && NO) 507 #endif 508 #ifndef CAMELLIA_256 509 #define CAMELLIA_256 (ALG_CAMELLIA && YES) 510 #endif 511 // Table 1:17 - Defines for TDES Symmetric Cipher Algorithm Constants 512 #ifndef TDES_128 513 #define TDES_128 (ALG_TDES && YES) 514 #endif 515 #ifndef TDES_192 516 #define TDES_192 (ALG_TDES && YES) 517 #endif 518 // Table 0:5 - Defines for Implemented Commands 519 #ifndef CC_ACT_SetTimeout 520 #define CC_ACT_SetTimeout CC_NO /* libtpms: NO */ 521 #endif 522 #ifndef CC_AC_GetCapability 523 #define CC_AC_GetCapability CC_NO /* kgold */ 524 #endif 525 #ifndef CC_AC_Send 526 #define CC_AC_Send CC_NO /* kgold */ 527 #endif 528 #ifndef CC_ActivateCredential 529 #define CC_ActivateCredential CC_YES 530 #endif 531 #ifndef CC_Certify 532 #define CC_Certify CC_YES 533 #endif 534 #ifndef CC_CertifyCreation 535 #define CC_CertifyCreation CC_YES 536 #endif 537 #ifndef CC_CertifyX509 538 #define CC_CertifyX509 CC_YES 539 #endif 540 #ifndef CC_ChangeEPS 541 #define CC_ChangeEPS CC_YES 542 #endif 543 #ifndef CC_ChangePPS 544 #define CC_ChangePPS CC_YES 545 #endif 546 #ifndef CC_Clear 547 #define CC_Clear CC_YES 548 #endif 549 #ifndef CC_ClearControl 550 #define CC_ClearControl CC_YES 551 #endif 552 #ifndef CC_ClockRateAdjust 553 #define CC_ClockRateAdjust CC_YES 554 #endif 555 #ifndef CC_ClockSet 556 #define CC_ClockSet CC_YES 557 #endif 558 #ifndef CC_Commit 559 #define CC_Commit (CC_YES && ALG_ECC) 560 #endif 561 #ifndef CC_ContextLoad 562 #define CC_ContextLoad CC_YES 563 #endif 564 #ifndef CC_ContextSave 565 #define CC_ContextSave CC_YES 566 #endif 567 #ifndef CC_Create 568 #define CC_Create CC_YES 569 #endif 570 #ifndef CC_CreateLoaded 571 #define CC_CreateLoaded CC_YES 572 #endif 573 #ifndef CC_CreatePrimary 574 #define CC_CreatePrimary CC_YES 575 #endif 576 #ifndef CC_DictionaryAttackLockReset 577 #define CC_DictionaryAttackLockReset CC_YES 578 #endif 579 #ifndef CC_DictionaryAttackParameters 580 #define CC_DictionaryAttackParameters CC_YES 581 #endif 582 #ifndef CC_Duplicate 583 #define CC_Duplicate CC_YES 584 #endif 585 #ifndef CC_ECC_Decrypt 586 #define CC_ECC_Decrypt (CC_NO && ALG_ECC) 587 #endif 588 #ifndef CC_ECC_Encrypt 589 #define CC_ECC_Encrypt (CC_NO && ALG_ECC) 590 #endif 591 #ifndef CC_ECC_Parameters 592 #define CC_ECC_Parameters (CC_YES && ALG_ECC) 593 #endif 594 #ifndef CC_ECDH_KeyGen 595 #define CC_ECDH_KeyGen (CC_YES && ALG_ECC) 596 #endif 597 #ifndef CC_ECDH_ZGen 598 #define CC_ECDH_ZGen (CC_YES && ALG_ECC) 599 #endif 600 #ifndef CC_EC_Ephemeral 601 #define CC_EC_Ephemeral (CC_YES && ALG_ECC) 602 #endif 603 #ifndef CC_EncryptDecrypt 604 #define CC_EncryptDecrypt CC_YES 605 #endif 606 #ifndef CC_EncryptDecrypt2 607 #define CC_EncryptDecrypt2 CC_YES 608 #endif 609 #ifndef CC_EventSequenceComplete 610 #define CC_EventSequenceComplete CC_YES 611 #endif 612 #ifndef CC_EvictControl 613 #define CC_EvictControl CC_YES 614 #endif 615 #ifndef CC_FieldUpgradeData 616 #define CC_FieldUpgradeData CC_NO 617 #endif 618 #ifndef CC_FieldUpgradeStart 619 #define CC_FieldUpgradeStart CC_NO 620 #endif 621 #ifndef CC_FirmwareRead 622 #define CC_FirmwareRead CC_NO 623 #endif 624 #ifndef CC_FlushContext 625 #define CC_FlushContext CC_YES 626 #endif 627 #ifndef CC_GetCapability 628 #define CC_GetCapability CC_YES 629 #endif 630 #ifndef CC_GetCommandAuditDigest 631 #define CC_GetCommandAuditDigest CC_YES 632 #endif 633 #ifndef CC_GetRandom 634 #define CC_GetRandom CC_YES 635 #endif 636 #ifndef CC_GetSessionAuditDigest 637 #define CC_GetSessionAuditDigest CC_YES 638 #endif 639 #ifndef CC_GetTestResult 640 #define CC_GetTestResult CC_YES 641 #endif 642 #ifndef CC_GetTime 643 #define CC_GetTime CC_YES 644 #endif 645 #ifndef CC_HMAC 646 #define CC_HMAC (CC_YES && !ALG_CMAC) 647 #endif 648 #ifndef CC_HMAC_Start 649 #define CC_HMAC_Start (CC_YES && !ALG_CMAC) 650 #endif 651 #ifndef CC_Hash 652 #define CC_Hash CC_YES 653 #endif 654 #ifndef CC_HashSequenceStart 655 #define CC_HashSequenceStart CC_YES 656 #endif 657 #ifndef CC_HierarchyChangeAuth 658 #define CC_HierarchyChangeAuth CC_YES 659 #endif 660 #ifndef CC_HierarchyControl 661 #define CC_HierarchyControl CC_YES 662 #endif 663 #ifndef CC_Import 664 #define CC_Import CC_YES 665 #endif 666 #ifndef CC_IncrementalSelfTest 667 #define CC_IncrementalSelfTest CC_YES 668 #endif 669 #ifndef CC_Load 670 #define CC_Load CC_YES 671 #endif 672 #ifndef CC_LoadExternal 673 #define CC_LoadExternal CC_YES 674 #endif 675 #ifndef CC_MAC 676 #define CC_MAC (CC_YES && ALG_CMAC) 677 #endif 678 #ifndef CC_MAC_Start 679 #define CC_MAC_Start (CC_YES && ALG_CMAC) 680 #endif 681 #ifndef CC_MakeCredential 682 #define CC_MakeCredential CC_YES 683 #endif 684 #ifndef CC_NV_Certify 685 #define CC_NV_Certify CC_YES 686 #endif 687 #ifndef CC_NV_ChangeAuth 688 #define CC_NV_ChangeAuth CC_YES 689 #endif 690 #ifndef CC_NV_DefineSpace 691 #define CC_NV_DefineSpace CC_YES 692 #endif 693 #ifndef CC_NV_Extend 694 #define CC_NV_Extend CC_YES 695 #endif 696 #ifndef CC_NV_GlobalWriteLock 697 #define CC_NV_GlobalWriteLock CC_YES 698 #endif 699 #ifndef CC_NV_Increment 700 #define CC_NV_Increment CC_YES 701 #endif 702 #ifndef CC_NV_Read 703 #define CC_NV_Read CC_YES 704 #endif 705 #ifndef CC_NV_ReadLock 706 #define CC_NV_ReadLock CC_YES 707 #endif 708 #ifndef CC_NV_ReadPublic 709 #define CC_NV_ReadPublic CC_YES 710 #endif 711 #ifndef CC_NV_SetBits 712 #define CC_NV_SetBits CC_YES 713 #endif 714 #ifndef CC_NV_UndefineSpace 715 #define CC_NV_UndefineSpace CC_YES 716 #endif 717 #ifndef CC_NV_UndefineSpaceSpecial 718 #define CC_NV_UndefineSpaceSpecial CC_YES 719 #endif 720 #ifndef CC_NV_Write 721 #define CC_NV_Write CC_YES 722 #endif 723 #ifndef CC_NV_WriteLock 724 #define CC_NV_WriteLock CC_YES 725 #endif 726 #ifndef CC_ObjectChangeAuth 727 #define CC_ObjectChangeAuth CC_YES 728 #endif 729 #ifndef CC_PCR_Allocate 730 #define CC_PCR_Allocate CC_YES 731 #endif 732 #ifndef CC_PCR_Event 733 #define CC_PCR_Event CC_YES 734 #endif 735 #ifndef CC_PCR_Extend 736 #define CC_PCR_Extend CC_YES 737 #endif 738 #ifndef CC_PCR_Read 739 #define CC_PCR_Read CC_YES 740 #endif 741 #ifndef CC_PCR_Reset 742 #define CC_PCR_Reset CC_YES 743 #endif 744 #ifndef CC_PCR_SetAuthPolicy 745 #define CC_PCR_SetAuthPolicy CC_YES 746 #endif 747 #ifndef CC_PCR_SetAuthValue 748 #define CC_PCR_SetAuthValue CC_YES 749 #endif 750 #ifndef CC_PP_Commands 751 #define CC_PP_Commands CC_YES 752 #endif 753 #ifndef CC_PolicyAuthValue 754 #define CC_PolicyAuthValue CC_YES 755 #endif 756 #ifndef CC_PolicyAuthorize 757 #define CC_PolicyAuthorize CC_YES 758 #endif 759 #ifndef CC_PolicyAuthorizeNV 760 #define CC_PolicyAuthorizeNV CC_YES 761 #endif 762 #ifndef CC_PolicyCommandCode 763 #define CC_PolicyCommandCode CC_YES 764 #endif 765 #ifndef CC_PolicyCounterTimer 766 #define CC_PolicyCounterTimer CC_YES 767 #endif 768 #ifndef CC_PolicyCpHash 769 #define CC_PolicyCpHash CC_YES 770 #endif 771 #ifndef CC_PolicyDuplicationSelect 772 #define CC_PolicyDuplicationSelect CC_YES 773 #endif 774 #ifndef CC_PolicyGetDigest 775 #define CC_PolicyGetDigest CC_YES 776 #endif 777 #ifndef CC_PolicyLocality 778 #define CC_PolicyLocality CC_YES 779 #endif 780 #ifndef CC_PolicyNV 781 #define CC_PolicyNV CC_YES 782 #endif 783 #ifndef CC_PolicyNameHash 784 #define CC_PolicyNameHash CC_YES 785 #endif 786 #ifndef CC_PolicyNvWritten 787 #define CC_PolicyNvWritten CC_YES 788 #endif 789 #ifndef CC_PolicyOR 790 #define CC_PolicyOR CC_YES 791 #endif 792 #ifndef CC_PolicyPCR 793 #define CC_PolicyPCR CC_YES 794 #endif 795 #ifndef CC_PolicyPassword 796 #define CC_PolicyPassword CC_YES 797 #endif 798 #ifndef CC_PolicyPhysicalPresence 799 #define CC_PolicyPhysicalPresence CC_YES 800 #endif 801 #ifndef CC_PolicyRestart 802 #define CC_PolicyRestart CC_YES 803 #endif 804 #ifndef CC_PolicySecret 805 #define CC_PolicySecret CC_YES 806 #endif 807 #ifndef CC_PolicySigned 808 #define CC_PolicySigned CC_YES 809 #endif 810 #ifndef CC_PolicyTemplate 811 #define CC_PolicyTemplate CC_YES 812 #endif 813 #ifndef CC_PolicyTicket 814 #define CC_PolicyTicket CC_YES 815 #endif 816 #ifndef CC_Policy_AC_SendSelect 817 #define CC_Policy_AC_SendSelect CC_NO /* kgold */ 818 #endif 819 #ifndef CC_Quote 820 #define CC_Quote CC_YES 821 #endif 822 #ifndef CC_RSA_Decrypt 823 #define CC_RSA_Decrypt (CC_YES && ALG_RSA) 824 #endif 825 #ifndef CC_RSA_Encrypt 826 #define CC_RSA_Encrypt (CC_YES && ALG_RSA) 827 #endif 828 #ifndef CC_ReadClock 829 #define CC_ReadClock CC_YES 830 #endif 831 #ifndef CC_ReadPublic 832 #define CC_ReadPublic CC_YES 833 #endif 834 #ifndef CC_Rewrap 835 #define CC_Rewrap CC_YES 836 #endif 837 #ifndef CC_SelfTest 838 #define CC_SelfTest CC_YES 839 #endif 840 #ifndef CC_SequenceComplete 841 #define CC_SequenceComplete CC_YES 842 #endif 843 #ifndef CC_SequenceUpdate 844 #define CC_SequenceUpdate CC_YES 845 #endif 846 #ifndef CC_SetAlgorithmSet 847 #define CC_SetAlgorithmSet CC_YES 848 #endif 849 #ifndef CC_SetCommandCodeAuditStatus 850 #define CC_SetCommandCodeAuditStatus CC_YES 851 #endif 852 #ifndef CC_SetPrimaryPolicy 853 #define CC_SetPrimaryPolicy CC_YES 854 #endif 855 #ifndef CC_Shutdown 856 #define CC_Shutdown CC_YES 857 #endif 858 #ifndef CC_Sign 859 #define CC_Sign CC_YES 860 #endif 861 #ifndef CC_StartAuthSession 862 #define CC_StartAuthSession CC_YES 863 #endif 864 #ifndef CC_Startup 865 #define CC_Startup CC_YES 866 #endif 867 #ifndef CC_StirRandom 868 #define CC_StirRandom CC_YES 869 #endif 870 #ifndef CC_TestParms 871 #define CC_TestParms CC_YES 872 #endif 873 #ifndef CC_Unseal 874 #define CC_Unseal CC_YES 875 #endif 876 #ifndef CC_Vendor_TCG_Test 877 #define CC_Vendor_TCG_Test CC_NO /* libtpms changed */ 878 #endif 879 #ifndef CC_VerifySignature 880 #define CC_VerifySignature CC_YES 881 #endif 882 #ifndef CC_ZGen_2Phase 883 #define CC_ZGen_2Phase (CC_YES && ALG_ECC) 884 #endif 885 #endif // _TPM_PROFIL 91 #endif // _TPM_PROFILE_H_ -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm2/Volatile.c ¶
r108932 r108933 45 45 # include <endian.h> 46 46 #endif 47 #endif 47 #endif /* VBOX */ 48 48 #include <string.h> 49 49 -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm_library.c ¶
r108932 r108933 521 521 int TPMLIB_LogPrintf(const char *format, ...) 522 522 { 523 #ifndef VBOX 523 #ifndef VBOX 524 524 unsigned level = debug_level, i; 525 525 va_list args; -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm_library_conf.h ¶
r108932 r108933 169 169 #define TPM_LONG_DURATION (300 * 1000 * 1000) 170 170 171 172 171 #ifdef VBOX 173 172 # if defined(RT_OS_WINDOWS) … … 195 194 # endif 196 195 #endif 196 197 197 #endif /* TPM_LIBRARY_CONF_H */ -
TabularUnified trunk/src/libs/libtpms-0.10.0/src/tpm_library_intern.h ¶
r108932 r108933 118 118 #define TPMLIB_LogTPM2Error(format, ...) \ 119 119 TPMLIB_LogPrintfA(~0, "libtpms/tpm2: "format, __VA_ARGS__) 120 #else 120 #else /* !VBOX*/ 121 121 # define TPMLIB_LogError(format, ...) 122 122 # define TPMLIB_LogTPM12Error(format, ...) 123 123 # define TPMLIB_LogTPM2Error(format, ...) 124 #endif 125 124 #endif /* !VBOX*/ 125 126 126 int TPMLIB_asprintf(char **strp, const char *fmt, ...); 127 127
Note:
See TracChangeset
for help on using the changeset viewer.