Desktop version

Home arrow Computer Science arrow A Practical Guide to TPM 2.0

Simple Code Example

The next code example, from the SAPI library test code, performs a TPM2_GetTestResult command three different ways: one-call, synchronous calls, and asynchronous calls. Comments help delineate the tests of the three different ways:

■ Note

CheckPassed() is a routine that compares the passed-in return value to 0. If they aren't equal, an error has occurred, and the routine prints an error message, cleans up, and exits the test program.

void TestGetTestResult()

{

UINT32 rval; TPM2B_MAX_BUFFER outData;

TPM_RC testResult; TSS2_SYS_CONTEXT *systemContext;

printf( " GET TEST RESULT TESTS: " );

// Initialize the system context structure.

systemContext = InitSysContext( 2000, resMgrTctiContext, &abiVersion ); if( systemContext == 0 )

{ Handle failure, cleanup, and exit. InitSysContextFailure();}

test the one-call apI.

//

// First test the one-call interface.

//

rval = Tss2_Sys_GetTestResult( systemContext, 0, &outData, &testResult, 0 );

CheckPassed(rval);

test the synchronous, multi-call apIs.

//

// Now test the synchronous, non-one-call APIs.

//

rval = Tss2_Sys_GetTestResult_Prepare( systemContext ); CheckPassed(rval);

// Execute the command synchronously. rval = Tss2_Sys_Execute( systemContext ); CheckPassed(rval);

// Get the command results

rval = Tss2_Sys_GetTestResult_Complete( systemContext, &outData,

&testResult ); CheckPassed(rval);

test the asynchronous, multi-call apIs.

//

// Now test the asynchronous, non-one-call interface.

//

rval = Tss2_Sys_GetTestResult_Prepare( systemContext ); CheckPassed(rval);

// Execute the command asynchronously.

rval = Tss2_Sys_ExecuteAsync( systemContext ); CheckPassed(rval);

// Get the command response. Wait a maximum of 20ms

// for response.

rval = Tss2_Sys_ExecuteFinish( systemContext, 20 ); CheckPassed(rval);

// Get the command results

rval = Tss2_Sys_GetTestResult_Complete( systemContext, &outData,

&testResult ); CheckPassed(rval);

// Tear down the system context. TeardownSysContext( systemContext );}

System API Test Code

As mentioned, the previous GetTestResult test is included as one of the tests in the SAPI test code. This section briefly describes the structure of the test code and some design features.

Many other tests in this code test various SAPI capabilities. But you should beware that this test suite is by no means comprehensive; there are too many permutations and not enough time for a single developer to write all the tests. These tests were written to provide sanity checks and, in some cases, more detailed tests of targeted functionality.

The test code resides in the Test pmclient subdirectory. In this directory, the tpmclient.cpp file contains the test application's initialization and control code as well as all the main test routines. Subdirectories of tpmclient provide support code needed for the tests. The simDriver subdirectory contains a device driver for communicating with the TPM simulator. The resourceMgr subdirectory contains code for a sample RM. And the sample subdirectory contains application-level code that performs the following tasks: maintaining session state information, calculating HMACs, and performing cryptographic functions.

A major design principle of the SAPI test code was to use the TPM itself for all cryptographic functions. No outside libraries such as OpenSSL are used. The reason for this was twofold. First, it increased the test coverage of the SAPI test code by calling TPM cryptographic commands. Second, it allowed the test application to be a stand-alone application with no dependency on outside libraries. And there was a third reason: the developer thought it was kind of a cool thing to do! The SAPI test code can be used as a starting point for developers: find a command you want to use that's called in the test code, and it will give you a significant boost in your code development.

The SAPI test code uses other elements of the TSS stack to perform its tests: the TCTI, TAB, and RM. Because SAPI uses the TCTI to send commands to the TAB, TCTI is described next.

 
< Prev   CONTENTS   Next >

Related topics