tpmclient.int.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************;
  3. * Copyright (c) 2015, Intel Corporation
  4. * All rights reserved.
  5. ***********************************************************************;
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdbool.h>
  11. #include <inttypes.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "tss2_sys.h"
  16. #include "tss2_tcti_device.h"
  17. #ifdef TCTI_MSSIM
  18. #include "tss2_tcti_mssim.h"
  19. #endif /* TCTI_MSSIM */
  20. #ifdef TCTI_SWTPM
  21. #include "tss2_tcti_swtpm.h"
  22. #endif /* TCTI_SWTPM */
  23. #include "../integration/context-util.h"
  24. #include "../integration/sys-util.h"
  25. #include "../integration/session-util.h"
  26. #include "util/tss2_endian.h"
  27. #include "sysapi_util.h"
  28. #define LOGMODULE testtpmclient
  29. #include "util/log.h"
  30. /*
  31. * TPM indices and sizes
  32. */
  33. #define NV_AUX_INDEX_SIZE 96
  34. #define NV_PS_INDEX_SIZE 34
  35. #define INDEX_AUX 0x01800003 /* NV Storage */
  36. #define INDEX_LCP_OWN 0x01400001 /* Launch Policy Owner */
  37. #define INDEX_LCP_SUP 0x01800001 /* Launch Policy Default (Supplier) */
  38. #define TPM20_INDEX_TEST1 0x01500015
  39. #define TPM20_INDEX_TEST2 0x01500016
  40. #define TPM20_INDEX_PASSWORD_TEST 0x01500020
  41. #define SESSIONS_COUNT 1
  42. #define SET_PCR_SELECT_BIT( pcrSelection, pcr ) \
  43. (pcrSelection).pcrSelect[( (pcr)/8 )] |= ( 1 << ( (pcr) % 8) );
  44. #define CLEAR_PCR_SELECT_BITS( pcrSelection ) \
  45. (pcrSelection).pcrSelect[0] = 0; \
  46. (pcrSelection).pcrSelect[1] = 0; \
  47. (pcrSelection).pcrSelect[2] = 0;
  48. #define SET_PCR_SELECT_SIZE( pcrSelection, size ) \
  49. (pcrSelection).sizeofSelect = size;
  50. TSS2_SYS_CONTEXT *sysContext;
  51. TSS2_TCTI_CONTEXT *resMgrTctiContext = 0;
  52. #define INIT_SIMPLE_TPM2B_SIZE(type) (type).size = sizeof(type) - 2;
  53. #define YES 1
  54. #define NO 0
  55. static void ErrorHandler(UINT32 rval, char *errorString, int errorStringSize)
  56. {
  57. UINT32 errorLevel = rval & TSS2_RC_LAYER_MASK;
  58. char levelString[32];
  59. switch (errorLevel)
  60. {
  61. case TSS2_TPM_RC_LAYER:
  62. strcpy(levelString, "TPM");
  63. break;
  64. case TSS2_SYS_RC_LAYER:
  65. strcpy(levelString, "System API");
  66. break;
  67. case TSS2_MU_RC_LAYER:
  68. strcpy(levelString, "System API TPM encoded");
  69. break;
  70. case TSS2_TCTI_RC_LAYER:
  71. strcpy(levelString, "TCTI");
  72. break;
  73. case TSS2_RESMGR_TPM_RC_LAYER:
  74. strcpy(levelString, "Resource Mgr TPM encoded");
  75. break;
  76. case TSS2_RESMGR_RC_LAYER:
  77. strcpy(levelString, "Resource Mgr");
  78. break;
  79. default:
  80. strcpy(levelString, "Unknown Level");
  81. break;
  82. }
  83. snprintf(errorString, errorStringSize, "%s Error: 0x%x\n", levelString, rval);
  84. }
  85. #define EXIT_SKIP 77
  86. static void Cleanup_exit(int rc)
  87. {
  88. if (resMgrTctiContext != NULL) {
  89. tcti_teardown(resMgrTctiContext);
  90. resMgrTctiContext = NULL;
  91. }
  92. exit(rc == EXIT_SKIP ? EXIT_SKIP : 1);
  93. }
  94. static void Cleanup()
  95. {
  96. Cleanup_exit(1);
  97. }
  98. static void InitSysContextFailure()
  99. {
  100. LOG_ERROR("InitSysContext failed, exiting...");
  101. Cleanup();
  102. }
  103. #define ERROR_STR_LEN 200
  104. #define CheckPassed(rval) { \
  105. char error_string[ERROR_STR_LEN]; \
  106. if ((rval) != TPM2_RC_SUCCESS) { \
  107. ErrorHandler((rval), error_string, ERROR_STR_LEN); \
  108. LOG_INFO("passing case: \tFAILED! %s (%s@%u)", \
  109. error_string, __FUNCTION__, __LINE__ ); \
  110. Cleanup_exit(rval); \
  111. } else { \
  112. LOG_INFO("passing case: \tPASSED! (%s@%u)", \
  113. __FUNCTION__, __LINE__); \
  114. } \
  115. }
  116. #define CheckFailed(rval, expected_rval) { \
  117. char error_string[ERROR_STR_LEN]; \
  118. if ((rval) != (expected_rval)) { \
  119. ErrorHandler((rval), error_string, ERROR_STR_LEN); \
  120. LOG_INFO("\tfailing case: FAILED! %s Ret code s/b: 0x%x, but was: 0x%x (%s@%u)", \
  121. error_string, (expected_rval), (rval), __FUNCTION__, __LINE__ ); \
  122. Cleanup_exit(rval); \
  123. } else { \
  124. LOG_INFO("\tfailing case: PASSED! (%s@%u)", \
  125. __FUNCTION__, __LINE__); \
  126. } \
  127. }
  128. static TSS2_RC TpmReset()
  129. {
  130. TSS2_RC rval = TSS2_RC_SUCCESS;
  131. #ifdef TCTI_SWTPM
  132. rval = Tss2_Tcti_Swtpm_Reset(resMgrTctiContext);
  133. /* If TCTI is not swtpm, bad context is returned. */
  134. if (rval != TSS2_TCTI_RC_BAD_CONTEXT) {
  135. return rval;
  136. } else {
  137. LOG_WARNING("TPM Reset failed: wrong TCTI type retrying with mssim...");
  138. }
  139. #endif /* TCTI_SWTPM */
  140. #ifdef TCTI_MSSIM
  141. rval = (TSS2_RC)tcti_platform_command( resMgrTctiContext, MS_SIM_POWER_OFF );
  142. if (rval == TSS2_RC_SUCCESS) {
  143. rval = (TSS2_RC)tcti_platform_command( resMgrTctiContext, MS_SIM_POWER_ON );
  144. }
  145. #endif /* TCTI_MSSIM */
  146. if (rval == TSS2_TCTI_RC_BAD_CONTEXT) {
  147. rval = EXIT_SKIP;
  148. }
  149. return rval;
  150. }
  151. static void TestDictionaryAttackLockReset()
  152. {
  153. UINT32 rval;
  154. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  155. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  156. .sessionHandle = TPM2_RH_PW,
  157. .sessionAttributes = 0,
  158. .nonce={.size=0},
  159. .hmac={.size=0}}}};
  160. LOG_INFO("DICTIONARY ATTACK LOCK RESET TEST :" );
  161. rval = Tss2_Sys_DictionaryAttackLockReset ( sysContext, TPM2_RH_LOCKOUT, &sessionsData, &sessionsDataOut );
  162. CheckPassed( rval );
  163. }
  164. static void TestTpmStartup()
  165. {
  166. UINT32 rval;
  167. LOG_INFO("STARTUP TESTS:" );
  168. /*
  169. * First test the one-call interface.
  170. */
  171. /* First must do TPM reset. */
  172. rval = TpmReset();
  173. CheckPassed(rval);
  174. /* This one should pass. */
  175. rval = Tss2_Sys_Startup( sysContext, TPM2_SU_CLEAR );
  176. CheckPassed(rval);
  177. /* This one should fail. */
  178. rval = Tss2_Sys_Startup( sysContext, TPM2_SU_CLEAR );
  179. CheckFailed( rval, TPM2_RC_INITIALIZE );
  180. /* Cycle power using simulator interface. */
  181. rval = TpmReset();
  182. CheckPassed(rval);
  183. /*
  184. * Now test the synchronous, non-one-call interface.
  185. */
  186. rval = Tss2_Sys_Startup_Prepare( sysContext, TPM2_SU_CLEAR );
  187. CheckPassed(rval);
  188. /* Execute the command synchronously. */
  189. rval = Tss2_Sys_Execute( sysContext );
  190. CheckPassed( rval );
  191. /* Cycle power using simulator interface. */
  192. rval = TpmReset();
  193. CheckPassed(rval);
  194. /*
  195. * Now test the asynchronous, non-one-call interface.
  196. */
  197. rval = Tss2_Sys_Startup_Prepare( sysContext, TPM2_SU_CLEAR );
  198. CheckPassed(rval);
  199. /* Execute the command asynchronously. */
  200. rval = Tss2_Sys_ExecuteAsync( sysContext );
  201. CheckPassed(rval);
  202. /*
  203. * Get the command response. Wait a maximum of 20ms
  204. * for response.
  205. */
  206. rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
  207. CheckPassed(rval);
  208. }
  209. static void TestTpmGetCapability()
  210. {
  211. UINT32 rval;
  212. char manuID[5] = " ";
  213. char *manuIDPtr = &manuID[0];
  214. TPMI_YES_NO moreData;
  215. TPMS_CAPABILITY_DATA capabilityData;
  216. LOG_INFO("GET_CAPABILITY TESTS:" );
  217. rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
  218. CheckPassed( rval );
  219. *((UINT32 *)manuIDPtr) = BE_TO_HOST_32(capabilityData.data.tpmProperties.tpmProperty[0].value);
  220. LOG_INFO("\t\tcount: %d, property: %x, manuId: %s",
  221. capabilityData.data.tpmProperties.count,
  222. capabilityData.data.tpmProperties.tpmProperty[0].property,
  223. manuID );
  224. rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_COMMAND_SIZE, 1, &moreData, &capabilityData, 0 );
  225. CheckPassed( rval );
  226. LOG_INFO("\t\tcount: %d, property: %x, max cmd size: %d",
  227. capabilityData.data.tpmProperties.count,
  228. capabilityData.data.tpmProperties.tpmProperty[0].property,
  229. capabilityData.data.tpmProperties.tpmProperty[0].value );
  230. rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_COMMAND_SIZE, 40, &moreData, &capabilityData, 0 );
  231. CheckPassed( rval );
  232. LOG_INFO("\t\tcount: %d, property: %x, max cmd size: %d",
  233. capabilityData.data.tpmProperties.count,
  234. capabilityData.data.tpmProperties.tpmProperty[0].property,
  235. capabilityData.data.tpmProperties.tpmProperty[0].value );
  236. rval = Tss2_Sys_GetCapability( sysContext, 0, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MAX_RESPONSE_SIZE, 1, &moreData, &capabilityData, 0 );
  237. CheckPassed( rval );
  238. LOG_INFO("\t count: %d, property: %x, max response size: %d",
  239. capabilityData.data.tpmProperties.count,
  240. capabilityData.data.tpmProperties.tpmProperty[0].property,
  241. capabilityData.data.tpmProperties.tpmProperty[0].value );
  242. rval = Tss2_Sys_GetCapability( sysContext, 0, 0xff, TPM2_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
  243. CheckFailed(rval, TPM2_RC_VALUE+TPM2_RC_1+TPM2_RC_P);
  244. }
  245. static void TestTpmClear()
  246. {
  247. UINT32 rval;
  248. TPM2B_AUTH hmac = { .size = 0 };
  249. TPM2B_NONCE nonce = { .size = 0 };
  250. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  251. TSS2L_SYS_AUTH_COMMAND sessionsDataIn = { .count = 1, .auths = {{
  252. .sessionHandle = TPM2_RH_PW,
  253. .sessionAttributes = 0,
  254. .nonce=nonce,
  255. .hmac=hmac}}};
  256. LOG_INFO("CLEAR and CLEAR CONTROL TESTS:" );
  257. rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, 0 );
  258. CheckPassed( rval );
  259. rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, YES, &sessionsDataOut );
  260. CheckPassed( rval );
  261. rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, 0 );
  262. CheckFailed( rval, TPM2_RC_DISABLED );
  263. rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
  264. CheckPassed( rval );
  265. sessionsDataIn.auths[0].sessionAttributes = 0xff;
  266. rval = Tss2_Sys_Clear ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, &sessionsDataOut );
  267. CheckFailed( rval, TPM2_RC_9 + TPM2_RC_RESERVED_BITS );
  268. rval = Tss2_Sys_ClearControl ( sysContext, TPM2_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
  269. CheckFailed( rval, TPM2_RC_9 + TPM2_RC_RESERVED_BITS );
  270. }
  271. #define SESSIONS_ABOVE_MAX_ACTIVE 0
  272. #define DEBUG_MAX_ACTIVE_SESSIONS 8
  273. #define DEBUG_GAP_MAX 2*DEBUG_MAX_ACTIVE_SESSIONS
  274. SESSION *sessions[SESSIONS_COUNT];
  275. static void TestStartAuthSession()
  276. {
  277. UINT32 rval;
  278. TPM2B_ENCRYPTED_SECRET encryptedSalt;
  279. TPMT_SYM_DEF symmetric;
  280. SESSION *authSession = NULL;
  281. TPM2B_NONCE nonceCaller;
  282. UINT16 i;
  283. TPM2_HANDLE badSessionHandle = 0x03010000;
  284. TPMS_AUTH_COMMAND sessionData;
  285. TPM2B_NONCE nonce;
  286. TPM2B_AUTH hmac;
  287. /* Init sessionHandle */
  288. sessionData.sessionHandle = badSessionHandle;
  289. /* Init nonce. */
  290. nonce.size = 0;
  291. sessionData.nonce = nonce;
  292. /* init hmac */
  293. hmac.size = 0;
  294. sessionData.hmac = hmac;
  295. /* Init session attributes */
  296. *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
  297. encryptedSalt.size = 0;
  298. LOG_INFO("START_AUTH_SESSION TESTS:" );
  299. symmetric.algorithm = TPM2_ALG_NULL;
  300. symmetric.keyBits.sym = 0;
  301. symmetric.mode.sym = 0;
  302. nonceCaller.size = 0;
  303. encryptedSalt.size = 0;
  304. /* Init session */
  305. rval = create_auth_session(&authSession, TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  306. CheckPassed( rval );
  307. rval = Tss2_Sys_FlushContext( sysContext, authSession->sessionHandle );
  308. CheckPassed( rval );
  309. end_auth_session( authSession );
  310. /* Init session */
  311. rval = create_auth_session(&authSession, TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, 0xff, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  312. CheckFailed( rval, TPM2_RC_VALUE + TPM2_RC_P + TPM2_RC_3 );
  313. /*
  314. * Try starting a bunch to see if resource manager handles this correctly.
  315. */
  316. for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
  317. {
  318. /* Init session struct */
  319. rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  320. CheckPassed( rval );
  321. LOG_INFO("Number of sessions created: %d", i+1 );
  322. }
  323. /* clean up the sessions that I don't want here. */
  324. for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *)); i++ )
  325. {
  326. rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
  327. CheckPassed( rval );
  328. end_auth_session(sessions[i]);
  329. }
  330. /* Now do some gap tests. */
  331. rval = create_auth_session(&sessions[0], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  332. CheckPassed( rval );
  333. for( i = 1; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
  334. {
  335. rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  336. CheckPassed( rval );
  337. rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
  338. CheckPassed( rval );
  339. end_auth_session(sessions[i]);
  340. }
  341. end_auth_session(sessions[0]);
  342. for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
  343. {
  344. rval = create_auth_session(&sessions[i], TPM2_RH_NULL, 0, TPM2_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY, &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  345. CheckPassed( rval );
  346. rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
  347. CheckPassed( rval );
  348. end_auth_session(sessions[i]);
  349. }
  350. }
  351. static void TestChangeEps()
  352. {
  353. UINT32 rval;
  354. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  355. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  356. .sessionHandle = TPM2_RH_PW,
  357. .sessionAttributes = 0,
  358. .nonce = {.size = 0},
  359. .hmac = {.size = 0}}}};
  360. LOG_INFO("CHANGE_EPS TESTS:" );
  361. rval = Tss2_Sys_ChangeEPS( sysContext, TPM2_RH_PLATFORM, &sessionsData, &sessionsDataOut );
  362. CheckPassed( rval );
  363. }
  364. static void TestChangePps()
  365. {
  366. UINT32 rval;
  367. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  368. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  369. .sessionHandle = TPM2_RH_PW,
  370. .sessionAttributes = 0,
  371. .nonce = {.size = 0},
  372. .hmac = {.size = 0}}}};
  373. LOG_INFO("CHANGE_PPS TESTS:" );
  374. rval = Tss2_Sys_ChangePPS( sysContext, TPM2_RH_PLATFORM, &sessionsData, &sessionsDataOut );
  375. CheckPassed( rval );
  376. }
  377. static void TestHierarchyChangeAuth()
  378. {
  379. UINT32 rval;
  380. TPM2B_AUTH newAuth;
  381. int i;
  382. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  383. .sessionHandle = TPM2_RH_PW,
  384. .sessionAttributes = 0,
  385. .nonce = {.size = 0},
  386. .hmac = {.size = 0}}}};
  387. LOG_INFO("HIERARCHY_CHANGE_AUTH TESTS:" );
  388. newAuth.size = 0;
  389. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
  390. CheckPassed( rval );
  391. /* Init new auth */
  392. newAuth.size = 20;
  393. for( i = 0; i < newAuth.size; i++ )
  394. newAuth.buffer[i] = i;
  395. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
  396. CheckPassed( rval );
  397. sessionsData.auths[0].hmac = newAuth;
  398. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
  399. CheckPassed( rval );
  400. /* Init new auth */
  401. newAuth.size = 0;
  402. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
  403. CheckPassed( rval );
  404. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM2_RH_PLATFORM, &sessionsData, &newAuth, 0 );
  405. CheckFailed( rval, TPM2_RC_1 + TPM2_RC_S + TPM2_RC_BAD_AUTH );
  406. rval = Tss2_Sys_HierarchyChangeAuth( sysContext, 0, &sessionsData, &newAuth, 0 );
  407. CheckFailed( rval, TPM2_RC_1 + TPM2_RC_VALUE );
  408. }
  409. #define PCR_0 0
  410. #define PCR_1 1
  411. #define PCR_2 2
  412. #define PCR_3 3
  413. #define PCR_4 4
  414. #define PCR_5 5
  415. #define PCR_6 6
  416. #define PCR_7 7
  417. #define PCR_8 8
  418. #define PCR_9 9
  419. #define PCR_10 10
  420. #define PCR_11 11
  421. #define PCR_12 12
  422. #define PCR_13 13
  423. #define PCR_14 14
  424. #define PCR_15 15
  425. #define PCR_16 16
  426. #define PCR_17 17
  427. #define PCR_18 18
  428. #define PCR_SIZE 20
  429. static void TestPcrExtend()
  430. {
  431. UINT32 rval;
  432. UINT16 i, digestSize;
  433. TPML_PCR_SELECTION pcrSelection;
  434. UINT32 pcrUpdateCounterBeforeExtend;
  435. UINT32 pcrUpdateCounterAfterExtend;
  436. UINT8 pcrBeforeExtend[PCR_SIZE];
  437. TPM2B_EVENT eventData;
  438. TPML_DIGEST pcrValues;
  439. TPML_DIGEST_VALUES digests;
  440. TPML_PCR_SELECTION pcrSelectionOut;
  441. UINT8 pcrAfterExtend[20];
  442. TSS2_TCTI_CONTEXT *tctiContext;
  443. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  444. .sessionHandle = TPM2_RH_PW,
  445. .sessionAttributes = 0,
  446. .nonce = {.size = 0},
  447. .hmac = {.size = 0}}}};
  448. LOG_INFO("PCR_EXTEND, PCR_EVENT, PCR_ALLOCATE, and PCR_READ TESTS:" );
  449. /* Init digests */
  450. digests.count = 1;
  451. digests.digests[0].hashAlg = TPM2_ALG_SHA1;
  452. digestSize = GetDigestSize( digests.digests[0].hashAlg );
  453. for( i = 0; i < digestSize; i++ )
  454. {
  455. digests.digests[0].digest.sha1[i] = (UINT8)(i % 256);
  456. }
  457. pcrSelection.count = 1;
  458. pcrSelection.pcrSelections[0].hash = TPM2_ALG_SHA1;
  459. pcrSelection.pcrSelections[0].sizeofSelect = 3;
  460. /* Clear out PCR select bit field */
  461. pcrSelection.pcrSelections[0].pcrSelect[0] = 0;
  462. pcrSelection.pcrSelections[0].pcrSelect[1] = 0;
  463. pcrSelection.pcrSelections[0].pcrSelect[2] = 0;
  464. /* Now set the PCR you want to read */
  465. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_17 );
  466. rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterBeforeExtend, &pcrSelectionOut, &pcrValues, 0 );
  467. CheckPassed( rval );
  468. if( pcrValues.digests[0].size <= PCR_SIZE &&
  469. pcrValues.digests[0].size <= sizeof( pcrValues.digests[0].buffer ) )
  470. memcpy( &( pcrBeforeExtend[0] ), &( pcrValues.digests[0].buffer[0] ), pcrValues.digests[0].size );
  471. /* Set locality 3 to enable PCR extend with PCR 17 */
  472. rval = Tss2_Sys_GetTctiContext(sysContext, &tctiContext);
  473. CheckPassed(rval);
  474. rval = Tss2_Tcti_SetLocality(tctiContext, 3);
  475. CheckPassed(rval);
  476. rval = Tss2_Sys_PCR_Extend( sysContext, PCR_17, &sessionsData, &digests, 0 );
  477. CheckPassed( rval );
  478. rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, &pcrSelectionOut, &pcrValues, 0 );
  479. CheckPassed( rval );
  480. memcpy( &( pcrAfterExtend[0] ), &( pcrValues.digests[0].buffer[0] ), pcrValues.digests[0].size );
  481. if( pcrUpdateCounterBeforeExtend == pcrUpdateCounterAfterExtend )
  482. {
  483. LOG_ERROR("ERROR!! pcrUpdateCounter didn't change value" );
  484. Cleanup();
  485. }
  486. if( 0 == memcmp( &( pcrBeforeExtend[0] ), &( pcrAfterExtend[0] ), 20 ) )
  487. {
  488. LOG_ERROR("ERROR!! PCR didn't change value" );
  489. Cleanup();
  490. }
  491. pcrSelection.pcrSelections[0].sizeofSelect = 255;
  492. rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, 0, 0, 0 );
  493. CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
  494. eventData.size = 4;
  495. eventData.buffer[0] = 0;
  496. eventData.buffer[1] = 0xff;
  497. eventData.buffer[2] = 0x55;
  498. eventData.buffer[3] = 0xaa;
  499. rval = Tss2_Sys_PCR_Event( sysContext, PCR_18, &sessionsData, &eventData, &digests, 0 );
  500. CheckPassed( rval );
  501. /* Reset locality and check whether extend PCR 17 is no possible */
  502. rval = Tss2_Tcti_SetLocality(tctiContext, 0);
  503. CheckPassed( rval );
  504. rval = Tss2_Sys_PCR_Extend( sysContext, PCR_17, &sessionsData, &digests, 0 );
  505. CheckFailed( rval, TPM2_RC_LOCALITY );
  506. }
  507. static void TestShutdown()
  508. {
  509. UINT32 rval;
  510. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  511. LOG_INFO("SHUTDOWN TESTS:" );
  512. rval = Tss2_Sys_Shutdown( sysContext, 0, TPM2_SU_STATE, &sessionsDataOut );
  513. CheckPassed( rval );
  514. rval = Tss2_Sys_Shutdown( sysContext, 0, TPM2_SU_CLEAR, &sessionsDataOut );
  515. CheckPassed( rval );
  516. rval = Tss2_Sys_Shutdown( sysContext, 0, 0xff, 0 );
  517. CheckFailed( rval, TPM2_RC_VALUE+TPM2_RC_1+TPM2_RC_P );
  518. }
  519. static void TestNV()
  520. {
  521. UINT32 rval;
  522. TPM2B_NV_PUBLIC publicInfo;
  523. TPM2B_AUTH nvAuth;
  524. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  525. int i;
  526. TPM2B_MAX_NV_BUFFER nvWriteData;
  527. TPM2B_MAX_NV_BUFFER nvData;
  528. TPM2B_NV_PUBLIC nvPublic;
  529. TPM2B_NAME nvName;
  530. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  531. .sessionHandle = TPM2_RH_PW,
  532. .sessionAttributes = 0,
  533. .nonce = {.size = 0},
  534. .hmac = {.size = 0}}}};
  535. LOG_INFO("NV INDEX TESTS:" );
  536. nvAuth.size = 20;
  537. for( i = 0; i < nvAuth.size; i++ ) {
  538. nvAuth.buffer[i] = (UINT8)i;
  539. }
  540. publicInfo.size = 0;
  541. publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST1;
  542. publicInfo.nvPublic.nameAlg = TPM2_ALG_SHA1;
  543. /* First zero out attributes. */
  544. *(UINT32 *)&( publicInfo.nvPublic.attributes ) = 0;
  545. /* Now set the attributes. */
  546. publicInfo.nvPublic.attributes |= TPMA_NV_PPREAD;
  547. publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
  548. publicInfo.nvPublic.attributes |= TPMA_NV_WRITE_STCLEAR;
  549. publicInfo.nvPublic.attributes |= TPMA_NV_PLATFORMCREATE;
  550. publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
  551. publicInfo.nvPublic.authPolicy.size = 0;
  552. publicInfo.nvPublic.dataSize = 32;
  553. INIT_SIMPLE_TPM2B_SIZE( nvData );
  554. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
  555. CheckFailed( rval, TPM2_RC_2 + TPM2_RC_HANDLE );
  556. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
  557. CheckPassed( rval );
  558. nvPublic.size = 0;
  559. INIT_SIMPLE_TPM2B_SIZE( nvName );
  560. rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
  561. CheckPassed( rval );
  562. INIT_SIMPLE_TPM2B_SIZE( nvData );
  563. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
  564. CheckFailed( rval, TPM2_RC_NV_UNINITIALIZED );
  565. /* Should fail since index is already defined. */
  566. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
  567. CheckFailed( rval, TPM2_RC_NV_DEFINED );
  568. nvWriteData.size = 4;
  569. for( i = 0; i < nvWriteData.size; i++ )
  570. nvWriteData.buffer[i] = 0xff - i;
  571. #if 1
  572. /*
  573. * The following, at one point, was commented out so that NVDefine will
  574. * work on successive invocations of client app.
  575. *
  576. * Noticed on 12/13/12, this doesn't seem to be necessary anymore.
  577. * Maybe something else fixed it.
  578. *
  579. * Seems to be a bug in TPM 2.0 simulator that if:
  580. * First pass of tpmclient.exe after restarting TPM 2.0 simulator will
  581. * work fine.
  582. * If NVWrite is done, subsequent invocations of tpmclient.exe will
  583. * ALWAYS fail on the first call to Tpm2NVDefineSpace with 0x2cb error.
  584. * Removing NVWrite fixes this.
  585. * And restarting TPM 2.0 simulator will make it work the first time
  586. * and fail subsequent times.
  587. * Removing NVWrite works around this problem.
  588. */
  589. rval = Tss2_Sys_NV_Write( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
  590. CheckPassed( rval );
  591. INIT_SIMPLE_TPM2B_SIZE( nvData );
  592. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
  593. CheckPassed( rval );
  594. rval = Tss2_Sys_NV_WriteLock( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &sessionsDataOut );
  595. CheckPassed( rval );
  596. rval = Tss2_Sys_NV_Write( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
  597. CheckFailed( rval, TPM2_RC_NV_LOCKED );
  598. #endif
  599. /* Now undefine the index. */
  600. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
  601. CheckPassed( rval );
  602. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
  603. CheckPassed( rval );
  604. /* Now undefine the index so that next run will work correctly. */
  605. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
  606. CheckPassed( rval );
  607. publicInfo.nvPublic.attributes &= ~TPMA_NV_PPREAD;
  608. publicInfo.nvPublic.attributes &= ~TPMA_NV_PPWRITE;
  609. publicInfo.nvPublic.attributes |= TPMA_NV_OWNERREAD;
  610. publicInfo.nvPublic.attributes |= TPMA_NV_OWNERWRITE;
  611. publicInfo.nvPublic.attributes &= ~TPMA_NV_PLATFORMCREATE;
  612. publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
  613. publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST2;
  614. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
  615. CheckPassed( rval );
  616. nvPublic.size = 0;
  617. INIT_SIMPLE_TPM2B_SIZE( nvName );
  618. rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST2, 0, &nvPublic, &nvName, 0 );
  619. CheckPassed( rval );
  620. INIT_SIMPLE_TPM2B_SIZE( nvData );
  621. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST2, &sessionsData, 32, 0, &nvData, 0 );
  622. CheckFailed( rval, TPM2_RC_NV_AUTHORIZATION );
  623. /* Now undefine the index. */
  624. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
  625. CheckPassed( rval );
  626. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
  627. CheckPassed( rval );
  628. /* Now undefine the index so that next run will work correctly. */
  629. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
  630. CheckPassed( rval );
  631. }
  632. static void TestHierarchyControl()
  633. {
  634. UINT32 rval;
  635. TPM2B_NV_PUBLIC publicInfo;
  636. TPM2B_AUTH nvAuth;
  637. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  638. int i;
  639. TPM2B_NAME nvName;
  640. TPM2B_NV_PUBLIC nvPublic;
  641. TPM2B_MAX_NV_BUFFER nvData;
  642. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  643. .sessionHandle = TPM2_RH_PW,
  644. .sessionAttributes = 0,
  645. .nonce = {.size = 0},
  646. .hmac = {.size = 0}}}};
  647. LOG_INFO("HIERARCHY CONTROL TESTS:" );
  648. nvAuth.size = 20;
  649. for( i = 0; i < nvAuth.size; i++ ) {
  650. nvAuth.buffer[i] = i;
  651. }
  652. publicInfo.size = 0;
  653. publicInfo.nvPublic.nvIndex = TPM20_INDEX_TEST1;
  654. publicInfo.nvPublic.nameAlg = TPM2_ALG_SHA1;
  655. /* First zero out attributes. */
  656. *(UINT32 *)&( publicInfo.nvPublic.attributes ) = 0;
  657. /* Now set the attributes. */
  658. publicInfo.nvPublic.attributes |= TPMA_NV_PPREAD;
  659. publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
  660. publicInfo.nvPublic.attributes |= TPMA_NV_PPWRITE;
  661. publicInfo.nvPublic.attributes |= TPMA_NV_WRITE_STCLEAR;
  662. publicInfo.nvPublic.attributes |= TPMA_NV_PLATFORMCREATE;
  663. publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
  664. publicInfo.nvPublic.authPolicy.size = 0;
  665. publicInfo.nvPublic.dataSize = 32;
  666. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
  667. CheckPassed( rval );
  668. /* Test SYS for case where nvPublic.size != 0 */
  669. nvPublic.size = 0xff;
  670. INIT_SIMPLE_TPM2B_SIZE( nvName );
  671. rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
  672. CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
  673. nvPublic.size = 0;
  674. INIT_SIMPLE_TPM2B_SIZE( nvName );
  675. rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
  676. CheckPassed( rval );
  677. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
  678. CheckFailed( rval, TPM2_RC_NV_UNINITIALIZED );
  679. rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, NO, &sessionsDataOut );
  680. CheckPassed( rval );
  681. rval = Tss2_Sys_NV_Read( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
  682. CheckFailed( rval, TPM2_RC_1 + TPM2_RC_HIERARCHY );
  683. rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, YES, &sessionsDataOut );
  684. CheckFailed( rval, TPM2_RC_1 + TPM2_RC_HIERARCHY );
  685. /* Need to do TPM reset and Startup to re-enable platform hierarchy. */
  686. rval = TpmReset();
  687. CheckPassed(rval);
  688. rval = Tss2_Sys_Startup ( sysContext, TPM2_SU_CLEAR );
  689. CheckPassed( rval );
  690. rval = Tss2_Sys_HierarchyControl( sysContext, TPM2_RH_PLATFORM, &sessionsData, TPM2_RH_PLATFORM, YES, &sessionsDataOut );
  691. CheckPassed( rval );
  692. /* Now undefine the index so that next run will work correctly. */
  693. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
  694. CheckPassed( rval );
  695. }
  696. typedef struct {
  697. char name[50];
  698. TSS2_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *trialPolicySession, TPM2B_DIGEST *policyDigest );
  699. TSS2_RC (*createObjectFn )( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest );
  700. TSS2_RC (*testPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession );
  701. } POLICY_TEST_SETUP;
  702. static TSS2_RC BuildPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession,
  703. TSS2_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest ),
  704. TPM2B_DIGEST *policyDigest, bool trialSession )
  705. {
  706. /*
  707. * NOTE: this policySession will be either a trial or normal policy session
  708. * depending on the value of the passed in trialSession parameter.
  709. */
  710. TPM2B_ENCRYPTED_SECRET encryptedSalt = {0,};
  711. TPMT_SYM_DEF symmetric;
  712. TSS2_RC rval;
  713. TPM2B_NONCE nonceCaller;
  714. nonceCaller.size = 0;
  715. /* Start policy session. */
  716. symmetric.algorithm = TPM2_ALG_NULL;
  717. rval = create_auth_session(policySession, TPM2_RH_NULL, 0, TPM2_RH_NULL, 0, &nonceCaller, &encryptedSalt, trialSession ? TPM2_SE_TRIAL : TPM2_SE_POLICY , &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  718. if( rval != TPM2_RC_SUCCESS )
  719. return rval;
  720. /* Send policy command. */
  721. rval = ( *buildPolicyFn )( sysContext, *policySession, policyDigest );
  722. CheckPassed( rval );
  723. /* Get policy hash. */
  724. INIT_SIMPLE_TPM2B_SIZE( *policyDigest );
  725. rval = Tss2_Sys_PolicyGetDigest( sysContext, (*policySession)->sessionHandle,
  726. 0, policyDigest, 0 );
  727. CheckPassed( rval );
  728. if( trialSession )
  729. {
  730. /* Need to flush the session here. */
  731. rval = Tss2_Sys_FlushContext( sysContext, (*policySession)->sessionHandle );
  732. CheckPassed( rval );
  733. /* And remove the session from sessions table. */
  734. end_auth_session( *policySession );
  735. }
  736. return rval;
  737. }
  738. static TSS2_RC CreateNVIndex( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest )
  739. {
  740. TSS2_RC rval = TPM2_RC_SUCCESS;
  741. TPMA_LOCALITY locality;
  742. TPM2B_ENCRYPTED_SECRET encryptedSalt = {0};
  743. TPMT_SYM_DEF symmetric;
  744. TPMA_NV nvAttributes;
  745. TPM2B_AUTH nvAuth;
  746. TPM2B_NONCE nonceCaller;
  747. nonceCaller.size = 0;
  748. /*
  749. * Since locality is a fairly simple command and we can guarantee
  750. * its correctness, we don't need a trial session for this.
  751. */
  752. /* Start real policy session */
  753. symmetric.algorithm = TPM2_ALG_NULL;
  754. rval = create_auth_session(policySession, TPM2_RH_NULL,
  755. 0, TPM2_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM2_SE_POLICY,
  756. &symmetric, TPM2_ALG_SHA256, resMgrTctiContext );
  757. CheckPassed( rval );
  758. /* Send PolicyLocality command */
  759. *(UINT8 *)( (void *)&locality ) = 0;
  760. locality |= TPMA_LOCALITY_TPM2_LOC_THREE;
  761. rval = Tss2_Sys_PolicyLocality( sysContext, (*policySession)->sessionHandle,
  762. 0, locality, 0 );
  763. CheckPassed( rval );
  764. /* Read policyHash */
  765. INIT_SIMPLE_TPM2B_SIZE( *policyDigest );
  766. rval = Tss2_Sys_PolicyGetDigest( sysContext,
  767. (*policySession)->sessionHandle, 0, policyDigest, 0 );
  768. CheckPassed( rval );
  769. nvAuth.size = 0;
  770. /* Now set the attributes. */
  771. *(UINT32 *)( (void *)&nvAttributes ) = 0;
  772. nvAttributes |= TPMA_NV_POLICYREAD;
  773. nvAttributes |= TPMA_NV_POLICYWRITE;
  774. nvAttributes |= TPMA_NV_PLATFORMCREATE;
  775. rval = DefineNvIndex( sysContext, TPM2_RH_PLATFORM, &nvAuth, policyDigest,
  776. TPM20_INDEX_PASSWORD_TEST, TPM2_ALG_SHA256, nvAttributes, 32 );
  777. CheckPassed( rval );
  778. rval = AddEntity(TPM20_INDEX_PASSWORD_TEST, &nvAuth);
  779. CheckPassed(rval);
  780. return rval;
  781. }
  782. static TSS2_RC TestLocality( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession )
  783. {
  784. TSS2_RC rval = TPM2_RC_SUCCESS;
  785. TPM2B_MAX_NV_BUFFER nvWriteData;
  786. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut = { 1, };
  787. TSS2_TCTI_CONTEXT *tctiContext;
  788. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths = {{
  789. .sessionHandle = policySession->sessionHandle,
  790. .sessionAttributes = TPMA_SESSION_CONTINUESESSION,
  791. .nonce = {.size = 0},
  792. .hmac = {.size = 0}}}};
  793. /* Init write data. */
  794. nvWriteData.size = 0;
  795. rval = Tss2_Sys_GetTctiContext(sysContext, &tctiContext);
  796. CheckPassed(rval);
  797. rval = Tss2_Tcti_SetLocality(tctiContext, 2);
  798. CheckPassed(rval);
  799. /* Do NV write using open session's policy. */
  800. rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
  801. TPM20_INDEX_PASSWORD_TEST,
  802. &sessionsData, &nvWriteData, 0, &sessionsDataOut );
  803. CheckFailed( rval, TPM2_RC_LOCALITY );
  804. rval = Tss2_Tcti_SetLocality(tctiContext, 3);
  805. CheckPassed( rval );
  806. /* Do NV write using open session's policy. */
  807. rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
  808. TPM20_INDEX_PASSWORD_TEST,
  809. &sessionsData, &nvWriteData, 0, &sessionsDataOut );
  810. CheckPassed( rval );
  811. /* Do another NV write using open session's policy. */
  812. rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
  813. TPM20_INDEX_PASSWORD_TEST,
  814. &sessionsData, &nvWriteData, 0, &sessionsDataOut );
  815. CheckFailed( rval, TPM2_RC_POLICY_FAIL + TPM2_RC_S + TPM2_RC_1 );
  816. /* Delete NV index */
  817. sessionsData.auths[0].sessionHandle = TPM2_RH_PW;
  818. sessionsData.auths[0].nonce.size = 0;
  819. sessionsData.auths[0].nonce.buffer[0] = 0xa5;
  820. sessionsData.auths[0].hmac.size = 0;
  821. /* Now undefine the index. */
  822. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM,
  823. TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
  824. CheckPassed( rval );
  825. DeleteEntity(TPM20_INDEX_PASSWORD_TEST);
  826. /* Reset locality */
  827. rval = Tss2_Tcti_SetLocality(tctiContext, 0);
  828. CheckPassed( rval );
  829. return rval;
  830. }
  831. UINT8 passwordPCRTestPassword[] = "password PCR";
  832. UINT8 dataBlob[] = "some data";
  833. TPM2_HANDLE blobHandle;
  834. TPM2B_AUTH blobAuth;
  835. static TSS2_RC BuildPasswordPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest )
  836. {
  837. TSS2_RC rval = TPM2_RC_SUCCESS;
  838. rval = Tss2_Sys_PolicyPassword( sysContext, policySession->sessionHandle, 0, 0 );
  839. CheckPassed( rval );
  840. return rval;
  841. }
  842. static TSS2_RC CreateDataBlob( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest )
  843. {
  844. TSS2_RC rval = TPM2_RC_SUCCESS;
  845. TPM2B_SENSITIVE_CREATE inSensitive;
  846. TPM2B_PUBLIC inPublic;
  847. TPM2B_DATA outsideInfo = {0,};
  848. TPML_PCR_SELECTION creationPcr = { 0 };
  849. TPM2B_PUBLIC outPublic;
  850. TPM2B_CREATION_DATA creationData;
  851. TPM2_HANDLE srkHandle;
  852. TPM2B_DIGEST creationHash;
  853. TPMT_TK_CREATION creationTicket;
  854. TPM2B_NAME srkName, blobName;
  855. TPM2B_DIGEST data;
  856. TPM2B_PRIVATE outPrivate;
  857. TSS2L_SYS_AUTH_COMMAND cmdAuthArray = { .count = 1, .auths = {{
  858. .sessionHandle = TPM2_RH_PW,
  859. .sessionAttributes = 0,
  860. .nonce = {.size = 0},
  861. .hmac = {.size = 0}}}};
  862. inSensitive.size = 0;
  863. inSensitive.sensitive.userAuth.size = 0;
  864. inSensitive.sensitive.data.size = 0;
  865. inPublic.size = 0;
  866. inPublic.publicArea.type = TPM2_ALG_RSA;
  867. inPublic.publicArea.nameAlg = TPM2_ALG_SHA1;
  868. *(UINT32 *)&( inPublic.publicArea.objectAttributes) = 0;
  869. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_RESTRICTED;
  870. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  871. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_DECRYPT;
  872. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
  873. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
  874. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
  875. inPublic.publicArea.authPolicy.size = 0;
  876. inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM2_ALG_AES;
  877. inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
  878. inPublic.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM2_ALG_CBC;
  879. inPublic.publicArea.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
  880. inPublic.publicArea.parameters.rsaDetail.keyBits = 2048;
  881. inPublic.publicArea.parameters.rsaDetail.exponent = 0;
  882. inPublic.publicArea.unique.rsa.size = 0;
  883. outPublic.size = 0;
  884. creationData.size = 0;
  885. INIT_SIMPLE_TPM2B_SIZE( creationHash );
  886. INIT_SIMPLE_TPM2B_SIZE( srkName );
  887. rval = Tss2_Sys_CreatePrimary( sysContext, TPM2_RH_PLATFORM, &cmdAuthArray,
  888. &inSensitive, &inPublic, &outsideInfo, &creationPcr,
  889. &srkHandle, &outPublic, &creationData, &creationHash,
  890. &creationTicket, &srkName, 0 );
  891. CheckPassed( rval );
  892. cmdAuthArray.auths[0].sessionHandle = TPM2_RH_PW;
  893. inSensitive.sensitive.userAuth.size = 0;
  894. blobAuth.size = sizeof( passwordPCRTestPassword );
  895. memcpy( &blobAuth.buffer, passwordPCRTestPassword, sizeof( passwordPCRTestPassword ) );
  896. CopySizedByteBuffer((TPM2B *)&inSensitive.sensitive.userAuth, (TPM2B *)&blobAuth);
  897. data.size = sizeof( dataBlob );
  898. memcpy( &data.buffer, dataBlob, sizeof( dataBlob ) );
  899. CopySizedByteBuffer((TPM2B *)&inSensitive.sensitive.data, (TPM2B *)&data);
  900. inPublic.publicArea.type = TPM2_ALG_KEYEDHASH;
  901. inPublic.publicArea.nameAlg = TPM2_ALG_SHA256;
  902. inPublic.publicArea.objectAttributes &= ~TPMA_OBJECT_RESTRICTED;
  903. inPublic.publicArea.objectAttributes &= ~TPMA_OBJECT_DECRYPT;
  904. inPublic.publicArea.objectAttributes &= ~TPMA_OBJECT_SENSITIVEDATAORIGIN;
  905. CopySizedByteBuffer((TPM2B *)&inPublic.publicArea.authPolicy, (TPM2B *)policyDigest);
  906. inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_NULL;
  907. inPublic.publicArea.unique.keyedHash.size = 0;
  908. outPublic.size = 0;
  909. creationData.size = 0;
  910. INIT_SIMPLE_TPM2B_SIZE( outPrivate );
  911. INIT_SIMPLE_TPM2B_SIZE( creationHash );
  912. rval = Tss2_Sys_Create( sysContext, srkHandle, &cmdAuthArray,
  913. &inSensitive, &inPublic, &outsideInfo, &creationPcr,
  914. &outPrivate, &outPublic, &creationData, &creationHash,
  915. &creationTicket, 0 );
  916. CheckPassed( rval );
  917. /* Now we need to load the object. */
  918. INIT_SIMPLE_TPM2B_SIZE( blobName );
  919. rval = Tss2_Sys_Load( sysContext, srkHandle, &cmdAuthArray, &outPrivate, &outPublic, &blobHandle, &blobName, 0 );
  920. CheckPassed( rval );
  921. return rval;
  922. }
  923. static TSS2_RC PasswordUnseal( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession )
  924. {
  925. TSS2_RC rval = TPM2_RC_SUCCESS;
  926. TPM2B_SENSITIVE_DATA outData;
  927. TSS2L_SYS_AUTH_COMMAND cmdAuthArray = { .count = 1, .auths = {{
  928. .sessionHandle = policySession->sessionHandle,
  929. .sessionAttributes = TPMA_SESSION_CONTINUESESSION,
  930. .nonce = {.size = 0},
  931. .hmac = {.size = 0}}}};
  932. /*
  933. * Now try to unseal the blob without setting the password.
  934. * This test should fail.
  935. */
  936. INIT_SIMPLE_TPM2B_SIZE( outData );
  937. rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
  938. CheckFailed( rval, TPM2_RC_S + TPM2_RC_1 + TPM2_RC_AUTH_FAIL );
  939. /* Clear DA lockout. */
  940. TestDictionaryAttackLockReset();
  941. /*
  942. * Now try to unseal the blob after setting the password.
  943. * This test should pass.
  944. */
  945. INIT_SIMPLE_TPM2B_SIZE( outData );
  946. cmdAuthArray.auths[0].hmac.size = sizeof( passwordPCRTestPassword );
  947. memcpy( &cmdAuthArray.auths[0].hmac.buffer, passwordPCRTestPassword, sizeof( passwordPCRTestPassword ) );
  948. rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
  949. CheckPassed( rval );
  950. /* Add test to make sure we unsealed correctly. */
  951. /*
  952. * Now we'll want to flush the data blob and remove it
  953. * from resource manager tables.
  954. */
  955. rval = Tss2_Sys_FlushContext( sysContext, blobHandle );
  956. CheckPassed( rval );
  957. return rval;
  958. }
  959. POLICY_TEST_SETUP policyTestSetups[] =
  960. {
  961. /*
  962. * NOTE: Since locality is a fairly simple command and we
  963. * can guarantee its correctness, we don't need a trial
  964. * session for this. buildPolicyFn pointer can be 0 in
  965. * this case.
  966. */
  967. { "LOCALITY", 0, CreateNVIndex, TestLocality },
  968. { "PASSWORD", BuildPasswordPolicy, CreateDataBlob, PasswordUnseal },
  969. };
  970. static void TestPolicy()
  971. {
  972. UINT32 rval;
  973. unsigned int i;
  974. SESSION *policySession = 0;
  975. LOG_INFO("POLICY TESTS:" );
  976. for( i = 0; i < ( sizeof( policyTestSetups ) / sizeof( POLICY_TEST_SETUP ) ); i++ )
  977. {
  978. TPM2B_DIGEST policyDigest;
  979. policyDigest.size = 0;
  980. rval = TPM2_RC_SUCCESS;
  981. LOG_INFO("Policy Test: %s", policyTestSetups[i].name );
  982. /* Create trial policy session and run policy commands, in order to create policyDigest. */
  983. if( policyTestSetups[i].buildPolicyFn != 0)
  984. {
  985. rval = BuildPolicy( sysContext, &policySession, policyTestSetups[i].buildPolicyFn, &policyDigest, true );
  986. CheckPassed( rval );
  987. LOGBLOB_DEBUG(&(policyDigest.buffer[0]), policyDigest.size, "Built policy digest:");
  988. }
  989. /* Create entity that will use that policyDigest as authPolicy. */
  990. if( policyTestSetups[i].createObjectFn != 0 )
  991. {
  992. LOGBLOB_DEBUG(&(policyDigest.buffer[0]), policyDigest.size,
  993. "Policy digest used to create object:");
  994. rval = ( *policyTestSetups[i].createObjectFn )( sysContext, &policySession, &policyDigest);
  995. CheckPassed( rval );
  996. }
  997. /*
  998. * Create real policy session and run policy commands; after this
  999. * we're ready to authorize actions on the entity.
  1000. */
  1001. if( policyTestSetups[i].buildPolicyFn != 0)
  1002. {
  1003. rval = BuildPolicy( sysContext, &policySession, policyTestSetups[i].buildPolicyFn, &policyDigest, false );
  1004. CheckPassed( rval );
  1005. LOGBLOB_DEBUG(&(policyDigest.buffer[0]), policyDigest.size,
  1006. "Command policy digest: ");
  1007. }
  1008. if( policySession )
  1009. {
  1010. /* Now do tests by authorizing actions on the entity. */
  1011. rval = ( *policyTestSetups[i].testPolicyFn)( sysContext, policySession );
  1012. CheckPassed( rval );
  1013. /* Need to flush the session here. */
  1014. rval = Tss2_Sys_FlushContext( sysContext, policySession->sessionHandle );
  1015. CheckPassed( rval );
  1016. /* And remove the session from test app session table. */
  1017. end_auth_session( policySession );
  1018. }
  1019. else
  1020. {
  1021. CheckFailed( rval, 0xffffffff );
  1022. }
  1023. }
  1024. }
  1025. #define MAX_TEST_SEQUENCES 10
  1026. static void TestHash()
  1027. {
  1028. UINT32 rval;
  1029. TPM2B_AUTH auth;
  1030. TPMI_DH_OBJECT sequenceHandle[MAX_TEST_SEQUENCES];
  1031. TSS2L_SYS_AUTH_COMMAND sessionsData;
  1032. TPM2B_MAX_BUFFER dataToHash;
  1033. TPM2B_DIGEST result;
  1034. TPMT_TK_HASHCHECK validation;
  1035. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1036. UINT8 memoryToHash[] =
  1037. {
  1038. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1039. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1040. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1041. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1042. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1043. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1044. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1045. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1046. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1047. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1048. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1049. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1050. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1051. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1052. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1053. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1054. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1055. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1056. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1057. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1058. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1059. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1060. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1061. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1062. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1063. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1064. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1065. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1066. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1067. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1068. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1069. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1070. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1071. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1072. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1073. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1074. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1075. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1076. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1077. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1078. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1079. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1080. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1081. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1082. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1083. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1084. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1085. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1086. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1087. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1088. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1089. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1090. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1091. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1092. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1093. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1094. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1095. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1096. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1097. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1098. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1099. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1101. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1102. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1103. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  1104. 0xde, 0xad, 0xbe, 0xef };
  1105. UINT8 goodHashValue[] =
  1106. { 0xB3, 0xFD, 0x6A, 0xD2, 0x9F, 0xD0, 0x13, 0x52, 0xBA, 0xFC,
  1107. 0x8B, 0x22, 0xC9, 0x6D, 0x88, 0x42, 0xA3, 0x3C, 0xB0, 0xC9 };
  1108. LOG_INFO("HASH TESTS:" );
  1109. auth.size = 2;
  1110. auth.buffer[0] = 0;
  1111. auth.buffer[1] = 0xff;
  1112. rval = Tss2_Sys_HashSequenceStart ( sysContext, 0, &auth, TPM2_ALG_SHA1, &sequenceHandle[0], 0 );
  1113. CheckPassed( rval );
  1114. sessionsData.auths[0].sessionHandle = TPM2_RH_PW;
  1115. sessionsData.auths[0].nonce.size = 0;
  1116. sessionsData.auths[0].hmac = auth;
  1117. sessionsData.auths[0].sessionAttributes = 0;
  1118. sessionsData.count = 1;
  1119. dataToHash.size = TPM2_MAX_DIGEST_BUFFER;
  1120. memcpy( &dataToHash.buffer[0], &memoryToHash[0], dataToHash.size );
  1121. rval = Tss2_Sys_SequenceUpdate ( sysContext, sequenceHandle[0], &sessionsData, &dataToHash, &sessionsDataOut );
  1122. CheckPassed( rval );
  1123. dataToHash.size = sizeof( memoryToHash ) - TPM2_MAX_DIGEST_BUFFER;
  1124. memcpy( &dataToHash.buffer[0], &memoryToHash[TPM2_MAX_DIGEST_BUFFER], dataToHash.size );
  1125. INIT_SIMPLE_TPM2B_SIZE( result );
  1126. rval = Tss2_Sys_SequenceComplete ( sysContext, sequenceHandle[0], &sessionsData, &dataToHash,
  1127. TPM2_RH_PLATFORM, &result, &validation, &sessionsDataOut );
  1128. CheckPassed( rval );
  1129. /* Test the resulting hash. */
  1130. if (memcmp(result.buffer, goodHashValue, result.size)) {
  1131. LOG_ERROR("ERROR!! resulting hash is incorrect." );
  1132. Cleanup();
  1133. }
  1134. }
  1135. static void TestQuote()
  1136. {
  1137. UINT32 rval;
  1138. TPM2B_DATA qualifyingData;
  1139. UINT8 qualDataString[] = { 0x00, 0xff, 0x55, 0xaa };
  1140. TPMT_SIG_SCHEME inScheme;
  1141. TPML_PCR_SELECTION pcrSelection;
  1142. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1143. TPM2B_ATTEST quoted;
  1144. TPMT_SIGNATURE signature;
  1145. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1,
  1146. .auths = {{
  1147. .sessionHandle = TPM2_RH_PW,
  1148. .sessionAttributes = 0,
  1149. .nonce = { .size = 0 },
  1150. .hmac = { .size = 0, .buffer={0x00} },
  1151. }},
  1152. };
  1153. TPM2_HANDLE handle, handle_parent;
  1154. LOG_INFO("QUOTE CONTROL TESTS:" );
  1155. qualifyingData.size = sizeof( qualDataString );
  1156. memcpy( &( qualifyingData.buffer[0] ), qualDataString, sizeof( qualDataString ) );
  1157. inScheme.scheme = TPM2_ALG_NULL;
  1158. pcrSelection.count = 1;
  1159. pcrSelection.pcrSelections[0].hash = TPM2_ALG_SHA256;
  1160. pcrSelection.pcrSelections[0].sizeofSelect = 3;
  1161. /* Clear out PCR select bit field */
  1162. pcrSelection.pcrSelections[0].pcrSelect[0] = 0;
  1163. pcrSelection.pcrSelections[0].pcrSelect[1] = 0;
  1164. pcrSelection.pcrSelections[0].pcrSelect[2] = 0;
  1165. /* Now set the PCR you want */
  1166. pcrSelection.pcrSelections[0].pcrSelect[( PCR_17/8 )] = ( 1 << ( PCR_18 % 8) );
  1167. rval = create_primary_rsa_2048_aes_128_cfb(sysContext, &handle_parent);
  1168. CheckPassed( rval );
  1169. rval = create_keyedhash_key (sysContext, handle_parent, &handle);
  1170. CheckPassed( rval );
  1171. /* Test with wrong type of key. */
  1172. INIT_SIMPLE_TPM2B_SIZE( quoted );
  1173. rval = Tss2_Sys_Quote ( sysContext, handle, &sessionsData, &qualifyingData, &inScheme,
  1174. &pcrSelection, &quoted, &signature, &sessionsDataOut );
  1175. CheckPassed( rval );
  1176. rval = Tss2_Sys_FlushContext(sysContext, handle);
  1177. CheckPassed( rval );
  1178. rval = Tss2_Sys_FlushContext(sysContext, handle_parent);
  1179. CheckPassed( rval );
  1180. }
  1181. TSS2L_SYS_AUTH_COMMAND nullSessionsData = { 1, { 0 } };
  1182. TSS2L_SYS_AUTH_RESPONSE nullSessionsDataOut = { 0, { 0 } };
  1183. TPM2B_NONCE nullSessionNonce, nullSessionNonceOut;
  1184. TPM2B_AUTH nullSessionHmac;
  1185. static void TestPcrAllocate()
  1186. {
  1187. UINT32 rval;
  1188. TPML_PCR_SELECTION pcrSelection;
  1189. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1190. TPMI_YES_NO allocationSuccess;
  1191. UINT32 maxPcr;
  1192. UINT32 sizeNeeded;
  1193. UINT32 sizeAvailable;
  1194. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths= {{
  1195. .sessionHandle = TPM2_RH_PW,
  1196. .sessionAttributes = 0,
  1197. .nonce={.size=0},
  1198. .hmac={.size=0}}}};
  1199. LOG_INFO("PCR ALLOCATE TEST :" );
  1200. pcrSelection.count = 0;
  1201. rval = Tss2_Sys_PCR_Allocate( sysContext, TPM2_RH_PLATFORM, &sessionsData, &pcrSelection,
  1202. &allocationSuccess, &maxPcr, &sizeNeeded, &sizeAvailable, &sessionsDataOut);
  1203. CheckPassed( rval );
  1204. pcrSelection.count = 3;
  1205. pcrSelection.pcrSelections[0].hash = TPM2_ALG_SHA256;
  1206. CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[0] );
  1207. SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[0], 3 );
  1208. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_5 );
  1209. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_7 );
  1210. pcrSelection.pcrSelections[1].hash = TPM2_ALG_SHA384;
  1211. CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[1] );
  1212. SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[1], 3 );
  1213. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[1], PCR_5 );
  1214. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[1], PCR_8 );
  1215. pcrSelection.pcrSelections[2].hash = TPM2_ALG_SHA256;
  1216. CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[2] );
  1217. SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[2], 3 );
  1218. SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[2], PCR_6 );
  1219. rval = Tss2_Sys_PCR_Allocate( sysContext, TPM2_RH_PLATFORM, &sessionsData, &pcrSelection,
  1220. &allocationSuccess, &maxPcr, &sizeNeeded, &sizeAvailable, &sessionsDataOut);
  1221. CheckPassed( rval );
  1222. }
  1223. static void TestUnseal()
  1224. {
  1225. UINT32 rval;
  1226. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1227. TPM2B_SENSITIVE_CREATE inSensitive;
  1228. TPML_PCR_SELECTION creationPCR;
  1229. TPM2B_DATA outsideInfo;
  1230. TPM2B_PUBLIC inPublic;
  1231. TPM2_HANDLE loadedObjectHandle, handle_parent;
  1232. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1,
  1233. .auths= {{
  1234. .sessionHandle = TPM2_RH_PW,
  1235. .sessionAttributes = 0,
  1236. .nonce={.size=0},
  1237. .hmac={.size=0, .buffer={0x00}}
  1238. }},
  1239. };
  1240. TPM2B_PRIVATE outPrivate;
  1241. TPM2B_PUBLIC outPublic;
  1242. TPM2B_CREATION_DATA creationData;
  1243. TPM2B_DIGEST creationHash;
  1244. TPMT_TK_CREATION creationTicket;
  1245. TPM2B_NAME name;
  1246. TPM2B_SENSITIVE_DATA outData;
  1247. const char authStr[] = "test";
  1248. const char sensitiveData[] = "this is sensitive";
  1249. LOG_INFO("UNSEAL TEST :" );
  1250. inSensitive.size = 0;
  1251. inSensitive.sensitive.userAuth.size = sizeof( authStr ) - 1;
  1252. memcpy( &( inSensitive.sensitive.userAuth.buffer[0] ), authStr, sizeof( authStr ) - 1 );
  1253. inSensitive.sensitive.data.size = sizeof( sensitiveData ) - 1;
  1254. memcpy( &( inSensitive.sensitive.data.buffer[0] ), sensitiveData, sizeof( sensitiveData ) - 1 );
  1255. inPublic.size = 0;
  1256. inPublic.publicArea.authPolicy.size = 0;
  1257. inPublic.publicArea.unique.keyedHash.size = 0;
  1258. outsideInfo.size = 0;
  1259. creationPCR.count = 0;
  1260. inPublic.publicArea.type = TPM2_ALG_KEYEDHASH;
  1261. inPublic.publicArea.nameAlg = TPM2_ALG_SHA1;
  1262. *(UINT32 *)&( inPublic.publicArea.objectAttributes) = 0;
  1263. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  1264. inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_NULL;
  1265. inPublic.publicArea.unique.keyedHash.size = 0;
  1266. outsideInfo.size = 0;
  1267. outPublic.size = 0;
  1268. creationData.size = 0;
  1269. rval = create_primary_rsa_2048_aes_128_cfb(sysContext, &handle_parent);
  1270. CheckPassed( rval );
  1271. INIT_SIMPLE_TPM2B_SIZE( creationHash );
  1272. INIT_SIMPLE_TPM2B_SIZE( outPrivate );
  1273. rval = Tss2_Sys_Create( sysContext, handle_parent, &sessionsData, &inSensitive, &inPublic,
  1274. &outsideInfo, &creationPCR,
  1275. &outPrivate, &outPublic, &creationData,
  1276. &creationHash, &creationTicket, &sessionsDataOut );
  1277. CheckPassed( rval );
  1278. INIT_SIMPLE_TPM2B_SIZE( name );
  1279. rval = Tss2_Sys_LoadExternal ( sysContext, 0, 0, &outPublic,
  1280. TPM2_RH_PLATFORM, &loadedObjectHandle, &name, 0 );
  1281. CheckPassed( rval );
  1282. rval = Tss2_Sys_FlushContext( sysContext, loadedObjectHandle );
  1283. CheckPassed( rval );
  1284. INIT_SIMPLE_TPM2B_SIZE( name );
  1285. rval = Tss2_Sys_Load (sysContext, handle_parent, &sessionsData, &outPrivate, &outPublic,
  1286. &loadedObjectHandle, &name, &sessionsDataOut);
  1287. CheckPassed(rval);
  1288. sessionsData.auths[0].hmac.size = sizeof( authStr ) - 1;
  1289. memcpy( &( sessionsData.auths[0].hmac.buffer[0] ), authStr, sizeof( authStr ) - 1 );
  1290. INIT_SIMPLE_TPM2B_SIZE( outData );
  1291. rval = Tss2_Sys_Unseal( sysContext, loadedObjectHandle, &sessionsData, &outData, &sessionsDataOut );
  1292. CheckPassed(rval);
  1293. rval = Tss2_Sys_FlushContext(sysContext, loadedObjectHandle);
  1294. CheckPassed(rval);
  1295. rval = Tss2_Sys_FlushContext(sysContext, handle_parent);
  1296. CheckPassed(rval);
  1297. }
  1298. static void CreatePasswordTestNV( TPMI_RH_NV_INDEX nvIndex, char * password )
  1299. {
  1300. UINT32 rval;
  1301. int i;
  1302. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1303. TPM2B_NV_PUBLIC publicInfo;
  1304. TPM2B_AUTH nvAuth;
  1305. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths= {{
  1306. .sessionHandle = TPM2_RH_PW,
  1307. .sessionAttributes = 0,
  1308. .nonce={.size=0},
  1309. .hmac={.size=0}}}};
  1310. nvAuth.size = strlen( password );
  1311. for( i = 0; i < nvAuth.size; i++ ) {
  1312. nvAuth.buffer[i] = password[i];
  1313. }
  1314. publicInfo.size = 0;
  1315. publicInfo.nvPublic.nvIndex = nvIndex;
  1316. publicInfo.nvPublic.nameAlg = TPM2_ALG_SHA1;
  1317. /* First zero out attributes. */
  1318. *(UINT32 *)&( publicInfo.nvPublic.attributes ) = 0;
  1319. /* Now set the attributes. */
  1320. publicInfo.nvPublic.attributes |= TPMA_NV_AUTHREAD;
  1321. publicInfo.nvPublic.attributes |= TPMA_NV_AUTHWRITE;
  1322. publicInfo.nvPublic.attributes |= TPMA_NV_PLATFORMCREATE;
  1323. publicInfo.nvPublic.attributes |= TPMA_NV_ORDERLY;
  1324. publicInfo.nvPublic.authPolicy.size = 0;
  1325. publicInfo.nvPublic.dataSize = 32;
  1326. rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM2_RH_PLATFORM,
  1327. &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
  1328. CheckPassed( rval );
  1329. }
  1330. static void PasswordTest()
  1331. {
  1332. char *password = "test password";
  1333. UINT32 rval;
  1334. int i;
  1335. /* Authorization array for response. */
  1336. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1337. /* Authorization array for command (only has one auth structure). */
  1338. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths= {{
  1339. .sessionHandle = TPM2_RH_PW,
  1340. .sessionAttributes = 0,
  1341. .nonce={.size=0},
  1342. .hmac={.size=0}}}};
  1343. TPM2B_MAX_NV_BUFFER nvWriteData;
  1344. LOG_INFO("PASSWORD TESTS:" );
  1345. /*
  1346. * Create an NV index that will use password authorizations.
  1347. * The password will be "test password".
  1348. */
  1349. CreatePasswordTestNV( TPM20_INDEX_PASSWORD_TEST, password );
  1350. /* Init password (HMAC field in authorization structure). */
  1351. sessionsData.auths[0].hmac.size = strlen( password );
  1352. memcpy( &( sessionsData.auths[0].hmac.buffer[0] ),
  1353. &( password[0] ), sessionsData.auths[0].hmac.size );
  1354. /* Initialize write data. */
  1355. nvWriteData.size = 4;
  1356. for( i = 0; i < nvWriteData.size; i++ )
  1357. nvWriteData.buffer[i] = 0xff - i;
  1358. /*
  1359. * Attempt write with the correct password.
  1360. * It should pass.
  1361. */
  1362. do {
  1363. rval = Tss2_Sys_NV_Write( sysContext,
  1364. TPM20_INDEX_PASSWORD_TEST,
  1365. TPM20_INDEX_PASSWORD_TEST,
  1366. &sessionsData, &nvWriteData, 0,
  1367. &sessionsDataOut );
  1368. } while (rval == TPM2_RC_RETRY);
  1369. /*
  1370. * Check that the function passed as
  1371. * expected. Otherwise, exit.
  1372. */
  1373. CheckPassed( rval );
  1374. /* Alter the password so it's incorrect. */
  1375. sessionsData.auths[0].hmac.buffer[4] = 0xff;
  1376. rval = Tss2_Sys_NV_Write( sysContext,
  1377. TPM20_INDEX_PASSWORD_TEST,
  1378. TPM20_INDEX_PASSWORD_TEST,
  1379. &sessionsData, &nvWriteData, 0,
  1380. &sessionsDataOut );
  1381. /*
  1382. * Check that the function failed as expected,
  1383. * since password was incorrect. If wrong
  1384. * response code received, exit.
  1385. */
  1386. CheckFailed( rval,
  1387. TPM2_RC_S + TPM2_RC_1 + TPM2_RC_AUTH_FAIL );
  1388. /*
  1389. * Change hmac to null one, since null auth is
  1390. * used to undefine the index.
  1391. */
  1392. sessionsData.auths[0].hmac.size = 0;
  1393. /* Now undefine the index. */
  1394. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM,
  1395. TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
  1396. CheckPassed( rval );
  1397. }
  1398. static void GetSetDecryptParamTests()
  1399. {
  1400. TPM2B_MAX_NV_BUFFER nvWriteData = {4, { 0xde, 0xad, 0xbe, 0xef,} };
  1401. TPM2B_MAX_NV_BUFFER nvWriteData1 = {4, { 0x01, 0x01, 0x02, 0x03,} };
  1402. const uint8_t *decryptParamBuffer;
  1403. size_t decryptParamSize;
  1404. size_t cpBufferUsedSize1, cpBufferUsedSize2;
  1405. const uint8_t *cpBuffer1, *cpBuffer2;
  1406. TSS2_RC rval;
  1407. int i;
  1408. TSS2_SYS_CONTEXT *decryptParamTestSysContext;
  1409. LOG_INFO("GET/SET DECRYPT PARAM TESTS:" );
  1410. /* Create two sysContext structures. */
  1411. decryptParamTestSysContext = sys_init_from_tcti_ctx(resMgrTctiContext);
  1412. if (decryptParamTestSysContext == NULL)
  1413. InitSysContextFailure();
  1414. /* Test for bad sequence: Tss2_Sys_GetDecryptParam */
  1415. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
  1416. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
  1417. /* Test for bad sequence: Tss2_Sys_SetDecryptParam */
  1418. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.buffer[0] ) );
  1419. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
  1420. /*
  1421. * NOTE: Two tests for BAD_SEQUENCE for GetDecryptParam and
  1422. * SetDecryptParam after ExecuteAsync are in the GetSetEncryptParamTests
  1423. * function, just because it's easier to do this way.
  1424. */
  1425. /* Do Prepare. */
  1426. rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
  1427. TPM20_INDEX_PASSWORD_TEST, &nvWriteData1, 0x55aa );
  1428. CheckPassed( rval );
  1429. /* Test for bad reference: Tss2_Sys_GetDecryptParam */
  1430. rval = Tss2_Sys_GetDecryptParam( 0, &decryptParamSize, &decryptParamBuffer );
  1431. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
  1432. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, 0, &decryptParamBuffer );
  1433. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
  1434. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, 0 );
  1435. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
  1436. /* Test for bad reference: Tss2_Sys_SetDecryptParam */
  1437. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, 0 );
  1438. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
  1439. rval = Tss2_Sys_SetDecryptParam( 0, 4, 0 );
  1440. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
  1441. /* Test for bad size. */
  1442. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 5, &( nvWriteData.buffer[0] ) );
  1443. CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
  1444. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 3, &( nvWriteData.buffer[0] ) );
  1445. CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
  1446. /* Test for good size. */
  1447. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.buffer[0] ) );
  1448. CheckPassed( rval );
  1449. /* Make sure that the set operation really did the right thing. */
  1450. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
  1451. CheckPassed( rval );
  1452. for( i = 0; i < 4; i++ )
  1453. {
  1454. if( decryptParamBuffer[i] != nvWriteData.buffer[i] )
  1455. {
  1456. LOG_ERROR("ERROR!! decryptParamBuffer[%d] s/b: %2.2x, was: %2.2x", i, nvWriteData.buffer[i], decryptParamBuffer[i] );
  1457. Cleanup();
  1458. }
  1459. }
  1460. rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize1, &cpBuffer1 );
  1461. CheckPassed( rval );
  1462. LOGBLOB_DEBUG((UINT8 *)cpBuffer1, cpBufferUsedSize1, "cpBuffer = ");
  1463. /* Test for no decrypt param. */
  1464. rval = Tss2_Sys_NV_Read_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, sizeof( nvWriteData ) - 2, 0 );
  1465. CheckPassed( rval );
  1466. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
  1467. CheckFailed( rval, TSS2_SYS_RC_NO_DECRYPT_PARAM );
  1468. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.buffer[0] ) );
  1469. CheckFailed( rval, TSS2_SYS_RC_NO_DECRYPT_PARAM );
  1470. /* Null decrypt param. */
  1471. rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
  1472. TPM20_INDEX_PASSWORD_TEST, 0, 0x55aa );
  1473. CheckPassed( rval );
  1474. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
  1475. CheckPassed( rval );
  1476. /* Check that size == 0. */
  1477. if( decryptParamSize != 0 )
  1478. {
  1479. LOG_ERROR("ERROR!! decryptParamSize s/b: 0, was: %u", (unsigned int)decryptParamSize );
  1480. Cleanup();
  1481. }
  1482. /* Test for insufficient size. */
  1483. /* Create a buffer that is too large */
  1484. size_t testBufferSize = TPM2_MAX_COMMAND_SIZE -
  1485. BE_TO_HOST_32(((TPM20_Header_In *)(((_TSS2_SYS_CONTEXT_BLOB *)decryptParamTestSysContext)->cmdBuffer))->commandSize) + 1;
  1486. UINT8 testBuffer [testBufferSize];
  1487. memset(testBuffer, 0, testBufferSize);
  1488. memcpy(testBuffer, nvWriteData.buffer, nvWriteData.size);
  1489. rval = Tss2_Sys_GetCpBuffer(decryptParamTestSysContext, &cpBufferUsedSize2, &cpBuffer2);
  1490. CheckPassed(rval);
  1491. rval = Tss2_Sys_SetDecryptParam(decryptParamTestSysContext, testBufferSize,
  1492. testBuffer);
  1493. CheckFailed(rval, TSS2_SYS_RC_INSUFFICIENT_CONTEXT);
  1494. /*
  1495. * Test that one less will work.
  1496. * This tests that we're checking the correct corner case.
  1497. */
  1498. testBufferSize -= 1;
  1499. rval = Tss2_Sys_SetDecryptParam(decryptParamTestSysContext, testBufferSize,
  1500. testBuffer);
  1501. CheckPassed(rval);
  1502. rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
  1503. TPM20_INDEX_PASSWORD_TEST, 0, 0x55aa );
  1504. CheckPassed( rval );
  1505. rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
  1506. CheckPassed( rval );
  1507. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.buffer[0] ) );
  1508. CheckPassed( rval );
  1509. rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize2, &cpBuffer2 );
  1510. CheckPassed( rval );
  1511. LOGBLOB_INFO((UINT8 *)cpBuffer2, cpBufferUsedSize2, "cpBuffer = ");
  1512. if( cpBufferUsedSize1 != cpBufferUsedSize2 )
  1513. {
  1514. LOG_ERROR("ERROR!! cpBufferUsedSize1(%x) != cpBufferUsedSize2(%x)", (UINT32)cpBufferUsedSize1, (UINT32)cpBufferUsedSize2 );
  1515. Cleanup();
  1516. }
  1517. for( i = 0; i < (int)cpBufferUsedSize1; i++ )
  1518. {
  1519. if( cpBuffer1[i] != cpBuffer2[i] )
  1520. {
  1521. LOG_ERROR("ERROR!! cpBufferUsedSize1[%d] s/b: %2.2x, was: %2.2x", i, cpBuffer1[i], cpBuffer2[i] );
  1522. Cleanup();
  1523. }
  1524. }
  1525. /* Test case of zero sized decrypt param, another case of bad size. */
  1526. nvWriteData1.size = 0;
  1527. rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
  1528. TPM20_INDEX_PASSWORD_TEST, &nvWriteData1, 0x55aa );
  1529. CheckPassed( rval );
  1530. rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 1, &( nvWriteData.buffer[0] ) );
  1531. CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
  1532. sys_teardown(decryptParamTestSysContext);
  1533. }
  1534. static void SysFinalizeTests()
  1535. {
  1536. LOG_INFO("SYS FINALIZE TESTS:" );
  1537. Tss2_Sys_Finalize( 0 );
  1538. /* Note: other cases tested by other tests. */
  1539. }
  1540. static void GetContextSizeTests()
  1541. {
  1542. TSS2_RC rval = TSS2_RC_SUCCESS;
  1543. TSS2_SYS_CONTEXT *testSysContext;
  1544. LOG_INFO("SYS GETCONTEXTSIZE TESTS:" );
  1545. testSysContext = sys_init_from_tcti_ctx(resMgrTctiContext);
  1546. if (testSysContext == NULL)
  1547. InitSysContextFailure();
  1548. rval = Tss2_Sys_Startup(testSysContext, TPM2_SU_CLEAR);
  1549. CheckPassed(rval);
  1550. rval = Tss2_Sys_GetTestResult_Prepare(testSysContext);
  1551. CheckPassed(rval);
  1552. sys_teardown(testSysContext);
  1553. }
  1554. static void GetTctiContextTests()
  1555. {
  1556. TSS2_RC rval = TSS2_RC_SUCCESS;
  1557. TSS2_SYS_CONTEXT *testSysContext;
  1558. TSS2_TCTI_CONTEXT *tctiContext;
  1559. LOG_INFO("SYS GETTCTICONTEXT TESTS:" );
  1560. testSysContext = sys_init_from_tcti_ctx(resMgrTctiContext);
  1561. if (testSysContext == NULL)
  1562. InitSysContextFailure();
  1563. rval = Tss2_Sys_GetTctiContext(testSysContext, 0);
  1564. CheckFailed(rval, TSS2_SYS_RC_BAD_REFERENCE);
  1565. rval = Tss2_Sys_GetTctiContext(0, &tctiContext);
  1566. CheckFailed(rval, TSS2_SYS_RC_BAD_REFERENCE);
  1567. sys_teardown(testSysContext);
  1568. }
  1569. static void GetSetEncryptParamTests()
  1570. {
  1571. TPM2B_MAX_NV_BUFFER nvWriteData = {4, { 0xde, 0xad, 0xbe, 0xef,} };
  1572. const uint8_t *encryptParamBuffer;
  1573. const uint8_t encryptParamBuffer1[4] = { 01, 02, 03, 04 };
  1574. size_t encryptParamSize;
  1575. TSS2_RC rval;
  1576. int i;
  1577. TPM2B_DIGEST authPolicy;
  1578. TPMA_NV nvAttributes;
  1579. TPM2B_AUTH nvAuth;
  1580. TSS2L_SYS_AUTH_RESPONSE sessionsDataOut;
  1581. TSS2L_SYS_AUTH_COMMAND sessionsData = { .count = 1, .auths= {{
  1582. .sessionHandle = TPM2_RH_PW }}};
  1583. TPM2B_MAX_NV_BUFFER nvReadData;
  1584. const uint8_t *cpBuffer;
  1585. _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
  1586. LOG_INFO("GET/SET ENCRYPT PARAM TESTS:" );
  1587. /* Do Prepare. */
  1588. rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
  1589. TPM20_INDEX_PASSWORD_TEST, 0, 0 );
  1590. CheckPassed( rval ); /* #1 */
  1591. resp_header_from_cxt(ctx)->tag = TPM2_ST_SESSIONS;
  1592. /* Test for bad sequence */
  1593. rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
  1594. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE ); /* #2 */
  1595. rval = Tss2_Sys_SetEncryptParam( sysContext, 4, &( nvWriteData.buffer[0] ) );
  1596. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE ); /* #3 */
  1597. /* Create NV index */
  1598. /* Set empty policy and auth value. */
  1599. authPolicy.size = 0;
  1600. nvAuth.size = 0;
  1601. /* Now set the attributes. */
  1602. *(UINT32 *)( (void *)&nvAttributes ) = 0;
  1603. nvAttributes |= TPMA_NV_AUTHREAD;
  1604. nvAttributes |= TPMA_NV_AUTHWRITE;
  1605. nvAttributes |= TPMA_NV_PLATFORMCREATE;
  1606. rval = DefineNvIndex( sysContext, TPM2_RH_PLATFORM, &nvAuth, &authPolicy,
  1607. TPM20_INDEX_PASSWORD_TEST, TPM2_ALG_SHA1, nvAttributes, 32 );
  1608. CheckPassed( rval ); /* #4 */
  1609. /* Write the index. */
  1610. retry:
  1611. rval = Tss2_Sys_NV_Write_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
  1612. TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
  1613. CheckPassed( rval ); /* #5 */
  1614. /* NOTE: add GetCpBuffer tests here, just because it's easier. */
  1615. rval = Tss2_Sys_GetCpBuffer( 0, (size_t *)4, (const uint8_t **)4 );
  1616. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #6 */
  1617. rval = Tss2_Sys_GetCpBuffer( sysContext, (size_t *)0, (const uint8_t **)4 );
  1618. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #7 */
  1619. rval = Tss2_Sys_GetCpBuffer( sysContext, (size_t *)4, (const uint8_t **)0 );
  1620. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #8 */
  1621. rval = Tss2_Sys_SetCmdAuths( sysContext, &sessionsData );
  1622. CheckPassed( rval ); /* #9 */
  1623. rval = Tss2_Sys_ExecuteAsync( sysContext );
  1624. CheckPassed( rval ); /* #10 */
  1625. /*
  1626. * NOTE: Stick two tests for BAD_SEQUENCE for GetDecryptParam and
  1627. * SetDecryptParam here, just because it's easier to do this way.
  1628. */
  1629. rval = Tss2_Sys_GetDecryptParam( sysContext, (size_t *)4, (const uint8_t **)4 );
  1630. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE ); /* #11 */
  1631. rval = Tss2_Sys_SetDecryptParam( sysContext, 10, (uint8_t *)4 );
  1632. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE ); /* #12 */
  1633. /*
  1634. * NOTE: Stick test for BAD_SEQUENCE for GetCpBuffer here, just
  1635. * because it's easier to do this way.
  1636. */
  1637. rval = Tss2_Sys_GetCpBuffer( sysContext, (size_t *)4, &cpBuffer );
  1638. CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE ); /* #13 */
  1639. /*
  1640. * Now finish the write command so that TPM isn't stuck trying
  1641. * to send a response.
  1642. */
  1643. rval = Tss2_Sys_ExecuteFinish( sysContext, -1 );
  1644. if (rval == TPM2_RC_RETRY) {
  1645. LOG_INFO ("got TPM2_RC_RETRY, trying again");
  1646. goto retry;
  1647. }
  1648. CheckPassed( rval ); /* #14 */
  1649. /* Test GetEncryptParam for no encrypt param case. */
  1650. rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
  1651. CheckFailed( rval, TSS2_SYS_RC_NO_ENCRYPT_PARAM ); /* #15 */
  1652. /* Test SetEncryptParam for no encrypt param case. */
  1653. rval = Tss2_Sys_SetEncryptParam( sysContext, encryptParamSize, encryptParamBuffer1 );
  1654. CheckFailed( rval, TSS2_SYS_RC_NO_ENCRYPT_PARAM ); /* #16 */
  1655. /* Now read it and do tests on get/set encrypt functions */
  1656. rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, 4, 0 );
  1657. CheckPassed( rval ); /* #17 */
  1658. INIT_SIMPLE_TPM2B_SIZE( nvReadData );
  1659. rval = Tss2_Sys_NV_Read( sysContext, TPM20_INDEX_PASSWORD_TEST,
  1660. TPM20_INDEX_PASSWORD_TEST, &sessionsData, 4, 0, &nvReadData, &sessionsDataOut );
  1661. CheckPassed( rval ); /* #18 */
  1662. rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
  1663. CheckPassed( rval ); /* #19 */
  1664. /* Test case of encryptParamSize being too small. */
  1665. encryptParamSize--;
  1666. rval = Tss2_Sys_SetEncryptParam( sysContext, encryptParamSize, encryptParamBuffer1 );
  1667. CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE ); /* #20 */
  1668. encryptParamSize += 2;
  1669. /* Size too large... */
  1670. rval = Tss2_Sys_SetEncryptParam( sysContext, encryptParamSize, encryptParamBuffer1 );
  1671. CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE ); /* #21 */
  1672. encryptParamSize--;
  1673. rval = Tss2_Sys_SetEncryptParam( sysContext, encryptParamSize, encryptParamBuffer1 );
  1674. CheckPassed( rval ); /* #22 */
  1675. rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
  1676. CheckPassed( rval ); /* #23 */
  1677. /* Test that encryptParamBuffer is the same as encryptParamBuffer1 */
  1678. for( i = 0; i < 4; i++ )
  1679. {
  1680. if( encryptParamBuffer[i] != encryptParamBuffer1[i] )
  1681. {
  1682. LOG_ERROR("ERROR!! encryptParamBuffer[%d] s/b: %2.2x, was: %2.2x", i, encryptParamBuffer[i], encryptParamBuffer1[i] );
  1683. Cleanup();
  1684. }
  1685. }
  1686. rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM2_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
  1687. CheckPassed( rval ); /* #24 */
  1688. /* Test for bad reference */
  1689. rval = Tss2_Sys_GetEncryptParam( 0, &encryptParamSize, &encryptParamBuffer );
  1690. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #25 */
  1691. rval = Tss2_Sys_GetEncryptParam( sysContext, 0, &encryptParamBuffer );
  1692. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #26 */
  1693. rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, 0 );
  1694. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #27 */
  1695. rval = Tss2_Sys_SetEncryptParam( sysContext, 4, 0 );
  1696. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #28 */
  1697. rval = Tss2_Sys_SetEncryptParam( 0, 4, encryptParamBuffer );
  1698. CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE ); /* #29 */
  1699. }
  1700. static void EcEphemeralTest()
  1701. {
  1702. TSS2_RC rval = TSS2_RC_SUCCESS;
  1703. TPM2B_ECC_POINT Q;
  1704. UINT16 counter;
  1705. LOG_INFO("EC Ephemeral TESTS:" );
  1706. /* Test SYS for case of Q size field not being set to 0. */
  1707. INIT_SIMPLE_TPM2B_SIZE( Q );
  1708. rval = Tss2_Sys_EC_Ephemeral( sysContext, 0, TPM2_ECC_BN_P256, &Q, &counter, 0 );
  1709. CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
  1710. Q.size = 0;
  1711. rval = Tss2_Sys_EC_Ephemeral( sysContext, 0, TPM2_ECC_BN_P256, &Q, &counter, 0 );
  1712. CheckPassed( rval );
  1713. }
  1714. int
  1715. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  1716. {
  1717. TSS2_RC rval = TSS2_RC_SUCCESS;
  1718. #if !defined(TCTI_SWTPM) && !defined(TCTI_MSSIM)
  1719. /* SKIP */
  1720. return EXIT_SKIP;
  1721. #endif
  1722. sysContext = sys_context;
  1723. rval = Tss2_Sys_GetTctiContext (sys_context, &resMgrTctiContext);
  1724. if (rval != TSS2_RC_SUCCESS) {
  1725. printf ("Failed to get TCTI context from sys_context: 0x%" PRIx32
  1726. "\n", rval);
  1727. return 1;
  1728. }
  1729. nullSessionsData.auths[0].sessionHandle = TPM2_RH_PW;
  1730. nullSessionsDataOut.count = 1;
  1731. nullSessionsDataOut.auths[0].nonce = nullSessionNonceOut;
  1732. nullSessionsDataOut.auths[0].hmac = nullSessionHmac;
  1733. nullSessionNonceOut.size = 0;
  1734. nullSessionNonce.size = 0;
  1735. rval = TpmReset();
  1736. CheckPassed(rval);
  1737. SysFinalizeTests();
  1738. GetContextSizeTests();
  1739. GetTctiContextTests();
  1740. GetSetDecryptParamTests();
  1741. TestTpmStartup();
  1742. /*
  1743. * Run this directly after Startup tests to test for
  1744. * a resource mgr corner case with SaveContext.
  1745. */
  1746. TestStartAuthSession();
  1747. /* Clear DA lockout. */
  1748. TestDictionaryAttackLockReset();
  1749. TestDictionaryAttackLockReset();
  1750. TestHierarchyControl();
  1751. GetSetEncryptParamTests();
  1752. TestTpmGetCapability();
  1753. TestPcrExtend();
  1754. TestHash();
  1755. TestPolicy();
  1756. TestTpmClear();
  1757. TestChangeEps();
  1758. TestChangePps();
  1759. TestHierarchyChangeAuth();
  1760. TestShutdown();
  1761. TestNV();
  1762. PasswordTest();
  1763. TestQuote();
  1764. TestDictionaryAttackLockReset();
  1765. TestPcrAllocate();
  1766. TestUnseal();
  1767. EcEphemeralTest();
  1768. return 0;
  1769. }