blob: b5c50226af1f19150639acc88d9689213974f19c [file] [log] [blame]
Will Arthur54e04e42015-07-15 11:29:25 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25// THE POSSIBILITY OF SUCH DAMAGE.
26//**********************************************************************;
27
28//
29// tpmclient.cpp : Defines the entry point for the console test application.
30//
31
32#ifdef _WIN32
33#include "stdafx.h"
34#else
35#include <stdarg.h>
36#endif
37
38#ifndef UNICODE
39#define UNICODE 1
40#endif
41
42#ifdef _WIN32
43// link with Ws2_32.lib
44#pragma comment(lib,"Ws2_32.lib")
45
46#include <winsock2.h>
47#include <ws2tcpip.h>
48#else
49#define sprintf_s snprintf
50#define sscanf_s sscanf
51#endif
52
53#include <stdio.h>
54#include <stdlib.h> // Needed for _wtoi
55#include <string.h>
56
57#include "tpm20.h"
58#include "sample.h"
59#include "resourcemgr.h"
60#include "tpmclient.h"
61#include "tss2_sysapi_util.h"
62#include "tpmsockets.h"
63#include "syscontext.h"
Will Arthurca8e7f32015-08-03 15:35:19 -040064#include "debug.h"
Will Arthur54e04e42015-07-15 11:29:25 -040065
66//
67// TPM indices and sizes
68//
69#define NV_AUX_INDEX_SIZE 96
70#define NV_PS_INDEX_SIZE 34
71#define NV_PO_INDEX_SIZE 34
72
73#define INDEX_AUX 0x01800003 // NV Storage
74#define INDEX_LCP_OWN 0x01400001 // Launch Policy Owner
75#define INDEX_LCP_SUP 0x01800001 // Launch Policy Default (Supplier)
76#define TPM20_INDEX_TEST1 0x01500015
77#define TPM20_INDEX_TEST2 0x01500016
78#define TPM20_INDEX_PASSWORD_TEST 0x01500020
79
80
81#define SET_PCR_SELECT_BIT( pcrSelection, pcr ) \
82 (pcrSelection).pcrSelect[( (pcr)/8 )] |= ( 1 << ( (pcr) % 8) );
83
84#define CLEAR_PCR_SELECT_BITS( pcrSelection ) \
85 (pcrSelection).pcrSelect[0] = 0; \
86 (pcrSelection).pcrSelect[1] = 0; \
87 (pcrSelection).pcrSelect[2] = 0;
88
89#define SET_PCR_SELECT_SIZE( pcrSelection, size ) \
90 (pcrSelection).sizeofSelect = size;
91
92char outFileName[200] = "";
93
94TPM_CC currentCommandCode;
95TPM_CC *currentCommandCodePtr = &currentCommandCode;
96
97#define errorStringSize 200
98char errorString[errorStringSize];
99
Will Arthurca8e7f32015-08-03 15:35:19 -0400100UINT8 simulator = 1;
101
Will Arthur54e04e42015-07-15 11:29:25 -0400102UINT32 tpmMaxResponseLen = TPMBUF_LEN;
103
104UINT8 pcrAfterExtend[20];
105TPM_HANDLE loadedRsaKeyHandle;
106TPM_HANDLE loadedSha1KeyHandle;
107
108TPM2B_AUTH loadedSha1KeyAuth;
109
110TPM_HANDLE handle1024, handle2048sha1, handle2048rsa;
111
112UINT32 passCount = 1;
113UINT32 demoDelay = 0;
114int debugLevel = 0;
115int startAuthSessionTestOnly = 0;
116UINT8 indent = 0;
117
118TSS2_SYS_CONTEXT *sysContext;
119
120#define rmInterfaceConfigSize 250
121char rmInterfaceConfig[rmInterfaceConfigSize];
122
Will Arthurca8e7f32015-08-03 15:35:19 -0400123TSS2_TCTI_DRIVER_INFO resMgrInterfaceInfo = { "resMgr", "", InitSocketsTcti, TeardownSocketsTcti };
Will Arthur54e04e42015-07-15 11:29:25 -0400124
125TSS2_TCTI_CONTEXT *resMgrTctiContext = 0;
126TSS2_ABI_VERSION abiVersion = { TSSWG_INTEROP, TSS_SAPI_FIRST_FAMILY, TSS_SAPI_FIRST_LEVEL, TSS_SAPI_FIRST_VERSION };
127
128UINT32 tpmSpecVersion = 0;
129
130//
131// These are helper functions that are called through function pointers so that
132// they could be swapped easily to point to different functions.
133//
134// Currently they are set to functions that use the TPM to perform the function, but SW-only
135// implementations could be substituted as well.
136//
137UINT32 ( *ComputeSessionHmacPtr )(
138 TSS2_SYS_CONTEXT *sysContext,
139 TPMS_AUTH_COMMAND *cmdAuth, // Pointer to session input struct
140 TPM_HANDLE entityHandle, // Used to determine if we're accessing a different
141 // resource than the bound resoure.
142 TPM_RC responseCode, // Response code for the command, 0xffff for "none" is
143 // used to indicate that no response code is present
144 // (used for calculating command HMACs vs response HMACs).
145 TPM_HANDLE handle1, // First handle == 0xff000000 indicates no handle
146 TPM_HANDLE handle2, // Second handle == 0xff000000 indicates no handle
147 TPMA_SESSION sessionAttributes, // Current session attributes
148 TPM2B_DIGEST *result, // Where the result hash is saved.
149 TPM_RC sessionCmdRval
150 ) = TpmComputeSessionHmac;
151
152TPM_RC ( *GetSessionAlgIdPtr )( TPMI_SH_AUTH_SESSION authHandle, TPMI_ALG_HASH *sessionAlgId ) = GetSessionAlgId;
153
154TPM_RC ( *CalcPHash )( TSS2_SYS_CONTEXT *sysContext,TPM_HANDLE handle1, TPM_HANDLE handle2, TPMI_ALG_HASH authHash,
155 TPM_RC responseCode, TPM2B_DIGEST *pHash ) = TpmCalcPHash;
156
157UINT32 (*HmacFunctionPtr)( TPM_ALG_ID hashAlg, TPM2B *key,TPM2B **bufferList, TPM2B_DIGEST *result ) = TpmHmac;
158
159UINT32 (*HashFunctionPtr)( TPMI_ALG_HASH hashAlg, UINT16 size, BYTE *data, TPM2B_DIGEST *result ) = TpmHash;
160
161UINT32 (*HandleToNameFunctionPtr)( TPM_HANDLE handle, TPM2B_NAME *name ) = TpmHandleToName;
162
163TPMI_SH_AUTH_SESSION StartPolicySession();
164
165TPMI_SH_AUTH_SESSION InitNvAuxPolicySession();
166
167FILE *outFp;
168
169//
170// Used by some high level sample routines to copy the results.
171//
172void copyData( UINT8 *to, UINT8 *from, UINT32 length )
173{
174 if( to != 0 && from != 0 )
175 memcpy( to, from, length );
176}
177
178int TpmClientPrintf( UINT8 type, const char *format, ...)
179{
180 va_list args;
181 int rval = 0;
182
183 OpenOutFile( &outFp );
184
185 if( outFp )
186 {
187 if( type == RM_PREFIX )
188 {
189 PrintRMDebugPrefix();
190 }
191
192 va_start( args, format );
193 rval = vfprintf( outFp, format, args );
194 va_end (args);
195
196 CloseOutFile( &outFp );
197 }
198 else
199 {
200 printf( "TpmClientPrintf failed\n" );
201 }
202
203 return rval;
204}
205
206TPM_RC CompareTPM2B( TPM2B *buffer1, TPM2B *buffer2 )
207{
208 if( buffer1->size != buffer2->size )
209 return TPM_RC_FAILURE;
210 for( int i = 0; i < buffer1->size; i++ )
211 {
212 if( buffer1->buffer[0] != buffer2->buffer[0] )
213 return TPM_RC_FAILURE;
214 }
215 return TPM_RC_SUCCESS;
216}
217
218void PrintSizedBufferOpen( TPM2B *sizedBuffer )
219{
220 int i;
221
222
223 OpenOutFile( &outFp );
224
225 if( outFp )
226 {
227 for( i = 0; i < sizedBuffer->size; i++ )
228 {
229 TpmClientPrintf( 0, "%2.2x ", sizedBuffer->buffer[i] );
230
231 if( ( (i+1) % 16 ) == 0 )
232 {
233 TpmClientPrintf( 0, "\n" );
234 }
235 }
236 TpmClientPrintf( 0, "\n" );
237
238 CloseOutFile( &outFp );
239 }
240 else
241 {
242 printf( "PrintSizedBufferOpen failed\n" );
243 }
244
245}
246
247void PrintSizedBuffer( TPM2B *sizedBuffer )
248{
249 int i;
250
251 for( i = 0; i < sizedBuffer->size; i++ )
252 {
253 TpmClientPrintf( 0, "%2.2x ", sizedBuffer->buffer[i] );
254
255 if( ( (i+1) % 16 ) == 0 )
256 {
257 TpmClientPrintf( 0, "\n" );
258 }
259 }
260 TpmClientPrintf( 0, "\n" );
261}
262
263#define LEVEL_STRING_SIZE 50
264
265void ErrorHandler( UINT32 rval )
266{
267 UINT32 errorLevel = rval & TSS2_ERROR_LEVEL_MASK;
268 char levelString[LEVEL_STRING_SIZE + 1];
269
270 switch( errorLevel )
271 {
272 case TSS2_TPM_ERROR_LEVEL:
273 strncpy( levelString, "TPM", LEVEL_STRING_SIZE );
274 break;
275 case TSS2_APP_ERROR_LEVEL:
276 strncpy( levelString, "Application", LEVEL_STRING_SIZE );
277 break;
278 case TSS2_SYS_ERROR_LEVEL:
279 strncpy( levelString, "System API", LEVEL_STRING_SIZE );
280 break;
281 case TSS2_SYS_PART2_ERROR_LEVEL:
282 strncpy( levelString, "System API TPM encoded", LEVEL_STRING_SIZE );
283 break;
284 case TSS2_TCTI_ERROR_LEVEL:
285 strncpy( levelString, "TCTI", LEVEL_STRING_SIZE );
286 break;
287 case TSS2_RESMGRTPM_ERROR_LEVEL:
288 strncpy( levelString, "Resource Mgr TPM encoded", LEVEL_STRING_SIZE );
289 break;
290 case TSS2_RESMGR_ERROR_LEVEL:
291 strncpy( levelString, "Resource Mgr", LEVEL_STRING_SIZE );
292 break;
293 case TSS2_DRIVER_ERROR_LEVEL:
294 strncpy( levelString, "Driver", LEVEL_STRING_SIZE );
295 break;
296 default:
297 strncpy( levelString, "Unknown Level", LEVEL_STRING_SIZE );
298 break;
299 }
300
301 sprintf_s( errorString, errorStringSize, "%s Error: 0x%x\n", levelString, rval );
302}
303
304char resMgrInterfaceName[] = "Resource Manager";
305
306TSS2_RC InitTctiResMgrContext( char *rmInterfaceConfig, TSS2_TCTI_CONTEXT **tctiContext, char *name )
307{
308 size_t size;
309
310 TSS2_RC rval;
311
312 rval = resMgrInterfaceInfo.initialize(NULL, &size, rmInterfaceConfig, 0, 0, &resMgrInterfaceName[0], 0 );
313 if( rval != TSS2_RC_SUCCESS )
314 return rval;
315
316 *tctiContext = (TSS2_TCTI_CONTEXT *)malloc(size);
317
318 if( *tctiContext )
319 {
320 rval = resMgrInterfaceInfo.initialize(*tctiContext, &size, rmInterfaceConfig, 0, 0, resMgrInterfaceName, 0 );
321 }
322 else
323 {
324 rval = TSS2_TCTI_RC_BAD_CONTEXT;
325 }
326 return rval;
327}
328
329TSS2_RC TeardownTctiResMgrContext( char *interfaceConfig, TSS2_TCTI_CONTEXT *tctiContext, char *name )
330{
331 return resMgrInterfaceInfo.teardown( tctiContext, interfaceConfig, name );
332}
333
334void Cleanup()
335{
336 fflush( stdout );
337
338 PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
339
340 TeardownTctiResMgrContext( rmInterfaceConfig, resMgrTctiContext, &resMgrInterfaceName[0] );
341
342#ifdef _WIN32
343 WSACleanup();
344#endif
345 exit(1);
346}
347
348void InitSysContextFailure()
349{
350 TpmClientPrintf( 0, "InitSysContext failed, exiting...\n" );
351 Cleanup();
352}
353
354void Delay( UINT16 delay)
355{
356 volatile UINT32 i, j;
357
358 for( j = 0; j < delay; j++ )
359 {
360 for( i = 0; i < 10000000; i++ )
361 ;
362 }
363}
364
365void CheckPassed( UINT32 rval )
366{
367 OpenOutFile( &outFp );
368 TpmClientPrintf( 0, "\tpassing case: " );
369 if ( rval != TPM_RC_SUCCESS) {
370 ErrorHandler( rval);
371 TpmClientPrintf( 0, "\tFAILED! %s\n", errorString );
372 Cleanup();
373 }
374 else
375 {
376 TpmClientPrintf( 0, "\tPASSED!\n" );
377 }
378
379 CloseOutFile( &outFp );
380 Delay(demoDelay);
381}
382
383TPMS_AUTH_COMMAND nullSessionData;
384TPMS_AUTH_RESPONSE nullSessionDataOut;
385TPMS_AUTH_COMMAND *nullSessionDataArray[1] = { &nullSessionData };
386TPMS_AUTH_RESPONSE *nullSessionDataOutArray[1] = { &nullSessionDataOut };
387TSS2_SYS_CMD_AUTHS nullSessionsData = { 1, &nullSessionDataArray[0] };
388TSS2_SYS_RSP_AUTHS nullSessionsDataOut = { 1, &nullSessionDataOutArray[0] };
389TPM2B_NONCE nullSessionNonce, nullSessionNonceOut;
390TPM2B_AUTH nullSessionHmac;
391
392void CheckFailed( UINT32 rval, UINT32 expectedTpmErrorCode )
393{
394 OpenOutFile( &outFp );
395 TpmClientPrintf( 0, "\tfailing case: " );
396 if ( rval != expectedTpmErrorCode) {
397 ErrorHandler( rval);
398 TpmClientPrintf( 0, "\tFAILED! Ret code s/b: %x, but was: %x\n", expectedTpmErrorCode, rval );
399 Cleanup();
400 }
401 else
402 {
403 TpmClientPrintf( 0, "\tPASSED!\n" );
404 }
405 fflush( stdout );
406 CloseOutFile( &outFp );
407 Delay(demoDelay);
408}
409
410TSS2_RC TpmReset()
411{
412 TSS2_RC rval = TSS2_RC_SUCCESS;
413
414 rval = (TSS2_RC)PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
415 if( rval == TSS2_RC_SUCCESS )
416 {
417 rval = (TSS2_RC)PlatformCommand( resMgrTctiContext, MS_SIM_POWER_ON );
418 }
419 return rval;
420}
421
422void GetTpmVersion()
423{
424 TSS2_RC rval = TSS2_RC_SUCCESS;
425 TPMS_CAPABILITY_DATA capabilityData;
426
427 rval = Tss2_Sys_GetCapability( sysContext, 0,
428 TPM_CAP_TPM_PROPERTIES, TPM_PT_REVISION,
429 1, 0, &capabilityData, 0 );
430 CheckPassed( rval );
431
432 if( capabilityData.data.tpmProperties.count == 1 &&
433 (capabilityData.data.tpmProperties.tpmProperty[0].property == TPM_PT_REVISION) )
434 {
435 tpmSpecVersion = capabilityData.data.tpmProperties.tpmProperty[0].value;
436 }
437 else
438 {
439 TpmClientPrintf( 0, "Failed to get TPM spec version!!\n" );
440 Cleanup();
441 }
442}
443
444
445void TestDictionaryAttackLockReset()
446{
447 UINT32 rval;
448 TPMS_AUTH_COMMAND sessionData;
449 TPMS_AUTH_RESPONSE sessionDataOut;
450 TSS2_SYS_CMD_AUTHS sessionsData;
451 TSS2_SYS_RSP_AUTHS sessionsDataOut;
452
453 TPMS_AUTH_COMMAND *sessionDataArray[1];
454 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
455
456 sessionDataArray[0] = &sessionData;
457 sessionDataOutArray[0] = &sessionDataOut;
458
459 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
460 sessionsData.cmdAuths = &sessionDataArray[0];
461
462 sessionsDataOut.rspAuthsCount = 1;
463
464 TpmClientPrintf( 0, "\nDICTIONARY ATTACK LOCK RESET TEST :\n" );
465
466 // Init authHandle
467 sessionData.sessionHandle = TPM_RS_PW;
468
469 // Init nonce.
470 sessionData.nonce.t.size = 0;
471
472 // init hmac
473 sessionData.hmac.t.size = 0;
474
475 // Init session attributes
476 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
477
478 sessionsData.cmdAuthsCount = 1;
479 sessionsData.cmdAuths[0] = &sessionData;
480
481 rval = Tss2_Sys_DictionaryAttackLockReset ( sysContext, TPM_RH_LOCKOUT, &sessionsData, &sessionsDataOut );
482 CheckPassed( rval );
483}
484
485TSS2_RC StartPolicySession( TPMI_SH_AUTH_SESSION *sessionHandle )
486{
487 UINT8 i;
488 TPM2B_NONCE nonceCaller, nonceTpm;
489 TPM2B_ENCRYPTED_SECRET salt;
490 TPMT_SYM_DEF symmetric;
491 UINT16 digestSize;
492 UINT32 rval;
493
494 digestSize = GetDigestSize( TPM_ALG_SHA1 );
495 nonceCaller.t.size = digestSize;
496 for( i = 0; i < nonceCaller.t.size; i++ )
497 nonceCaller.t.buffer[i] = 0;
498
499 salt.t.size = 0;
500 symmetric.algorithm = TPM_ALG_NULL;
501
502 // Create policy session
503 rval = Tss2_Sys_StartAuthSession ( sysContext, TPM_RH_NULL, TPM_RH_NULL, 0, &nonceCaller, &salt,
504 TPM_SE_POLICY, &symmetric, TPM_ALG_SHA1, sessionHandle, &nonceTpm, 0 );
505 return( rval );
506}
507
508void TestTpmStartup()
509{
510 UINT32 rval;
511
512 TpmClientPrintf( 0, "\nSTARTUP TESTS:\n" );
513
514 //
515 // First test the one-call interface.
516 //
517
518 // First must do TPM reset.
519 rval = TpmReset();
520 CheckPassed(rval);
521
522 // This one should pass.
523 rval = Tss2_Sys_Startup( sysContext, TPM_SU_CLEAR );
524 CheckPassed(rval);
525
526 // This one should fail.
527 rval = Tss2_Sys_Startup( sysContext, TPM_SU_CLEAR );
528 CheckFailed( rval, TPM_RC_INITIALIZE );
529
530
531 // Cycle power using simulator interface.
532 rval = PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
533 CheckPassed( rval );
534 rval = PlatformCommand( resMgrTctiContext, MS_SIM_POWER_ON );
535 CheckPassed( rval );
536
537
538 //
539 // Now test the syncronous, non-one-call interface.
540 //
541 rval = Tss2_Sys_Startup_Prepare( sysContext, TPM_SU_CLEAR );
542 CheckPassed(rval);
543
544 // Execute the command syncronously.
545 rval = Tss2_Sys_Execute( sysContext );
546
547 // Cycle power using simulator interface.
548 rval = PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
549 CheckPassed( rval );
550 rval = PlatformCommand( resMgrTctiContext, MS_SIM_POWER_ON );
551 CheckPassed( rval );
552
553
554 //
555 // Now test the asyncronous, non-one-call interface.
556 //
557 rval = Tss2_Sys_Startup_Prepare( sysContext, TPM_SU_CLEAR );
558 CheckPassed(rval);
559
560 // Execute the command asyncronously.
561 rval = Tss2_Sys_ExecuteAsync( sysContext );
562 CheckPassed(rval);
563
564 // Get the command response. Wait a maximum of 20ms
565 // for response.
566 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
567 CheckPassed(rval);
568}
569
570void TestSapiApis()
571{
572 UINT32 rval;
573 TPM2B_MAX_BUFFER outData = { { MAX_DIGEST_BUFFER, } };
574 TPM_RC testResult;
575
576 TpmClientPrintf( 0, "\nGET TEST RESULT TESTS:\n" );
577
578 //
579 // First test the one-call interface.
580 //
581 rval = Tss2_Sys_GetTestResult( sysContext, 0, &outData, &testResult, 0 );
582 CheckPassed(rval);
583
584 //
585 // Now test the syncronous, non-one-call interface.
586 //
587 rval = Tss2_Sys_GetTestResult_Prepare( sysContext );
588 CheckPassed(rval);
589
590 // Execute the command syncronously.
591 rval = Tss2_Sys_Execute( sysContext );
592 CheckPassed(rval);
593
594 // Get the command results
595 rval = Tss2_Sys_GetTestResult_Complete( sysContext, &outData, &testResult );
596 CheckPassed(rval);
597
598 //
599 // Now test the asyncronous, non-one-call interface.
600 //
601 rval = Tss2_Sys_GetTestResult_Prepare( sysContext );
602 CheckPassed(rval);
603
604 // Execute the command asyncronously.
605 rval = Tss2_Sys_ExecuteAsync( sysContext );
606 CheckPassed(rval);
607
608 // Get the command response. Wait a maximum of 20ms
609 // for response.
610 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
611 CheckPassed(rval);
612
613 // Get the command results
614 rval = Tss2_Sys_GetTestResult_Complete( sysContext, &outData, &testResult );
615 CheckPassed(rval);
616
617 // Check case of ExecuteFinish receving TPM error code.
618 // Subsequent _Complete call should fail with SEQUENCE error.
619 rval = TpmReset();
620 CheckPassed(rval);
621
622 rval = Tss2_Sys_GetCapability_Prepare( sysContext,
623 TPM_CAP_TPM_PROPERTIES, TPM_PT_ACTIVE_SESSIONS_MAX,
624 1 );
625 CheckPassed(rval);
626
627 // Execute the command asyncronously.
628 rval = Tss2_Sys_ExecuteAsync( sysContext );
629 CheckPassed(rval);
630
631 // Get the command response. Wait a maximum of 20ms
632 // for response.
633 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
634 CheckFailed( rval, TPM_RC_INITIALIZE );
635
636 // Get the command results
637 rval = Tss2_Sys_GetCapability_Complete( sysContext, 0, 0 );
638 CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
639
640 rval = Tss2_Sys_Startup( sysContext, TPM_SU_CLEAR );
641 CheckPassed(rval);
642}
643
644
645void TestTpmSelftest()
646{
647 UINT32 rval;
648
649 TpmClientPrintf( 0, "\nSELFTEST TESTS:\n" );
650
651 rval = Tss2_Sys_SelfTest( sysContext, 0, YES, 0);
652 CheckPassed( rval );
653
654 rval = Tss2_Sys_SelfTest( sysContext, 0, NO, 0);
655 CheckPassed( rval );
656
657 rval = Tss2_Sys_SelfTest( sysContext, 0, YES, 0);
658 CheckPassed( rval );
659
660}
661
662void TestTpmGetCapability()
663{
664 UINT32 rval;
665
666 char manuID[5] = " ";
667 char *manuIDPtr = &manuID[0];
668 TPMI_YES_NO moreData;
669 TPMS_CAPABILITY_DATA capabilityData;
670
671 TpmClientPrintf( 0, "\nGET_CAPABILITY TESTS:\n" );
672
673 rval = Tss2_Sys_GetCapability( sysContext, 0, TPM_CAP_TPM_PROPERTIES, TPM_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
674 CheckPassed( rval );
675
676 *( (UINT32 *)manuIDPtr ) = CHANGE_ENDIAN_DWORD( capabilityData.data.tpmProperties.tpmProperty[0].value );
677 TpmClientPrintf( 0, "\t\tcount: %d, property: %x, manuId: %s\n",
678 capabilityData.data.tpmProperties.count,
679 capabilityData.data.tpmProperties.tpmProperty[0].property,
680 manuID );
681
682 rval = Tss2_Sys_GetCapability( sysContext, 0, TPM_CAP_TPM_PROPERTIES, TPM_PT_MAX_COMMAND_SIZE, 1, &moreData, &capabilityData, 0 );
683 CheckPassed( rval );
684 TpmClientPrintf( 0, "\t\tcount: %d, property: %x, max cmd size: %d\n",
685 capabilityData.data.tpmProperties.count,
686 capabilityData.data.tpmProperties.tpmProperty[0].property,
687 capabilityData.data.tpmProperties.tpmProperty[0].value );
688
689
690 rval = Tss2_Sys_GetCapability( sysContext, 0, TPM_CAP_TPM_PROPERTIES, TPM_PT_MAX_COMMAND_SIZE, 40, &moreData, &capabilityData, 0 );
691 CheckPassed( rval );
692 TpmClientPrintf( 0, "\t\tcount: %d, property: %x, max cmd size: %d\n",
693 capabilityData.data.tpmProperties.count,
694 capabilityData.data.tpmProperties.tpmProperty[0].property,
695 capabilityData.data.tpmProperties.tpmProperty[0].value );
696
697
698 rval = Tss2_Sys_GetCapability( sysContext, 0, TPM_CAP_TPM_PROPERTIES, TPM_PT_MAX_RESPONSE_SIZE, 1, &moreData, &capabilityData, 0 );
699 CheckPassed( rval );
700 TpmClientPrintf( 0, "\t count: %d, property: %x, max response size: %d\n",
701 capabilityData.data.tpmProperties.count,
702 capabilityData.data.tpmProperties.tpmProperty[0].property,
703 capabilityData.data.tpmProperties.tpmProperty[0].value );
704
705 rval = Tss2_Sys_GetCapability( sysContext, 0, 0xff, TPM_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
706
707 if( tpmSpecVersion == 115 || tpmSpecVersion == 119 )
708 {
709 CheckFailed( rval, TPM_RC_VALUE );
710 }
711 else
712 {
713 CheckFailed( rval, TPM_RC_VALUE+TPM_RC_1+TPM_RC_P );
714 }
715}
716
717void TestTpmClear()
718{
719 UINT32 rval;
720 TPM2B_AUTH hmac;
721 TPMS_AUTH_COMMAND sessionData;
722 TPMS_AUTH_RESPONSE sessionDataOut;
723 TPM2B_NONCE nonce;
724 TSS2_SYS_CMD_AUTHS sessionsDataIn;
725 TSS2_SYS_RSP_AUTHS sessionsDataOut;
726
727 TPMS_AUTH_COMMAND *sessionDataArray[1];
728 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
729
730 sessionDataArray[0] = &sessionData;
731 sessionDataOutArray[0] = &sessionDataOut;
732
733 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
734 sessionsDataIn.cmdAuths = &sessionDataArray[0];
735
736 sessionsDataOut.rspAuthsCount = 1;
737 sessionsDataOut.rspAuths[0] = &sessionDataOut;
738
739 TpmClientPrintf( 0, "\nCLEAR and CLEAR CONTROL TESTS:\n" );
740
741 // Init sessionHandle
742 sessionData.sessionHandle = TPM_RS_PW;
743
744 // Init nonce.
745 nonce.t.size = 0;
746 sessionData.nonce = nonce;
747
748 // init hmac
749 hmac.t.size = 0;
750 sessionData.hmac = hmac;
751
752 // Init session attributes
753 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
754
755 sessionsDataIn.cmdAuthsCount = 1;
756 sessionsDataIn.cmdAuths[0] = &sessionData;
757
758 rval = Tss2_Sys_Clear ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, 0 );
759 CheckPassed( rval );
760
761 rval = Tss2_Sys_ClearControl ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, YES, &sessionsDataOut );
762 CheckPassed( rval );
763
764 rval = Tss2_Sys_Clear ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, 0 );
765 CheckFailed( rval, TPM_RC_DISABLED );
766
767 rval = Tss2_Sys_ClearControl ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
768 CheckPassed( rval );
769
770 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0xff;
771 sessionsDataIn.cmdAuths[0] = &sessionData;
772 rval = Tss2_Sys_Clear ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, &sessionsDataOut );
773 CheckFailed( rval, TPM_RC_9 + TPM_RC_RESERVED_BITS );
774
775 rval = Tss2_Sys_ClearControl ( sysContext, TPM_RH_PLATFORM, &sessionsDataIn, NO, &sessionsDataOut );
776 CheckFailed( rval, TPM_RC_9 + TPM_RC_RESERVED_BITS );
777
778 hmac.t.size = 0;
779
780
781}
782
783#ifdef DEBUG_GAP_HANDLING
784
785#define SESSIONS_ABOVE_MAX_ACTIVE 1
786// SESSION sessions[DEBUG_GAP_MAX*3];
787 SESSION *sessions[300];
788#else
789
790#define SESSIONS_ABOVE_MAX_ACTIVE 0
791#define DEBUG_MAX_ACTIVE_SESSIONS 8
792#define DEBUG_GAP_MAX 2*DEBUG_MAX_ACTIVE_SESSIONS
793 SESSION *sessions[5];
794
795#endif
796
797void TestStartAuthSession()
798{
799 UINT32 rval;
800 TPM2B_ENCRYPTED_SECRET encryptedSalt;
801 TPMT_SYM_DEF symmetric;
802 SESSION *authSession;
803 TPM2B_NONCE nonceCaller;
804 UINT16 i, debugGapMax = DEBUG_GAP_MAX, debugMaxActiveSessions = DEBUG_MAX_ACTIVE_SESSIONS;
805 TPMA_LOCALITY locality;
806 TPM_HANDLE badSessionHandle = 0x03010000;
807
808 TPMS_AUTH_COMMAND sessionData;
809 TPM2B_NONCE nonce;
810 TSS2_SYS_CMD_AUTHS sessionsDataIn;
811
812 TPMS_AUTH_COMMAND *sessionDataArray[1];
813
814 TPM2B_AUTH hmac;
815
816 TPMS_CONTEXT evictedSessionContext;
817 TPM_HANDLE evictedHandle;
818
819 sessionDataArray[0] = &sessionData;
820
821 sessionsDataIn.cmdAuths = &sessionDataArray[0];
822
823 sessionsDataIn.cmdAuthsCount = 1;
824
825 // Init sessionHandle
826 sessionData.sessionHandle = badSessionHandle;
827
828 // Init nonce.
829 nonce.t.size = 0;
830 sessionData.nonce = nonce;
831
832 // init hmac
833 hmac.t.size = 0;
834 sessionData.hmac = hmac;
835
836 // Init session attributes
837 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
838
839 encryptedSalt.t.size = 0;
840
841 TpmClientPrintf( 0, "\nSTART_AUTH_SESSION TESTS:\n" );
842
843 symmetric.algorithm = TPM_ALG_NULL;
844 symmetric.keyBits.sym = 0;
845 symmetric.mode.sym = 0;
846
847 nonceCaller.t.size = 0;
848
849 encryptedSalt.t.size = 0;
850
851 // Init session
852 rval = StartAuthSessionWithParams( &authSession, TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
853 CheckPassed( rval );
854
855 rval = Tss2_Sys_FlushContext( sysContext, authSession->sessionHandle );
856 CheckPassed( rval );
857 EndAuthSession( authSession );
858
859 // Init session
860 rval = StartAuthSessionWithParams( &authSession, TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, 0xff, &symmetric, TPM_ALG_SHA256 );
861 CheckFailed( rval, TPM_RC_VALUE + TPM_RC_P + TPM_RC_3 );
862
863 // Try starting a bunch to see if resource manager handles this correctly.
864
865#ifdef DEBUG_GAP_HANDLING
866 for( i = 0; i < debugMaxActiveSessions*3; i++ )
867#else
868 for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
869#endif
870 {
871// TpmClientPrintf( 0, "i = 0x%4.4x\n", i );
872
873 // Init session struct
874 rval = StartAuthSessionWithParams( &sessions[i], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
875 CheckPassed( rval );
876 TpmClientPrintf( 0, "Number of sessions created: %d\n\n", i );
877
878#ifdef DEBUG_GAP_HANDLING
879 if( i == 0 )
880 {
881 // Save evicted session's context so we can use it for a test.
882 rval = Tss2_Sys_ContextSave( sysContext, sessions[i]->sessionHandle, &evictedSessionContext );
883 CheckPassed( rval );
884 }
885#endif
886 }
887
888#ifdef DEBUG_GAP_HANDLING
889 TpmClientPrintf( 0, "loading evicted session's context\n" );
890 // Now try loading an evicted session's context.
891 // NOTE: simulator versions 01.19 and earlier this test will fail due to a
892 // simulator bug (unless patches have been applied).
893 rval = Tss2_Sys_ContextLoad( sysContext, &evictedSessionContext, &evictedHandle );
894 CheckFailed( rval, TPM_RC_HANDLE + TPM_RC_P + ( 1 << 8 ));
895#endif
896
897 // Now try two ways of using a bad session handle. Both should fail.
898
899 // first way is to use as command parameter.
900 *(UINT8 *)( (void *)&locality ) = 0;
901 locality.TPM_LOC_THREE = 1;
902 rval = Tss2_Sys_PolicyLocality( sysContext, badSessionHandle, 0, locality, 0 );
903 CheckFailed( rval, TSS2_RESMGRTPM_ERROR_LEVEL + TPM_RC_HANDLE + ( 1 << 8 ) );
904
905 // Second way is to use as handle in session area.
906 rval = Tss2_Sys_PolicyLocality( sysContext, sessions[0]->sessionHandle, &sessionsDataIn, locality, 0 );
907 CheckFailed( rval, TSS2_RESMGRTPM_ERROR_LEVEL + TPM_RC_VALUE + TPM_RC_S + ( 1 << 8 ) );
908
909 // clean up the sessions that I don't want here.
910#ifdef DEBUG_GAP_HANDLING
911 for( i = 0; i < ( debugMaxActiveSessions*3); i++ )
912#else
913 for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *)); i++ )
914#endif
915 {
916// TpmClientPrintf( 0, "i(2) = 0x%4.4x\n", i );
917 rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
918
919 rval = EndAuthSession( sessions[i] );
920 }
921
922 // Now do some gap tests.
923 rval = StartAuthSessionWithParams( &sessions[0], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
924 CheckPassed( rval );
925
926#ifdef DEBUG_GAP_HANDLING
927// for( i = 1; i < debugGapMax/2; i++ )
928 for( i = 1; i < 300; i++ )
929#else
930 for( i = 1; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
931#endif
932 {
933// TpmClientPrintf( 0, "i(3) = 0x%4.4x\n", i );
934
935 rval = StartAuthSessionWithParams( &sessions[i], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
936 CheckPassed( rval );
937
938 rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
939 CheckPassed( rval );
940
941 rval = EndAuthSession( sessions[i] );
942 CheckPassed( rval );
943 }
944
945#ifdef DEBUG_GAP_HANDLING
946 // Now do some gap tests.
947 rval = StartAuthSessionWithParams( &sessions[8], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
948 CheckPassed( rval );
949#endif
950
951#ifdef DEBUG_GAP_HANDLING
952 for( i = 9; i < debugGapMax; i++ )
953#else
954 for( i = 0; i < ( sizeof(sessions) / sizeof (SESSION *) ); i++ )
955#endif
956 {
957// TpmClientPrintf( 0, "i(4) = 0x%4.4x\n", i );
958 rval = StartAuthSessionWithParams( &sessions[i], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
959 CheckPassed( rval );
960
961 rval = Tss2_Sys_FlushContext( sysContext, sessions[i]->sessionHandle );
962 CheckPassed( rval );
963
964 rval = EndAuthSession( sessions[i] );
965 CheckPassed( rval );
966
967 }
968
969#ifdef DEBUG_GAP_HANDLING
970 for( i = 0; i < 5; i++ )
971 {
972// TpmClientPrintf( 0, "i(5) = 0x%4.4x\n", i );
973 rval = StartAuthSessionWithParams( &sessions[i+16], TPM_RH_NULL, 0, TPM_RH_PLATFORM, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 );
974 CheckPassed( rval );
975 }
976
977 for( i = 0; i < 5; i++ )
978 {
979// TpmClientPrintf( 0, "i(6) = 0x%4.4x\n", i );
980 rval = Tss2_Sys_FlushContext( sysContext, sessions[i+16]->sessionHandle );
981 CheckPassed( rval );
982
983 rval = EndAuthSession( sessions[i+16] );
984 CheckPassed( rval );
985 }
986
987 rval = Tss2_Sys_FlushContext( sysContext, sessions[0]->sessionHandle );
988 CheckPassed( rval );
989
990 rval = EndAuthSession( sessions[0] );
991 CheckPassed( rval );
992
993 rval = Tss2_Sys_FlushContext( sysContext, sessions[8]->sessionHandle );
994 CheckPassed( rval );
995
996 rval = EndAuthSession( sessions[8] );
997 CheckPassed( rval );
998#endif
999
1000}
1001
1002void TestChangeEps()
1003{
1004 UINT32 rval;
1005 TSS2_SYS_CMD_AUTHS sessionsData;
1006 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1007
1008 TPMS_AUTH_COMMAND sessionData;
1009 TPMS_AUTH_RESPONSE sessionDataOut;
1010
1011 TPMS_AUTH_COMMAND *sessionDataArray[1];
1012 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1013
1014 sessionDataArray[0] = &sessionData;
1015 sessionDataOutArray[0] = &sessionDataOut;
1016
1017 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1018 sessionsData.cmdAuths = &sessionDataArray[0];
1019
1020 sessionsDataOut.rspAuthsCount = 1;
1021
1022 TpmClientPrintf( 0, "\nCHANGE_EPS TESTS:\n" );
1023
1024 sessionsData.cmdAuthsCount = 1;
1025
1026 // Init authHandle
1027 sessionsData.cmdAuths[0]->sessionHandle = TPM_RS_PW;
1028
1029 // Init nonce.
1030 sessionsData.cmdAuths[0]->nonce.t.size = 0;
1031
1032 // init hmac
1033 sessionsData.cmdAuths[0]->hmac.t.size = 0;
1034
1035 // Init session attributes
1036 *( (UINT8 *)((void *)&sessionsData.cmdAuths[0]->sessionAttributes ) ) = 0;
1037
1038 rval = Tss2_Sys_ChangeEPS( sysContext, TPM_RH_PLATFORM, &sessionsData, &sessionsDataOut );
1039 CheckPassed( rval );
1040
1041 sessionsData.cmdAuths[0]->hmac.t.size = 0x10;
1042
1043 rval = Tss2_Sys_ChangeEPS( sysContext, TPM_RH_PLATFORM, &sessionsData, 0 );
1044 CheckFailed( rval, TPM_RC_1 + TPM_RC_S + TPM_RC_BAD_AUTH );
1045}
1046
1047void TestChangePps()
1048{
1049 UINT32 rval;
1050 TSS2_SYS_CMD_AUTHS sessionsData;
1051
1052 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1053
1054 TPMS_AUTH_COMMAND sessionData;
1055 TPMS_AUTH_RESPONSE sessionDataOut;
1056
1057 TPMS_AUTH_COMMAND *sessionDataArray[1];
1058 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1059
1060 sessionDataArray[0] = &sessionData;
1061 sessionDataOutArray[0] = &sessionDataOut;
1062
1063 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1064 sessionsData.cmdAuths = &sessionDataArray[0];
1065
1066 sessionsDataOut.rspAuthsCount = 1;
1067
1068 TpmClientPrintf( 0, "\nCHANGE_PPS TESTS:\n" );
1069
1070 sessionsData.cmdAuthsCount = 1;
1071
1072 // Init authHandle
1073 sessionsData.cmdAuths[0]->sessionHandle = TPM_RS_PW;
1074
1075 // Init nonce.
1076 sessionsData.cmdAuths[0]->nonce.t.size = 0;
1077
1078 // init hmac
1079 sessionsData.cmdAuths[0]->hmac.t.size = 0;
1080
1081 // Init session attributes
1082 *( (UINT8 *)((void *)&sessionsData.cmdAuths[0]->sessionAttributes ) ) = 0;
1083
1084 rval = Tss2_Sys_ChangePPS( sysContext, TPM_RH_PLATFORM, &sessionsData, &sessionsDataOut );
1085 CheckPassed( rval );
1086
1087 sessionsData.cmdAuths[0]->hmac.t.size = 0x10;
1088
1089 rval = Tss2_Sys_ChangePPS( sysContext, TPM_RH_PLATFORM, &sessionsData, &sessionsDataOut );
1090 CheckFailed( rval, TPM_RC_1 + TPM_RC_S + TPM_RC_BAD_AUTH );
1091}
1092
1093void TestHierarchyChangeAuth()
1094{
1095 UINT32 rval;
1096 TPM2B_AUTH newAuth;
1097 TPMS_AUTH_COMMAND sessionData;
1098 TSS2_SYS_CMD_AUTHS sessionsData;
1099 int i;
1100
1101 TPMS_AUTH_COMMAND *sessionDataArray[1];
1102
1103 sessionDataArray[0] = &sessionData;
1104
1105 sessionsData.cmdAuths = &sessionDataArray[0];
1106
1107 TpmClientPrintf( 0, "\nHIERARCHY_CHANGE_AUTH TESTS:\n" );
1108
1109 // Init authHandle
1110 sessionData.sessionHandle = TPM_RS_PW;
1111
1112 // Init nonce.
1113 sessionData.nonce.t.size = 0;
1114
1115 // init hmac
1116 sessionData.hmac.t.size = 0;
1117
1118 // Init session attributes
1119 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1120
1121 sessionsData.cmdAuthsCount = 1;
1122 sessionsData.cmdAuths[0] = &sessionData;
1123
1124 newAuth.t.size = 0;
1125 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM_RH_PLATFORM, &sessionsData, &newAuth, 0 );
1126 CheckPassed( rval );
1127
1128 // Init new auth
1129 newAuth.t.size = 20;
1130 for( i = 0; i < newAuth.t.size; i++ )
1131 newAuth.t.buffer[i] = i;
1132
1133 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM_RH_PLATFORM, &sessionsData, &newAuth, 0 );
1134 CheckPassed( rval );
1135
1136 sessionData.hmac = newAuth;
1137 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM_RH_PLATFORM, &sessionsData, &newAuth, 0 );
1138 CheckPassed( rval );
1139
1140 // Init new auth
1141 newAuth.t.size = 0;
1142
1143 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM_RH_PLATFORM, &sessionsData, &newAuth, 0 );
1144 CheckPassed( rval );
1145
1146 sessionsData.cmdAuths[0] = &sessionData;
1147 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, TPM_RH_PLATFORM, &sessionsData, &newAuth, 0 );
1148 CheckFailed( rval, TPM_RC_1 + TPM_RC_S + TPM_RC_BAD_AUTH );
1149
1150 rval = Tss2_Sys_HierarchyChangeAuth( sysContext, 0, &sessionsData, &newAuth, 0 );
1151 CheckFailed( rval, TPM_RC_1 + TPM_RC_VALUE );
1152}
1153
1154#define PCR_0 0
1155#define PCR_1 1
1156#define PCR_2 2
1157#define PCR_3 3
1158#define PCR_4 4
1159#define PCR_5 5
1160#define PCR_6 6
1161#define PCR_7 7
1162#define PCR_8 8
1163#define PCR_9 9
1164#define PCR_10 10
1165#define PCR_11 11
1166#define PCR_12 12
1167#define PCR_13 13
1168#define PCR_14 14
1169#define PCR_15 15
1170#define PCR_16 16
1171#define PCR_17 17
1172#define PCR_18 18
1173
1174#define PCR_SIZE 20
1175
1176void TestPcrExtend()
1177{
1178 UINT32 rval;
1179 TPMS_AUTH_COMMAND sessionData;
1180 TSS2_SYS_CMD_AUTHS sessionsData;
1181 UINT16 i, digestSize;
1182 TPML_PCR_SELECTION pcrSelection;
1183 UINT32 pcrUpdateCounterBeforeExtend;
1184 UINT32 pcrUpdateCounterAfterExtend;
1185 UINT8 pcrBeforeExtend[PCR_SIZE];
1186 TPM2B_EVENT eventData;
1187 TPML_DIGEST pcrValues;
1188 TPML_DIGEST_VALUES digests;
1189 TPML_PCR_SELECTION pcrSelectionOut;
1190
1191 TPMS_AUTH_COMMAND *sessionDataArray[1];
1192
1193 sessionDataArray[0] = &sessionData;
1194 sessionsData.cmdAuths = &sessionDataArray[0];
1195
1196 TpmClientPrintf( 0, "\nPCR_EXTEND, PCR_EVENT, PCR_ALLOCATE, and PCR_READ TESTS:\n" );
1197
1198 // Init authHandle
1199 sessionData.sessionHandle = TPM_RS_PW;
1200
1201 // Init nonce.
1202 sessionData.nonce.t.size = 0;
1203
1204 // init hmac
1205 sessionData.hmac.t.size = 0;
1206
1207 // Init session attributes
1208 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1209
1210 // Init digests
1211 digests.count = 1;
1212 digests.digests[0].hashAlg = TPM_ALG_SHA1;
1213 digestSize = GetDigestSize( digests.digests[0].hashAlg );
1214
1215 for( i = 0; i < digestSize; i++ )
1216 {
1217 digests.digests[0].digest.sha1[i] = (UINT8)(i % 256);
1218 }
1219
1220 pcrSelection.count = 1;
1221 pcrSelection.pcrSelections[0].hash = TPM_ALG_SHA1;
1222 pcrSelection.pcrSelections[0].sizeofSelect = 3;
1223
1224 // Clear out PCR select bit field
1225 pcrSelection.pcrSelections[0].pcrSelect[0] = 0;
1226 pcrSelection.pcrSelections[0].pcrSelect[1] = 0;
1227 pcrSelection.pcrSelections[0].pcrSelect[2] = 0;
1228
1229 // Now set the PCR you want to read
1230 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_17 );
1231
1232 rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterBeforeExtend, &pcrSelectionOut, &pcrValues, 0 );
1233 CheckPassed( rval );
1234
1235 if( pcrValues.digests[0].t.size <= PCR_SIZE &&
1236 pcrValues.digests[0].t.size <= sizeof( pcrValues.digests[0].t.buffer ) )
1237 memcpy( &( pcrBeforeExtend[0] ), &( pcrValues.digests[0].t.buffer[0] ), pcrValues.digests[0].t.size );
1238
1239 sessionsData.cmdAuthsCount = 1;
1240 sessionsData.cmdAuths[0] = &sessionData;
1241
1242 rval = Tss2_Sys_PCR_Extend( sysContext, PCR_17, &sessionsData, &digests, 0 );
1243 CheckPassed( rval );
1244
1245 rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, &pcrSelectionOut, &pcrValues, 0 );
1246 CheckPassed( rval );
1247
1248 memcpy( &( pcrAfterExtend[0] ), &( pcrValues.digests[0].t.buffer[0] ), pcrValues.digests[0].t.size );
1249
1250 if( pcrUpdateCounterBeforeExtend == pcrUpdateCounterAfterExtend )
1251 {
1252 TpmClientPrintf( 0, "ERROR!! pcrUpdateCounter didn't change value\n" );
1253 Cleanup();
1254 }
1255
1256 if( 0 == memcmp( &( pcrBeforeExtend[0] ), &( pcrAfterExtend[0] ), 20 ) )
1257 {
1258 TpmClientPrintf( 0, "ERROR!! PCR didn't change value\n" );
1259 Cleanup();
1260 }
1261
1262 pcrSelection.pcrSelections[0].sizeofSelect = 4;
1263
1264 rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrSelection, &pcrUpdateCounterAfterExtend, 0, 0, 0 );
1265 CheckFailed( rval, TPM_RC_1 + TPM_RC_P + TPM_RC_VALUE );
1266
1267 eventData.t.size = 4;
1268 eventData.t.buffer[0] = 0;
1269 eventData.t.buffer[1] = 0xff;
1270 eventData.t.buffer[2] = 0x55;
1271 eventData.t.buffer[3] = 0xaa;
1272
1273 rval = Tss2_Sys_PCR_Event( sysContext, PCR_18, &sessionsData, &eventData, &digests, 0 );
1274 CheckPassed( rval );
1275}
1276
1277void TestGetRandom()
1278{
1279 UINT32 rval;
1280 TPM2B_DIGEST randomBytes1, randomBytes2;
1281
1282 TpmClientPrintf( 0, "\nGET_RANDOM TESTS:\n" );
1283
1284 rval = Tss2_Sys_GetRandom( sysContext, 0, 20, &randomBytes1, 0 );
1285 CheckPassed( rval );
1286
1287 rval = Tss2_Sys_GetRandom( sysContext, 0, 20, &randomBytes2, 0 );
1288 CheckPassed( rval );
1289
1290 if( 0 == memcmp( &randomBytes1, &randomBytes2, 20 ) )
1291 {
1292 TpmClientPrintf( 0, "ERROR!! Random value is the same\n" );
1293 Cleanup();
1294 }
1295}
1296
1297void TestShutdown()
1298{
1299 UINT32 rval;
1300 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1301
1302 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1303 TPMS_AUTH_RESPONSE sessionDataOut;
1304
1305 sessionDataOutArray[0] = &sessionDataOut;
1306
1307 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1308
1309 sessionsDataOut.rspAuthsCount = 1;
1310
1311 TpmClientPrintf( 0, "\nSHUTDOWN TESTS:\n" );
1312
1313 rval = Tss2_Sys_Shutdown( sysContext, 0, TPM_SU_STATE, &sessionsDataOut );
1314 CheckPassed( rval );
1315
1316 rval = Tss2_Sys_Shutdown( sysContext, 0, TPM_SU_CLEAR, &sessionsDataOut );
1317 CheckPassed( rval );
1318
1319 if( !( tpmSpecVersion == 115 || tpmSpecVersion == 119 ) )
1320 {
1321 rval = Tss2_Sys_Shutdown( sysContext, 0, 0xff, 0 );
1322 CheckFailed( rval, TPM_RC_VALUE+TPM_RC_1+TPM_RC_P );
1323 }
1324}
1325
1326void TestNV()
1327{
1328 UINT32 rval;
1329 TPM2B_NV_PUBLIC publicInfo;
1330 TPM2B_AUTH nvAuth;
1331 TPMS_AUTH_COMMAND sessionData;
1332 TPMS_AUTH_RESPONSE sessionDataOut;
1333 TSS2_SYS_CMD_AUTHS sessionsData;
1334 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1335 int i;
1336 TPM2B_MAX_NV_BUFFER nvWriteData;
1337 TPM2B_MAX_NV_BUFFER nvData;
1338
1339 TPM2B_NV_PUBLIC nvPublic;
1340 TPM2B_NAME nvName;
1341
1342 TPMS_AUTH_COMMAND *sessionDataArray[1];
1343 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1344
1345 sessionDataArray[0] = &sessionData;
1346 sessionDataOutArray[0] = &sessionDataOut;
1347
1348 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1349 sessionsData.cmdAuths = &sessionDataArray[0];
1350
1351 sessionsDataOut.rspAuthsCount = 1;
1352
1353 TpmClientPrintf( 0, "\nNV INDEX TESTS:\n" );
1354
1355 nvAuth.t.size = 20;
1356 for( i = 0; i < nvAuth.t.size; i++ )
1357 nvAuth.t.buffer[i] = (UINT8)i;
1358
1359 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
1360 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
1361 sizeof( UINT16 );
1362 publicInfo.t.nvPublic.nvIndex = TPM20_INDEX_TEST1;
1363 publicInfo.t.nvPublic.nameAlg = TPM_ALG_SHA1;
1364
1365 // First zero out attributes.
1366 *(UINT32 *)&( publicInfo.t.nvPublic.attributes ) = 0;
1367
1368 // Now set the attributes.
1369 publicInfo.t.nvPublic.attributes.TPMA_NV_PPREAD = 1;
1370 publicInfo.t.nvPublic.attributes.TPMA_NV_PPWRITE = 1;
1371 publicInfo.t.nvPublic.attributes.TPMA_NV_WRITE_STCLEAR = 1;
1372 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
1373 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
1374 publicInfo.t.nvPublic.authPolicy.t.size = 0;
1375 publicInfo.t.nvPublic.dataSize = 32;
1376
1377 sessionData.sessionHandle = TPM_RS_PW;
1378
1379 // Init nonce.
1380 sessionData.nonce.t.size = 0;
1381
1382 // init hmac
1383 sessionData.hmac.t.size = 0;
1384
1385 // Init session attributes
1386 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1387
1388 sessionsData.cmdAuthsCount = 1;
1389 sessionsData.cmdAuths[0] = &sessionData;
1390
1391 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
1392 CheckFailed( rval, TPM_RC_2 + TPM_RC_HANDLE );
1393
1394 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
1395 CheckPassed( rval );
1396
1397 nvPublic.t.size = 0;
1398 rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
1399 CheckPassed( rval );
1400
1401 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
1402 CheckFailed( rval, TPM_RC_NV_UNINITIALIZED );
1403
1404 // Should fail since index is already defined.
1405 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
1406 CheckFailed( rval, TPM_RC_NV_DEFINED );
1407
1408 nvWriteData.t.size = 4;
1409 for( i = 0; i < nvWriteData.t.size; i++ )
1410 nvWriteData.t.buffer[i] = 0xff - i;
1411
1412#if 1
1413 //
1414 // Following, at one point, was commented out so that NVDefine will work on successive
1415 // invocations of client app.
1416 //
1417 // Noticed on 12/13/12, this doesn't seem to be necessary anymore. Maybe something else
1418 // I did fixed it.
1419 //
1420 // Seems to be a bug in TPM 2.0 simulator that if:
1421 // First pass of tpmclient.exe after restarting TPM 2.0 simulator will work fine.
1422 // If NVWrite is done, subsequent invocations of tpmclient.exe will ALWAYS fail on
1423 // first call to Tpm2NVDefineSpace with 0x2cb error. Removing NVWrite removes this.
1424 // And restarting TPM 2.0 simulator will make it work the first time and fail
1425 // subsequent times.
1426 // Removing NVWrite works around this problem.
1427 //
1428 rval = Tss2_Sys_NV_Write( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
1429 CheckPassed( rval );
1430
1431 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
1432 CheckPassed( rval );
1433
1434 rval = Tss2_Sys_NV_WriteLock( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &sessionsDataOut );
1435 CheckPassed( rval );
1436
1437 rval = Tss2_Sys_NV_Write( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
1438 CheckFailed( rval, TPM_RC_NV_LOCKED );
1439#endif
1440
1441 // Now undefine the index.
1442 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
1443 CheckPassed( rval );
1444
1445 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
1446 CheckPassed( rval );
1447
1448 // Now undefine the index so that next run will work correctly.
1449 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
1450 CheckPassed( rval );
1451
1452 publicInfo.t.nvPublic.attributes.TPMA_NV_PPREAD = 0;
1453 publicInfo.t.nvPublic.attributes.TPMA_NV_PPWRITE = 0;
1454 publicInfo.t.nvPublic.attributes.TPMA_NV_OWNERREAD = 1;
1455 publicInfo.t.nvPublic.attributes.TPMA_NV_OWNERWRITE = 1;
1456 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 0;
1457 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
1458 publicInfo.t.nvPublic.nvIndex = TPM20_INDEX_TEST2;
1459 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
1460 CheckPassed( rval );
1461
1462 nvPublic.t.size = 0;
1463 rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST2, 0, &nvPublic, &nvName, 0 );
1464 CheckPassed( rval );
1465
1466 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST2, &sessionsData, 32, 0, &nvData, 0 );
1467 CheckFailed( rval, TPM_RC_NV_AUTHORIZATION );
1468
1469 // Now undefine the index.
1470 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
1471 CheckPassed( rval );
1472
1473 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_OWNER, &sessionsData, &nvAuth, &publicInfo, 0 );
1474 CheckPassed( rval );
1475
1476 // Now undefine the index so that next run will work correctly.
1477 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_OWNER, TPM20_INDEX_TEST2, &sessionsData, 0 );
1478 CheckPassed( rval );
1479
1480#if 0
1481 TpmClientPrintf( 0, "\nStart of NVUndefineSpaceSpecial test\n" );
1482
1483 // Init nonceNewer
1484 digestSize = GetDigestSize( TPM_ALG_SHA1 );
1485 nonceNewer.t.size = digestSize;
1486 for( i = 0; i < nonceNewer.t.size; i++ )
1487 nonceNewer.t.buffer[i] = 0;
1488
1489 // Init salt
1490 salt.t.size = 0;
1491
1492 // Init symmetric.
1493 symmetric.algorithm = TPM_ALG_NULL;
1494 symmetric.keyBits.sym = 0;
1495 symmetric.mode.sym = 0;
1496 rval = Tss2_Sys_StartAuthSession ( sysContext, TPM_RH_NULL, TPM_RH_PLATFORM, 0, &nonceNewer, &salt,
1497 TPM_SE_TRIAL, &symmetric, TPM_ALG_SHA1, &nvSessionHandle, &nvSessionNonce );
1498 CheckPassed( rval );
1499
1500 nvSessionHandle = ( ( TPM20_StartAuthSession_Out *)(TpmOutBuff) )->sessionHandle;
1501
1502 rval = Tss2_Sys_PolicyCommandCode ( sysContext, nvSessionHandle, 0, TPM_CC_NV_UndefineSpaceSpecial, 0 );
1503 CheckPassed( rval );
1504
1505 rval = Tss2_Sys_PolicyGetDigest( sysContext, nvSessionHandle, 0, &nvAuth1, 0 );
1506 CheckPassed( rval );
1507
1508 rval = Tss2_Sys_FlushContext( sysContext, nvSessionHandle );
1509
1510 nvAuth1 = (TPM2B_AUTH *)&( ( ( TPM20_PolicyGetDigest_Out *)(TpmOutBuff) )->otherData );
1511 publicInfo.t.nvPublic.authPolicy.t.size = nvAuth1->t.size;
1512 memcpy( &( publicInfo.t.nvPublic.authPolicy.t.buffer[0] ),c
1513 &( nvAuth1->t.buffer[0] ),
1514 publicInfo.t.nvPublic.authPolicy.t.size );
1515
1516 publicInfo.t.nvPublic.attributes.TPMA_NV_POLICY_DELETE = 1;
1517 publicInfo.t.nvPublic.attributes.TPMA_NV_PPREAD = 1;
1518 publicInfo.t.nvPublic.attributes.TPMA_NV_PPWRITE = 1;
1519 publicInfo.t.nvPublic.attributes.TPMA_NV_OWNERREAD = 0;
1520 publicInfo.t.nvPublic.attributes.TPMA_NV_OWNERWRITE = 0;
1521 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
1522 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
1523
1524 InitNullSession( &nvSession );
1525
1526 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
1527 CheckPassed( rval );
1528
1529 rval = Tss2_Sys_StartAuthSession ( sysContext, TPM_RH_NULL, TPM_RH_PLATFORM, 0, &nonceCaller, &salt,
1530 TPM_SE_POLICY, &symmetric, TPM_ALG_SHA1, &nvSessionHandle, &nvSessionNonce );
1531 CheckPassed( rval );
1532
1533 nvSession.sessionHandle = nvSessionHandle = ( ( TPM20_StartAuthSession_Out *)(TpmOutBuff) )->sessionHandle;
1534
1535 rval = Tss2_Sys_PolicyCommandCode ( sysContext, nvSessionHandle, 0, TPM_CC_NV_UndefineSpaceSpecial, 0 );
1536 CheckPassed( rval );
1537
1538 nvSession->hmac = publicInfo.t.nvPublic.authPolicy;
1539
1540 nvSessions.cmdAuthsCount = 2;
1541 nvSessions.session[0] = nvSession;
1542
1543 rval = Tss2_Sys_NVUndefineSpaceSpecial( sysContext, TPM20_INDEX_TEST2, TPM_RH_PLATFORM, &nvSessions, &sessionsData );
1544 CheckPassed( rval );
1545#endif
1546}
1547
1548void TestHierarchyControl()
1549{
1550 UINT32 rval;
1551 TPM2B_NV_PUBLIC publicInfo;
1552 TPM2B_AUTH nvAuth;
1553 TPMS_AUTH_COMMAND sessionData;
1554 TPMS_AUTH_RESPONSE sessionDataOut;
1555 TSS2_SYS_CMD_AUTHS sessionsData;
1556 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1557 int i;
1558 TPM2B_NAME nvName;
1559 TPM2B_NV_PUBLIC nvPublic;
1560 TPM2B_MAX_NV_BUFFER nvData;
1561
1562 TPMS_AUTH_COMMAND *sessionDataArray[1];
1563 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1564
1565 sessionDataArray[0] = &sessionData;
1566 sessionDataOutArray[0] = &sessionDataOut;
1567
1568 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1569 sessionsData.cmdAuths = &sessionDataArray[0];
1570
1571 sessionsDataOut.rspAuthsCount = 1;
1572
1573 TpmClientPrintf( 0, "\nHIERARCHY CONTROL TESTS:\n" );
1574
1575 nvAuth.t.size = 20;
1576 for( i = 0; i < nvAuth.t.size; i++ )
1577 nvAuth.t.buffer[i] = i;
1578
1579 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
1580 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
1581 sizeof( UINT16 );
1582 publicInfo.t.nvPublic.nvIndex = TPM20_INDEX_TEST1;
1583 publicInfo.t.nvPublic.nameAlg = TPM_ALG_SHA1;
1584
1585 // First zero out attributes.
1586 *(UINT32 *)&( publicInfo.t.nvPublic.attributes ) = 0;
1587
1588 // Now set the attributes.
1589 publicInfo.t.nvPublic.attributes.TPMA_NV_PPREAD = 1;
1590 publicInfo.t.nvPublic.attributes.TPMA_NV_PPWRITE = 1;
1591 publicInfo.t.nvPublic.attributes.TPMA_NV_PPWRITE = 1;
1592 publicInfo.t.nvPublic.attributes.TPMA_NV_WRITE_STCLEAR = 1;
1593 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
1594 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
1595 publicInfo.t.nvPublic.authPolicy.t.size = 0;
1596 publicInfo.t.nvPublic.dataSize = 32;
1597
1598 sessionData.sessionHandle = TPM_RS_PW;
1599
1600 // Init nonce.
1601 sessionData.nonce.t.size = 0;
1602
1603 // init hmac
1604 sessionData.hmac.t.size = 0;
1605
1606 // Init session attributes
1607 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1608
1609 sessionsData.cmdAuthsCount = 1;
1610 sessionsData.cmdAuths[0] = &sessionData;
1611
1612 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &sessionsData, &nvAuth, &publicInfo, 0 );
1613 CheckPassed( rval );
1614
1615 // Test SAPI for case where nvPublic.t.size != 0
1616 nvPublic.t.size = 0xff;
1617 rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
1618 CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
1619
1620 nvPublic.t.size = 0;
1621 rval = Tss2_Sys_NV_ReadPublic( sysContext, TPM20_INDEX_TEST1, 0, &nvPublic, &nvName, 0 );
1622 CheckPassed( rval );
1623
1624 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
1625 CheckFailed( rval, TPM_RC_NV_UNINITIALIZED );
1626
1627 rval = Tss2_Sys_HierarchyControl( sysContext, TPM_RH_PLATFORM, &sessionsData, TPM_RH_PLATFORM, NO, &sessionsDataOut );
1628 CheckPassed( rval );
1629
1630 rval = Tss2_Sys_NV_Read( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 32, 0, &nvData, &sessionsDataOut );
1631 CheckFailed( rval, TPM_RC_1 + TPM_RC_HIERARCHY );
1632
1633 rval = Tss2_Sys_HierarchyControl( sysContext, TPM_RH_PLATFORM, &sessionsData, TPM_RH_PLATFORM, YES, &sessionsDataOut );
1634 CheckFailed( rval, TPM_RC_1 + TPM_RC_HIERARCHY );
1635
1636 // Need to do TPM reset and Startup to re-enable platform hierarchy.
1637 rval = TpmReset();
1638 CheckPassed(rval);
1639
1640 rval = Tss2_Sys_Startup ( sysContext, TPM_SU_CLEAR );
1641 CheckPassed( rval );
1642
1643 rval = Tss2_Sys_HierarchyControl( sysContext, TPM_RH_PLATFORM, &sessionsData, TPM_RH_PLATFORM, YES, &sessionsDataOut );
1644 CheckPassed( rval );
1645
1646 // Now undefine the index so that next run will work correctly.
1647 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &sessionsData, 0 );
1648 CheckPassed( rval );
1649}
1650
1651TPM2B_PUBLIC inPublic = { { sizeof( TPM2B_PUBLIC ) - 2, } };
1652
1653void TestCreate(){
1654 UINT32 rval;
1655 TPM2B_SENSITIVE_CREATE inSensitive = { { sizeof( TPM2B_SENSITIVE_CREATE ) - 2, } };
1656 TPM2B_DATA outsideInfo = { { sizeof( TPM2B_DATA ) - 2, } };
1657 TPML_PCR_SELECTION creationPCR;
1658 TPMS_AUTH_COMMAND sessionData;
1659 TPMS_AUTH_RESPONSE sessionDataOut;
1660 TSS2_SYS_CMD_AUTHS sessionsData;
1661
1662 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1663 TPM2B_NAME name = { { sizeof( TPM2B_NAME ) - 2, } };
1664 TPM2B_NAME name1 = { { sizeof( TPM2B_NAME ) - 2, } };
1665 TPM2B_PRIVATE outPrivate = { { sizeof( TPM2B_PRIVATE ) - 2, } };
1666 TPM2B_PUBLIC outPublic = { { sizeof( TPM2B_PUBLIC ) - 2, } };
1667 TPM2B_CREATION_DATA creationData = { { sizeof( TPM2B_CREATION_DATA ) - 2, } };
1668 TPM2B_DIGEST creationHash = { { sizeof( TPM2B_DIGEST ) - 2, } };
1669 TPMT_TK_CREATION creationTicket = { 0, 0, { { sizeof( TPM2B_DIGEST ) - 2, } } };
1670
1671 TPMS_AUTH_COMMAND *sessionDataArray[1];
1672 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1673
1674 sessionDataArray[0] = &sessionData;
1675 sessionDataOutArray[0] = &sessionDataOut;
1676
1677 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1678 sessionsData.cmdAuths = &sessionDataArray[0];
1679
1680 sessionsDataOut.rspAuthsCount = 1;
1681
1682 TpmClientPrintf( 0, "\nCREATE, CREATE PRIMARY, and LOAD TESTS:\n" );
1683
1684 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
1685 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
1686 inSensitive.t.sensitive.data.t.size = 0;
1687 inSensitive.t.size = loadedSha1KeyAuth.b.size + 2;
1688
1689 inPublic.t.publicArea.type = TPM_ALG_RSA;
1690 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
1691
1692 // First clear attributes bit field.
1693 *(UINT32 *)&( inPublic.t.publicArea.objectAttributes) = 0;
1694 inPublic.t.publicArea.objectAttributes.restricted = 1;
1695 inPublic.t.publicArea.objectAttributes.userWithAuth = 1;
1696 inPublic.t.publicArea.objectAttributes.decrypt = 1;
1697 inPublic.t.publicArea.objectAttributes.fixedTPM = 1;
1698 inPublic.t.publicArea.objectAttributes.fixedParent = 1;
1699 inPublic.t.publicArea.objectAttributes.sensitiveDataOrigin = 1;
1700
1701 inPublic.t.publicArea.authPolicy.t.size = 0;
1702
1703 inPublic.t.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
1704 inPublic.t.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
1705 inPublic.t.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_ECB;
1706 inPublic.t.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
1707 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 1024;
1708 inPublic.t.publicArea.parameters.rsaDetail.exponent = 0;
1709
1710 inPublic.t.publicArea.unique.rsa.t.size = 0;
1711
1712 outsideInfo.t.size = 0;
1713 creationPCR.count = 0;
1714
1715 sessionData.sessionHandle = TPM_RS_PW;
1716
1717 // Init nonce.
1718 sessionData.nonce.t.size = 0;
1719
1720 // init hmac
1721 sessionData.hmac.t.size = 0;
1722
1723 // Init session attributes
1724 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1725
1726 sessionsData.cmdAuthsCount = 1;
1727 sessionsData.cmdAuths[0] = &sessionData;
1728
1729 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 2048;
1730
1731 // Do SAPI test for non-zero sized outPublic
1732 outPublic.t.size = 0xff;
1733 creationData.t.size = 0;
1734 rval = Tss2_Sys_CreatePrimary( sysContext, TPM_RH_PLATFORM, &sessionsData, &inSensitive, &inPublic,
1735 &outsideInfo, &creationPCR, &handle2048rsa, &outPublic, &creationData, &creationHash,
1736 &creationTicket, &name, &sessionsDataOut );
1737 CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
1738
1739 rval = Tss2_Sys_FlushContext( sysContext, handle2048rsa );
1740 CheckPassed( rval );
1741#if 0
1742 // Do SAPI test for non-zero sized creationData
1743 outPublic.t.size = 0;
1744 creationData.t.size = 0x10;
1745 rval = Tss2_Sys_CreatePrimary( sysContext, TPM_RH_PLATFORM, &sessionsData, &inSensitive, &inPublic,
1746 &outsideInfo, &creationPCR, &handle2048rsa, &outPublic, &creationData, &creationHash,
1747 &creationTicket, &name, &sessionsDataOut );
1748 CheckFailed( rval, TSS2_SYS_RC_INSUFFICIENT_BUFFER );
1749#endif
1750
1751 outPublic.t.size = 0;
1752 creationData.t.size = sizeof( TPM2B_CREATION_DATA ) - 2;
1753 outPublic.t.publicArea.authPolicy.t.size = sizeof( TPM2B_DIGEST ) - 2;
1754 outPublic.t.publicArea.unique.keyedHash.t.size = sizeof( TPM2B_DIGEST ) - 2;
1755 rval = Tss2_Sys_CreatePrimary( sysContext, TPM_RH_PLATFORM, &sessionsData, &inSensitive, &inPublic,
1756 &outsideInfo, &creationPCR, &handle2048rsa, &outPublic, &creationData, &creationHash,
1757 &creationTicket, &name, &sessionsDataOut );
1758 CheckPassed( rval );
1759
1760 TpmClientPrintf( 0, "\nNew key successfully created in platform hierarchy (RSA 2048). Handle: 0x%8.8x\n",
1761 handle2048rsa );
1762
1763 sessionData.hmac.t.size = 2;
1764 sessionData.hmac.t.buffer[0] = 0x00;
1765 sessionData.hmac.t.buffer[1] = 0xff;
1766
1767 inPublic.t.publicArea.type = TPM_ALG_KEYEDHASH;
1768 inPublic.t.publicArea.objectAttributes.decrypt = 0;
1769 inPublic.t.publicArea.objectAttributes.sign = 1;
1770
1771 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM_ALG_HMAC;
1772 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = TPM_ALG_SHA1;
1773
1774 inPublic.t.publicArea.unique.keyedHash.t.size = 0;
1775
1776 outsideInfo.t.size = 0;
1777 outPublic.t.size = 0;
1778 creationData.t.size = 0;
1779 rval = Tss2_Sys_Create( sysContext, handle2048rsa, &sessionsData, &inSensitive, &inPublic,
1780 &outsideInfo, &creationPCR,
1781 &outPrivate, &outPublic, &creationData,
1782 &creationHash, &creationTicket, &sessionsDataOut );
1783 CheckPassed( rval );
1784
1785 rval = Tss2_Sys_Load ( sysContext, handle2048rsa, &sessionsData, &outPrivate, &outPublic,
1786 &loadedSha1KeyHandle, &name, &sessionsDataOut);
1787 CheckPassed( rval );
1788
1789 rval = (*HandleToNameFunctionPtr)( loadedSha1KeyHandle, &name1 );
1790 CheckPassed( rval );
1791 OpenOutFile( &outFp );
1792 TpmClientPrintf( 0, "Name of loaded key: " );
1793 PrintSizedBuffer( (TPM2B *)&name1 );
1794 CloseOutFile( &outFp );
1795
1796 rval = CompareTPM2B( &name.b, &name1.b );
1797 CheckPassed( rval );
1798
1799 TpmClientPrintf( 0, "\nLoaded key handle: %8.8x\n", loadedSha1KeyHandle );
1800}
1801
1802void TestEvict()
1803{
1804 TPM_RC rval = TPM_RC_SUCCESS;
1805 TPM2B_SENSITIVE_CREATE inSensitive = { { sizeof( TPM2B_SENSITIVE_CREATE ) - 2, } };
1806 TPM2B_DATA outsideInfo = { { sizeof( TPM2B_DATA ) - 2, } };
1807 TPML_PCR_SELECTION creationPCR;
1808 TPMS_AUTH_COMMAND sessionData;
1809 TPMS_AUTH_RESPONSE sessionDataOut;
1810 TSS2_SYS_CMD_AUTHS sessionsData;
1811 TSS2_SYS_RSP_AUTHS sessionsDataOut;
1812
1813 TPM2B_PRIVATE outPrivate = { { sizeof( TPM2B_PRIVATE ) - 2, } };
1814 TPM2B_PUBLIC outPublic = { { sizeof( TPM2B_PUBLIC ) - 2, } };
1815 TPM2B_CREATION_DATA creationData = { { sizeof( TPM2B_CREATION_DATA ) - 2, } };
1816 TPM2B_DIGEST creationHash = { { sizeof( TPM2B_DIGEST ) - 2, } };
1817 TPMT_TK_CREATION creationTicket = { 0, 0, { { sizeof( TPM2B_DIGEST ) - 2, } } };
1818
1819 TPMS_AUTH_COMMAND *sessionDataArray[1];
1820 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
1821
1822 TSS2_TCTI_CONTEXT *otherResMgrTctiContext = 0;
1823 TSS2_SYS_CONTEXT *otherSysContext;
1824 char otherResMgrInterfaceName[] = "Other Resource Manager";
1825
1826 sessionDataArray[0] = &sessionData;
1827 sessionDataOutArray[0] = &sessionDataOut;
1828
1829 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
1830 sessionsData.cmdAuths = &sessionDataArray[0];
1831 sessionsData.cmdAuthsCount = 1;
1832
1833 sessionsDataOut.rspAuthsCount = 1;
1834
1835 outsideInfo.t.size = 0;
1836 creationPCR.count = 0;
1837
1838 TpmClientPrintf( 0, "\nEVICT CONTROL TESTS:\n" );
1839
1840 // Make transient key persistent.
1841 sessionData.sessionHandle = TPM_RS_PW;
1842
1843 // Init nonce.
1844 sessionData.nonce.t.size = 0;
1845
1846 // init hmac
1847 sessionData.hmac.t.size = 0;
1848
1849 // Init session attributes
1850 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1851
1852 rval = Tss2_Sys_EvictControl( sysContext, TPM_RH_PLATFORM, handle2048rsa, &sessionsData, 0x81800000, &sessionsDataOut );
1853 CheckPassed( rval );
1854
1855 sessionData.sessionHandle = TPM_RS_PW;
1856
1857 // Init nonce.
1858 sessionData.nonce.t.size = 0;
1859
1860 // Init session attributes
1861 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1862
1863 // Create new key under persistent one.
1864 sessionData.hmac.t.size = 2;
1865 sessionData.hmac.t.buffer[0] = 0x00;
1866 sessionData.hmac.t.buffer[1] = 0xff;
1867
1868 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
1869 inPublic.t.publicArea.type = TPM_ALG_KEYEDHASH;
1870 inPublic.t.publicArea.objectAttributes.decrypt = 0;
1871 inPublic.t.publicArea.objectAttributes.sign = 1;
1872
1873 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM_ALG_HMAC;
1874 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = TPM_ALG_SHA1;
1875
1876 inPublic.t.publicArea.unique.keyedHash.t.size = 0;
1877
1878 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
1879 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
1880 inSensitive.t.sensitive.data.t.size = 0;
1881 inSensitive.t.size = loadedSha1KeyAuth.b.size + 2;
1882
1883 outsideInfo.t.size = 0;
1884 outPublic.t.size = 0;
1885 creationData.t.size = 0;
1886
1887 // Try creating a key under the persistent key using a different context.
1888
1889 rval = InitTctiResMgrContext( rmInterfaceConfig, &otherResMgrTctiContext, &otherResMgrInterfaceName[0] );
1890 if( rval != TSS2_RC_SUCCESS )
1891 {
1892 TpmClientPrintf( 0, "Resource Mgr, %s, failed initialization: 0x%x. Exiting...\n", resMgrInterfaceInfo.shortName, rval );
1893 Cleanup();
1894 return;
1895 }
1896 else
1897 {
1898 (( TSS2_TCTI_CONTEXT_INTEL *)otherResMgrTctiContext )->status.debugMsgLevel = debugLevel;
1899 }
1900
1901 otherSysContext = InitSysContext( 0, otherResMgrTctiContext, &abiVersion );
1902 if( otherSysContext == 0 )
1903 {
1904 InitSysContextFailure();
1905 }
1906
1907 rval = Tss2_Sys_Create( otherSysContext, 0x81800000, &sessionsData, &inSensitive, &inPublic,
1908 &outsideInfo, &creationPCR,
1909 &outPrivate, &outPublic, &creationData,
1910 &creationHash, &creationTicket, &sessionsDataOut );
1911 CheckPassed( rval );
1912
1913 rval = TeardownTctiResMgrContext( rmInterfaceConfig, otherResMgrTctiContext, &otherResMgrInterfaceName[0] );
1914 CheckPassed( rval );
1915
1916 TeardownSysContext( &otherSysContext );
1917
1918 outsideInfo.t.size = 0;
1919 outPublic.t.size = 0;
1920 creationData.t.size = 0;
1921
1922 // Try creating a key under the transient key. This should work, too.
1923 rval = Tss2_Sys_Create( sysContext, handle2048rsa, &sessionsData, &inSensitive, &inPublic,
1924 &outsideInfo, &creationPCR,
1925 &outPrivate, &outPublic, &creationData,
1926 &creationHash, &creationTicket, &sessionsDataOut );
1927 CheckPassed( rval );
1928
1929 // Reset persistent key to be transitent.
1930 sessionData.hmac.t.size = 0;
1931 rval = Tss2_Sys_EvictControl( sysContext, TPM_RH_PLATFORM, 0x81800000, &sessionsData, 0x81800000, &sessionsDataOut );
1932 CheckPassed( rval );
1933}
1934
1935TPM_RC DefineNvIndex( TPMI_RH_PROVISION authHandle, TPMI_SH_AUTH_SESSION sessionAuthHandle, TPM2B_AUTH *auth, TPM2B_DIGEST *authPolicy,
1936 TPMI_RH_NV_INDEX nvIndex, TPMI_ALG_HASH nameAlg, TPMA_NV attributes, UINT16 size )
1937{
1938 TPM_RC rval = TPM_RC_SUCCESS;
1939 TPM2B_NV_PUBLIC publicInfo;
1940
1941 // Command and response session data structures.
1942 TPMS_AUTH_COMMAND sessionData = { sessionAuthHandle, };
1943 TPMS_AUTH_RESPONSE sessionDataOut;
1944 TPMS_AUTH_COMMAND *sessionDataArray[1] = { &sessionData };
1945 TPMS_AUTH_RESPONSE *sessionDataOutArray[1] = { &sessionDataOut };
1946 TSS2_SYS_CMD_AUTHS sessionsData = { 1, &sessionDataArray[0] };
1947 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, &sessionDataOutArray[0] };
1948 // Init nonce.
1949 sessionData.nonce.t.size = 0;
1950 // init hmac
1951 sessionData.hmac.t.size = 0;
1952 // Init session attributes
1953 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
1954
1955 attributes.TPMA_NV_ORDERLY = 1;
1956
1957 // Init public info structure.
1958 publicInfo.t.nvPublic.attributes = attributes;
1959 CopySizedByteBuffer( &publicInfo.t.nvPublic.authPolicy.b, &authPolicy->b );
1960 publicInfo.t.nvPublic.dataSize = size;
1961 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
1962 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
1963 sizeof( UINT16 );
1964 publicInfo.t.nvPublic.nvIndex = nvIndex;
1965 publicInfo.t.nvPublic.nameAlg = nameAlg;
1966
1967 // Create the index
1968 rval = Tss2_Sys_NV_DefineSpace( sysContext, authHandle, &sessionsData, auth, &publicInfo, &sessionsDataOut );
1969
1970 return rval;
1971}
1972
1973typedef struct {
1974 char name[50];
1975 TPM_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *trialPolicySession, TPM2B_DIGEST *policyDigest );
1976 TPM_RC (*createObjectFn )( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest );
1977 TPM_RC (*testPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession );
1978} POLICY_TEST_SETUP;
1979
1980TPM_RC BuildPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession,
1981 TPM_RC (*buildPolicyFn )( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest ),
1982 TPM2B_DIGEST *policyDigest, bool trialSession )
1983{
1984 // NOTE: this policySession will be either a trial or normal policy session
1985 // depending on the value of the passed in trialSession parameter.
1986 TPM2B_ENCRYPTED_SECRET encryptedSalt = { {0}, };
1987 TPMT_SYM_DEF symmetric;
1988 TPM_RC rval;
1989 TPM2B_NONCE nonceCaller;
1990
1991 nonceCaller.t.size = 0;
1992
1993 // Start policy session.
1994 symmetric.algorithm = TPM_ALG_NULL;
1995 rval = StartAuthSessionWithParams( policySession, TPM_RH_NULL, 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, trialSession ? TPM_SE_TRIAL : TPM_SE_POLICY , &symmetric, TPM_ALG_SHA256 );
1996 if( rval != TPM_RC_SUCCESS )
1997 return rval;
1998
1999 // Send policy command.
2000 rval = ( *buildPolicyFn )( sysContext, *policySession, policyDigest );
2001 CheckPassed( rval );
2002
2003 // Get policy hash.
2004 rval = Tss2_Sys_PolicyGetDigest( sysContext, (*policySession)->sessionHandle,
2005 0, policyDigest, 0 );
2006 CheckPassed( rval );
2007
2008 if( trialSession )
2009 {
2010 // Need to flush the session here.
2011 rval = Tss2_Sys_FlushContext( sysContext, (*policySession)->sessionHandle );
2012 CheckPassed( rval );
2013
2014 // And remove the session from sessions table.
2015 rval = EndAuthSession( *policySession );
2016 CheckPassed( rval );
2017 }
2018
2019 return rval;
2020}
2021
2022TPM_RC CreateNVIndex( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest )
2023{
2024 TPM_RC rval = TPM_RC_SUCCESS;
2025 TPMA_LOCALITY locality;
2026 TPM2B_ENCRYPTED_SECRET encryptedSalt = { {0}, };
2027 TPMT_SYM_DEF symmetric;
2028 TPMA_NV nvAttributes;
2029 TPM2B_AUTH nvAuth;
2030 TPM2B_NONCE nonceCaller;
2031
2032 nonceCaller.t.size = 0;
2033
2034 // Since locality is a fairly simple command and we can guarantee
2035 // its correctness, we don't need a trial session for this.
2036
2037 // Start real policy session
2038 symmetric.algorithm = TPM_ALG_NULL;
2039 rval = StartAuthSessionWithParams( policySession, TPM_RH_NULL,
2040 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY,
2041 &symmetric, TPM_ALG_SHA256 );
2042 CheckPassed( rval );
2043
2044 // Send PolicyLocality command
2045 *(UINT8 *)( (void *)&locality ) = 0;
2046 locality.TPM_LOC_THREE = 1;
2047 rval = Tss2_Sys_PolicyLocality( sysContext, (*policySession)->sessionHandle,
2048 0, locality, 0 );
2049 CheckPassed( rval );
2050
2051 // Read policyHash
2052 rval = Tss2_Sys_PolicyGetDigest( sysContext,
2053 (*policySession)->sessionHandle, 0, policyDigest, 0 );
2054 CheckPassed( rval );
2055
2056 nvAuth.t.size = 0;
2057
2058 // Now set the attributes.
2059 *(UINT32 *)( (void *)&nvAttributes ) = 0;
2060 nvAttributes.TPMA_NV_POLICYREAD = 1;
2061 nvAttributes.TPMA_NV_POLICYWRITE = 1;
2062 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
2063
2064 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW, &nvAuth, policyDigest,
2065 TPM20_INDEX_PASSWORD_TEST, TPM_ALG_SHA256, nvAttributes, 32 );
2066 CheckPassed( rval );
2067
2068 AddEntity( TPM20_INDEX_PASSWORD_TEST, &nvAuth );
2069 CheckPassed( rval );
2070
2071 return rval;
2072}
2073
2074
2075TPM_RC TestLocality( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession )
2076{
2077 TSS2_RC rval = TPM_RC_SUCCESS;
2078 TSS2_SYS_CMD_AUTHS sessionsData;
2079 TPM2B_MAX_NV_BUFFER nvWriteData;
2080 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, };
2081 TPMS_AUTH_COMMAND sessionData;
2082 TPMS_AUTH_RESPONSE sessionDataOut;
2083
2084 TPMS_AUTH_COMMAND *sessionDataArray[1];
2085 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
2086
2087 sessionDataArray[0] = &sessionData;
2088 sessionDataOutArray[0] = &sessionDataOut;
2089
2090 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
2091 sessionsData.cmdAuths = &sessionDataArray[0];
2092
2093 // Init write data.
2094 nvWriteData.t.size = 0;
2095
2096 sessionsData.cmdAuthsCount = 1;
2097 sessionsData.cmdAuths[0]->sessionHandle = policySession->sessionHandle;
2098 sessionsData.cmdAuths[0]->nonce.t.size = 0;
2099 sessionsData.cmdAuths[0]->hmac.t.size = 0;
2100
2101 *(UINT8 *)( (void *)&( sessionsData.cmdAuths[0]->sessionAttributes ) ) = 0;
2102 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 1;
2103
2104 rval = SetLocality( sysContext, 2 );
2105 CheckPassed( rval );
2106
2107 // Do NV write using open session's policy.
2108 rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
2109 TPM20_INDEX_PASSWORD_TEST,
2110 &sessionsData, &nvWriteData, 0, &sessionsDataOut );
2111 CheckFailed( rval, TPM_RC_LOCALITY );
2112
2113 rval = SetLocality( sysContext, 3 );
2114 CheckPassed( rval );
2115
2116 // Do NV write using open session's policy.
2117 rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
2118 TPM20_INDEX_PASSWORD_TEST,
2119 &sessionsData, &nvWriteData, 0, &sessionsDataOut );
2120 CheckPassed( rval );
2121
2122 // Do another NV write using open session's policy.
2123 rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST,
2124 TPM20_INDEX_PASSWORD_TEST,
2125 &sessionsData, &nvWriteData, 0, &sessionsDataOut );
2126 CheckFailed( rval, TPM_RC_POLICY_FAIL + TPM_RC_S + TPM_RC_1 );
2127
2128 // Delete NV index
2129 sessionsData.cmdAuths[0]->sessionHandle = TPM_RS_PW;
2130 sessionsData.cmdAuths[0]->nonce.t.size = 0;
2131 sessionsData.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
2132 sessionData.hmac.t.size = 0;
2133
2134 // Now undefine the index.
2135 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM,
2136 TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
2137 CheckPassed( rval );
2138
2139 rval = DeleteEntity( TPM20_INDEX_PASSWORD_TEST );
2140 CheckPassed( rval );
2141
2142 return rval;
2143}
2144
2145UINT8 passwordPCRTestPassword[] = "password PCR";
2146UINT8 dataBlob[] = "some data";
2147TPM_HANDLE blobHandle;
2148TPM2B_AUTH blobAuth;
2149
2150TPM_RC BuildPasswordPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest )
2151{
2152 TPM_RC rval = TPM_RC_SUCCESS;
2153
2154 rval = Tss2_Sys_PolicyPassword( sysContext, policySession->sessionHandle, 0, 0 );
2155 CheckPassed( rval );
2156
2157 return rval;
2158}
2159
2160TPM_RC BuildAuthValuePolicy( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest )
2161{
2162 TPM_RC rval = TPM_RC_SUCCESS;
2163
2164 rval = Tss2_Sys_PolicyAuthValue( sysContext, policySession->sessionHandle, 0, 0 );
2165 CheckPassed( rval );
2166
2167 return rval;
2168}
2169
2170
2171TPM_RC BuildPasswordPcrPolicy( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession, TPM2B_DIGEST *policyDigest )
2172{
2173 TPM_RC rval = TPM_RC_SUCCESS;
2174 TPM2B_DIGEST pcrDigest;
2175 TPML_PCR_SELECTION pcrs;
2176 TPML_DIGEST pcrValues;
2177 UINT32 pcrUpdateCounter;
2178 TPML_PCR_SELECTION pcrSelectionOut;
2179
2180 pcrDigest.t.size = 0;
2181 rval = Tss2_Sys_PolicyPassword( sysContext, policySession->sessionHandle, 0, 0 );
2182 CheckPassed( rval );
2183
2184 pcrs.count = 1;
2185 pcrs.pcrSelections[0].hash = TPM_ALG_SHA1;
2186 pcrs.pcrSelections[0].sizeofSelect = 3;
2187 pcrs.pcrSelections[0].pcrSelect[0] = 0;
2188 pcrs.pcrSelections[0].pcrSelect[1] = 0;
2189 pcrs.pcrSelections[0].pcrSelect[2] = 0;
2190 SET_PCR_SELECT_BIT( pcrs.pcrSelections[0], PCR_0 );
2191 SET_PCR_SELECT_BIT( pcrs.pcrSelections[0], PCR_3 );
2192
2193 //
2194 // Compute pcrDigest
2195 //
2196 // Read PCRs
2197 rval = Tss2_Sys_PCR_Read( sysContext, 0, &pcrs, &pcrUpdateCounter, &pcrSelectionOut, &pcrValues, 0 );
2198 CheckPassed( rval );
2199 // Hash them together
2200 rval = TpmHashSequence( policySession->authHash, pcrValues.count, &pcrValues.digests[0], &pcrDigest );
2201
2202 rval = Tss2_Sys_PolicyPCR( sysContext, policySession->sessionHandle, 0, &pcrDigest, &pcrs, 0 );
2203 CheckPassed( rval );
2204
2205 return rval;
2206}
2207
2208
2209TPM_RC CreateDataBlob( TSS2_SYS_CONTEXT *sysContext, SESSION **policySession, TPM2B_DIGEST *policyDigest )
2210{
2211 TPM_RC rval = TPM_RC_SUCCESS;
2212 TPMS_AUTH_COMMAND cmdAuth;
2213 TPMS_AUTH_COMMAND *cmdSessionArray[1] = { &cmdAuth };
2214 TSS2_SYS_CMD_AUTHS cmdAuthArray = { 1, &cmdSessionArray[0] };
2215 TPM2B_SENSITIVE_CREATE inSensitive;
2216 TPM2B_PUBLIC inPublic;
2217 TPM2B_DATA outsideInfo = { { 0, } };
2218 TPML_PCR_SELECTION creationPcr = { 0 };
2219 TPM2B_PUBLIC outPublic;
2220 TPM2B_CREATION_DATA creationData;
2221 TPM_HANDLE srkHandle;
2222 TPM2B_DIGEST creationHash;
2223 TPMT_TK_CREATION creationTicket;
2224 TPM2B_NAME srkName, blobName;
2225 TPM2B_DIGEST data;
2226 TPM2B_PRIVATE outPrivate;
2227
2228 cmdAuth.sessionHandle = TPM_RS_PW;
2229 cmdAuth.nonce.t.size = 0;
2230 *( (UINT8 *)((void *)&cmdAuth.sessionAttributes ) ) = 0;
2231 cmdAuth.hmac.t.size = 0;
2232
2233 inSensitive.t.sensitive.userAuth.t.size = 0;
2234 inSensitive.t.sensitive.data.t.size = 0;
2235
2236 inPublic.t.publicArea.type = TPM_ALG_RSA;
2237 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
2238 *(UINT32 *)&( inPublic.t.publicArea.objectAttributes) = 0;
2239 inPublic.t.publicArea.objectAttributes.restricted = 1;
2240 inPublic.t.publicArea.objectAttributes.userWithAuth = 1;
2241 inPublic.t.publicArea.objectAttributes.decrypt = 1;
2242 inPublic.t.publicArea.objectAttributes.fixedTPM = 1;
2243 inPublic.t.publicArea.objectAttributes.fixedParent = 1;
2244 inPublic.t.publicArea.objectAttributes.sensitiveDataOrigin = 1;
2245 inPublic.t.publicArea.authPolicy.t.size = 0;
2246 inPublic.t.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
2247 inPublic.t.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
2248 inPublic.t.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_CBC;
2249 inPublic.t.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
2250 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 2048;
2251 inPublic.t.publicArea.parameters.rsaDetail.exponent = 0;
2252 inPublic.t.publicArea.unique.rsa.t.size = 0;
2253
2254 outPublic.t.size = 0;
2255 creationData.t.size = 0;
2256 rval = Tss2_Sys_CreatePrimary( sysContext, TPM_RH_PLATFORM, &cmdAuthArray,
2257 &inSensitive, &inPublic, &outsideInfo, &creationPcr,
2258 &srkHandle, &outPublic, &creationData, &creationHash,
2259 &creationTicket, &srkName, 0 );
2260 CheckPassed( rval );
2261
2262 cmdAuth.sessionHandle = TPM_RS_PW;
2263
2264 inSensitive.t.sensitive.userAuth.t.size = 0;
2265 blobAuth.t.size = sizeof( passwordPCRTestPassword );
2266 memcpy( &blobAuth.t.buffer, passwordPCRTestPassword, sizeof( passwordPCRTestPassword ) );
2267 CopySizedByteBuffer( &(inSensitive.t.sensitive.userAuth.b ), &blobAuth.b );
2268 data.t.size = sizeof( dataBlob );
2269 memcpy( &data.t.buffer, dataBlob, sizeof( dataBlob ) );
2270 CopySizedByteBuffer( &(inSensitive.t.sensitive.data.b ), &data.b );
2271
2272 inPublic.t.publicArea.type = TPM_ALG_KEYEDHASH;
2273 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA256;
2274 inPublic.t.publicArea.objectAttributes.restricted = 0;
2275 inPublic.t.publicArea.objectAttributes.decrypt = 0;
2276 inPublic.t.publicArea.objectAttributes.sensitiveDataOrigin = 0;
2277 CopySizedByteBuffer( &( inPublic.t.publicArea.authPolicy.b), &( policyDigest->b ) );
2278 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM_ALG_NULL;
2279 inPublic.t.publicArea.unique.keyedHash.t.size = 0;
2280
2281 outPublic.t.size = 0;
2282 creationData.t.size = 0;
2283 rval = Tss2_Sys_Create( sysContext, srkHandle, &cmdAuthArray,
2284 &inSensitive, &inPublic, &outsideInfo, &creationPcr,
2285 &outPrivate, &outPublic, &creationData, &creationHash,
2286 &creationTicket, 0 );
2287 CheckPassed( rval );
2288
2289 // Now we need to load the object.
2290 rval = Tss2_Sys_Load( sysContext, srkHandle, &cmdAuthArray, &outPrivate, &outPublic, &blobHandle, &blobName, 0 );
2291 CheckPassed( rval );
2292
2293 return rval;
2294}
2295
2296TPM_RC AuthValueUnseal( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession )
2297{
2298 TPM_RC rval = TPM_RC_SUCCESS;
2299 TPM2B_SENSITIVE_DATA outData;
2300 TPMS_AUTH_COMMAND cmdAuth;
2301 TPMS_AUTH_COMMAND *cmdSessionArray[1] = { &cmdAuth };
2302 TSS2_SYS_CMD_AUTHS cmdAuthArray = { 1, &cmdSessionArray[0] };
2303
2304 cmdAuth.sessionHandle = policySession->sessionHandle;
2305 cmdAuth.nonce.t.size = 0;
2306 *( (UINT8 *)((void *)&cmdAuth.sessionAttributes ) ) = 0;
2307 cmdAuth.sessionAttributes.continueSession = 1;
2308 cmdAuth.hmac.t.size = 0;
2309
2310 // Now try to unseal the blob without setting the HMAC.
2311 // This test should fail.
2312 rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
2313 CheckFailed( rval, TPM_RC_S + TPM_RC_1 + TPM_RC_AUTH_FAIL );
2314
2315 // Clear DA lockout.
2316 TestDictionaryAttackLockReset();
2317
2318 //
2319 // Now try to unseal the blob after setting the HMAC.
2320 // This test should pass.
2321 //
2322
2323 // First, call Prepare.
2324 rval = Tss2_Sys_Unseal_Prepare( sysContext, blobHandle );
2325 CheckPassed( rval );
2326
2327 rval = AddEntity( blobHandle, &blobAuth );
2328 CheckPassed( rval );
2329
2330 // Roll nonces for command.
2331 RollNonces( policySession, &cmdAuth.nonce );
2332
2333 // Now generate the HMAC.
2334 rval = ComputeCommandHmacs( sysContext,
2335 blobHandle,
2336 TPM_HT_NO_HANDLE, &cmdAuthArray, 1 );
2337 CheckPassed( rval );
2338
2339 rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
2340 CheckPassed( rval );
2341
2342 rval = DeleteEntity( blobHandle );
2343 CheckPassed( rval );
2344
2345 // Add test to make sure we unsealed correctly.
2346
2347 // Now we'll want to flush the data blob and remove it
2348 // from resource manager tables.
2349 rval = Tss2_Sys_FlushContext( sysContext, blobHandle );
2350 CheckPassed( rval );
2351
2352 return rval;
2353}
2354
2355TPM_RC PasswordUnseal( TSS2_SYS_CONTEXT *sysContext, SESSION *policySession )
2356{
2357 TPM_RC rval = TPM_RC_SUCCESS;
2358 TPM2B_SENSITIVE_DATA outData;
2359 TPMS_AUTH_COMMAND cmdAuth;
2360 TPMS_AUTH_COMMAND *cmdSessionArray[1] = { &cmdAuth };
2361 TSS2_SYS_CMD_AUTHS cmdAuthArray = { 1, &cmdSessionArray[0] };
2362
2363 cmdAuth.sessionHandle = policySession->sessionHandle;
2364 cmdAuth.nonce.t.size = 0;
2365 *( (UINT8 *)((void *)&cmdAuth.sessionAttributes ) ) = 0;
2366 cmdAuth.sessionAttributes.continueSession = 1;
2367 cmdAuth.hmac.t.size = 0;
2368
2369 // Now try to unseal the blob without setting the password.
2370 // This test should fail.
2371 rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
2372 CheckFailed( rval, TPM_RC_S + TPM_RC_1 + TPM_RC_AUTH_FAIL );
2373
2374 // Clear DA lockout.
2375 TestDictionaryAttackLockReset();
2376
2377 // Now try to unseal the blob after setting the password.
2378 // This test should pass.
2379 cmdAuth.hmac.t.size = sizeof( passwordPCRTestPassword );
2380 memcpy( &cmdAuth.hmac.t.buffer, passwordPCRTestPassword, sizeof( passwordPCRTestPassword ) );
2381 rval = Tss2_Sys_Unseal( sysContext, blobHandle, &cmdAuthArray, &outData, 0 );
2382 CheckPassed( rval );
2383
2384 // Add test to make sure we unsealed correctly.
2385
2386 // Now we'll want to flush the data blob and remove it
2387 // from resource manager tables.
2388 rval = Tss2_Sys_FlushContext( sysContext, blobHandle );
2389 CheckPassed( rval );
2390
2391 return rval;
2392}
2393
2394POLICY_TEST_SETUP policyTestSetups[] =
2395{
2396 // NOTE: Since locality is a fairly simple command and we
2397 // can guarantee its correctness, we don't need a trial
2398 // session for this. buildPolicyFn pointer can be 0 in
2399 // this case.
2400 { "LOCALITY", 0, CreateNVIndex, TestLocality },
2401 { "PASSWORD", BuildPasswordPolicy, CreateDataBlob, PasswordUnseal },
2402 { "PASSWORD/PCR", BuildPasswordPcrPolicy, CreateDataBlob, PasswordUnseal },
2403 { "AUTHVALUE", BuildAuthValuePolicy, CreateDataBlob, AuthValueUnseal },
2404 // TBD...
2405};
2406
2407void TestPolicy()
2408{
2409 UINT32 rval;
2410 unsigned int i;
2411 SESSION *policySession = 0;
2412
2413 TpmClientPrintf( 0, "\nPOLICY TESTS:\n" );
2414
2415 for( i = 0; i < ( sizeof( policyTestSetups ) / sizeof( POLICY_TEST_SETUP ) ); i++ )
2416 {
2417 TPM2B_DIGEST policyDigest;
2418
2419 rval = TPM_RC_SUCCESS;
2420
2421 TpmClientPrintf( 0, "Policy Test: %s\n", policyTestSetups[i].name );
2422
2423 // Create trial policy session and run policy commands, in order to create policyDigest.
2424 if( policyTestSetups[i].buildPolicyFn != 0)
2425 {
2426 rval = BuildPolicy( sysContext, &policySession, policyTestSetups[i].buildPolicyFn, &policyDigest, true );
2427 CheckPassed( rval );
2428 }
2429
2430 // Create entity that will use that policyDigest as authPolicy.
2431 if( policyTestSetups[i].createObjectFn != 0 )
2432 {
2433 rval = ( *policyTestSetups[i].createObjectFn )( sysContext, &policySession, &policyDigest);
2434 CheckPassed( rval );
2435 }
2436
2437 // Create real policy session and run policy commands; after this we're ready
2438 // to authorize actions on the entity.
2439 if( policyTestSetups[i].buildPolicyFn != 0)
2440 {
2441 rval = BuildPolicy( sysContext, &policySession, policyTestSetups[i].buildPolicyFn, &policyDigest, false );
2442 CheckPassed( rval );
2443 }
2444
2445 if( policySession )
2446 {
2447 // Now do tests by authorizing actions on the entity.
2448 rval = ( *policyTestSetups[i].testPolicyFn)( sysContext, policySession );
2449 CheckPassed( rval );
2450
2451 // Need to flush the session here.
2452 rval = Tss2_Sys_FlushContext( sysContext, policySession->sessionHandle );
2453 CheckPassed( rval );
2454
2455 // And remove the session from test app session table.
2456 rval = EndAuthSession( policySession );
2457 }
2458 else
2459 {
2460 CheckFailed( rval, 0xffffffff );
2461 }
2462
2463 CheckPassed( rval );
2464 }
2465}
2466
2467#define MAX_TEST_SEQUENCES 10
2468void TestHash()
2469{
2470 UINT32 rval;
2471 TPM2B_AUTH auth;
2472 TPMI_DH_OBJECT sequenceHandle[MAX_TEST_SEQUENCES];
2473 TPMS_AUTH_COMMAND sessionData, sessionData1;
2474 TPMS_AUTH_RESPONSE sessionDataOut, sessionDataOut1;
2475 TSS2_SYS_CMD_AUTHS sessionsData;
2476 int i;
2477 TPM2B_MAX_BUFFER dataToHash;
2478
2479 UINT8 memoryToHash[] =
2480 {
2481 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2482 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2483 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2484 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2485 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2486 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2487 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2488 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2489 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2490 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2491 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2492 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2493 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2494 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2495 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2496 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2497 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2498 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2499 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2500 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2501 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2502 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2503 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2504 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2505 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2506 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2507 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2508 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2509 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2510 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2511 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2512 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2513 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2514 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2515 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2516 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2517 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2518 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2519 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2520 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2521 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2522 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2523 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2524 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2525 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2526 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2527 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2528 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2529 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2530 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2531 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2532 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2533 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2534 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2535 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2536 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2537 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2538 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2539 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2540 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2541 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2542 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2543 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2544 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2545 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2546 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
2547 0xde, 0xad, 0xbe, 0xef
2548 };
2549
2550 // Known good hash of above memory.
2551 UINT8 goodHashValue[] =
2552 { 0xB3, 0xFD, 0x6A, 0xD2, 0x9F, 0xD0, 0x13, 0x52, 0xBA, 0xFC,
2553 0x8B, 0x22, 0xC9, 0x6D, 0x88, 0x42, 0xA3, 0x3C, 0xB0, 0xC9 };
2554
2555 // Hash to be calculated by TPM.
2556 TPM2B_DIGEST result;
2557 TPMT_TK_HASHCHECK validation;
2558 TSS2_SYS_RSP_AUTHS sessionsDataOut;
2559
2560 TPMS_AUTH_COMMAND *sessionDataArray[2];
2561 TPMS_AUTH_RESPONSE *sessionDataOutArray[2];
2562
2563 sessionDataArray[0] = &sessionData;
2564 sessionDataOutArray[0] = &sessionDataOut;
2565 sessionDataArray[1] = &sessionData1;
2566 sessionDataOutArray[1] = &sessionDataOut1;
2567
2568 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
2569 sessionsData.cmdAuths = &sessionDataArray[0];
2570
2571 sessionsDataOut.rspAuthsCount = 1;
2572
2573 TpmClientPrintf( 0, "\nHASH TESTS:\n" );
2574
2575 auth.t.size = 2;
2576 auth.t.buffer[0] = 0;
2577 auth.t.buffer[1] = 0xff;
2578 rval = Tss2_Sys_HashSequenceStart ( sysContext, 0, &auth, TPM_ALG_SHA1, &sequenceHandle[0], 0 );
2579 CheckPassed( rval );
2580
2581 // Init authHandle
2582 sessionData.sessionHandle = TPM_RS_PW;
2583
2584 // Init nonce.
2585 sessionData.nonce.t.size = 0;
2586
2587 // init hmac
2588 sessionData.hmac = auth;
2589
2590 // Init session attributes
2591 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
2592
2593 sessionsData.cmdAuthsCount = 1;
2594 sessionsData.cmdAuths[0] = &sessionData;
2595
2596 dataToHash.t.size = MAX_DIGEST_BUFFER;
2597 memcpy( &dataToHash.t.buffer[0], &memoryToHash[0], dataToHash.t.size );
2598
2599 rval = Tss2_Sys_SequenceUpdate ( sysContext, sequenceHandle[0], &sessionsData, &dataToHash, &sessionsDataOut );
2600 CheckPassed( rval );
2601
2602 // Now try starting a bunch of sequences to see what happens.
2603 // This checks that the resource manager properly saves and restores the context
2604 // of the interrupted original sequence.
2605 for( i = 1; i < 5; i++ )
2606 {
2607 rval = Tss2_Sys_HashSequenceStart ( sysContext, 0, &auth, TPM_ALG_SHA1, &sequenceHandle[i], 0 );
2608 CheckPassed( rval );
2609 }
2610
2611 // Now end the created sequences.
2612 dataToHash.t.size = 0;
2613 for( i = 1; i < 5; i++ )
2614 {
2615 rval = Tss2_Sys_SequenceComplete ( sysContext, sequenceHandle[i], &sessionsData, &dataToHash,
2616 TPM_RH_PLATFORM, &result, &validation, &sessionsDataOut );
2617 CheckPassed( rval );
2618 }
2619
2620 // Now try to finish the interrupted sequence.
2621 rval = Tss2_Sys_SequenceUpdate ( sysContext, sequenceHandle[0], &sessionsData, &dataToHash, &sessionsDataOut );
2622 CheckPassed( rval );
2623
2624 dataToHash.t.size = sizeof( memoryToHash ) - MAX_DIGEST_BUFFER;
2625 memcpy( &dataToHash.t.buffer[0], &memoryToHash[MAX_DIGEST_BUFFER], dataToHash.t.size );
2626 rval = Tss2_Sys_SequenceComplete ( sysContext, sequenceHandle[0], &sessionsData, &dataToHash,
2627 TPM_RH_PLATFORM, &result, &validation, &sessionsDataOut );
2628 CheckPassed( rval );
2629
2630 // Test the resulting hash.
2631 if( memcmp( (void *)&( result.t.buffer[0] ), (void *)&( goodHashValue[0] ), result.t.size ) )
2632 {
2633 TpmClientPrintf( 0, "ERROR!! resulting hash is incorrect.\n" );
2634 Cleanup();
2635 }
2636
2637 // Now try starting a bunch of sequences to see what happens.
2638 // This stresses the resource manager.
2639 for( i = 0; i < MAX_TEST_SEQUENCES; i++ )
2640 {
2641 rval = Tss2_Sys_HashSequenceStart ( sysContext, 0, &auth, TPM_ALG_SHA1, &sequenceHandle[i], 0 );
2642 CheckPassed( rval );
2643 }
2644
2645 // Now end them all
2646 dataToHash.t.size = 0;
2647 for( i = (MAX_TEST_SEQUENCES - 1); i >= 0; i-- )
2648// for( i = 0; i < MAX_TEST_SEQUENCES; i++ )
2649 {
2650 rval = Tss2_Sys_SequenceComplete ( sysContext, sequenceHandle[i], &sessionsData, &dataToHash,
2651 TPM_RH_PLATFORM, &result, &validation, &sessionsDataOut );
2652 CheckPassed( rval );
2653 }
2654}
2655
2656void TestQuote()
2657{
2658 UINT32 rval;
2659 TPM2B_DATA qualifyingData;
2660 UINT8 qualDataString[] = { 0x00, 0xff, 0x55, 0xaa };
2661 TPMT_SIG_SCHEME inScheme;
2662 TPML_PCR_SELECTION pcrSelection;
2663 TPMS_AUTH_COMMAND sessionData;
2664 TPMS_AUTH_RESPONSE sessionDataOut;
2665 TSS2_SYS_CMD_AUTHS sessionsData;
2666 TSS2_SYS_RSP_AUTHS sessionsDataOut;
2667 TPM2B_ATTEST quoted;
2668 TPMT_SIGNATURE signature;
2669
2670 TPMS_AUTH_COMMAND *sessionDataArray[1];
2671 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
2672
2673 sessionDataArray[0] = &sessionData;
2674 sessionDataOutArray[0] = &sessionDataOut;
2675
2676 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
2677 sessionsData.cmdAuths = &sessionDataArray[0];
2678
2679 sessionsDataOut.rspAuthsCount = 1;
2680
2681 TpmClientPrintf( 0, "\nQUOTE CONTROL TESTS:\n" );
2682
2683 // Init authHandle
2684 sessionData.sessionHandle = TPM_RS_PW;
2685
2686 // Init nonce.
2687 sessionData.nonce.t.size = 0;
2688
2689 // init hmac
2690 sessionData.hmac.t.size = 2;
2691 sessionData.hmac.t.buffer[0] = 0x00;
2692 sessionData.hmac.t.buffer[1] = 0xff;
2693
2694 // Init session attributes
2695 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
2696
2697 qualifyingData.t.size = sizeof( qualDataString );
2698 memcpy( &( qualifyingData.t.buffer[0] ), qualDataString, sizeof( qualDataString ) );
2699
2700 inScheme.scheme = TPM_ALG_NULL;
2701
2702 pcrSelection.count = 1;
2703 pcrSelection.pcrSelections[0].hash = TPM_ALG_SHA1;
2704 pcrSelection.pcrSelections[0].sizeofSelect = 3;
2705
2706 // Clear out PCR select bit field
2707 pcrSelection.pcrSelections[0].pcrSelect[0] = 0;
2708 pcrSelection.pcrSelections[0].pcrSelect[1] = 0;
2709 pcrSelection.pcrSelections[0].pcrSelect[2] = 0;
2710
2711 // Now set the PCR you want
2712 pcrSelection.pcrSelections[0].pcrSelect[( PCR_17/8 )] = ( 1 << ( PCR_18 % 8) );
2713
2714 sessionsData.cmdAuthsCount = 1;
2715 sessionsData.cmdAuths[0] = &sessionData;
2716
2717 // Test with wrong type of key.
2718 rval = Tss2_Sys_Quote ( sysContext, loadedSha1KeyHandle, &sessionsData, &qualifyingData, &inScheme,
2719 &pcrSelection, &quoted, &signature, &sessionsDataOut );
2720 CheckPassed( rval );
2721
2722 // Create signing key
2723
2724
2725 // Now test quote operation
2726// Tpm20Quote ( ??TPMI_DH_OBJECT signHandle, TPM2B_DATA *qualifyingData,
2727// TPMT_SIG_SCHEME *inScheme, TPML_PCR_SELECTION *pcrSelect,
2728// TPMS_AUTH_COMMAND *sessionsData)
2729
2730}
2731
2732void ProvisionOtherIndices()
2733{
2734 UINT32 rval;
2735 TPMI_SH_AUTH_SESSION otherIndicesPolicyAuthHandle;
2736 TPM2B_DIGEST nvPolicyHash;
2737 TPM2B_AUTH nvAuth;
2738 TPMS_AUTH_COMMAND otherIndicesSessionData;
2739 TPMS_AUTH_RESPONSE otherIndicesSessionDataOut;
2740 TSS2_SYS_CMD_AUTHS otherIndicesSessionsData;
2741 TSS2_SYS_RSP_AUTHS otherIndicesSessionsDataOut;
2742 TPM2B_NV_PUBLIC publicInfo;
2743
2744 TPMS_AUTH_COMMAND *sessionDataArray[1];
2745 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
2746
2747 sessionDataArray[0] = &otherIndicesSessionData;
2748 sessionDataOutArray[0] = &otherIndicesSessionDataOut;
2749
2750 otherIndicesSessionsDataOut.rspAuths = &sessionDataOutArray[0];
2751 otherIndicesSessionsData.cmdAuths = &sessionDataArray[0];
2752
2753 otherIndicesSessionsDataOut.rspAuthsCount = 1;
2754
2755 TpmClientPrintf( 0, "\nPROVISION OTHER NV INDICES:\n" );
2756
2757 //
2758 // AUX index: Write is controlled by TPM2_PolicyLocality; Read is controlled by authValue and is unrestricted since authValue is set to emptyBuffer
2759 // Do this by setting up two policies and ORing them together when creating AuxIndex:
2760 // 1. PolicyLocality(3) && PolicyCommand(NVWrite)
2761 // 2. EmptyAuth policy && PolicyCommand(NVRead)
2762 // Page 126 of Part 1 describes how to do this.
2763 //
2764
2765 // Steps:
2766 rval = StartPolicySession( &otherIndicesPolicyAuthHandle );
2767 CheckPassed( rval );
2768
2769 // 3. GetPolicyDigest and save it
2770 rval = Tss2_Sys_PolicyGetDigest( sysContext, otherIndicesPolicyAuthHandle, 0, &nvPolicyHash, 0 );
2771 CheckPassed( rval );
2772
2773 // Now save the policy digest from the first OR branch.
2774 DEBUG_PRINT_BUFFER( &( nvPolicyHash.t.buffer[0] ), nvPolicyHash.t.size );
2775
2776 // 4. CreateNvIndex
2777 otherIndicesSessionData.sessionHandle = TPM_RS_PW;
2778
2779 // Init nonce.
2780 otherIndicesSessionData.nonce.t.size = 0;
2781
2782 // init hmac
2783 otherIndicesSessionData.hmac.t.size = 0;
2784
2785 // init nvAuth
2786 nvAuth.t.size = 0;
2787
2788 // Init session attributes
2789 *( (UINT8 *)((void *)&otherIndicesSessionData.sessionAttributes ) ) = 0;
2790
2791 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
2792 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
2793 sizeof( UINT16 );
2794 publicInfo.t.nvPublic.nvIndex = INDEX_LCP_SUP;
2795 publicInfo.t.nvPublic.nameAlg = TPM_ALG_SHA1;
2796
2797 // First zero out attributes.
2798 *(UINT32 *)&( publicInfo.t.nvPublic.attributes ) = 0;
2799
2800 // Now set the attributes.
2801 publicInfo.t.nvPublic.attributes.TPMA_NV_AUTHREAD = 1;
2802 publicInfo.t.nvPublic.attributes.TPMA_NV_AUTHWRITE = 1;
2803 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
2804 // Following commented out for convenience during development.
2805 // publicInfo.t.nvPublic.attributes.TPMA_NV_POLICY_DELETE = 1;
2806 publicInfo.t.nvPublic.attributes.TPMA_NV_WRITEDEFINE = 1;
2807 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
2808
2809 publicInfo.t.nvPublic.authPolicy.t.size = 0;
2810 publicInfo.t.nvPublic.dataSize = NV_PS_INDEX_SIZE;
2811
2812 otherIndicesSessionsData.cmdAuthsCount = 1;
2813 otherIndicesSessionsData.cmdAuths[0] = &otherIndicesSessionData;
2814
2815 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &otherIndicesSessionsData,
2816 &nvAuth, &publicInfo, &otherIndicesSessionsDataOut );
2817 CheckPassed( rval );
2818
2819 publicInfo.t.nvPublic.nvIndex = INDEX_LCP_OWN;
2820 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &otherIndicesSessionsData,
2821 &nvAuth, &publicInfo, &otherIndicesSessionsDataOut );
2822 CheckPassed( rval );
2823
2824 // Now teardown session
2825 rval = Tss2_Sys_FlushContext( sysContext, otherIndicesPolicyAuthHandle );
2826 CheckPassed( rval );
2827}
2828
2829
2830TSS2_RC InitNvAuxPolicySession( TPMI_SH_AUTH_SESSION *nvAuxPolicySessionHandle )
2831{
2832 TPMA_LOCALITY locality;
2833 TPM_RC rval;
2834
2835 rval = StartPolicySession( nvAuxPolicySessionHandle );
2836 CheckPassed( rval );
2837
2838 // 2. PolicyLocality(3)
2839 *(UINT8 *)((void *)&locality) = 0;
2840 locality.TPM_LOC_THREE = 1;
2841 locality.TPM_LOC_FOUR = 1;
2842 rval = Tss2_Sys_PolicyLocality( sysContext, *nvAuxPolicySessionHandle, 0, locality, 0 );
2843
2844 return( rval );
2845}
2846
2847void ProvisionNvAux()
2848{
2849 UINT32 rval;
2850 TPMI_SH_AUTH_SESSION nvAuxPolicyAuthHandle;
2851 TPM2B_DIGEST nvPolicyHash;
2852 TPM2B_AUTH nvAuth;
2853 TPMS_AUTH_COMMAND nvAuxSessionData;
2854 TPMS_AUTH_RESPONSE nvAuxSessionDataOut;
2855 TSS2_SYS_CMD_AUTHS nvAuxSessionsData;
2856 TSS2_SYS_RSP_AUTHS nvAuxSessionsDataOut;
2857 TPM2B_NV_PUBLIC publicInfo;
2858
2859 TPMS_AUTH_COMMAND *sessionDataArray[1];
2860 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
2861
2862 sessionDataArray[0] = &nvAuxSessionData;
2863 sessionDataOutArray[0] = &nvAuxSessionDataOut;
2864
2865 nvAuxSessionsDataOut.rspAuths = &sessionDataOutArray[0];
2866 nvAuxSessionsData.cmdAuths = &sessionDataArray[0];
2867
2868 nvAuxSessionsDataOut.rspAuthsCount = 1;
2869
2870 TpmClientPrintf( 0, "\nPROVISION NV AUX:\n" );
2871
2872 //
2873 // AUX index: Write is controlled by TPM2_PolicyLocality; Read is controlled by authValue and is unrestricted since authValue is set to emptyBuffer
2874 // Do this by setting up two policies and ORing them together when creating AuxIndex:
2875 // 1. PolicyLocality(3) && PolicyCommand(NVWrite)
2876 // 2. EmptyAuth policy && PolicyCommand(NVRead)
2877 // Page 126 of Part 1 describes how to do this.
2878 //
2879
2880 // Steps:
2881 rval = InitNvAuxPolicySession( &nvAuxPolicyAuthHandle );
2882 CheckPassed( rval );
2883
2884 // 3. GetPolicyDigest and save it
2885 rval = Tss2_Sys_PolicyGetDigest( sysContext, nvAuxPolicyAuthHandle, 0, &nvPolicyHash, 0 );
2886 CheckPassed( rval );
2887
2888 // Now save the policy digest.
2889 DEBUG_PRINT_BUFFER( &( nvPolicyHash.t.buffer[0] ), nvPolicyHash.t.size );
2890
2891 // 4. CreateNvIndex
2892 nvAuxSessionData.sessionHandle = TPM_RS_PW;
2893
2894 // Init nonce.
2895 nvAuxSessionData.nonce.t.size = 0;
2896
2897 // init hmac
2898 nvAuxSessionData.hmac.t.size = 0;
2899
2900 // init nvAuth
2901 nvAuth.t.size = 0;
2902
2903 // Init session attributes
2904 *( (UINT8 *)((void *)&nvAuxSessionData.sessionAttributes ) ) = 0;
2905
2906 nvAuxSessionsData.cmdAuthsCount = 1;
2907 nvAuxSessionsData.cmdAuths[0] = &nvAuxSessionData;
2908
2909 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
2910 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
2911 sizeof( UINT16 );
2912 publicInfo.t.nvPublic.nvIndex = INDEX_AUX;
2913 publicInfo.t.nvPublic.nameAlg = TPM_ALG_SHA1;
2914
2915 // First zero out attributes.
2916 *(UINT32 *)&( publicInfo.t.nvPublic.attributes ) = 0;
2917
2918 // Now set the attributes.
2919 publicInfo.t.nvPublic.attributes.TPMA_NV_AUTHREAD = 1;
2920 publicInfo.t.nvPublic.attributes.TPMA_NV_POLICYWRITE = 1;
2921 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
2922 // Following commented out for convenience during development.
2923 // publicInfo.t.nvPublic.attributes.TPMA_NV_POLICY_DELETE = 1;
2924
2925 publicInfo.t.nvPublic.authPolicy.t.size = GetDigestSize( TPM_ALG_SHA1 );
2926 memcpy( (UINT8 *)&( publicInfo.t.nvPublic.authPolicy.t.buffer ), (UINT8 *)&(nvPolicyHash.t.buffer[0]),
2927 nvPolicyHash.t.size );
2928
2929 publicInfo.t.nvPublic.dataSize = NV_AUX_INDEX_SIZE;
2930
2931 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM, &nvAuxSessionsData,
2932 &nvAuth, &publicInfo, &nvAuxSessionsDataOut );
2933 CheckPassed( rval );
2934
2935 // Now teardown session
2936 rval = Tss2_Sys_FlushContext( sysContext, nvAuxPolicyAuthHandle );
2937 CheckPassed( rval );
2938}
2939
2940void TpmAuxWrite( int locality)
2941{
2942 TSS2_RC rval;
2943 int i;
2944 TPMI_SH_AUTH_SESSION nvAuxPolicyAuthHandle;
2945 TPM2B_MAX_NV_BUFFER nvWriteData;
2946
2947 rval = InitNvAuxPolicySession( &nvAuxPolicyAuthHandle );
2948 CheckPassed( rval );
2949
2950 // Now we're going to test it.
2951 nvWriteData.t.size = 4;
2952 for( i = 0; i < nvWriteData.t.size; i++ )
2953 nvWriteData.t.buffer[i] = 0xff - i;
2954
2955 nullSessionData.sessionHandle = nvAuxPolicyAuthHandle;
2956
2957 // Make sure that session terminates after NVWrite completes.
2958 nullSessionData.sessionAttributes.continueSession = 0;
2959
2960 rval = SetLocality( sysContext, locality );
2961 CheckPassed( rval );
2962
2963 nullSessionsData.cmdAuthsCount = 1;
2964 nullSessionsData.cmdAuths[0] = &nullSessionData;
2965
2966 rval = Tss2_Sys_NV_Write( sysContext, INDEX_AUX, INDEX_AUX, &nullSessionsData, &nvWriteData, 0, &nullSessionsDataOut );
2967
2968 {
2969 TSS2_RC setLocalityRval;
2970 setLocalityRval = SetLocality( sysContext, 3 );
2971 CheckPassed( setLocalityRval );
2972 }
2973
2974 if( locality == 3 || locality == 4 )
2975 {
2976 CheckPassed( rval );
2977
2978 // No teardown of session needed, since the authorization was
2979 // successful.
2980 }
2981 else
2982 {
2983 CheckFailed( rval, TPM_RC_LOCALITY );
2984
2985 // Now teardown session
2986 rval = Tss2_Sys_FlushContext( sysContext, nvAuxPolicyAuthHandle );
2987 CheckPassed( rval );
2988 }
2989}
2990
2991void TpmAuxReadWriteTest()
2992{
2993 UINT32 rval;
2994 int testLocality;
2995 TPM2B_MAX_NV_BUFFER nvData;
2996
2997 TpmClientPrintf( 0, "TPM AUX READ/WRITE TEST\n" );
2998
2999 nullSessionData.sessionAttributes.continueSession = 0;
3000
3001 // Try writing it from all localities. Only locality 3 should work.
3002 for( testLocality = 0; testLocality < 5; testLocality++ )
3003 {
3004 TpmAuxWrite( testLocality );
3005 }
3006
3007 nullSessionData.sessionHandle = TPM_RS_PW;
3008
3009 nullSessionsData.cmdAuths[0] = &nullSessionData;
3010
3011 // Try reading it from all localities. They all should work.
3012 for( testLocality = 0; testLocality < 5; testLocality++ )
3013 {
3014 rval = SetLocality( sysContext, testLocality );
3015 CheckPassed( rval );
3016
3017 rval = Tss2_Sys_NV_Read( sysContext, INDEX_AUX, INDEX_AUX, &nullSessionsData, 4, 0, &nvData, &nullSessionsDataOut );
3018 CheckPassed( rval );
3019
3020 rval = SetLocality( sysContext, 3 );
3021 CheckPassed( rval );
3022 }
3023}
3024
3025void TpmOtherIndicesReadWriteTest()
3026{
3027 UINT32 rval;
3028 TPM2B_MAX_NV_BUFFER nvWriteData;
3029 int i;
3030 TPM2B_MAX_NV_BUFFER nvData;
3031
3032 nullSessionData.sessionHandle = TPM_RS_PW;
3033
3034 TpmClientPrintf( 0, "TPM OTHER READ/WRITE TEST\n" );
3035
3036 nvWriteData.t.size = 4;
3037 for( i = 0; i < nvWriteData.t.size; i++ )
3038 nvWriteData.t.buffer[i] = 0xff - i;
3039
3040 nullSessionsData.cmdAuthsCount = 1;
3041 nullSessionsData.cmdAuths[0] = &nullSessionData;
3042
3043 rval = Tss2_Sys_NV_Write( sysContext, INDEX_LCP_SUP, INDEX_LCP_SUP, &nullSessionsData, &nvWriteData, 0, &nullSessionsDataOut );
3044 CheckPassed( rval );
3045
3046 rval = Tss2_Sys_NV_Write( sysContext, INDEX_LCP_OWN, INDEX_LCP_OWN, &nullSessionsData, &nvWriteData, 0, &nullSessionsDataOut );
3047 CheckPassed( rval );
3048
3049 rval = Tss2_Sys_NV_Read( sysContext, INDEX_LCP_SUP, INDEX_LCP_SUP, &nullSessionsData, 4, 0, &nvData, &nullSessionsDataOut );
3050 CheckPassed( rval );
3051
3052 rval = Tss2_Sys_NV_Read( sysContext, INDEX_LCP_OWN, INDEX_LCP_OWN, &nullSessionsData, 4, 0, &nvData, &nullSessionsDataOut );
3053 CheckPassed( rval );
3054}
3055
3056void NvIndexProto()
3057{
3058 UINT32 rval;
3059
3060 TpmClientPrintf( 0, "\nNV INDEX PROTOTYPE TESTS:\n" );
3061
3062
3063 // AUX index: Write is controlled by TPM2_PolicyLocality; Read is controlled by authValue and is unrestricted since authValue is set to emptyBuffer
3064 // PS index: Write and read are unrestricted until TPM2_WriteLock. After that content is write protected
3065 // PO index: Write is restricted by ownerAuth; Read is controlled by authValue and is unrestricted since authValue is set to emptyBuffer
3066
3067 // Now we need to configure NV indices
3068 ProvisionNvAux();
3069
3070 ProvisionOtherIndices();
3071
3072 TpmAuxReadWriteTest();
3073
3074 TpmOtherIndicesReadWriteTest();
3075
3076 // Now undefine the aux index, so that subsequent test passes will work.
3077 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, INDEX_AUX, &nullSessionsData, &nullSessionsDataOut );
3078 CheckPassed( rval );
3079
3080 // Now undefine the other indices, so that subsequent test passes will work.
3081 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, INDEX_LCP_SUP, &nullSessionsData, &nullSessionsDataOut );
3082 CheckPassed( rval );
3083
3084 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, INDEX_LCP_OWN, &nullSessionsData, &nullSessionsDataOut );
3085 CheckPassed( rval );
3086}
3087
3088void TestPcrAllocate()
3089{
3090 UINT32 rval;
3091 TPML_PCR_SELECTION pcrSelection;
3092 TPMS_AUTH_COMMAND sessionData;
3093 TPMS_AUTH_RESPONSE sessionDataOut;
3094 TSS2_SYS_CMD_AUTHS sessionsData;
3095 TSS2_SYS_RSP_AUTHS sessionsDataOut;
3096 TPMI_YES_NO allocationSuccess;
3097 UINT32 maxPcr;
3098 UINT32 sizeNeeded;
3099 UINT32 sizeAvailable;
3100
3101 TPMS_AUTH_COMMAND *sessionDataArray[1];
3102 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
3103
3104 sessionDataArray[0] = &sessionData;
3105 sessionDataOutArray[0] = &sessionDataOut;
3106
3107 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
3108 sessionsData.cmdAuths = &sessionDataArray[0];
3109
3110 sessionsDataOut.rspAuthsCount = 1;
3111
3112 TpmClientPrintf( 0, "\nPCR ALLOCATE TEST :\n" );
3113
3114 // Init authHandle
3115 sessionData.sessionHandle = TPM_RS_PW;
3116
3117 // Init nonce.
3118 sessionData.nonce.t.size = 0;
3119
3120 // init hmac
3121 sessionData.hmac.t.size = 0;
3122
3123 // Init session attributes
3124 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
3125
3126 pcrSelection.count = 0;
3127
3128 sessionsData.cmdAuthsCount = 1;
3129 sessionsData.cmdAuths[0] = &sessionData;
3130
3131 rval = Tss2_Sys_PCR_Allocate( sysContext, TPM_RH_PLATFORM, &sessionsData, &pcrSelection,
3132 &allocationSuccess, &maxPcr, &sizeNeeded, &sizeAvailable, &sessionsDataOut);
3133 CheckPassed( rval );
3134
3135 pcrSelection.count = 3;
3136 pcrSelection.pcrSelections[0].hash = TPM_ALG_SHA256;
3137 CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[0] );
3138 SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[0], 3 );
3139 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_5 );
3140 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[0], PCR_7 );
3141 pcrSelection.pcrSelections[1].hash = TPM_ALG_SHA384;
3142 CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[1] );
3143 SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[1], 3 );
3144 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[1], PCR_5 );
3145 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[1], PCR_8 );
3146 pcrSelection.pcrSelections[2].hash = TPM_ALG_SHA256;
3147 CLEAR_PCR_SELECT_BITS( pcrSelection.pcrSelections[2] );
3148 SET_PCR_SELECT_SIZE( pcrSelection.pcrSelections[2], 3 );
3149 SET_PCR_SELECT_BIT( pcrSelection.pcrSelections[2], PCR_6 );
3150
3151 rval = Tss2_Sys_PCR_Allocate( sysContext, TPM_RH_PLATFORM, &sessionsData, &pcrSelection,
3152 &allocationSuccess, &maxPcr, &sizeNeeded, &sizeAvailable, &sessionsDataOut);
3153 CheckPassed( rval );
3154}
3155
3156void TestUnseal()
3157{
3158 UINT32 rval;
3159 TPMS_AUTH_COMMAND sessionData;
3160 TPMS_AUTH_RESPONSE sessionDataOut;
3161 TSS2_SYS_CMD_AUTHS sessionsData;
3162 TSS2_SYS_RSP_AUTHS sessionsDataOut;
3163 TPM2B_SENSITIVE_CREATE inSensitive;
3164 TPML_PCR_SELECTION creationPCR;
3165 TPM2B_DATA outsideInfo;
3166 TPM2B_PUBLIC inPublic;
3167 TPM_HANDLE loadedObjectHandle;
3168
3169 TPMS_AUTH_COMMAND *sessionDataArray[1];
3170 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
3171
3172 sessionDataArray[0] = &sessionData;
3173 sessionDataOutArray[0] = &sessionDataOut;
3174
3175 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
3176 sessionsData.cmdAuths = &sessionDataArray[0];
3177
3178 sessionsDataOut.rspAuthsCount = 1;
3179
3180 TPM2B_PRIVATE outPrivate;
3181 TPM2B_PUBLIC outPublic;
3182 TPM2B_CREATION_DATA creationData;
3183 TPM2B_DIGEST creationHash;
3184 TPMT_TK_CREATION creationTicket;
3185 TPM2B_NAME name;
3186 TPM2B_SENSITIVE_DATA outData;
3187
3188
3189 const char authStr[] = "test";
3190 const char sensitiveData[] = "this is sensitive";
3191
3192 TpmClientPrintf( 0, "\nUNSEAL TEST :\n" );
3193
3194 sessionData.sessionHandle = TPM_RS_PW;
3195
3196 // Init nonce.
3197 sessionData.nonce.t.size = 0;
3198
3199 sessionData.hmac.t.size = 2;
3200 sessionData.hmac.t.buffer[0] = 0x00;
3201 sessionData.hmac.t.buffer[1] = 0xff;
3202
3203 // Init session attributes
3204 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
3205
3206 inSensitive.t.sensitive.userAuth.t.size = sizeof( authStr ) - 1;
3207 memcpy( &( inSensitive.t.sensitive.userAuth.t.buffer[0] ), authStr, sizeof( authStr ) - 1 );
3208 inSensitive.t.sensitive.data.t.size = sizeof( sensitiveData ) - 1;
3209 memcpy( &( inSensitive.t.sensitive.data.t.buffer[0] ), sensitiveData, sizeof( sensitiveData ) - 1 );
3210
3211 inPublic.t.publicArea.authPolicy.t.size = 0;
3212
3213 inPublic.t.publicArea.unique.keyedHash.t.size = 0;
3214
3215 outsideInfo.t.size = 0;
3216 creationPCR.count = 0;
3217
3218 inPublic.t.publicArea.type = TPM_ALG_KEYEDHASH;
3219 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
3220
3221 *(UINT32 *)&( inPublic.t.publicArea.objectAttributes) = 0;
3222 inPublic.t.publicArea.objectAttributes.userWithAuth = 1;
3223
3224 inPublic.t.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM_ALG_NULL;
3225
3226 inPublic.t.publicArea.unique.keyedHash.t.size = 0;
3227
3228 outsideInfo.t.size = 0;
3229
3230 sessionsData.cmdAuthsCount = 1;
3231 sessionsData.cmdAuths[0] = &sessionData;
3232
3233 outPublic.t.size = 0;
3234 creationData.t.size = 0;
3235 rval = Tss2_Sys_Create( sysContext, handle2048rsa, &sessionsData, &inSensitive, &inPublic,
3236 &outsideInfo, &creationPCR,
3237 &outPrivate, &outPublic, &creationData,
3238 &creationHash, &creationTicket, &sessionsDataOut );
3239 CheckPassed( rval );
3240
3241 rval = Tss2_Sys_LoadExternal ( sysContext, 0, 0, &outPublic,
3242 TPM_RH_PLATFORM, &loadedObjectHandle, &name, 0 );
3243 CheckPassed( rval );
3244
3245 rval = Tss2_Sys_FlushContext( sysContext, loadedObjectHandle );
3246 CheckPassed( rval );
3247
3248 rval = Tss2_Sys_Load ( sysContext, handle2048rsa, &sessionsData, &outPrivate, &outPublic,
3249 &loadedObjectHandle, &name, &sessionsDataOut);
3250 CheckPassed( rval );
3251
3252 sessionData.hmac.t.size = sizeof( authStr ) - 1;
3253 memcpy( &( sessionData.hmac.t.buffer[0] ), authStr, sizeof( authStr ) - 1 );
3254
3255 rval = Tss2_Sys_Unseal( sysContext, loadedObjectHandle, &sessionsData, &outData, &sessionsDataOut );
3256
3257 rval = Tss2_Sys_FlushContext( sysContext, loadedObjectHandle );
3258 CheckPassed( rval );
3259
3260 CheckPassed( rval );
3261}
3262
3263
3264void CreatePasswordTestNV( TPMI_RH_NV_INDEX nvIndex, char * password )
3265{
3266 UINT32 rval;
3267 int i;
3268 TPMS_AUTH_COMMAND sessionData;
3269 TPMS_AUTH_RESPONSE sessionDataOut;
3270 TSS2_SYS_CMD_AUTHS sessionsData;
3271 TSS2_SYS_RSP_AUTHS sessionsDataOut;
3272 TPM2B_NV_PUBLIC publicInfo;
3273 TPM2B_AUTH nvAuth;
3274
3275 TPMS_AUTH_COMMAND *sessionDataArray[1];
3276 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
3277
3278 sessionDataArray[0] = &sessionData;
3279 sessionDataOutArray[0] = &sessionDataOut;
3280
3281 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
3282 sessionsData.cmdAuths = &sessionDataArray[0];
3283
3284 sessionData.sessionHandle = TPM_RS_PW;
3285
3286 // Init nonce.
3287 sessionData.nonce.t.size = 0;
3288
3289 // init hmac
3290 sessionData.hmac.t.size = 0;
3291
3292 // Init session attributes
3293 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
3294
3295 sessionsDataOut.rspAuthsCount = 1;
3296
3297 sessionsData.cmdAuthsCount = 1;
3298 sessionsData.cmdAuths[0] = &sessionData;
3299
3300 nvAuth.t.size = strlen( password );
3301 for( i = 0; i < nvAuth.t.size; i++ )
3302 nvAuth.t.buffer[i] = password[i];
3303
3304 publicInfo.t.size = sizeof( TPMI_RH_NV_INDEX ) +
3305 sizeof( TPMI_ALG_HASH ) + sizeof( TPMA_NV ) + sizeof( UINT16) +
3306 sizeof( UINT16 );
3307 publicInfo.t.nvPublic.nvIndex = nvIndex;
3308 publicInfo.t.nvPublic.nameAlg = TPM_ALG_SHA1;
3309
3310 // First zero out attributes.
3311 *(UINT32 *)&( publicInfo.t.nvPublic.attributes ) = 0;
3312
3313 // Now set the attributes.
3314 publicInfo.t.nvPublic.attributes.TPMA_NV_AUTHREAD = 1;
3315 publicInfo.t.nvPublic.attributes.TPMA_NV_AUTHWRITE = 1;
3316 publicInfo.t.nvPublic.attributes.TPMA_NV_PLATFORMCREATE = 1;
3317 publicInfo.t.nvPublic.attributes.TPMA_NV_ORDERLY = 1;
3318 publicInfo.t.nvPublic.authPolicy.t.size = 0;
3319 publicInfo.t.nvPublic.dataSize = 32;
3320
3321 rval = Tss2_Sys_NV_DefineSpace( sysContext, TPM_RH_PLATFORM,
3322 &sessionsData, &nvAuth, &publicInfo, &sessionsDataOut );
3323 CheckPassed( rval );
3324}
3325
3326// Password used to authorize access to the NV index.
3327char password[] = "test password";
3328
3329void PasswordTest()
3330{
3331 UINT32 rval;
3332 int i;
3333
3334 // Authorization structure for command.
3335 TPMS_AUTH_COMMAND sessionData;
3336
3337 // Authorization structure for response.
3338 TPMS_AUTH_RESPONSE sessionDataOut;
3339
3340 // Create and init authorization area for command:
3341 // only 1 authorization area.
3342 TPMS_AUTH_COMMAND *sessionDataArray[1] = { &sessionData };
3343
3344 // Create authorization area for response:
3345 // only 1 authorization area.
3346 TPMS_AUTH_RESPONSE *sessionDataOutArray[1] = { &sessionDataOut };
3347
3348 // Authorization array for command (only has one auth structure).
3349 TSS2_SYS_CMD_AUTHS sessionsData = { 1, &sessionDataArray[0] };
3350
3351 // Authorization array for response (only has one auth structure).
3352 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, &sessionDataOutArray[0] };
3353 TPM2B_MAX_NV_BUFFER nvWriteData;
3354
3355 TpmClientPrintf( 0, "\nPASSWORD TESTS:\n" );
3356
3357 // Create an NV index that will use password
3358 // authorizations the password will be
3359 // "test password".
3360 CreatePasswordTestNV( TPM20_INDEX_PASSWORD_TEST, password );
3361
3362 //
3363 // Initialize the command authorization area.
3364 //
3365
3366 // Init sessionHandle, nonce, session
3367 // attributes, and hmac (password).
3368 sessionData.sessionHandle = TPM_RS_PW;
3369 // Set zero sized nonce.
3370 sessionData.nonce.t.size = 0;
3371 // sessionAttributes is a bit field. To initialize
3372 // it to 0, cast to a pointer to UINT8 and
3373 // write 0 to that pointer.
3374 *( (UINT8 *)&sessionData.sessionAttributes ) = 0;
3375
3376 // Init password (HMAC field in authorization structure).
3377 sessionData.hmac.t.size = strlen( password );
3378 memcpy( &( sessionData.hmac.t.buffer[0] ),
3379 &( password[0] ), sessionData.hmac.t.size );
3380
3381 // Initialize write data.
3382 nvWriteData.t.size = 4;
3383 for( i = 0; i < nvWriteData.t.size; i++ )
3384 nvWriteData.t.buffer[i] = 0xff - i;
3385
3386 // Attempt write with the correct password.
3387 // It should pass.
3388 rval = Tss2_Sys_NV_Write( sysContext,
3389 TPM20_INDEX_PASSWORD_TEST,
3390 TPM20_INDEX_PASSWORD_TEST,
3391 &sessionsData, &nvWriteData, 0,
3392 &sessionsDataOut );
3393 // Check that the function passed as
3394 // expected. Otherwise, exit.
3395 CheckPassed( rval );
3396
3397 // Alter the password so it's incorrect.
3398 sessionData.hmac.t.buffer[4] = 0xff;
3399 rval = Tss2_Sys_NV_Write( sysContext,
3400 TPM20_INDEX_PASSWORD_TEST,
3401 TPM20_INDEX_PASSWORD_TEST,
3402 &sessionsData, &nvWriteData, 0,
3403 &sessionsDataOut );
3404 // Check that the function failed as expected,
3405 // since password was incorrect. If wrong
3406 // response code received, exit.
3407 CheckFailed( rval,
3408 TPM_RC_S + TPM_RC_1 + TPM_RC_AUTH_FAIL );
3409
3410 // Change hmac to null one, since null auth is
3411 // used to undefine the index.
3412 sessionData.hmac.t.size = 0;
3413
3414 // Now undefine the index.
3415 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM,
3416 TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
3417 CheckPassed( rval );
3418}
3419
3420
3421void SimplePolicyTest()
3422{
3423 UINT32 rval, sessionCmdRval;
3424 TPM2B_AUTH nvAuth;
3425 SESSION *nvSession, *trialPolicySession;
3426 TPMA_NV nvAttributes;
3427 TPM2B_DIGEST authPolicy;
3428 TPM2B_NAME nvName;
3429 TPM2B_MAX_NV_BUFFER nvWriteData, nvReadData;
3430 UINT8 dataToWrite[] = { 0x00, 0xff, 0x55, 0xaa };
3431 int i;
3432 TPM2B_ENCRYPTED_SECRET encryptedSalt;
3433 TPMT_SYM_DEF symmetric;
3434 TPMA_SESSION sessionAttributes;
3435
3436 // Command authorization area: one password session.
3437 TPMS_AUTH_COMMAND nvCmdAuth = { TPM_RS_PW, };
3438 TPMS_AUTH_COMMAND *nvCmdAuthArray[1] = { &nvCmdAuth };
3439 TSS2_SYS_CMD_AUTHS nvCmdAuths = { 1, &nvCmdAuthArray[0] };
3440
3441 // Response authorization area.
3442 TPMS_AUTH_RESPONSE nvRspAuth;
3443 TPMS_AUTH_RESPONSE *nvRspAuthArray[1] = { &nvRspAuth };
3444 TSS2_SYS_RSP_AUTHS nvRspAuths = { 1, &nvRspAuthArray[0] };
3445 TPM_ALG_ID sessionAlg = TPM_ALG_SHA256;
3446 TPM2B_NONCE nonceCaller;
3447
3448 nonceCaller.t.size = 0;
3449
3450 TpmClientPrintf( 0, "\nSIMPLE POLICY TEST:\n" );
3451
3452 //
3453 // Create NV index.
3454 //
3455
3456 // Setup the NV index's authorization value.
3457 nvAuth.t.size = 0;
3458
3459 // Zero sized encrypted salt, since the session
3460 // is unsalted.
3461 encryptedSalt.t.size = 0;
3462
3463 // No symmetric algorithm.
3464 symmetric.algorithm = TPM_ALG_NULL;
3465
3466 //
3467 // Create the NV index's authorization policy
3468 // using a trial policy session.
3469 //
3470 rval = StartAuthSessionWithParams( &trialPolicySession, TPM_RH_NULL,
3471 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM_SE_TRIAL,
3472 &symmetric, sessionAlg );
3473 CheckPassed( rval );
3474
3475 rval = Tss2_Sys_PolicyAuthValue( sysContext, trialPolicySession->sessionHandle, 0, 0 );
3476 CheckPassed( rval );
3477
3478 // Get policy digest.
3479 rval = Tss2_Sys_PolicyGetDigest( sysContext, trialPolicySession->sessionHandle,
3480 0, &authPolicy, 0 );
3481 CheckPassed( rval );
3482
3483 // End the trial session by flushing it.
3484 rval = Tss2_Sys_FlushContext( sysContext, trialPolicySession->sessionHandle );
3485 CheckPassed( rval );
3486
3487 // And remove the trial policy session from sessions table.
3488 rval = EndAuthSession( trialPolicySession );
3489 CheckPassed( rval );
3490
3491 // Now set the NV index's attributes:
3492 // policyRead, authWrite, and platormCreate.
3493 *(UINT32 *)( (void *)&nvAttributes ) = 0;
3494 nvAttributes.TPMA_NV_POLICYREAD = 1;
3495 nvAttributes.TPMA_NV_POLICYWRITE = 1;
3496 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
3497
3498 // Create the NV index.
3499 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW,
3500 &nvAuth, &authPolicy, TPM20_INDEX_PASSWORD_TEST,
3501 sessionAlg, nvAttributes, 32 );
3502 CheckPassed( rval );
3503
3504 // Add index and associated authorization value to
3505 // entity table. This helps when we need
3506 // to calculate HMACs.
3507 AddEntity( TPM20_INDEX_PASSWORD_TEST, &nvAuth );
3508 CheckPassed( rval );
3509
3510 // Get the name of the NV index.
3511 rval = (*HandleToNameFunctionPtr)( TPM20_INDEX_PASSWORD_TEST,
3512 &nvName );
3513 CheckPassed( rval );
3514
3515 //
3516 // Start real (non-trial) policy authorization session:
3517 // it's an unbound and unsalted session, no symmetric
3518 // encryption algorithm, and SHA256 is the session's
3519 // hash algorithm.
3520 //
3521
3522 // Zero sized encrypted salt, since the session
3523 // is unsalted.
3524 encryptedSalt.t.size = 0;
3525
3526 // No symmetric algorithm.
3527 symmetric.algorithm = TPM_ALG_NULL;
3528
3529 // Create the session.
3530 // Session state (session handle, nonces, etc.) gets
3531 // saved into nvSession structure for later use.
3532 rval = StartAuthSessionWithParams( &nvSession, TPM_RH_NULL,
3533 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY,
3534 &symmetric, sessionAlg );
3535 CheckPassed( rval );
3536
3537 // Get the name of the session and save it in
3538 // the nvSession structure.
3539 rval = (*HandleToNameFunctionPtr)( nvSession->sessionHandle,
3540 &nvSession->name );
3541 CheckPassed( rval );
3542
3543 // Initialize NV write data.
3544 nvWriteData.t.size = sizeof( dataToWrite );
3545 for( i = 0; i < nvWriteData.t.size; i++ )
3546 {
3547 nvWriteData.t.buffer[i] = dataToWrite[i];
3548 }
3549
3550 //
3551 // Now setup for writing the NV index.
3552 //
3553
3554 rval = Tss2_Sys_PolicyAuthValue( sysContext, nvSession->sessionHandle, 0, 0 );
3555 CheckPassed( rval );
3556
3557 // Get policy digest.
3558 rval = Tss2_Sys_PolicyGetDigest( sysContext, trialPolicySession->sessionHandle,
3559 0, &authPolicy, 0 );
3560 CheckPassed( rval );
3561
3562 // First call prepare in order to create cpBuffer.
3563 rval = Tss2_Sys_NV_Write_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
3564 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
3565 CheckPassed( rval );
3566
3567 // Configure command authorization area, except for HMAC.
3568 nvCmdAuths.cmdAuths[0]->sessionHandle = nvSession->sessionHandle;
3569 nvCmdAuths.cmdAuths[0]->nonce.t.size = 1;
3570 nvCmdAuths.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
3571 *( (UINT8 *)((void *)&sessionAttributes ) ) = 0;
3572 nvCmdAuths.cmdAuths[0]->sessionAttributes = sessionAttributes;
3573 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 1;
3574
3575 // Roll nonces for command
3576 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
3577
3578 // Complete command authorization area, by computing
3579 // HMAC and setting it in nvCmdAuths.
3580 rval = ComputeCommandHmacs( sysContext,
3581 TPM20_INDEX_PASSWORD_TEST,
3582 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
3583 TPM_RC_FAILURE );
3584 CheckPassed( rval );
3585
3586 // Finally!! Write the data to the NV index.
3587 // If the command is successful, the command
3588 // HMAC was correct.
3589 sessionCmdRval = Tss2_Sys_NV_Write( sysContext,
3590 TPM20_INDEX_PASSWORD_TEST,
3591 TPM20_INDEX_PASSWORD_TEST,
3592 &nvCmdAuths, &nvWriteData, 0, &nvRspAuths );
3593 CheckPassed( sessionCmdRval );
3594
3595 // Roll nonces for response
3596 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
3597
3598 if( sessionCmdRval == TPM_RC_SUCCESS )
3599 {
3600 // If the command was successful, check the
3601 // response HMAC to make sure that the
3602 // response was received correctly.
3603 rval = CheckResponseHMACs( sysContext, sessionCmdRval,
3604 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
3605 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
3606 CheckPassed( rval );
3607 }
3608
3609 rval = Tss2_Sys_PolicyAuthValue( sysContext, nvSession->sessionHandle, 0, 0 );
3610 CheckPassed( rval );
3611
3612 // First call prepare in order to create cpBuffer.
3613 rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
3614 TPM20_INDEX_PASSWORD_TEST, sizeof( dataToWrite ), 0 );
3615 CheckPassed( rval );
3616
3617 // Roll nonces for command
3618 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
3619
3620 // End the session after next command.
3621 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 0;
3622
3623 // Complete command authorization area, by computing
3624 // HMAC and setting it in nvCmdAuths.
3625 rval = ComputeCommandHmacs( sysContext,
3626 TPM20_INDEX_PASSWORD_TEST,
3627 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
3628 TPM_RC_FAILURE );
3629 CheckPassed( rval );
3630
3631 // And now read the data back.
3632 // If the command is successful, the command
3633 // HMAC was correct.
3634 sessionCmdRval = Tss2_Sys_NV_Read( sysContext,
3635 TPM20_INDEX_PASSWORD_TEST,
3636 TPM20_INDEX_PASSWORD_TEST,
3637 &nvCmdAuths, sizeof( dataToWrite ), 0,
3638 &nvReadData, &nvRspAuths );
3639 CheckPassed( sessionCmdRval );
3640
3641 // Roll nonces for response
3642 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
3643
3644 if( sessionCmdRval == TPM_RC_SUCCESS )
3645 {
3646 // If the command was successful, check the
3647 // response HMAC to make sure that the
3648 // response was received correctly.
3649 rval = CheckResponseHMACs( sysContext, sessionCmdRval,
3650 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
3651 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
3652 CheckPassed( rval );
3653 }
3654
3655 // Check that write and read data are equal.
3656 if( memcmp( (void *)&nvReadData.t.buffer[0],
3657 (void *)&nvWriteData.t.buffer[0], nvReadData.t.size ) )
3658 {
3659 TpmClientPrintf( 0, "ERROR!! read data not equal to written data\n" );
3660 Cleanup();
3661 }
3662
3663 //
3664 // Now cleanup: undefine the NV index and delete
3665 // the NV index's entity table entry.
3666 //
3667
3668 // Setup authorization for undefining the NV index.
3669 nvCmdAuths.cmdAuths[0]->sessionHandle = TPM_RS_PW;
3670 nvCmdAuths.cmdAuths[0]->nonce.t.size = 0;
3671 nvCmdAuths.cmdAuths[0]->hmac.t.size = 0;
3672
3673 // Undefine the NV index.
3674 rval = Tss2_Sys_NV_UndefineSpace( sysContext,
3675 TPM_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST,
3676 &nvCmdAuths, 0 );
3677 CheckPassed( rval );
3678
3679 // Delete the NV index's entry in the entity table.
3680 rval = DeleteEntity( TPM20_INDEX_PASSWORD_TEST );
3681 CheckPassed( rval );
3682}
3683
3684void SimpleHmacTest()
3685{
3686 UINT32 rval, sessionCmdRval;
3687 TPM2B_AUTH nvAuth;
3688 SESSION *nvSession;
3689 TPMA_NV nvAttributes;
3690 TPM2B_DIGEST authPolicy;
3691 TPM2B_NAME nvName;
3692 TPM2B_MAX_NV_BUFFER nvWriteData, nvReadData;
3693 UINT8 dataToWrite[] = { 0x00, 0xff, 0x55, 0xaa };
3694 char sharedSecret[] = "shared secret";
3695 int i;
3696 TPM2B_ENCRYPTED_SECRET encryptedSalt;
3697 TPMT_SYM_DEF symmetric;
3698 TPMA_SESSION sessionAttributes;
3699
3700 // Command authorization area: one password session.
3701 TPMS_AUTH_COMMAND nvCmdAuth = { TPM_RS_PW, };
3702 TPMS_AUTH_COMMAND *nvCmdAuthArray[1] = { &nvCmdAuth };
3703 TSS2_SYS_CMD_AUTHS nvCmdAuths = { 1, &nvCmdAuthArray[0] };
3704
3705 // Response authorization area.
3706 TPMS_AUTH_RESPONSE nvRspAuth;
3707 TPMS_AUTH_RESPONSE *nvRspAuthArray[1] = { &nvRspAuth };
3708 TSS2_SYS_RSP_AUTHS nvRspAuths = { 1, &nvRspAuthArray[0] };
3709 TPM2B_NONCE nonceCaller;
3710
3711 nonceCaller.t.size = 0;
3712
3713 TpmClientPrintf( 0, "\nSIMPLE HMAC SESSION TEST:\n" );
3714
3715 //
3716 // Create NV index.
3717 //
3718
3719 // Setup the NV index's authorization value.
3720 nvAuth.t.size = strlen( sharedSecret );
3721 for( i = 0; i < nvAuth.t.size; i++ )
3722 nvAuth.t.buffer[i] = sharedSecret[i];
3723
3724 // Set NV index's authorization policy
3725 // to zero sized policy since we won't be
3726 // using policy to authorize.
3727 authPolicy.t.size = 0;
3728
3729 // Now set the NV index's attributes:
3730 // policyRead, authWrite, and platormCreate.
3731 *(UINT32 *)( (void *)&nvAttributes ) = 0;
3732 nvAttributes.TPMA_NV_AUTHREAD = 1;
3733 nvAttributes.TPMA_NV_AUTHWRITE = 1;
3734 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
3735
3736 // Create the NV index.
3737 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW,
3738 &nvAuth, &authPolicy, TPM20_INDEX_PASSWORD_TEST,
3739 TPM_ALG_SHA256, nvAttributes, 32 );
3740 CheckPassed( rval );
3741
3742 // Add index and associated authorization value to
3743 // entity table. This helps when we need
3744 // to calculate HMACs.
3745 AddEntity( TPM20_INDEX_PASSWORD_TEST, &nvAuth );
3746 CheckPassed( rval );
3747
3748 // Get the name of the NV index.
3749 rval = (*HandleToNameFunctionPtr)( TPM20_INDEX_PASSWORD_TEST,
3750 &nvName );
3751 CheckPassed( rval );
3752
3753 //
3754 // Start HMAC authorization session: it's an
3755 // unbound and unsalted session, no symmetric
3756 // encryption algorithm, and SHA256 is the session's
3757 // hash algorithm.
3758 //
3759
3760 // Zero sized encrypted salt, since the session
3761 // is unsalted.
3762 encryptedSalt.t.size = 0;
3763
3764 // No symmetric algorithm.
3765 symmetric.algorithm = TPM_ALG_NULL;
3766
3767 // Create the session.
3768 // Session state (session handle, nonces, etc.) gets
3769 // saved into nvSession structure for later use.
3770 rval = StartAuthSessionWithParams( &nvSession, TPM_RH_NULL,
3771 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM_SE_HMAC,
3772 &symmetric, TPM_ALG_SHA256 );
3773 CheckPassed( rval );
3774
3775 // Get the name of the session and save it in
3776 // the nvSession structure.
3777 rval = (*HandleToNameFunctionPtr)( nvSession->sessionHandle,
3778 &nvSession->name );
3779 CheckPassed( rval );
3780
3781 // Initialize NV write data.
3782 nvWriteData.t.size = sizeof( dataToWrite );
3783 for( i = 0; i < nvWriteData.t.size; i++ )
3784 {
3785 nvWriteData.t.buffer[i] = dataToWrite[i];
3786 }
3787
3788 //
3789 // Now setup for writing the NV index.
3790 //
3791
3792 // First call prepare in order to create cpBuffer.
3793 rval = Tss2_Sys_NV_Write_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
3794 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
3795 CheckPassed( rval );
3796
3797 // Configure command authorization area, except for HMAC.
3798 nvCmdAuths.cmdAuths[0]->sessionHandle = nvSession->sessionHandle;
3799 nvCmdAuths.cmdAuths[0]->nonce.t.size = 1;
3800 nvCmdAuths.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
3801 *( (UINT8 *)(&sessionAttributes ) ) = 0;
3802 nvCmdAuths.cmdAuths[0]->sessionAttributes = sessionAttributes;
3803 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 1;
3804
3805 // Roll nonces for command
3806 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
3807
3808 // Complete command authorization area, by computing
3809 // HMAC and setting it in nvCmdAuths.
3810 rval = ComputeCommandHmacs( sysContext,
3811 TPM20_INDEX_PASSWORD_TEST,
3812 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
3813 TPM_RC_FAILURE );
3814 CheckPassed( rval );
3815
3816 // Finally!! Write the data to the NV index.
3817 // If the command is successful, the command
3818 // HMAC was correct.
3819 sessionCmdRval = Tss2_Sys_NV_Write( sysContext,
3820 TPM20_INDEX_PASSWORD_TEST,
3821 TPM20_INDEX_PASSWORD_TEST,
3822 &nvCmdAuths, &nvWriteData, 0, &nvRspAuths );
3823 CheckPassed( sessionCmdRval );
3824
3825 // Roll nonces for response
3826 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
3827
3828 if( sessionCmdRval == TPM_RC_SUCCESS )
3829 {
3830 // If the command was successful, check the
3831 // response HMAC to make sure that the
3832 // response was received correctly.
3833 rval = CheckResponseHMACs( sysContext, sessionCmdRval,
3834 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
3835 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
3836 CheckPassed( rval );
3837 }
3838
3839 // First call prepare in order to create cpBuffer.
3840 rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
3841 TPM20_INDEX_PASSWORD_TEST, sizeof( dataToWrite ), 0 );
3842 CheckPassed( rval );
3843
3844 // Roll nonces for command
3845 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
3846
3847 // End the session after next command.
3848 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 0;
3849
3850 // Complete command authorization area, by computing
3851 // HMAC and setting it in nvCmdAuths.
3852 rval = ComputeCommandHmacs( sysContext,
3853 TPM20_INDEX_PASSWORD_TEST,
3854 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
3855 TPM_RC_FAILURE );
3856 CheckPassed( rval );
3857
3858 // And now read the data back.
3859 // If the command is successful, the command
3860 // HMAC was correct.
3861 sessionCmdRval = Tss2_Sys_NV_Read( sysContext,
3862 TPM20_INDEX_PASSWORD_TEST,
3863 TPM20_INDEX_PASSWORD_TEST,
3864 &nvCmdAuths, sizeof( dataToWrite ), 0,
3865 &nvReadData, &nvRspAuths );
3866 CheckPassed( sessionCmdRval );
3867
3868 // Roll nonces for response
3869 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
3870
3871 if( sessionCmdRval == TPM_RC_SUCCESS )
3872 {
3873 // If the command was successful, check the
3874 // response HMAC to make sure that the
3875 // response was received correctly.
3876 rval = CheckResponseHMACs( sysContext, sessionCmdRval,
3877 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
3878 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
3879 CheckPassed( rval );
3880 }
3881
3882 // Check that write and read data are equal.
3883 if( memcmp( (void *)&nvReadData.t.buffer[0],
3884 (void *)&nvWriteData.t.buffer[0], nvReadData.t.size ) )
3885 {
3886 TpmClientPrintf( 0, "ERROR!! read data not equal to written data\n" );
3887 Cleanup();
3888 }
3889
3890 //
3891 // Now cleanup: undefine the NV index and delete
3892 // the NV index's entity table entry.
3893 //
3894
3895 // Setup authorization for undefining the NV index.
3896 nvCmdAuths.cmdAuths[0]->sessionHandle = TPM_RS_PW;
3897 nvCmdAuths.cmdAuths[0]->nonce.t.size = 0;
3898 nvCmdAuths.cmdAuths[0]->hmac.t.size = 0;
3899
3900 // Undefine the NV index.
3901 rval = Tss2_Sys_NV_UndefineSpace( sysContext,
3902 TPM_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST,
3903 &nvCmdAuths, 0 );
3904 CheckPassed( rval );
3905
3906 // Delete the NV index's entry in the entity table.
3907 rval = DeleteEntity( TPM20_INDEX_PASSWORD_TEST );
3908 CheckPassed( rval );
3909
3910 rval = EndAuthSession( nvSession );
3911
3912 CheckPassed( rval );
3913}
3914
3915
3916void SimpleHmacOrPolicyTest( bool hmacTest )
3917{
3918 UINT32 rval, sessionCmdRval;
3919 TPM2B_AUTH nvAuth;
3920 SESSION *nvSession, *trialPolicySession;
3921 TPMA_NV nvAttributes;
3922 TPM2B_DIGEST authPolicy;
3923 TPM2B_NAME nvName;
3924 TPM2B_MAX_NV_BUFFER nvWriteData, nvReadData;
3925 UINT8 dataToWrite[] = { 0x00, 0xff, 0x55, 0xaa };
3926 char sharedSecret[] = "shared secret";
3927 int i;
3928 TPM2B_ENCRYPTED_SECRET encryptedSalt;
3929 TPMT_SYM_DEF symmetric;
3930 TPMA_SESSION sessionAttributes;
3931 TPM_SE tpmSe;
3932 char *testString;
3933 char testStringHmac[] = "HMAC";
3934 char testStringPolicy[] = "POLICY";
3935
3936 // Command authorization area: one password session.
3937 TPMS_AUTH_COMMAND nvCmdAuth = { TPM_RS_PW, };
3938 TPMS_AUTH_COMMAND *nvCmdAuthArray[1] = { &nvCmdAuth };
3939 TSS2_SYS_CMD_AUTHS nvCmdAuths = { 1, &nvCmdAuthArray[0] };
3940
3941 // Response authorization area.
3942 TPMS_AUTH_RESPONSE nvRspAuth;
3943 TPMS_AUTH_RESPONSE *nvRspAuthArray[1] = { &nvRspAuth };
3944 TSS2_SYS_RSP_AUTHS nvRspAuths = { 1, &nvRspAuthArray[0] };
3945
3946 TSS2_SYS_CONTEXT *simpleTestContext;
3947 TPM2B_NONCE nonceCaller;
3948
3949 nonceCaller.t.size = 0;
3950
3951 if( hmacTest )
3952 testString = testStringHmac;
3953 else
3954 testString = testStringPolicy;
3955
3956 TpmClientPrintf( 0, "\nSIMPLE %s SESSION TEST:\n", testString );
3957
3958 // Create sysContext structure.
3959 simpleTestContext = InitSysContext( 1000, resMgrTctiContext, &abiVersion );
3960 if( simpleTestContext == 0 )
3961 {
3962 InitSysContextFailure();
3963 }
3964
3965 // Setup the NV index's authorization value.
3966 nvAuth.t.size = strlen( sharedSecret );
3967 for( i = 0; i < nvAuth.t.size; i++ )
3968 nvAuth.t.buffer[i] = sharedSecret[i];
3969
3970 //
3971 // Create NV index.
3972 //
3973 if( hmacTest )
3974 {
3975 // Setup the NV index's authorization value.
3976 nvAuth.t.size = strlen( sharedSecret );
3977 for( i = 0; i < nvAuth.t.size; i++ )
3978 nvAuth.t.buffer[i] = sharedSecret[i];
3979
3980 // Set NV index's authorization policy
3981 // to zero sized policy since we won't be
3982 // using policy to authorize.
3983
3984 authPolicy.t.size = 0;
3985 }
3986 else
3987 {
3988 // Zero sized encrypted salt, since the session
3989 // is unsalted.
3990
3991 encryptedSalt.t.size = 0;
3992
3993 // No symmetric algorithm.
3994 symmetric.algorithm = TPM_ALG_NULL;
3995
3996 //
3997 // Create the NV index's authorization policy
3998 // using a trial policy session.
3999 //
4000 rval = StartAuthSessionWithParams( &trialPolicySession,
4001 TPM_RH_NULL, 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt,
4002 TPM_SE_TRIAL,
4003 &symmetric, TPM_ALG_SHA256 );
4004 CheckPassed( rval );
4005
4006 rval = Tss2_Sys_PolicyAuthValue( simpleTestContext,
4007 trialPolicySession->sessionHandle, 0, 0 );
4008 CheckPassed( rval );
4009
4010 // Get policy digest.
4011 rval = Tss2_Sys_PolicyGetDigest( simpleTestContext,
4012 trialPolicySession->sessionHandle,
4013 0, &authPolicy, 0 );
4014 CheckPassed( rval );
4015
4016 // End the trial session by flushing it.
4017 rval = Tss2_Sys_FlushContext( simpleTestContext,
4018 trialPolicySession->sessionHandle );
4019 CheckPassed( rval );
4020
4021 // And remove the trial policy session from
4022 // sessions table.
4023 rval = EndAuthSession( trialPolicySession );
4024 CheckPassed( rval );
4025 }
4026
4027 // Now set the NV index's attributes:
4028 // policyRead, authWrite, and platormCreate.
4029 *(UINT32 *)( &nvAttributes ) = 0;
4030 if( hmacTest )
4031 {
4032 nvAttributes.TPMA_NV_AUTHREAD = 1;
4033 nvAttributes.TPMA_NV_AUTHWRITE = 1;
4034 }
4035 else
4036 {
4037 nvAttributes.TPMA_NV_POLICYREAD = 1;
4038 nvAttributes.TPMA_NV_POLICYWRITE = 1;
4039 }
4040 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
4041
4042 // Create the NV index.
4043 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW,
4044 &nvAuth, &authPolicy, TPM20_INDEX_PASSWORD_TEST,
4045 TPM_ALG_SHA256, nvAttributes, 32 );
4046 CheckPassed( rval );
4047
4048 // Add index and associated authorization value to
4049 // entity table. This helps when we need
4050 // to calculate HMACs.
4051 AddEntity( TPM20_INDEX_PASSWORD_TEST, &nvAuth );
4052 CheckPassed( rval );
4053
4054 // Get the name of the NV index.
4055 rval = (*HandleToNameFunctionPtr)(
4056 TPM20_INDEX_PASSWORD_TEST,
4057 &nvName );
4058 CheckPassed( rval );
4059
4060
4061 //
4062 // Start HMAC or real (non-trial) policy authorization session:
4063 // it's an unbound and unsalted session, no symmetric
4064 // encryption algorithm, and SHA256 is the session's
4065 // hash algorithm.
4066 //
4067
4068 // Zero sized encrypted salt, since the session
4069 // is unsalted.
4070 encryptedSalt.t.size = 0;
4071
4072 // No symmetric algorithm.
4073 symmetric.algorithm = TPM_ALG_NULL;
4074
4075 // Create the session, hmac or policy depending
4076 // on hmacTest.
4077 // Session state (session handle, nonces, etc.) gets
4078 // saved into nvSession structure for later use.
4079 if( hmacTest )
4080 tpmSe = TPM_SE_HMAC;
4081 else
4082 tpmSe = TPM_SE_POLICY;
4083
4084 rval = StartAuthSessionWithParams( &nvSession, TPM_RH_NULL,
4085 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, tpmSe,
4086 &symmetric, TPM_ALG_SHA256 );
4087 CheckPassed( rval );
4088
4089 // Get the name of the session and save it in
4090 // the nvSession structure.
4091 rval = (*HandleToNameFunctionPtr)( nvSession->sessionHandle,
4092 &(nvSession->name) );
4093 CheckPassed( rval );
4094
4095 // Initialize NV write data.
4096 nvWriteData.t.size = sizeof( dataToWrite );
4097 for( i = 0; i < nvWriteData.t.size; i++ )
4098 {
4099 nvWriteData.t.buffer[i] = dataToWrite[i];
4100 }
4101
4102 //
4103 // Now setup for writing the NV index.
4104 //
4105 if( !hmacTest )
4106 {
4107 // Send policy command.
4108 rval = Tss2_Sys_PolicyAuthValue( simpleTestContext,
4109 nvSession->sessionHandle, 0, 0 );
4110 CheckPassed( rval );
4111 }
4112
4113 // First call prepare in order to create cpBuffer.
4114 rval = Tss2_Sys_NV_Write_Prepare( simpleTestContext,
4115 TPM20_INDEX_PASSWORD_TEST,
4116 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
4117 CheckPassed( rval );
4118
4119 // Configure command authorization area, except for HMAC.
4120 nvCmdAuths.cmdAuths[0]->sessionHandle =
4121 nvSession->sessionHandle;
4122 nvCmdAuths.cmdAuths[0]->nonce.t.size = 1;
4123 nvCmdAuths.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
4124 *( (UINT8 *)(&sessionAttributes ) ) = 0;
4125 nvCmdAuths.cmdAuths[0]->sessionAttributes = sessionAttributes;
4126 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 1;
4127
4128 // Roll nonces for command
4129 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
4130
4131 // Complete command authorization area, by computing
4132 // HMAC and setting it in nvCmdAuths.
4133 rval = ComputeCommandHmacs( simpleTestContext,
4134 TPM20_INDEX_PASSWORD_TEST,
4135 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
4136 TPM_RC_FAILURE );
4137 CheckPassed( rval );
4138
4139 // Finally!! Write the data to the NV index.
4140 // If the command is successful, the command
4141 // HMAC was correct.
4142 sessionCmdRval = Tss2_Sys_NV_Write( simpleTestContext,
4143 TPM20_INDEX_PASSWORD_TEST,
4144 TPM20_INDEX_PASSWORD_TEST,
4145 &nvCmdAuths, &nvWriteData, 0, &nvRspAuths );
4146 CheckPassed( sessionCmdRval );
4147
4148 // Roll nonces for response
4149 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
4150
4151 if( sessionCmdRval == TPM_RC_SUCCESS )
4152 {
4153 // If the command was successful, check the
4154 // response HMAC to make sure that the
4155 // response was received correctly.
4156 rval = CheckResponseHMACs( simpleTestContext, sessionCmdRval,
4157 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
4158 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
4159 CheckPassed( rval );
4160 }
4161
4162 if( !hmacTest )
4163 {
4164 // Send policy command.
4165 rval = Tss2_Sys_PolicyAuthValue( simpleTestContext,
4166 nvSession->sessionHandle, 0, 0 );
4167 CheckPassed( rval );
4168 }
4169 // First call prepare in order to create cpBuffer.
4170 rval = Tss2_Sys_NV_Read_Prepare( simpleTestContext,
4171 TPM20_INDEX_PASSWORD_TEST,
4172 TPM20_INDEX_PASSWORD_TEST,
4173 sizeof( dataToWrite ), 0 );
4174 CheckPassed( rval );
4175
4176 // Roll nonces for command
4177 RollNonces( nvSession, &nvCmdAuths.cmdAuths[0]->nonce );
4178
4179 // End the session after next command.
4180 nvCmdAuths.cmdAuths[0]->sessionAttributes.continueSession = 0;
4181
4182 // Complete command authorization area, by computing
4183 // HMAC and setting it in nvCmdAuths.
4184 rval = ComputeCommandHmacs( simpleTestContext,
4185 TPM20_INDEX_PASSWORD_TEST,
4186 TPM20_INDEX_PASSWORD_TEST, &nvCmdAuths,
4187 TPM_RC_FAILURE );
4188 CheckPassed( rval );
4189
4190 // And now read the data back.
4191 // If the command is successful, the command
4192 // HMAC was correct.
4193 sessionCmdRval = Tss2_Sys_NV_Read( simpleTestContext,
4194 TPM20_INDEX_PASSWORD_TEST,
4195 TPM20_INDEX_PASSWORD_TEST,
4196 &nvCmdAuths, sizeof( dataToWrite ), 0,
4197 &nvReadData, &nvRspAuths );
4198 CheckPassed( sessionCmdRval );
4199
4200 // Roll nonces for response
4201 RollNonces( nvSession, &nvRspAuths.rspAuths[0]->nonce );
4202
4203 if( sessionCmdRval == TPM_RC_SUCCESS )
4204 {
4205 // If the command was successful, check the
4206 // response HMAC to make sure that the
4207 // response was received correctly.
4208 rval = CheckResponseHMACs( simpleTestContext, sessionCmdRval,
4209 &nvCmdAuths, TPM20_INDEX_PASSWORD_TEST,
4210 TPM20_INDEX_PASSWORD_TEST, &nvRspAuths );
4211 CheckPassed( rval );
4212 }
4213
4214 // Check that write and read data are equal.
4215 if( memcmp( (void *)&nvReadData.t.buffer[0],
4216 (void *)&nvWriteData.t.buffer[0], nvReadData.t.size ) )
4217 {
4218 TpmClientPrintf( 0, "ERROR!! read data not equal to written data\n" );
4219 Cleanup();
4220 }
4221
4222 //
4223 // Now cleanup: undefine the NV index and delete
4224 // the NV index's entity table entry.
4225 //
4226
4227 // Setup authorization for undefining the NV index.
4228 nvCmdAuths.cmdAuths[0]->sessionHandle = TPM_RS_PW;
4229 nvCmdAuths.cmdAuths[0]->nonce.t.size = 0;
4230 nvCmdAuths.cmdAuths[0]->hmac.t.size = 0;
4231
4232 // Undefine the NV index.
4233 rval = Tss2_Sys_NV_UndefineSpace( simpleTestContext,
4234 TPM_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST,
4235 &nvCmdAuths, 0 );
4236 CheckPassed( rval );
4237
4238 // Delete the NV index's entry in the entity table.
4239 rval = DeleteEntity( TPM20_INDEX_PASSWORD_TEST );
4240 CheckPassed( rval );
4241
4242 // Remove the real session from sessions table.
4243 rval = EndAuthSession( nvSession );
4244
4245 CheckPassed( rval );
4246
4247 TeardownSysContext( &simpleTestContext );
4248}
4249
4250
4251typedef struct {
4252 TPMI_DH_OBJECT tpmKey;
4253 TPMI_DH_ENTITY bound;
4254 TPM2B_MAX_BUFFER *salt;
4255 char hmacTestDescription[50];
4256} HMAC_TEST_SETUP;
4257
4258TPM2B_MAX_BUFFER nullSalt = { { 0, { 0xa4 } }, };
4259TPM2B_MAX_BUFFER nonNullSalt = { { 2, { 0xa5, 0 } } };
4260
4261HMAC_TEST_SETUP hmacTestSetups[] =
4262{
4263 { TPM_RH_NULL, TPM_RH_NULL, &nullSalt, "UNBOUND/UNSALTED SESSION TEST" },
4264 { TPM_RH_NULL, TPM20_INDEX_PASSWORD_TEST, &nullSalt, "BOUND SESSION TEST" },
4265 { 0, TPM_RH_NULL, &nonNullSalt, "SALTED SESSION TEST" },
4266 { 0, TPM20_INDEX_PASSWORD_TEST, &nonNullSalt, "BOUND/SALTED SESSION TEST" },
4267};
4268
4269#define PLAINTEXT_SESSION 0
4270#define DECRYPT_SESSION 1
4271#define ENCRYPT_SESSION 2
4272
4273//UINT8 decryptEncryptSetups[] = { PLAINTEXT_SESSION, DECRYPT_SESSION, ENCRYPT_SESSION };
4274UINT8 decryptEncryptSetups[] = { PLAINTEXT_SESSION };
4275
4276#define CFB_MODE 0
4277#define XOR_MODE 1
4278
4279void HmacSessionTest()
4280{
4281 UINT32 rval;
4282 unsigned int i, j, k, decryptEncryptMode;
4283 TPM2B_MAX_NV_BUFFER nvWriteData;
4284 UINT8 dataToWrite[] = { 0x00, 0xff, 0x55, 0xaa };
4285 TPM2B_NAME nvName;
4286 TPM_RC sessionCmdRval;
4287
4288 SESSION *nvSession;
4289 TSS2_SYS_CONTEXT *rdSysContext;
4290 TSS2_SYS_CONTEXT *wrSysContext;
4291 TPM2B_AUTH nvAuth;
4292 TPMT_SYM_DEF symmetric;
4293 TPM2B_NONCE nonceOlder;
4294
4295 // Create two sysContext structures.
4296 rdSysContext = InitSysContext( 1000, resMgrTctiContext, &abiVersion );
4297 if( rdSysContext == 0 )
4298 {
4299 InitSysContextFailure();
4300 }
4301
4302 wrSysContext = InitSysContext( 1000, resMgrTctiContext, &abiVersion );
4303 if( wrSysContext == 0 )
4304 {
4305 InitSysContextFailure();
4306 }
4307
4308 char sharedSecret[] = "shared secret";
4309
4310 char buffer1contents[] = "test";
4311 char buffer2contents[] = "string";
4312
4313 TPM2B_MAX_BUFFER buffer1;
4314 TPM2B_MAX_BUFFER buffer2;
4315
4316// TPM2B_IV ivIn, ivOut;
4317 TPM2B_MAX_NV_BUFFER nvData;
4318 TPM2B_ENCRYPTED_SECRET encryptedSalt;
4319
4320 encryptedSalt.t.size = 0;
4321// ivIn.t.size = 0;
4322// ivOut.t.size = 0;
4323
4324 buffer1.t.size = strlen( buffer1contents );
4325 memcpy (buffer1.t.buffer, buffer1contents, buffer1.t.size );
4326 buffer2.t.size = strlen( buffer2contents );
4327 memcpy (buffer2.t.buffer, buffer2contents, buffer2.t.size );
4328
4329
4330 for( j = 0; j < sizeof( hmacTestSetups ) / sizeof( HMAC_TEST_SETUP ); j++ )
4331 {
4332 if( hmacTestSetups[j].salt == &nonNullSalt )
4333 {
4334 hmacTestSetups[j].tpmKey = handle2048rsa;
4335 }
4336 }
4337 TpmClientPrintf( 0, "\nHMAC SESSION TESTS:\n" );
4338
4339 for( j = 0; j < sizeof( hmacTestSetups ) / sizeof( HMAC_TEST_SETUP ); j++ )
4340 {
4341 // Iterate through variations of decrypt and encrypt sessions.
4342 for( k = 0; k < sizeof( decryptEncryptSetups ); k++ )
4343 {
4344 for( decryptEncryptMode = CFB_MODE; decryptEncryptMode < XOR_MODE; decryptEncryptMode++ )
4345 {
4346 TPMS_AUTH_COMMAND sessionData = { TPM_RS_PW, };
4347 TPMS_AUTH_RESPONSE sessionDataOut;
4348 TPMS_AUTH_COMMAND *sessionDataArray[1] = { &sessionData };
4349 TPMS_AUTH_RESPONSE *sessionDataOutArray[1] = { &sessionDataOut };
4350 TSS2_SYS_CMD_AUTHS sessionsData = { 1, &sessionDataArray[0] };
4351 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, &sessionDataOutArray[0] };
4352
4353 TPMT_RSA_DECRYPT inScheme;
4354 TPM2B_DATA label;
4355 TPM2B_DIGEST authPolicy;
4356 TPMA_NV nvAttributes;
4357
4358 TpmClientPrintf( 0, "\n\n%s:\n", hmacTestSetups[j].hmacTestDescription );
4359
4360 if( hmacTestSetups[j].tpmKey != TPM_RH_NULL )
4361 {
4362 sessionsData.cmdAuths[0]->hmac = loadedSha1KeyAuth;
4363 sessionsData.cmdAuths[0]->sessionHandle = TPM_RS_PW;
4364 sessionsData.cmdAuths[0]->nonce.t.size = 0;
4365 *( (UINT8 *)(&sessionData.sessionAttributes ) ) = 0;
4366
4367 inScheme.scheme = TPM_ALG_OAEP;
4368 inScheme.details.oaep.hashAlg = TPM_ALG_SHA1;
4369 memcpy( &( label.b.buffer ), "SECRET", 1 + strlen( "SECRET" ) );
4370 label.t.size = strlen( "SECRET" ) + 1;
4371
4372 // Encrypt salt with tpmKey.
4373 rval = Tss2_Sys_RSA_Encrypt( sysContext, handle2048rsa,
4374 0, (TPM2B_PUBLIC_KEY_RSA *)( hmacTestSetups[j].salt ),
4375 &inScheme, &label, (TPM2B_PUBLIC_KEY_RSA *)&encryptedSalt, 0 );
4376 CheckPassed( rval );
4377 }
4378
4379 // init hmac
4380 sessionData.hmac.t.size = 0;
4381
4382 // NOW CREATE THE INDEX
4383 authPolicy.t.size = 0;
4384
4385 nvAuth.t.size = strlen( sharedSecret );
4386 for( i = 0; i < nvAuth.t.size; i++ )
4387 nvAuth.t.buffer[i] = sharedSecret[i];
4388
4389 // Now set the attributes.
4390 *(UINT32 *)( &nvAttributes ) = 0;
4391 nvAttributes.TPMA_NV_AUTHREAD = 1;
4392 nvAttributes.TPMA_NV_AUTHWRITE = 1;
4393 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
4394
4395 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW, &nvAuth, &authPolicy,
4396 TPM20_INDEX_PASSWORD_TEST, TPM_ALG_SHA1, nvAttributes, 32 );
4397 CheckPassed( rval );
4398
4399 AddEntity( TPM20_INDEX_PASSWORD_TEST, &nvAuth );
4400 CheckPassed( rval );
4401
4402 // Get the name using TPM function.
4403 rval = (*HandleToNameFunctionPtr)( TPM20_INDEX_PASSWORD_TEST, &nvName );
4404 CheckPassed( rval );
4405
4406 //
4407 // Start session
4408 //
4409 nonceOlder.t.size = GetDigestSize( TPM_ALG_SHA1 );
4410 for( i = 0; i < nonceOlder.t.size; i++ )
4411 nonceOlder.t.buffer[i] = 0;
4412
4413 if( decryptEncryptSetups[k] == PLAINTEXT_SESSION )
4414 {
4415 symmetric.algorithm = TPM_ALG_NULL;
4416 }
4417 else if( decryptEncryptSetups[k] == DECRYPT_SESSION || decryptEncryptSetups[k] == ENCRYPT_SESSION )
4418 {
4419 if( decryptEncryptMode == CFB_MODE )
4420 {
4421 symmetric.algorithm = TPM_ALG_AES;
4422 symmetric.keyBits.aes = 128;
4423 symmetric.mode.aes = TPM_ALG_CFB;
4424 }
4425 else if( decryptEncryptMode == XOR_MODE )
4426 {
4427 symmetric.algorithm = TPM_ALG_XOR;
4428 symmetric.keyBits.exclusiveOr = TPM_ALG_SHA256;
4429 }
4430 }
4431
4432 rval = StartAuthSessionWithParams( &nvSession, hmacTestSetups[j].tpmKey,
4433 hmacTestSetups[j].salt, hmacTestSetups[j].bound, &nvAuth, &nonceOlder, &encryptedSalt,
4434 TPM_SE_HMAC, &symmetric, TPM_ALG_SHA1 );
4435 CheckPassed( rval );
4436
4437 // Get and print name of the session.
4438 rval = (*HandleToNameFunctionPtr)( nvSession->sessionHandle, &nvSession->name );
4439 CheckPassed( rval );
4440
4441 OpenOutFile( &outFp );
4442 TpmClientPrintf( 0, "Name of authSession: " );
4443 PrintSizedBuffer( (TPM2B *)&nvSession->name );
4444 CloseOutFile( &outFp );
4445
4446 // Init write data.
4447 nvWriteData.t.size = sizeof( dataToWrite );
4448
4449 for( i = 0; i < nvWriteData.t.size; i++ )
4450 {
4451 nvWriteData.t.buffer[i] = dataToWrite[i];
4452 }
4453
4454 if( decryptEncryptSetups[k] == DECRYPT_SESSION )
4455 {
4456 if( decryptEncryptMode == CFB_MODE )
4457 {
4458// rval = EncryptCFB( &nvSession, &( nvWriteData.b ) );
4459 }
4460 else if( decryptEncryptMode == XOR_MODE )
4461 {
4462// rval = EncryptXOR( &nvSession, &( nvWriteData.b ) );
4463 }
4464 sessionsData.cmdAuths[0]->sessionAttributes.decrypt = 1;
4465 }
4466 else
4467 {
4468 sessionsData.cmdAuths[0]->sessionAttributes.decrypt = 0;
4469 }
4470 sessionsData.cmdAuths[0]->sessionAttributes.encrypt = 0;
4471
4472 CheckPassed( rval );
4473
4474 sessionsData.cmdAuths[0]->sessionHandle = nvSession->sessionHandle;
4475 sessionsData.cmdAuths[0]->nonce.t.size = 1;
4476 sessionsData.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
4477
4478 // Roll nonces for command
4479 RollNonces( nvSession, &sessionsData.cmdAuths[0]->nonce );
4480
4481 // Now try writing with bad HMAC.
4482 rval = Tss2_Sys_NV_Write_Prepare( wrSysContext, TPM20_INDEX_PASSWORD_TEST,
4483 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
4484 CheckPassed( rval );
4485
4486 rval = ComputeCommandHmacs( wrSysContext,
4487 TPM20_INDEX_PASSWORD_TEST,
4488 TPM20_INDEX_PASSWORD_TEST, &sessionsData, TPM_RC_FAILURE );
4489 CheckPassed( rval );
4490
4491 // Diddle with HMAC to force failure
4492 sessionsData.cmdAuths[0]->hmac.t.buffer[0] =
4493 ~( sessionsData.cmdAuths[0]->hmac.t.buffer[0] );
4494
4495 sessionCmdRval = Tss2_Sys_NV_Write( wrSysContext, TPM20_INDEX_PASSWORD_TEST,
4496 TPM20_INDEX_PASSWORD_TEST,
4497 &sessionsData, &nvWriteData, 0, &sessionsDataOut );
4498 CheckFailed( sessionCmdRval, TPM_RC_S + TPM_RC_1 + TPM_RC_AUTH_FAIL );
4499
4500 // Since command failed, no need to roll nonces.
4501
4502 TestDictionaryAttackLockReset();
4503
4504 // Now try writing with good HMAC.
4505
4506 // Do stage 1 of NVRead, followed by stage 1 and 2 of NVWrite, followed by
4507 // stage 2 of NVRead. This tests that the staged processing is thread-safe.
4508 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 0;
4509 rval = Tss2_Sys_NV_Read_Prepare( rdSysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, sizeof( dataToWrite ), 0 );
4510 CheckPassed( rval );
4511
4512 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 1;
4513 rval = Tss2_Sys_NV_Write_Prepare( wrSysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST,
4514 &nvWriteData, 0 );
4515 CheckPassed( rval );
4516
4517 rval = ComputeCommandHmacs( wrSysContext, TPM20_INDEX_PASSWORD_TEST,
4518 TPM20_INDEX_PASSWORD_TEST, &sessionsData, sessionCmdRval );
4519 CheckPassed( rval );
4520
4521 sessionCmdRval = Tss2_Sys_NV_Write( wrSysContext,
4522 TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
4523 CheckPassed( sessionCmdRval );
4524 if( sessionCmdRval == TPM_RC_SUCCESS )
4525 {
4526 // Roll nonces for response
4527 RollNonces( nvSession, &sessionsDataOut.rspAuths[0]->nonce );
4528
4529 rval = CheckResponseHMACs( wrSysContext, sessionCmdRval,
4530 &sessionsData, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsDataOut );
4531 CheckPassed( rval );
4532 }
4533
4534 // Roll nonces for command
4535 RollNonces( nvSession, &sessionsData.cmdAuths[0]->nonce );
4536
4537 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 0;
4538 rval = ComputeCommandHmacs( rdSysContext, TPM20_INDEX_PASSWORD_TEST,
4539 TPM20_INDEX_PASSWORD_TEST, &sessionsData, sessionCmdRval );
4540 CheckPassed( rval );
4541
4542 if( decryptEncryptSetups[k] == ENCRYPT_SESSION )
4543 {
4544 sessionsData.cmdAuths[0]->sessionAttributes.encrypt = 1;
4545 }
4546 else
4547 {
4548 sessionsData.cmdAuths[0]->sessionAttributes.encrypt = 0;
4549 }
4550 sessionsData.cmdAuths[0]->sessionAttributes.decrypt = 0;
4551
4552 sessionCmdRval = Tss2_Sys_NV_Read( rdSysContext, TPM20_INDEX_PASSWORD_TEST,
4553 TPM20_INDEX_PASSWORD_TEST, &sessionsData, sizeof( dataToWrite ), 0, &nvData, &sessionsDataOut );
4554 CheckPassed( sessionCmdRval );
4555 if( sessionCmdRval == TPM_RC_SUCCESS )
4556 {
4557 // Roll nonces for response
4558 RollNonces( nvSession, &sessionsDataOut.rspAuths[0]->nonce );
4559
4560 rval = CheckResponseHMACs( rdSysContext, sessionCmdRval,
4561 &sessionsData, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsDataOut );
4562 CheckPassed( rval );
4563
4564 if( decryptEncryptSetups[k] == ENCRYPT_SESSION )
4565 {
4566 if( decryptEncryptMode == CFB_MODE )
4567 {
4568// rval = EncryptCFB( nvSession, &( nvData.b ) );
4569 }
4570 else if( decryptEncryptMode == XOR_MODE )
4571 {
4572// rval = EncryptXOR( nvSession, &( nvData.b ) );
4573 }
4574 CheckPassed( rval );
4575 }
4576
4577 // Check that write actually worked.
4578 rval = CompareTPM2B( &(nvWriteData.b), &(nvData.b) );
4579 CheckPassed( rval );
4580 }
4581
4582 //
4583 // NOTE: When running against version of the simulator for TPM spec versions later
4584 // than version 0.98, in order for the session to act as a bound session when accessing
4585 // the bind entity, we will need to restart the session here since the name of the bound
4586 // entity changed.
4587 //
4588 if( hmacTestSetups[j].bound != TPM_RH_NULL )
4589 {
4590 rval = EndAuthSession( nvSession );
4591 CheckPassed( rval );
4592
4593 //
4594 // Start session
4595 //
4596 nonceOlder.t.size = GetDigestSize( TPM_ALG_SHA1 );
4597 for( i = 0; i < nonceOlder.t.size; i++ )
4598 nonceOlder.t.buffer[i] = 0;
4599
4600 symmetric.algorithm = TPM_ALG_AES;
4601 symmetric.keyBits.aes = 128;
4602 symmetric.mode.aes = TPM_ALG_CFB;
4603
4604 rval = StartAuthSessionWithParams( &nvSession, hmacTestSetups[j].tpmKey, hmacTestSetups[j].salt, hmacTestSetups[j].bound, &nvAuth, &nonceOlder, &encryptedSalt, TPM_SE_HMAC, &symmetric, TPM_ALG_SHA1 );
4605 CheckPassed( rval );
4606
4607 CopySizedByteBuffer( &( nvSession->authValueBind.b ), &( nvAuth.b ) );
4608
4609 // Now try writing with good HMAC.
4610 sessionsData.cmdAuths[0]->sessionHandle = nvSession->sessionHandle;
4611 sessionsData.cmdAuths[0]->nonce.t.size = 1;
4612 sessionsData.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
4613 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 1;
4614 sessionsData.cmdAuths[0]->sessionAttributes.decrypt = 1;
4615
4616 // TBD: Need to encrypt data before sending.
4617
4618 rval = Tss2_Sys_NV_Write_Prepare( wrSysContext, TPM20_INDEX_PASSWORD_TEST,
4619 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
4620 CheckPassed( rval );
4621
4622 // Roll nonces for command
4623 RollNonces( nvSession, &sessionsData.cmdAuths[0]->nonce );
4624
4625 rval = ComputeCommandHmacs( wrSysContext, TPM20_INDEX_PASSWORD_TEST,
4626 TPM20_INDEX_PASSWORD_TEST, &sessionsData, TPM_RC_FAILURE );
4627 CheckPassed( rval );
4628
4629 sessionCmdRval = Tss2_Sys_NV_Write( wrSysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
4630 sessionsData.cmdAuths[0]->sessionAttributes.decrypt = 0;
4631 CheckPassed( sessionCmdRval );
4632 if( sessionCmdRval == TPM_RC_SUCCESS )
4633 {
4634 // Roll nonces for response
4635 RollNonces( nvSession, &sessionsDataOut.rspAuths[0]->nonce );
4636
4637 rval = CheckResponseHMACs( wrSysContext, sessionCmdRval,
4638 &sessionsData, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsDataOut );
4639 CheckPassed( rval );
4640 }
4641
4642 // Need to do GetSessionAuditDigest to check that audit digest changed.
4643
4644 sessionsData.cmdAuths[0]->sessionAttributes.continueSession = 0;
4645 sessionsData.cmdAuths[0]->sessionAttributes.encrypt = 1;
4646 sessionsData.cmdAuths[0]->sessionAttributes.audit = 1;
4647
4648 rval = Tss2_Sys_NV_Read_Prepare( rdSysContext, TPM20_INDEX_PASSWORD_TEST,
4649 TPM20_INDEX_PASSWORD_TEST, sizeof( dataToWrite ), 0 );
4650 CheckPassed( rval );
4651
4652 // Roll nonces for command
4653 RollNonces( nvSession, &sessionsData.cmdAuths[0]->nonce );
4654
4655 rval = ComputeCommandHmacs( rdSysContext, TPM20_INDEX_PASSWORD_TEST,
4656 TPM20_INDEX_PASSWORD_TEST, &sessionsData, sessionCmdRval );
4657 CheckPassed( rval );
4658
4659 sessionCmdRval = Tss2_Sys_NV_Read( rdSysContext, TPM20_INDEX_PASSWORD_TEST,
4660 TPM20_INDEX_PASSWORD_TEST, &sessionsData, sizeof( dataToWrite ), 0, &nvData, &sessionsDataOut );
4661 sessionsData.cmdAuths[0]->sessionAttributes.encrypt = 0;
4662 sessionsData.cmdAuths[0]->sessionAttributes.audit = 0;
4663 if( sessionCmdRval == TPM_RC_SUCCESS )
4664 {
4665 // Roll nonces for response
4666 RollNonces( nvSession, &sessionsDataOut.rspAuths[0]->nonce );
4667
4668 rval = CheckResponseHMACs( rdSysContext, sessionCmdRval,
4669 &sessionsData, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsDataOut );
4670 CheckPassed( rval );
4671 }
4672
4673 // TBD: Need to decrypt response data.
4674
4675 CheckPassed( rval );
4676 // TBD: Need to do GetSessionAuditDigest to check that audit digest changed.
4677 }
4678
4679 // Removed comparison for now until we figure out how to properly
4680 // encrypt data before writing to NV index.
4681 // rval = CompareTPM2B( &(nvWriteData.b), &(nvData.b) );
4682 // CheckPassed( rval );
4683
4684 sessionsData.cmdAuths[0]->sessionHandle = TPM_RS_PW;
4685 sessionsData.cmdAuths[0]->nonce.t.size = 0;
4686 sessionsData.cmdAuths[0]->nonce.t.buffer[0] = 0xa5;
4687 sessionData.hmac.t.size = 0;
4688 // Now undefine the index.
4689 rval = Tss2_Sys_NV_UndefineSpace( wrSysContext, TPM_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
4690 CheckPassed( rval );
4691 rval = DeleteEntity( TPM20_INDEX_PASSWORD_TEST );
4692 CheckPassed( rval );
4693 rval = EndAuthSession( nvSession );
4694 CheckPassed( rval );
4695 }
4696 }
4697 }
4698
4699 TeardownSysContext( &wrSysContext );
4700 TeardownSysContext( &rdSysContext );
4701
4702}
4703
4704UINT32 writeDataString = 0xdeadbeef;
4705
4706void TestEncryptDecryptSession()
4707{
4708 TSS2_RC rval = TSS2_RC_SUCCESS;
4709 SESSION *encryptDecryptSession;
4710 TPMT_SYM_DEF symmetric;
4711 TPM2B_MAX_NV_BUFFER writeData, encryptedWriteData;
4712 TPM2B_MAX_NV_BUFFER encryptedReadData, decryptedReadData,
4713 readData;
4714 size_t decryptParamSize;
4715 uint8_t *decryptParamBuffer;
4716 size_t encryptParamSize;
4717 uint8_t *encryptParamBuffer;
4718 TPM2B_AUTH nvAuth;
4719 TPM2B_DIGEST authPolicy;
4720 TPMA_NV nvAttributes;
4721 int i;
4722 TPMA_SESSION sessionAttributes;
4723 TPM2B_NONCE nonceCaller;
4724
4725 nonceCaller.t.size = 0;
4726
4727 // Authorization structure for undefine command.
4728 TPMS_AUTH_COMMAND nvUndefineAuth;
4729
4730 // Create and init authorization area for undefine command:
4731 // only 1 authorization area.
4732 TPMS_AUTH_COMMAND *nvUndefineAuthArray[1] = { &nvUndefineAuth };
4733
4734 // Authorization array for command (only has one auth structure).
4735 TSS2_SYS_CMD_AUTHS nvUndefineAuths = { 1, &nvUndefineAuthArray[0] };
4736
4737 TpmClientPrintf( 0, "\n\nDECRYPT/ENCRYPT SESSION TESTS:\n" );
4738
4739 writeData.t.size = sizeof( writeDataString );
4740 memcpy( (void *)&writeData.t.buffer, (void *)&writeDataString,
4741 sizeof( writeDataString ) );
4742
4743
4744 // Create NV index with empty auth value.
4745 *(UINT32 *)( (void *)&nvAttributes ) = 0;
4746 nvAttributes.TPMA_NV_AUTHREAD = 1;
4747 nvAttributes.TPMA_NV_AUTHWRITE = 1;
4748 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
4749
4750 // No authorization required.
4751 authPolicy.t.size = 0;
4752 nvAuth.t.size = 0;
4753 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW,
4754 &nvAuth, &authPolicy, TPM20_INDEX_TEST1,
4755 TPM_ALG_SHA1, nvAttributes,
4756 sizeof( writeDataString ) );
4757
4758 //
4759 // 1st pass with CFB mode.
4760 // 2nd pass with XOR mode.
4761 //
4762 for( i = 0; i < 2; i++ )
4763 {
4764 // Authorization structure for NV
4765 // read/write commands.
4766 TPMS_AUTH_COMMAND nvRdWrCmdAuth;
4767
4768 // Authorization structure for
4769 // encrypt/decrypt session.
4770 TPMS_AUTH_COMMAND decryptEncryptSessionCmdAuth;
4771
4772 // Create and init authorization area for
4773 // NV read/write commands:
4774 // 2 authorization areas.
4775 TPMS_AUTH_COMMAND *nvRdWrCmdAuthArray[2] =
4776 { &nvRdWrCmdAuth, &decryptEncryptSessionCmdAuth };
4777
4778 // Authorization array for commands
4779 // (has two auth structures).
4780 TSS2_SYS_CMD_AUTHS nvRdWrCmdAuths =
4781 { 2, &nvRdWrCmdAuthArray[0] };
4782
4783 // Authorization structure for NV read/write responses.
4784 TPMS_AUTH_RESPONSE nvRdWrRspAuth;
4785 // Authorization structure for decrypt/encrypt
4786 // session responses.
4787 TPMS_AUTH_RESPONSE decryptEncryptSessionRspAuth;
4788
4789 // Create and init authorization area for NV
4790 // read/write responses: 2 authorization areas.
4791 TPMS_AUTH_RESPONSE *nvRdWrRspAuthArray[2] =
4792 { &nvRdWrRspAuth, &decryptEncryptSessionRspAuth };
4793 // Authorization array for responses
4794 // (has two auth structures).
4795 TSS2_SYS_RSP_AUTHS nvRdWrRspAuths =
4796 { 2, &nvRdWrRspAuthArray[0] };
4797
4798 // Setup session parameters.
4799 if( i == 0 )
4800 {
4801 // AES encryption/decryption and CFB mode.
4802 symmetric.algorithm = TPM_ALG_AES;
4803 symmetric.keyBits.aes = 128;
4804 symmetric.mode.aes = TPM_ALG_CFB;
4805 }
4806 else
4807 {
4808 // XOR encryption/decryption.
4809 symmetric.algorithm = TPM_ALG_XOR;
4810 symmetric.keyBits.exclusiveOr = TPM_ALG_SHA256;
4811 }
4812
4813 // Start policy session for decrypt/encrypt session.
4814 rval = StartAuthSessionWithParams( &encryptDecryptSession,
4815 TPM_RH_NULL, 0, TPM_RH_NULL, 0, &nonceCaller, 0, TPM_SE_POLICY,
4816 &symmetric, TPM_ALG_SHA256 );
4817 CheckPassed( rval );
4818
4819 //
4820 // Write TPM index with encrypted parameter used
4821 // as the data to write. Set session for encrypt.
4822 // Use asyncronous APIs to do this.
4823 //
4824 // 1st time: use null buffer, 2nd time use populated one;
4825 // this tests different cases for SetDecryptParam function.
4826 //
4827
4828 // Prepare the input parameters, using unencypted
4829 // write data. This will be encrypted before the
4830 // command is sent to the TPM.
4831 rval = Tss2_Sys_NV_Write_Prepare( sysContext,
4832 TPM20_INDEX_TEST1, TPM20_INDEX_TEST1,
4833 ( i == 0 ? (TPM2B_MAX_NV_BUFFER *)0 : &writeData ),
4834 0 );
4835 CheckPassed( rval );
4836
4837 // Set up password authorization session structure.
4838 nvRdWrCmdAuth.sessionHandle = TPM_RS_PW;
4839 nvRdWrCmdAuth.nonce.t.size = 0;
4840 *( (UINT8 *)((void *)&nvRdWrCmdAuth.sessionAttributes ) ) = 0;
4841 nvRdWrCmdAuth.hmac.t.size = nvAuth.t.size;
4842 memcpy( (void *)&nvRdWrCmdAuth.hmac.t.buffer[0],
4843 (void *)&nvAuth.t.buffer[0],
4844 nvRdWrCmdAuth.hmac.t.size );
4845
4846 // Set up encrypt/decrypt session structure.
4847 decryptEncryptSessionCmdAuth.sessionHandle =
4848 encryptDecryptSession->sessionHandle;
4849 decryptEncryptSessionCmdAuth.nonce.t.size = 0;
4850 *( (UINT8 *)((void *)&sessionAttributes ) ) = 0;
4851 decryptEncryptSessionCmdAuth.sessionAttributes =
4852 sessionAttributes;
4853 decryptEncryptSessionCmdAuth.sessionAttributes.continueSession
4854 = 1;
4855 decryptEncryptSessionCmdAuth.sessionAttributes.decrypt = 1;
4856 decryptEncryptSessionCmdAuth.hmac.t.size = 0;
4857
4858 rval = Tss2_Sys_SetCmdAuths( sysContext, &nvRdWrCmdAuths );
4859 CheckPassed( rval );
4860
4861 // Get decrypt parameter.
4862 rval = Tss2_Sys_GetDecryptParam( sysContext,
4863 &decryptParamSize,
4864 (const uint8_t **)&decryptParamBuffer );
4865 CheckPassed( rval );
4866
4867 if( i == 0 )
4868 {
4869 // 1st pass: test case of Prepare inputting a NULL decrypt
4870 // param; decryptParamSize should be 0.
4871 if( decryptParamSize != 0 )
4872 {
4873 TpmClientPrintf( 0, "ERROR!! decryptParamSize != 0\n" );
4874 Cleanup();
4875 }
4876 }
4877
4878 // Roll nonces for command.
4879 RollNonces( encryptDecryptSession,
4880 &decryptEncryptSessionCmdAuth.nonce );
4881
4882 // Encrypt write data.
4883 rval = EncryptCommandParam( encryptDecryptSession,
4884 (TPM2B_MAX_BUFFER *)&encryptedWriteData,
4885 (TPM2B_MAX_BUFFER *)&writeData, &nvAuth );
4886 CheckPassed( rval );
4887
4888 // Now set decrypt parameter.
4889 rval = Tss2_Sys_SetDecryptParam( sysContext,
4890 (uint8_t )encryptedWriteData.t.size,
4891 (uint8_t *)&encryptedWriteData.t.buffer[0] );
4892 CheckPassed( rval );
4893
4894 // Now write the data to the NV index.
4895 rval = Tss2_Sys_ExecuteAsync( sysContext );
4896 CheckPassed( rval );
4897
4898 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
4899 CheckPassed( rval );
4900
4901 rval = Tss2_Sys_GetRspAuths( sysContext, &nvRdWrRspAuths );
4902 CheckPassed( rval );
4903
4904 // Roll the nonces for response
4905 RollNonces( encryptDecryptSession,
4906 &nvRdWrRspAuths.rspAuths[1]->nonce );
4907
4908
4909 // Don't need nonces for anything else, so roll
4910 // the nonces for next command.
4911 RollNonces( encryptDecryptSession,
4912 &decryptEncryptSessionCmdAuth.nonce );
4913
4914 // Now read the data without encrypt set.
4915 nvRdWrCmdAuths.cmdAuthsCount = 1;
4916 nvRdWrRspAuths.rspAuthsCount = 1;
4917 rval = Tss2_Sys_NV_Read( sysContext, TPM20_INDEX_TEST1,
4918 TPM20_INDEX_TEST1, &nvRdWrCmdAuths,
4919 sizeof( writeDataString ), 0, &readData,
4920 &nvRdWrRspAuths );
4921 CheckPassed( rval );
4922 nvRdWrCmdAuths.cmdAuthsCount = 2;
4923 nvRdWrRspAuths.rspAuthsCount = 2;
4924
4925 // Roll the nonces for response
4926 RollNonces( encryptDecryptSession,
4927 &nvRdWrRspAuths.rspAuths[1]->nonce );
4928
4929 // Check that write and read data are equal. This
4930 // verifies that the decrypt session was setup correctly.
4931 // If it wasn't, the data stored in the TPM would still
4932 // be encrypted, and this test would fail.
4933 if( memcmp( (void *)&readData.t.buffer[0],
4934 (void *)&writeData.t.buffer[0], readData.t.size ) )
4935 {
4936 TpmClientPrintf( 0, "ERROR!! read data not equal to written data\n" );
4937 Cleanup();
4938 }
4939
4940 //
4941 // Read TPM index with encrypt session; use
4942 // syncronous APIs to do this.
4943 //
4944
4945 rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_TEST1,
4946 TPM20_INDEX_TEST1, sizeof( writeDataString ), 0 );
4947 CheckPassed( rval );
4948
4949 // Roll the nonces for next command.
4950 RollNonces( encryptDecryptSession,
4951 &decryptEncryptSessionCmdAuth.nonce );
4952
4953 decryptEncryptSessionCmdAuth.sessionAttributes.decrypt = 0;
4954 decryptEncryptSessionCmdAuth.sessionAttributes.encrypt = 1;
4955 decryptEncryptSessionCmdAuth.sessionAttributes.continueSession =
4956 1;
4957
4958 rval = Tss2_Sys_SetCmdAuths( sysContext, &nvRdWrCmdAuths );
4959 CheckPassed( rval );
4960
4961 //
4962 // Now Read the data.
4963 //
4964 rval = Tss2_Sys_Execute( sysContext );
4965 CheckPassed( rval );
4966
4967 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize,
4968 (const uint8_t **)&encryptParamBuffer );
4969 CheckPassed( rval );
4970
4971 rval = Tss2_Sys_GetRspAuths( sysContext, &nvRdWrRspAuths );
4972 CheckPassed( rval );
4973
4974 // Roll the nonces for response
4975 RollNonces( encryptDecryptSession,
4976 &nvRdWrRspAuths.rspAuths[1]->nonce );
4977
4978 // Decrypt read data.
4979 encryptedReadData.t.size = encryptParamSize;
4980 memcpy( (void *)&encryptedReadData.t.buffer[0],
4981 (void *)encryptParamBuffer, encryptParamSize );
4982 rval = DecryptResponseParam( encryptDecryptSession,
4983 (TPM2B_MAX_BUFFER *)&decryptedReadData,
4984 (TPM2B_MAX_BUFFER *)&encryptedReadData, &nvAuth );
4985 CheckPassed( rval );
4986
4987 // Roll the nonces.
4988 RollNonces( encryptDecryptSession,
4989 &nvRdWrRspAuths.rspAuths[1]->nonce );
4990
4991 rval = Tss2_Sys_SetEncryptParam( sysContext,
4992 (uint8_t)decryptedReadData.t.size,
4993 (uint8_t *)&decryptedReadData.t.buffer[0] );
4994 CheckPassed( rval );
4995
4996 // Get the command results, in this case the read data.
4997 rval = Tss2_Sys_NV_Read_Complete( sysContext, &readData );
4998 CheckPassed( rval );
4999
5000 TpmClientPrintf( 0, "Decrypted read data = " );
5001 DEBUG_PRINT_BUFFER( &readData.t.buffer[0], (UINT32 )readData.t.size );
5002
5003 // Check that write and read data are equal.
5004 if( memcmp( (void *)&readData.t.buffer[0],
5005 (void *)&writeData.t.buffer[0], readData.t.size ) )
5006 {
5007 TpmClientPrintf( 0, "ERROR!! read data not equal to written data\n" );
5008 Cleanup();
5009 }
5010
5011 rval = Tss2_Sys_FlushContext( sysContext,
5012 encryptDecryptSession->sessionHandle );
5013 CheckPassed( rval );
5014
5015 rval = EndAuthSession( encryptDecryptSession );
5016 CheckPassed( rval );
5017 }
5018
5019 // Set authorization for NV undefine command.
5020 nvUndefineAuth.sessionHandle = TPM_RS_PW;
5021 nvUndefineAuth.nonce.t.size = 0;
5022 *( (UINT8 *)((void *)&nvUndefineAuth.sessionAttributes ) ) = 0;
5023 nvUndefineAuth.hmac.t.size = 0;
5024
5025 // Undefine NV index.
5026 rval = Tss2_Sys_NV_UndefineSpace( sysContext,
5027 TPM_RH_PLATFORM, TPM20_INDEX_TEST1, &nvUndefineAuths, 0 );
5028 CheckPassed( rval );
5029}
5030
5031
5032void TestRsaEncryptDecrypt()
5033{
5034 TPM2B_SENSITIVE_CREATE inSensitive;
5035 TPM2B_PUBLIC inPublic;
5036 TPMS_AUTH_COMMAND sessionData;
5037 TPMS_AUTH_RESPONSE sessionDataOut;
5038 TPM2B_DATA outsideInfo;
5039 TPML_PCR_SELECTION creationPCR;
5040 TPM_RC rval;
5041 TPM2B_PRIVATE outPrivate;
5042 TPM2B_PUBLIC outPublic;
5043 TPM2B_CREATION_DATA creationData;
5044 TPM2B_DIGEST creationHash;
5045 TPMT_TK_CREATION creationTicket;
5046 TPM2B_NAME rsaKeyName;
5047
5048 TPMS_AUTH_COMMAND *sessionDataArray[1] = { &sessionData };
5049 TPMS_AUTH_RESPONSE *sessionDataOutArray[1] = { &sessionDataOut };
5050
5051 TSS2_SYS_CMD_AUTHS sessionsData = { 1, &sessionDataArray[0] };
5052 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, &sessionDataOutArray[0] };
5053
5054 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
5055 inSensitive.t.sensitive.data.t.size = 0;
5056
5057 // Init nonce.
5058 sessionData.nonce.t.size = 0;
5059
5060 // init hmac
5061 sessionData.hmac.t.size = 0;
5062
5063 // Init session attributes
5064 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
5065
5066 // Create public/private key pair.
5067 inPublic.t.publicArea.type = TPM_ALG_RSA;
5068 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
5069 *(UINT32 *)( (void *)&( inPublic.t.publicArea.objectAttributes ) ) = 0;
5070 inPublic.t.publicArea.objectAttributes.decrypt = 1;
5071 inPublic.t.publicArea.authPolicy.t.size = 0;
5072
5073 inPublic.t.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
5074 inPublic.t.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
5075 inPublic.t.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_CTR;
5076 inPublic.t.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
5077 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 1024;
5078 inPublic.t.publicArea.parameters.rsaDetail.exponent = 0;
5079 inPublic.t.publicArea.unique.rsa.t.size = 0;
5080
5081 outsideInfo.t.size = 0;
5082 outPublic.t.size = 0;
5083 creationData.t.size = 0;
5084 rval = Tss2_Sys_Create( sysContext, handle2048rsa, &sessionsData, &inSensitive, &inPublic,
5085 &outsideInfo, &creationPCR,
5086 &outPrivate, &outPublic, &creationData,
5087 &creationHash, &creationTicket, &sessionsDataOut );
5088
5089 CheckPassed( rval );
5090
5091 // Load private key into TPM
5092 rval = Tss2_Sys_Load ( sysContext, handle2048rsa, 0, &outPrivate, &outPublic,
5093 &loadedSha1KeyHandle, &rsaKeyName, &sessionsDataOut);
5094 CheckPassed( rval );
5095
5096
5097
5098 // Encrypt message with public key
5099
5100 // Print encrypted message.
5101
5102 // Decrypt message with private key.
5103
5104 // Print decrypted message.
5105
5106}
5107
5108
5109void GetSetDecryptParamTests()
5110{
5111 TPM2B_MAX_NV_BUFFER nvWriteData = { { 4, { 0xde, 0xad, 0xbe, 0xef, } } };
5112 TPM2B_MAX_NV_BUFFER nvWriteData1 = { { 4, { 0x01, 0x01, 0x02, 0x03, } } };
5113 const uint8_t *decryptParamBuffer;
5114 size_t decryptParamSize;
5115 size_t cpBufferUsedSize1, cpBufferUsedSize2;
5116 const uint8_t *cpBuffer1, *cpBuffer2;
5117 TSS2_RC rval;
5118 int i;
5119 TSS2_SYS_CONTEXT *decryptParamTestSysContext;
5120
5121 TpmClientPrintf( 0, "\nGET/SET DECRYPT PARAM TESTS:\n" );
5122
5123 // Create two sysContext structures.
5124 decryptParamTestSysContext = InitSysContext( MAX_NV_BUFFER_SIZE, resMgrTctiContext, &abiVersion );
5125 if( decryptParamTestSysContext == 0 )
5126 {
5127 InitSysContextFailure();
5128 }
5129
5130 // Test for bad sequence
5131 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
5132 CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
5133
5134 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.t.buffer[0] ) );
5135 CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
5136
5137 // Do Prepare.
5138 rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
5139 TPM20_INDEX_PASSWORD_TEST, &nvWriteData1, 0x55aa );
5140 CheckPassed( rval );
5141
5142 // Test for bad reference
5143 rval = Tss2_Sys_GetDecryptParam( 0, &decryptParamSize, &decryptParamBuffer );
5144 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5145
5146 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, 0, &decryptParamBuffer );
5147 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5148
5149 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, 0 );
5150 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5151
5152 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, 0 );
5153 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5154
5155 rval = Tss2_Sys_SetDecryptParam( 0, 4, 0 );
5156 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5157
5158 // Test for bad size.
5159 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 5, &( nvWriteData.t.buffer[0] ) );
5160 CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
5161
5162 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 3, &( nvWriteData.t.buffer[0] ) );
5163 CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
5164
5165 // Test for good size.
5166 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.t.buffer[0] ) );
5167 CheckPassed( rval );
5168
5169 // Make sure that the set operation really did the right thing.
5170 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
5171 CheckPassed( rval );
5172 for( i = 0; i < 4; i++ )
5173 {
5174 if( decryptParamBuffer[i] != nvWriteData.t.buffer[i] )
5175 {
5176 TpmClientPrintf( 0, "ERROR!! decryptParamBuffer[%d] s/b: %2.2x, was: %2.2x\n", i, nvWriteData.t.buffer[i], decryptParamBuffer[i] );
5177 Cleanup();
5178 }
5179 }
5180
5181 rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize1, &cpBuffer1 );
5182 CheckPassed( rval );
5183
5184 OpenOutFile( &outFp );
5185#ifdef DEBUG
5186 TpmClientPrintf( 0, "cpBuffer = ");
5187#endif
5188 DEBUG_PRINT_BUFFER( (UINT8 *)cpBuffer1, cpBufferUsedSize1 );
5189 CloseOutFile( &outFp );
5190
5191 // Test for no decrypt param.
5192 rval = Tss2_Sys_NV_Read_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, sizeof( nvWriteData ) - 2, 0 );
5193 CheckPassed( rval );
5194
5195 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
5196 CheckFailed( rval, TSS2_SYS_RC_NO_DECRYPT_PARAM );
5197
5198 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.t.buffer[0] ) );
5199 CheckFailed( rval, TSS2_SYS_RC_NO_DECRYPT_PARAM );
5200
5201 // Null decrypt param.
5202 rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
5203 TPM20_INDEX_PASSWORD_TEST, 0, 0x55aa );
5204 CheckPassed( rval );
5205
5206 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
5207 CheckPassed( rval );
5208
5209 // Check that size == 0.
5210 if( decryptParamSize != 0 )
5211 {
5212 TpmClientPrintf( 0, "ERROR!! decryptParamSize s/b: 0, was: %u\n", (unsigned int)decryptParamSize );
5213 Cleanup();
5214 }
5215
5216 // Test for insufficient size.
5217 rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize2, &cpBuffer2 );
5218 CheckPassed( rval );
5219 nvWriteData.t.size = MAX_NV_BUFFER_SIZE -
5220 CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)( ( (_TSS2_SYS_CONTEXT_BLOB *)decryptParamTestSysContext )->tpmInBuffPtr ) )->commandSize ) +
5221 1;
5222 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, nvWriteData.t.size, &( nvWriteData.t.buffer[0] ) );
5223 CheckFailed( rval, TSS2_SYS_RC_INSUFFICIENT_BUFFER );
5224
5225 // Test that one less will work. This tests that we're checking the correct corner case.
5226 nvWriteData.t.size -= 1;
5227 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, nvWriteData.t.size, &( nvWriteData.t.buffer[0] ) );
5228 CheckPassed( rval );
5229
5230
5231 rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
5232 TPM20_INDEX_PASSWORD_TEST, 0, 0x55aa );
5233 CheckPassed( rval );
5234
5235 rval = Tss2_Sys_GetDecryptParam( decryptParamTestSysContext, &decryptParamSize, &decryptParamBuffer );
5236 CheckPassed( rval );
5237
5238 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 4, &( nvWriteData.t.buffer[0] ) );
5239 CheckPassed( rval );
5240
5241 rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize2, &cpBuffer2 );
5242 CheckPassed( rval );
5243
5244 OpenOutFile( &outFp );
5245#ifdef DEBUG
5246 TpmClientPrintf( 0, "cpBuffer = ");
5247#endif
5248 DEBUG_PRINT_BUFFER( (UINT8 *)cpBuffer2, cpBufferUsedSize2 );
5249 CloseOutFile( &outFp );
5250
5251 if( cpBufferUsedSize1 != cpBufferUsedSize2 )
5252 {
5253 TpmClientPrintf( 0, "ERROR!! cpBufferUsedSize1(%x) != cpBufferUsedSize2(%x)\n", (UINT32)cpBufferUsedSize1, (UINT32)cpBufferUsedSize2 );
5254 Cleanup();
5255 }
5256 for( i = 0; i < (int)cpBufferUsedSize1; i++ )
5257 {
5258 if( cpBuffer1[i] != cpBuffer2[i] )
5259 {
5260 TpmClientPrintf( 0, "ERROR!! cpBufferUsedSize1[%d] s/b: %2.2x, was: %2.2x\n", i, cpBuffer1[i], cpBuffer2[i] );
5261 Cleanup();
5262 }
5263 }
5264
5265 // Test case of zero sized decrypt param, another case of bad size.
5266 nvWriteData1.t.size = 0;
5267 rval = Tss2_Sys_NV_Write_Prepare( decryptParamTestSysContext, TPM20_INDEX_PASSWORD_TEST,
5268 TPM20_INDEX_PASSWORD_TEST, &nvWriteData1, 0x55aa );
5269 CheckPassed( rval );
5270
5271 rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, 1, &( nvWriteData.t.buffer[0] ) );
5272 CheckFailed( rval, TSS2_SYS_RC_BAD_SIZE );
5273
5274 TeardownSysContext( &decryptParamTestSysContext );
5275}
5276
5277void GetSetEncryptParamTests()
5278{
5279 TPM2B_MAX_NV_BUFFER nvWriteData = { { 4, { 0xde, 0xad, 0xbe, 0xef, } } };
5280 const uint8_t *encryptParamBuffer;
5281 const uint8_t encryptParamBuffer1[4] = { 01, 02, 03, 04 };
5282 size_t encryptParamSize;
5283 TSS2_RC rval;
5284 int i;
5285 TPM2B_DIGEST authPolicy;
5286 TPMA_NV nvAttributes;
5287 TPM2B_AUTH nvAuth;
5288
5289 TPMS_AUTH_COMMAND sessionData = { TPM_RS_PW, };
5290 TPMS_AUTH_RESPONSE sessionDataOut;
5291 TPMS_AUTH_COMMAND *sessionDataArray[1] = { &sessionData };
5292 TPMS_AUTH_RESPONSE *sessionDataOutArray[1] = { &sessionDataOut };
5293 TSS2_SYS_CMD_AUTHS sessionsData = { 1, &sessionDataArray[0] };
5294 TSS2_SYS_RSP_AUTHS sessionsDataOut = { 1, &sessionDataOutArray[0] };
5295
5296 TPM2B_MAX_NV_BUFFER nvReadData;
5297
5298 TpmClientPrintf( 0, "\nGET/SET ENCRYPT PARAM TESTS:\n" );
5299
5300 // Do Prepare.
5301 rval = Tss2_Sys_NV_Write_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
5302 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
5303 CheckPassed( rval );
5304
5305 // Test for bad sequence
5306 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
5307 CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
5308
5309 rval = Tss2_Sys_SetEncryptParam( sysContext, 4, &( nvWriteData.t.buffer[0] ) );
5310 CheckFailed( rval, TSS2_SYS_RC_BAD_SEQUENCE );
5311
5312 // Create NV index
5313
5314 // Set empty policy and auth value.
5315 authPolicy.t.size = 0;
5316 nvAuth.t.size = 0;
5317
5318 // Now set the attributes.
5319 *(UINT32 *)( (void *)&nvAttributes ) = 0;
5320 nvAttributes.TPMA_NV_AUTHREAD = 1;
5321 nvAttributes.TPMA_NV_AUTHWRITE = 1;
5322 nvAttributes.TPMA_NV_PLATFORMCREATE = 1;
5323
5324 rval = DefineNvIndex( TPM_RH_PLATFORM, TPM_RS_PW, &nvAuth, &authPolicy,
5325 TPM20_INDEX_PASSWORD_TEST, TPM_ALG_SHA1, nvAttributes, 32 );
5326 CheckPassed( rval );
5327
5328 // Write the index.
5329 rval = Tss2_Sys_NV_Write_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST,
5330 TPM20_INDEX_PASSWORD_TEST, &nvWriteData, 0 );
5331 CheckPassed( rval );
5332
5333 rval = Tss2_Sys_NV_Write( sysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, &sessionsData, &nvWriteData, 0, &sessionsDataOut );
5334 CheckPassed( rval );
5335
5336 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
5337 CheckFailed( rval, TSS2_SYS_RC_NO_ENCRYPT_PARAM );
5338
5339 // Now read it and do tests on get/set encrypt functions
5340 rval = Tss2_Sys_NV_Read_Prepare( sysContext, TPM20_INDEX_PASSWORD_TEST, TPM20_INDEX_PASSWORD_TEST, 4, 0 );
5341 CheckPassed( rval );
5342
5343 rval = Tss2_Sys_NV_Read( sysContext, TPM20_INDEX_PASSWORD_TEST,
5344 TPM20_INDEX_PASSWORD_TEST, &sessionsData, 4, 0, &nvReadData, &sessionsDataOut );
5345 CheckPassed( rval );
5346
5347 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
5348 CheckPassed( rval );
5349
5350 rval = Tss2_Sys_SetEncryptParam( sysContext, encryptParamSize, encryptParamBuffer1 );
5351 CheckPassed( rval );
5352
5353 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, &encryptParamBuffer );
5354 CheckPassed( rval );
5355
5356 // Test that encryptParamBuffer is the same as encryptParamBuffer1
5357 for( i = 0; i < 4; i++ )
5358 {
5359 if( encryptParamBuffer[i] != encryptParamBuffer1[i] )
5360 {
5361 TpmClientPrintf( 0, "ERROR!! encryptParamBuffer[%d] s/b: %2.2x, was: %2.2x\n", i, encryptParamBuffer[i], encryptParamBuffer1[i] );
5362 Cleanup();
5363 }
5364 }
5365
5366 rval = Tss2_Sys_NV_UndefineSpace( sysContext, TPM_RH_PLATFORM, TPM20_INDEX_PASSWORD_TEST, &sessionsData, 0 );
5367 CheckPassed( rval );
5368
5369
5370 // Test for bad reference
5371 rval = Tss2_Sys_GetEncryptParam( 0, &encryptParamSize, &encryptParamBuffer );
5372 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5373
5374 rval = Tss2_Sys_GetEncryptParam( sysContext, 0, &encryptParamBuffer );
5375 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5376
5377 rval = Tss2_Sys_GetEncryptParam( sysContext, &encryptParamSize, 0 );
5378 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5379
5380 rval = Tss2_Sys_SetEncryptParam( sysContext, 4, 0 );
5381 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5382
5383 rval = Tss2_Sys_SetEncryptParam( 0, 4, encryptParamBuffer );
5384 CheckFailed( rval, TSS2_SYS_RC_BAD_REFERENCE );
5385}
5386
5387void TestRM()
5388{
5389 TSS2_TCTI_CONTEXT *otherResMgrTctiContext = 0;
5390 TSS2_SYS_CONTEXT *otherSysContext;
5391 TPM2B_SENSITIVE_CREATE inSensitive;
5392 TPM2B_PUBLIC inPublic;
5393 TPM2B_DATA outsideInfo;
5394 TPML_PCR_SELECTION creationPCR;
5395 TPMS_AUTH_COMMAND sessionData;
5396 TPMS_AUTH_RESPONSE sessionDataOut;
5397 TSS2_SYS_CMD_AUTHS sessionsData;
5398
5399 TSS2_SYS_RSP_AUTHS sessionsDataOut;
5400 TPM2B_NAME name;
5401 TPM2B_PRIVATE outPrivate;
5402 TPM2B_PUBLIC outPublic;
5403 TPM2B_CREATION_DATA creationData;
5404 TPM2B_DIGEST creationHash;
5405 TPMT_TK_CREATION creationTicket;
5406
5407 TPMS_AUTH_COMMAND *sessionDataArray[1];
5408 TPMS_AUTH_RESPONSE *sessionDataOutArray[1];
5409 TSS2_RC rval = TSS2_RC_SUCCESS;
5410
5411 TPMS_CONTEXT context;
5412 TSS2_TCTI_CONTEXT *tctiContext;
5413
5414 TPMI_DH_CONTEXT loadedHandle, newHandle, newNewHandle, newHandleDummy;
5415 TPMS_CONTEXT newContext;
5416 char otherResMgrInterfaceName[] = "Test RM Resource Manager";
5417
5418 TpmClientPrintf( 0, "\nRM TESTS:\n" );
5419
5420 sessionDataArray[0] = &sessionData;
5421 sessionDataOutArray[0] = &sessionDataOut;
5422
5423 sessionsDataOut.rspAuths = &sessionDataOutArray[0];
5424 sessionsData.cmdAuths = &sessionDataArray[0];
5425
5426 sessionsDataOut.rspAuthsCount = 1;
5427 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
5428 inSensitive.t.sensitive.userAuth = loadedSha1KeyAuth;
5429 inSensitive.t.sensitive.data.t.size = 0;
5430 inSensitive.t.size = loadedSha1KeyAuth.b.size + 2;
5431
5432 inPublic.t.publicArea.type = TPM_ALG_RSA;
5433 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
5434
5435 // First clear attributes bit field.
5436 *(UINT32 *)&( inPublic.t.publicArea.objectAttributes) = 0;
5437 inPublic.t.publicArea.objectAttributes.restricted = 1;
5438 inPublic.t.publicArea.objectAttributes.userWithAuth = 1;
5439 inPublic.t.publicArea.objectAttributes.decrypt = 1;
5440 inPublic.t.publicArea.objectAttributes.fixedTPM = 1;
5441 inPublic.t.publicArea.objectAttributes.fixedParent = 1;
5442 inPublic.t.publicArea.objectAttributes.sensitiveDataOrigin = 1;
5443
5444 inPublic.t.publicArea.authPolicy.t.size = 0;
5445
5446 inPublic.t.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
5447 inPublic.t.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
5448 inPublic.t.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_ECB;
5449 inPublic.t.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
5450 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 1024;
5451 inPublic.t.publicArea.parameters.rsaDetail.exponent = 0;
5452
5453 inPublic.t.publicArea.unique.rsa.t.size = 0;
5454
5455 outsideInfo.t.size = 0;
5456 creationPCR.count = 0;
5457
5458 sessionData.sessionHandle = TPM_RS_PW;
5459
5460 // Init nonce.
5461 sessionData.nonce.t.size = 0;
5462
5463 // init hmac
5464 sessionData.hmac.t.size = 0;
5465
5466 // Init session attributes
5467 *( (UINT8 *)((void *)&sessionData.sessionAttributes ) ) = 0;
5468
5469 sessionsData.cmdAuthsCount = 1;
5470 sessionsData.cmdAuths[0] = &sessionData;
5471
5472 rval = InitTctiResMgrContext( rmInterfaceConfig, &otherResMgrTctiContext, &otherResMgrInterfaceName[0] );
5473 if( rval != TSS2_RC_SUCCESS )
5474 {
5475 TpmClientPrintf( 0, "Resource Mgr, %s, failed initialization: 0x%x. Exiting...\n", resMgrInterfaceInfo.shortName, rval );
5476 Cleanup();
5477 return;
5478 }
5479 else
5480 {
5481 (( TSS2_TCTI_CONTEXT_INTEL *)otherResMgrTctiContext )->status.debugMsgLevel = debugLevel;
5482 }
5483
5484 otherSysContext = InitSysContext( 0, otherResMgrTctiContext, &abiVersion );
5485 if( otherSysContext == 0 )
5486 {
5487 InitSysContextFailure();
5488 }
5489
5490 // TEST OWNERSHIP
5491
5492 // Try to access a key created by the first TCTI context.
5493 sessionData.hmac.t.size = 2;
5494 sessionData.hmac.t.buffer[0] = 0x00;
5495 sessionData.hmac.t.buffer[1] = 0xff;
5496
5497 inPublic.t.publicArea.type = TPM_ALG_RSA;
5498 inPublic.t.publicArea.nameAlg = TPM_ALG_SHA1;
5499
5500 // First clear attributes bit field.
5501 *(UINT32 *)&( inPublic.t.publicArea.objectAttributes) = 0;
5502 inPublic.t.publicArea.objectAttributes.restricted = 1;
5503 inPublic.t.publicArea.objectAttributes.userWithAuth = 1;
5504 inPublic.t.publicArea.objectAttributes.decrypt = 1;
5505 inPublic.t.publicArea.objectAttributes.fixedTPM = 1;
5506 inPublic.t.publicArea.objectAttributes.fixedParent = 1;
5507 inPublic.t.publicArea.objectAttributes.sensitiveDataOrigin = 1;
5508
5509 inPublic.t.publicArea.authPolicy.t.size = 0;
5510
5511 inPublic.t.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
5512 inPublic.t.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
5513 inPublic.t.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_ECB;
5514 inPublic.t.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
5515 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 2048;
5516 inPublic.t.publicArea.parameters.rsaDetail.exponent = 0;
5517
5518 inPublic.t.publicArea.unique.rsa.t.size = 0;
5519
5520 outsideInfo.t.size = 0;
5521
5522 // This one should fail, because a different context is trying to use the primary object.
5523 outPublic.t.size = 0;
5524 creationData.t.size = 0;
5525 rval = Tss2_Sys_Create( otherSysContext, handle2048rsa, &sessionsData, &inSensitive, &inPublic,
5526 &outsideInfo, &creationPCR,
5527 &outPrivate, &outPublic, &creationData,
5528 &creationHash, &creationTicket, &sessionsDataOut );
5529 CheckFailed( rval, TSS2_RESMGR_UNOWNED_HANDLE );
5530
5531 // This one should pass, because the same context is allowed to save the context.
5532 rval = Tss2_Sys_ContextSave( sysContext, handle2048rsa, &context );
5533 CheckPassed( rval );
5534
5535 // This one should pass, since we saved the context first.
5536 rval = Tss2_Sys_ContextLoad( otherSysContext, &context, &loadedHandle );
5537 CheckPassed( rval );
5538
5539 rval = Tss2_Sys_FlushContext( otherSysContext, loadedHandle );
5540 CheckPassed( rval );
5541
5542 // NOW, DO SOME LOCALITY TESTS
5543
5544 // Test with null tctiContext ptr.
5545 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->setLocality)( 0, 0 );
5546 CheckFailed( rval, TSS2_TCTI_RC_BAD_REFERENCE );
5547
5548 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->setLocality)( otherResMgrTctiContext, 0 );
5549 CheckPassed( rval );
5550
5551 // Now try changing localities between send and receive.
5552 rval = Tss2_Sys_ContextLoad( otherSysContext, &context, &loadedHandle );
5553 CheckPassed( rval );
5554
5555 rval = Tss2_Sys_FlushContext_Prepare( otherSysContext, loadedHandle );
5556 CheckPassed( rval );
5557
5558 rval = Tss2_Sys_ExecuteAsync( otherSysContext );
5559 CheckPassed( rval );
5560
5561 // This should fail because locality is changing between send and receive.
5562 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->setLocality)( otherResMgrTctiContext, 1 );
5563 CheckFailed( rval, TSS2_TCTI_RC_BAD_SEQUENCE );
5564
5565 rval = Tss2_Sys_ExecuteFinish( otherSysContext, TSS2_TCTI_TIMEOUT_BLOCK );
5566 CheckPassed( rval );
5567
5568 // NOW, DO SOME CANCEL TESTS
5569
5570 rval = Tss2_Sys_GetTctiContext( sysContext, &tctiContext );
5571 CheckPassed( rval );
5572
5573 // Try cancel with null tctiContext ptr.
5574 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->cancel)( 0 );
5575 CheckFailed( rval, TSS2_TCTI_RC_BAD_REFERENCE );
5576
5577 // Try cancel when no commands are pending.
5578 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->cancel)( otherResMgrTctiContext );
5579 CheckFailed( rval, TSS2_TCTI_RC_BAD_SEQUENCE );
5580
5581 // Then try cancel with a pending command: send cancel before blocking _Finish call.
5582 inPublic.t.publicArea.parameters.rsaDetail.keyBits = 2048;
5583 rval = Tss2_Sys_CreatePrimary_Prepare( sysContext, TPM_RH_PLATFORM, &inSensitive, &inPublic,
5584 &outsideInfo, &creationPCR );
5585 CheckPassed( rval );
5586
5587 //
5588 // NOTE: there are race conditions in tests that use cancel and
5589 // are expecting to receive the CANCEL response code. The tests
5590 // typically pass, but may occasionally fail on the order of
5591 // 1 out of 500 or so test passes.
5592 //
5593 // The OS could delay the test app long enough for the TPM to
5594 // complete the CreatePrimary before the test app gets to run
5595 // again. To make these tests robust would require some way to
5596 // create a critical section in the test app.
5597 //
5598 sessionData.hmac.t.size = 0;
5599 sessionData.nonce.t.size = 0;
5600 rval = Tss2_Sys_SetCmdAuths( sysContext, &sessionsData );
5601 CheckPassed( rval );
5602
5603 rval = Tss2_Sys_ExecuteAsync( sysContext );
5604 CheckPassed( rval );
5605
5606 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->cancel)( tctiContext );
5607 CheckPassed( rval );
5608
5609 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
5610 CheckFailed( rval, TPM_RC_CANCELED );
5611
5612 // Then try cancel with a pending command: send cancel after non-blocking _Finish call.
5613 rval = Tss2_Sys_CreatePrimary_Prepare( sysContext, TPM_RH_PLATFORM, &inSensitive, &inPublic,
5614 &outsideInfo, &creationPCR );
5615 CheckPassed( rval );
5616
5617 rval = Tss2_Sys_SetCmdAuths( sysContext, &sessionsData );
5618 CheckPassed( rval );
5619
5620 rval = Tss2_Sys_ExecuteAsync( sysContext );
5621 CheckPassed( rval );
5622
5623 rval = Tss2_Sys_ExecuteFinish( sysContext, 0 );
5624 CheckFailed( rval, TSS2_TCTI_RC_TRY_AGAIN );
5625
5626 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->cancel)( tctiContext );
5627 CheckPassed( rval );
5628
5629 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
5630 CheckFailed( rval, TPM_RC_CANCELED );
5631
5632 // Then try cancel from a different connection: it should just get a sequence error.
5633 rval = Tss2_Sys_CreatePrimary_Prepare( sysContext, TPM_RH_PLATFORM, &inSensitive, &inPublic,
5634 &outsideInfo, &creationPCR );
5635 CheckPassed( rval );
5636
5637 rval = Tss2_Sys_SetCmdAuths( sysContext, &sessionsData );
5638 CheckPassed( rval );
5639
5640 rval = Tss2_Sys_ExecuteAsync( sysContext );
5641 CheckPassed( rval );
5642
5643 rval = (((TSS2_TCTI_CONTEXT_COMMON_V1 *)otherResMgrTctiContext)->cancel)( otherResMgrTctiContext );
5644 CheckFailed( rval, TSS2_TCTI_RC_BAD_SEQUENCE );
5645
5646 rval = Tss2_Sys_ExecuteFinish( sysContext, TSS2_TCTI_TIMEOUT_BLOCK );
5647 CheckPassed( rval );
5648
5649 outPublic.t.size = 0;
5650 creationData.t.size = 0;
5651 rval = Tss2_Sys_CreatePrimary_Complete( sysContext, &newHandle, &outPublic, &creationData,
5652 &creationHash, &creationTicket, &name );
5653 CheckPassed( rval );
5654
5655 //
5656 // Now try saving context for object and loading it using a different connection.
5657 //
5658
5659 // First save context.
5660 rval = Tss2_Sys_ContextSave( sysContext, newHandle, &newContext );
5661 CheckPassed( rval );
5662
5663 //
5664 // Now create an object with different hierarchy. This will make sure that
5665 // RM is getting correct hierarchy in it's table.
5666 // NOTE: this test can only be verified by looking at RM output.
5667 //
5668 outPublic.t.size = 0;
5669 creationData.t.size = 0;
5670 rval = Tss2_Sys_CreatePrimary( sysContext, TPM_RH_ENDORSEMENT, &sessionsData, &inSensitive, &inPublic,
5671 &outsideInfo, &creationPCR, &newHandleDummy, &outPublic, &creationData, &creationHash,
5672 &creationTicket, &name, &sessionsDataOut );
5673 CheckPassed( rval );
5674
5675 // Now try loading the context using a different connection.
5676 rval = Tss2_Sys_ContextLoad( otherSysContext, &newContext, &newNewHandle );
5677 CheckPassed( rval );
5678
5679 // Flush original connection's object.
5680 rval = Tss2_Sys_FlushContext( sysContext, newHandle );
5681 CheckPassed( rval );
5682
5683 // Now try flushing new object from wrong connection. Shouldn't be able to.
5684 rval = Tss2_Sys_FlushContext( sysContext, newNewHandle );
5685 CheckFailed( rval, TSS2_RESMGR_UNOWNED_HANDLE );
5686
5687 // Now flush new object from other connection. Should work.
5688 rval = Tss2_Sys_FlushContext( otherSysContext, newNewHandle );
5689 CheckPassed( rval );
5690
5691 // Now flush dummy object.
5692 rval = Tss2_Sys_FlushContext( sysContext, newHandleDummy );
5693 CheckPassed( rval );
5694
5695 rval = TeardownTctiResMgrContext( rmInterfaceConfig, otherResMgrTctiContext, &otherResMgrInterfaceName[0] );
5696 CheckPassed( rval );
5697
5698 TeardownSysContext( &otherSysContext );
5699}
5700
5701void EcEphemeralTest()
5702{
5703 TSS2_RC rval = TSS2_RC_SUCCESS;
5704 TPM2B_ECC_POINT Q;
5705 UINT16 counter;
5706
5707 TpmClientPrintf( 0, "\nEC Ephemeral TESTS:\n" );
5708
5709 // Test SAPI for case of Q size field not being set to 0.
5710 Q.t.size = 0xff;
5711 rval = Tss2_Sys_EC_Ephemeral( sysContext, 0, TPM_ECC_BN_P256, &Q, &counter, 0 );
5712 CheckFailed( rval, TSS2_SYS_RC_BAD_VALUE );
5713
5714 Q.t.size = 0;
5715 rval = Tss2_Sys_EC_Ephemeral( sysContext, 0, TPM_ECC_BN_P256, &Q, &counter, 0 );
5716 CheckPassed( rval );
5717}
5718
5719
5720void AbiVersionTests()
5721{
5722 UINT32 contextSize = 1000;
5723 TSS2_RC rval;
5724 TSS2_SYS_CONTEXT *sysContext;
5725 TSS2_ABI_VERSION tstAbiVersion = { TSSWG_INTEROP, TSS_SAPI_FIRST_FAMILY, TSS_SAPI_FIRST_LEVEL, TSS_SAPI_FIRST_VERSION };
5726
5727 TpmClientPrintf( 0, "\nABI NEGOTIATION TESTS:\n" );
5728
5729 // Get the size needed for system context structure.
5730 contextSize = Tss2_Sys_GetContextSize( contextSize );
5731
5732 // Allocate the space for the system context structure.
5733 sysContext = (TSS2_SYS_CONTEXT *)malloc( contextSize );
5734
5735 if( sysContext != 0 )
5736 {
5737 // Initialized the system context structure.
5738 tstAbiVersion.tssCreator = 0xF0000000;
5739 rval = Tss2_Sys_Initialize( sysContext, contextSize, resMgrTctiContext, &tstAbiVersion );
5740 CheckFailed( rval, TSS2_SYS_RC_ABI_MISMATCH );
5741
5742 tstAbiVersion.tssCreator = TSSWG_INTEROP;
5743 tstAbiVersion.tssFamily = 0xF0000000;
5744 rval = Tss2_Sys_Initialize( sysContext, contextSize, resMgrTctiContext, &tstAbiVersion );
5745 CheckFailed( rval, TSS2_SYS_RC_ABI_MISMATCH );
5746
5747 tstAbiVersion.tssFamily = TSS_SAPI_FIRST_FAMILY;
5748 tstAbiVersion.tssLevel = 0xF0000000;
5749 rval = Tss2_Sys_Initialize( sysContext, contextSize, resMgrTctiContext, &tstAbiVersion );
5750 CheckFailed( rval, TSS2_SYS_RC_ABI_MISMATCH );
5751
5752 tstAbiVersion.tssLevel = TSS_SAPI_FIRST_LEVEL;
5753 tstAbiVersion.tssVersion = 0xF0000000;
5754 rval = Tss2_Sys_Initialize( sysContext, contextSize, resMgrTctiContext, &tstAbiVersion );
5755 CheckFailed( rval, TSS2_SYS_RC_ABI_MISMATCH );
5756 }
5757 free( sysContext );
5758}
5759
5760
5761#ifdef __cplusplus
5762extern "C" {
5763#endif
5764
5765extern int dummy_test();
5766
5767#ifdef __cplusplus
5768}
5769#endif
5770
5771extern TSS2_RC SocketSendTpmCommand(
5772 TSS2_TCTI_CONTEXT *tctiContext, /* in */
5773 size_t command_size, /* in */
5774 uint8_t *command_buffer /* in */
5775 );
5776
5777TSS2_RC SocketReceiveTpmResponse(
5778 TSS2_TCTI_CONTEXT *tctiContext, /* in */
5779 size_t *response_size, /* out */
5780 unsigned char *response_buffer, /* in */
5781 int32_t timeout
5782 );
5783
5784void TpmTest()
5785{
5786 TSS2_RC rval = TSS2_RC_SUCCESS;
5787 UINT32 i;
5788
5789 nullSessionsDataOut.rspAuthsCount = 1;
5790 nullSessionsDataOut.rspAuths[0]->nonce = nullSessionNonceOut;
5791 nullSessionsDataOut.rspAuths[0]->hmac = nullSessionHmac;
5792 nullSessionNonceOut.t.size = 0;
5793 nullSessionNonce.t.size = 0;
5794
5795 loadedSha1KeyAuth.t.size = 2;
5796 loadedSha1KeyAuth.t.buffer[0] = 0x00;
5797 loadedSha1KeyAuth.t.buffer[1] = 0xff;
5798
5799 InitEntities();
5800
5801 InitNullSession( &nullSessionData);
5802
5803 AbiVersionTests();
5804
5805 GetSetDecryptParamTests();
5806
5807 rval = PlatformCommand( resMgrTctiContext, MS_SIM_POWER_ON );
5808 CheckPassed( rval );
5809
5810 rval = PlatformCommand( resMgrTctiContext, MS_SIM_NV_ON );
5811 CheckPassed( rval );
5812
5813 TestTpmStartup();
5814
5815 GetTpmVersion();
5816
5817 // Clear DA lockout.
5818 TestDictionaryAttackLockReset();
5819
5820 TestTpmSelftest();
5821
5822 TestSapiApis();
5823
5824 TestDictionaryAttackLockReset();
5825
5826 TestCreate();
5827
5828 if( startAuthSessionTestOnly == 1 )
5829 {
5830 TestStartAuthSession();
5831 goto endTests;
5832 }
5833
5834 TestHierarchyControl();
5835
5836 NvIndexProto();
5837
5838 GetSetEncryptParamTests();
5839
5840 TestEncryptDecryptSession();
5841
5842 SimpleHmacOrPolicyTest( true );
5843
5844 SimpleHmacOrPolicyTest( false );
5845
5846 for( i = 1; i <= (UINT32)passCount; i++ )
5847 {
5848 TpmClientPrintf( 0, "\n****** PASS #: %d ******\n\n", i );
5849
5850 TestTpmGetCapability();
5851
5852 TestPcrExtend();
5853
5854 TestHash();
5855
5856 TestPolicy();
5857
5858 TestTpmClear();
5859
5860 TestChangeEps();
5861
5862 TestChangePps();
5863
5864 TestHierarchyChangeAuth();
5865
5866 TestGetRandom();
5867
5868 if( i < 2 )
5869 TestShutdown();
5870
5871 TestNV();
5872
5873 TestCreate();
5874
5875 TestEvict();
5876
5877 NvIndexProto();
5878
5879 PasswordTest();
5880
5881 HmacSessionTest();
5882
5883 TestQuote();
5884
5885 TestDictionaryAttackLockReset();
5886
5887 TestPcrAllocate();
5888
5889 TestUnseal();
5890
5891 TestRM();
5892
5893 EcEphemeralTest();
5894#if 0
5895 TestRsaEncryptDecrypt();
5896#endif
5897 }
5898
5899 // Clear out RM entries for objects.
5900 rval = Tss2_Sys_FlushContext( sysContext, handle2048rsa );
5901 CheckPassed( rval );
5902 rval = Tss2_Sys_FlushContext( sysContext, loadedSha1KeyHandle );
5903 CheckPassed( rval );
5904
5905endTests:
5906 PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
5907}
5908
5909
5910char version[] = "0.90";
5911
5912void PrintHelp()
5913{
5914 printf( "TPM client test app, Version %s\nUsage: tpmclient [-host hostname|ip_addr] [-port port] [-passes passNum] [-demoDelay delay] [-dbg dbgLevel] [-startAuthSessionTest]\n"
5915 "\n"
5916 "where:\n"
5917 "\n"
5918 "-host specifies the host IP address (default: %s)\n"
5919 "-port specifies the port number (default: %d)\n"
5920 "-passes specifies the number of test passes (default: 1)\n"
5921 "-demoDelay specifies a delay in units of loops, not time (default: 0)\n"
5922 "-dbgLevel specifies level of debug messages:\n"
5923 " 0 (high level test results)\n"
5924 " 1 (test app send/receive byte streams)\n"
5925 " 2 (resource manager send/receive byte streams)\n"
5926 " 3 (resource manager tables)\n"
5927 "-startAuthSessionTest enables some special tests of the resource manager for starting sessions\n"
5928#ifdef SHARED_OUT_FILE
5929 "-out selects the output file (default is stdout)\n"
5930#endif
5931 , version, DEFAULT_HOSTNAME, DEFAULT_RESMGR_TPM_PORT );
5932}
5933
5934int main(int argc, char* argv[])
5935{
5936 char hostName[HOSTNAME_LENGTH] = DEFAULT_HOSTNAME;
5937 int port = DEFAULT_RESMGR_TPM_PORT;
5938 int count;
5939 TSS2_RC rval;
5940
5941 setvbuf (stdout, NULL, _IONBF, BUFSIZ);
5942#ifdef SHARED_OUT_FILE
5943 if( argc > 12 )
5944#else
5945 if( argc > 10 )
5946#endif
5947 {
5948 PrintHelp();
5949 return 1;
5950 }
5951 else
5952 {
5953 for( count = 1; count < argc; count++ )
5954 {
5955 if( 0 == strcmp( argv[count], "-host" ) )
5956 {
5957 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04005958 if( count >= argc || ( strlen( argv[count] ) + 1 <= HOSTNAME_LENGTH ) )
Will Arthur54e04e42015-07-15 11:29:25 -04005959 {
5960 if( 1 != sscanf( argv[count], "%199s", &hostName[0] ) )
5961 {
5962 PrintHelp();
5963 return 1;
5964 }
5965 }
5966 else
5967 {
5968 PrintHelp();
5969 return 1;
5970 }
5971 }
5972 else if( 0 == strcmp( argv[count], "-port" ) )
5973 {
5974 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04005975 if( count >= argc || 1 != sscanf_s( argv[count], "%d", &port ) )
Will Arthur54e04e42015-07-15 11:29:25 -04005976 {
5977 PrintHelp();
5978 return 1;
5979 }
5980 }
5981 else if( 0 == strcmp( argv[count], "-passes" ) )
5982 {
5983 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04005984 if( count >= argc || 1 != sscanf_s( argv[count], "%x", &passCount ) )
Will Arthur54e04e42015-07-15 11:29:25 -04005985 {
5986 PrintHelp();
5987 return 1;
5988 }
5989 }
5990 else if( 0 == strcmp( argv[count], "-demoDelay" ) )
5991 {
5992 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04005993 if( count >= argc || 1 != sscanf_s( argv[count], "%x", &demoDelay ) )
Will Arthur54e04e42015-07-15 11:29:25 -04005994 {
5995 PrintHelp();
5996 return 1;
5997 }
5998 }
5999 else if( 0 == strcmp( argv[count], "-dbg" ) )
6000 {
6001 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04006002 if( count >= argc || 1 != sscanf_s( argv[count], "%d", &debugLevel ) )
Will Arthur54e04e42015-07-15 11:29:25 -04006003 {
6004 PrintHelp();
6005 return 1;
6006 }
6007 }
6008 else if( 0 == strcmp( argv[count], "-startAuthSessionTest" ) )
6009 {
6010 startAuthSessionTestOnly = 1;
6011 }
6012#ifdef SHARED_OUT_FILE
6013 else if( 0 == strcmp( argv[count], "-out" ) )
6014 {
6015 count++;
Will Arthur16fc1802015-08-20 17:07:34 -04006016 if( count >= argc || 1 != sscanf_s( argv[count], "%199s", &outFileName, sizeof( outFileName ) ) )
Will Arthur54e04e42015-07-15 11:29:25 -04006017 {
6018 PrintHelp();
6019 return 1;
6020 }
6021 else
6022 {
6023 OpenOutFile( &outFp );
6024
6025 if( outFp == 0 )
6026 {
6027 printf( "Unable to open file, %s\n", &outFileName[0] );
6028 PrintHelp();
6029 return 1;
6030 }
6031 CloseOutFile( &outFp );
6032 }
6033 }
6034#endif
6035 else
6036 {
6037 PrintHelp();
6038 return 1;
6039 }
6040 }
6041 }
6042
6043 if( 0 == strcmp( outFileName, "" ) )
6044 {
6045 outFp = stdout;
6046 }
6047 else
6048 {
6049 outFp = 0;
6050 }
6051
6052 sprintf_s( rmInterfaceConfig, rmInterfaceConfigSize, "%s %d ", hostName, port );
6053
6054 rval = InitTctiResMgrContext( rmInterfaceConfig, &resMgrTctiContext, &resMgrInterfaceName[0] );
6055 if( rval != TSS2_RC_SUCCESS )
6056 {
6057 TpmClientPrintf( 0, "Resource Mgr, %s, failed initialization: 0x%x. Exiting...\n", resMgrInterfaceInfo.shortName, rval );
6058 Cleanup();
6059 return( 1 );
6060 }
6061 else
6062 {
6063 (( TSS2_TCTI_CONTEXT_INTEL *)resMgrTctiContext )->status.debugMsgLevel = debugLevel;
6064 }
6065
6066 sysContext = InitSysContext( 0, resMgrTctiContext, &abiVersion );
6067 if( sysContext == 0 )
6068 {
6069 InitSysContextFailure();
6070 }
6071 else
6072 {
6073 TpmTest();
6074
6075 rval = TeardownTctiResMgrContext( rmInterfaceConfig, resMgrTctiContext, &resMgrInterfaceName[0] );
6076 CheckPassed( rval );
6077
6078 TeardownSysContext( &sysContext );
6079 }
6080
6081 return 0;
6082}
6083
6084