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