int main(int argc, char **argv){

    atexit(atexit_function);

    if(argc != 5){
        printf("usage: ./records_counter <file name> <threads number> <records number> <searched word>");
        exit(EXIT_FAILURE);
    }
    
    if( (file_desc = open(argv[1], O_RDONLY)) == -1)
        print_error_exit(errno, "can not open file");

    if( (pthread_mutex_init(&p_mutex, NULL)) !=0 )
        print_error_exit(errno, "error in pthread_mutex_init");

    threads_number = atoi(argv[2]);
    records_number = atoi(argv[3]);

    threads = malloc( sizeof(pthread_t) *threads_number);
    int i;
    for(i = 0; i < threads_number; i++){
        if(pthread_create(&threads[i], NULL, search_file, argv[4]) == -1);
    }
    
    for(i = 0; i < threads_number; i++){
        if(pthread_join(threads[i], NULL) == -1)
            print_error(errno, "error in pthread_join");
    }
    
    exit(EXIT_SUCCESS);
}
void
tc_create_policy(TSS_HCONTEXT hContext, UINT32 type, UINT32 flags, TSS_HOBJECT hObject)
{
	TSS_HPOLICY hPolicy;
	TSS_RESULT result;

	//Create Policy Object
	tc_create_object(hContext, type, flags, &hPolicy);

	//Set Policy Secret
	result = Tspi_Policy_SetSecret(hPolicy, TESTSUITE_KEY_SECRET_MODE,
			TESTSUITE_KEY_SECRET_LEN, TESTSUITE_KEY_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Assign Policy to Object (Key)
	result = Tspi_Policy_AssignToObject(hPolicy, hObject);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_AssignToObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
}
void
tc_create_key(TSS_HCONTEXT hContext, TSS_HKEY hKey, TSS_HKEY hParent, UINT32 flags)
{
	TSS_RESULT result;

	//Create Policy
	if (flags & TSS_KEY_AUTHORIZATION) {
		//Create Usage Policy Object
		tc_create_policy(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, hKey);

		if (flags & TSS_KEY_MIGRATABLE) {
			//Create Migration Policy Object
			tc_create_policy(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_MIGRATION, hKey);
		}
	}

	//Create Key
	result = Tspi_Key_CreateKey(hKey, hParent, 0);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_CreateKey", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
}
void
tc_load_key(TSS_HCONTEXT hContext, TSS_HKEY hKey, TSS_HKEY hParent)
{
	TSS_RESULT result;

	result = Tspi_Key_LoadKey(hKey, hParent);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
}
void
tc_set_attribdata(TSS_HCONTEXT hContext, TSS_HOBJECT hObject, UINT32 flag, UINT32 subflag, UINT32 blobSize, BYTE *blob)
{
	TSS_RESULT result;

	result = Tspi_SetAttribData(hObject, flag, subflag, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_SetAttribData", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
}
void
tc_create_object(TSS_HCONTEXT hContext, UINT32 type, UINT32 flags, TSS_HOBJECT *hObject)
{
	TSS_RESULT result;

	//Create Object
	result = Tspi_Context_CreateObject(hContext, type, flags, hObject);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
}
int
main_v1_2(char version)
{
	char		*function = "Tspi_PcrComposite_GetPcrLocality01";
	TSS_HCONTEXT	hContext;
	TSS_HPCRS	hPcrComposite;
	TSS_RESULT	result;
	UINT32		pLocalityValue;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// create object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
					   TSS_PCRS_STRUCT_INFO_LONG, &hPcrComposite);
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_GetPcrLocality( hPcrComposite, &pLocalityValue );
	if ( result != TSS_SUCCESS )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
		print_end_test( function );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( 0 );
	}
}
Beispiel #8
0
int
main_v1_2(char version)
{
    char	     *nameOfFunction    = "Tspi_Nv_ReadValue03";

    TSS_HCONTEXT hContext           = NULL_HCONTEXT;
    TSS_HNVSTORE hNVStore           = 0;//NULL_HNVSTORE
    TSS_HOBJECT  hPolObject         = NULL_HOBJECT;
    TSS_HPOLICY  hPolicy            = NULL_HPOLICY;
    TSS_HTPM     hTPM               = NULL_HTPM;
    BYTE         *auth              = "123456";
    UINT32       auth_length        = 6;
    BYTE         *policyData;
    UINT32       read_space         = 10;
    TSS_RESULT   result;

    print_begin_test(nameOfFunction);

    //Create Context
    result = Tspi_Context_Create(&hContext);
    if (result != TSS_SUCCESS) {
        print_error("Tspi_Context_Create ", result);
        print_error_exit(nameOfFunction, err_string(result));
        exit(result);
    }
    //Connect Context
    result = Tspi_Context_Connect(hContext,NULL);
    if (result != TSS_SUCCESS) {
        print_error("Tspi_Context_Connect", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Create TPM NV object */
    result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_NV, 0,&hNVStore);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Context_CreateObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

#ifdef NV_LOCKED
    /* Get TPM object */
    result = Tspi_Context_GetTpmObject(hContext, &hTPM);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Context_GetTpmObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    result = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hPolicy);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_GetPolicyObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set password */
    result = Tspi_Policy_SetSecret(hPolicy, TESTSUITE_OWNER_SECRET_MODE,
                                   TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);

    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Policy_SetSecret", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }
#endif

    /* Create policy object for the NV object*/
    result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &hPolObject);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Context_CreateObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set password */
    result = Tspi_Policy_SetSecret(hPolObject, TSS_SECRET_MODE_PLAIN, auth_length, auth);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Policy_SetSecret", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set password */
    result = Tspi_Policy_AssignToObject(hPolObject, hNVStore);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Policy_AssignToObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }


    /* Set the index to be defined. */
    result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_INDEX, 0,0x00011143);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_SetAttribUint32 for setting NV index", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }


    /* Set the permission for the index. */
    result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_PERMISSIONS, 0, 0x42000);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_SetAttribUint32 for setting permission", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }


    /* Set the data size to be defined. */
    result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_DATASIZE, 0, 0xa);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_SetAttribUint32 for setting data size", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /*Define NV space*/
    result = Tspi_NV_DefineSpace(hNVStore, 0, 0);



    //Create Context
    result = Tspi_Context_Create(&hContext);
    if (result != TSS_SUCCESS) {
        print_error("Tspi_Context_Create ", result);
        print_error_exit(nameOfFunction, err_string(result));
        exit(result);
    }
    //Connect Context
    result = Tspi_Context_Connect(hContext,NULL);
    if (result != TSS_SUCCESS) {
        print_error("Tspi_Context_Connect", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Create TPM NV object */
    result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_NV, 0,&hNVStore);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Context_CreateObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Create policy object for the NV object*/
    result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &hPolObject);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Context_CreateObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set password */
    result = Tspi_Policy_SetSecret(hPolObject, TSS_SECRET_MODE_PLAIN, auth_length, auth);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Policy_SetSecret", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set password */
    result = Tspi_Policy_AssignToObject(hPolObject, hNVStore);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_Policy_AssignToObject", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    /* Set the index to be defined. */
    result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_INDEX, 0,0x00011143);
    if (result != TSS_SUCCESS)
    {
        print_error("Tspi_SetAttribUint32 for setting NV index", result);
        print_error_exit(nameOfFunction, err_string(result));
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

    result = Tspi_NV_ReadValue(hNVStore,/*read_offset*/0, &read_space, &policyData);

#ifdef CLEAR_TEST_INDEX
    Tspi_Context_GetTpmObject(hContext, &hTPM);
    Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hPolicy);
    Tspi_Policy_SetSecret(hPolicy, TESTSUITE_OWNER_SECRET_MODE,
                          TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);
    Tspi_NV_ReleaseSpace(hNVStore);
#endif

#ifdef NV_LOCKED
    if (TSS_ERROR_CODE(result)== TSS_SUCCESS)
    {
        print_success(nameOfFunction, result);
        print_end_test(nameOfFunction);
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(0);
    }
    else {
        print_error("Tspi_NV_ReadValue", result);
        print_end_test(nameOfFunction);
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }

#else
    if (TSS_ERROR_CODE(result)== TSS_SUCCESS)
    {
        print_success(nameOfFunction, result);
        print_end_test(nameOfFunction);
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(0);
    }
    else {
        print_error("Tspi_NV_ReadValue", result);
        print_end_test(nameOfFunction);
        Tspi_Context_FreeMemory(hContext, NULL);
        Tspi_Context_Close(hContext);
        exit(result);
    }
#endif
}
main_v1_1(void){

	char		*nameOfFunction = "Tspi_GetAttribData19";
	TSS_FLAG	initFlags;
	TSS_HKEY	hKey;
	TSS_HCONTEXT	hContext;
	TSS_RESULT	result;
	TSS_HKEY	hSRK;
	BYTE*		uuid;
	UINT32		uuidLength;
	int		rc;
	TSS_UUID	null_uuid, key_uuid;
	initFlags	= TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048  |
			TSS_KEY_VOLATILE | TSS_KEY_NO_AUTHORIZATION |
			TSS_KEY_NOT_MIGRATABLE;

	memset(&null_uuid, 0, sizeof(TSS_UUID));
	memset(&key_uuid, 0x7f, sizeof(TSS_UUID));

	print_begin_test(nameOfFunction);

		//Create Context and connect
	result = connect_load_srk(&hContext, &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("connect_load_srk", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Create Key Object
	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_RSAKEY,
					   initFlags, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Create Key in the TPM
	result = Tspi_Key_CreateKey(hKey, hSRK, 0);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_CreateKey", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Call GetAttribData, uuid should be all 0's
	result = Tspi_GetAttribData(hKey,
				    TSS_TSPATTRIB_KEY_UUID, 0,
				    &uuidLength, &uuid);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetAttribData", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

		//Check length and data
	if (uuidLength != sizeof(TSS_UUID)) {
		print_verifyerr("uuid length from Tspi_GetAttribData", 0, 1);
		print_error("uuid length from Tspi_GetAttribData", TSS_E_FAIL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	if ((rc = memcmp(uuid, &null_uuid, uuidLength))) {
		print_verifyerr("a null uuid from Tspi_GetAttribData", 0, rc);
		print_hex(uuid, sizeof(TSS_UUID));
		print_error("uuid NULL from Tspi_GetAttribData", TSS_E_FAIL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	Tspi_Context_FreeMemory(hContext, uuid);

register_key:
		//Register Key
	result = Tspi_Context_RegisterKey(hContext, hKey, TSS_PS_TYPE_SYSTEM,
					  key_uuid, TSS_PS_TYPE_SYSTEM,
					  SRK_UUID);
	if (TSS_ERROR_CODE(result) == TSS_E_KEY_ALREADY_REGISTERED) {
		result = Tspi_Context_UnregisterKey(hContext,
						    TSS_PS_TYPE_SYSTEM,
						    key_uuid, &hKey);
		if (result != TSS_SUCCESS) {
			print_error("Tspi_Context_UnregisterKey", result);
			print_error_exit(nameOfFunction, err_string(result));
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
		goto register_key;
	} else if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_RegisterKey", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		//Close the object
	result = Tspi_Context_CloseObject(hContext, hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CloseObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	hKey = 0;
		//Load the key by UUID from PS
	result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
					    key_uuid, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		//Call GetAttribData, uuid should be equal to key_uuid
	result = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_KEY_UUID, 0,
				    &uuidLength, &uuid);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetAttribData", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Check length and data
	if (uuidLength != sizeof(TSS_UUID)) {
		print_verifyerr("uuid length from Tspi_GetAttribData", 0, 1);
		print_error("uuid length from Tspi_GetAttribData", TSS_E_FAIL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	if ((rc = memcmp(uuid, &key_uuid, uuidLength))) {
		print_verifyerr("key's uuid from Tspi_GetAttribData", 0, rc);
		print_hex((BYTE *)&key_uuid, sizeof(TSS_UUID));
		print_error("key's uuid from Tspi_GetAttribData", TSS_E_FAIL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	print_success(nameOfFunction, result);
	print_end_test(nameOfFunction);
	Tspi_Context_Close(hContext);
	exit(0);
}
int
main_v1_2( char version )
{
	char			*function = "Tspi_TPM_SetOperatorAuth01";
	TSS_HCONTEXT		hContext;
	TSS_HTPM		hTPM;
	TSS_HKEY		hSRK;
	TSS_HPOLICY		hOperatorPolicy;
	TSS_RESULT		result;

	print_begin_test( function );

	result = connect_load_all(&hContext, &hSRK, &hTPM);
	if ( result != TSS_SUCCESS )
	{
		print_error( "connect_load_all", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY,
			TSS_POLICY_OPERATOR, &hOperatorPolicy);
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Policy_SetSecret( hOperatorPolicy, TESTSUITE_OPERATOR_SECRET_MODE,
					TESTSUITE_OPERATOR_SECRET_LEN, TESTSUITE_OPERATOR_SECRET);
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_TPM_SetOperatorAuth( hTPM, hOperatorPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_TPM_SetOperatorAuth", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	else
	{
		print_success( function, result );
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
}
int
main_v1_1( void )
{
	char		*function = "key_auth_check01";
	TSS_HCONTEXT	hContext;
	UINT32		exitCode;
	TSS_HKEY	hSRK;
	TSS_HKEY	hMSigningKey;
	TSS_UUID	migratableSignUUID = {0x1, 0x55, 0x67, 0x8, 0x5, { 6, 7, 8, 9, 10, 2 } };
	TSS_HHASH	hHash;
	BYTE		*prgbSignature;
	UINT32		pulSignatureLength;
	TSS_RESULT	result;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, get_server(GLOBALSERVER) );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						SRK_UUID, &hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject( hSRK, TSS_POLICY_USAGE,
					&srkUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Set Secret
	result = Tspi_Policy_SetSecret( srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
				TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#endif

		//Create Signing Key
	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_RSAKEY,
						TSS_KEY_SIZE_2048 |
						TSS_KEY_TYPE_SIGNING |
						TSS_KEY_AUTHORIZATION,
						&hMSigningKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (signing key)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// get signing key's policy
	result = Tspi_GetPolicyObject( hMSigningKey, TSS_POLICY_USAGE,
					&keyUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Set Secret
	result = Tspi_Policy_SetSecret( keyUsagePolicy, TESTSUITE_KEY_SECRET_MODE,
					TESTSUITE_KEY_SECRET_LEN, TESTSUITE_KEY_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_CreateKey( hMSigningKey, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (signing key)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_LoadKey( hMSigningKey, hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKey (hMSigningKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// create hash
	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_HASH,
						TSS_HASH_SHA1, &hHash );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hash)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Hash_UpdateHashValue( hHash, 20, "Je pense, danc je s" );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Hash_SetHashValue", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}


	result = Tspi_Hash_Sign( hHash, hMSigningKey, &pulSignatureLength,
					&prgbSignature );
	if ( result != TSS_SUCCESS )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			exitCode = 1;
		}
		else
		{
			print_error_nonapi( function, result );
			exitCode = 1;
		}
	}
	else
	{
		result = Tspi_Context_RegisterKey(hContext, hMSigningKey, TSS_PS_TYPE_SYSTEM,
						migratableSignUUID, TSS_PS_TYPE_SYSTEM, SRK_UUID);
		if (result != TSS_SUCCESS)
		{
			print_error( "Tspi_Context_RegisterKey", result );
			print_error_exit( function, err_string(result) );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit( result );
		}
		print_success( function, result );
		exitCode = 0;
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( exitCode );
}
int
main_v1_1( void )
{
	char			*function = "Tspi_Policy_FlushSecret02";
	TSS_RESULT		result;
	TSS_HPOLICY		hPolicy;
	TSS_HPOLICY		whPolicy = -1;
	TSS_HKEY		hSRK;
	TSS_HCONTEXT		hContext;
	UINT32			exitCode = 0;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, get_server(GLOBALSERVER) );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// get policy
	result = Tspi_Context_GetDefaultPolicy ( hContext, &hPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_GetDefaultPolicy", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						SRK_UUID, &hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Set Secret
	result = Tspi_Policy_SetSecret( hPolicy, TESTSUITE_NEW_SECRET_MODE,
					TESTSUITE_NEW_SECRET_LEN, TESTSUITE_NEW_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Flush Secret
	result = Tspi_Policy_FlushSecret( whPolicy );
	if ( TSS_ERROR_CODE(result) != TSS_E_INVALID_HANDLE )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
		}
		else
		{
			print_error_nonapi( function, result );
		}
		exitCode = result;
	}
	else
	{
		print_success( function, result );
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( exitCode );
}
int
main_v1_1( void )
{
	char			*function = "Tspi_PolicyChecking01";
	char			*function1 = "Tspi_GetAttribUint32";
	TSS_RESULT		result;
	TSS_HPOLICY		hPolicy;
	TSS_HCONTEXT		hContext;
	TSS_HOBJECT		hObject;
	UINT32			ES, exitCode1;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// get policy
	result = Tspi_Context_GetDefaultPolicy ( hContext, &hPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_GetDefaultPolicy", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

/* ################# CHECKING DEFAULTS ################### */

	result = Tspi_GetAttribUint32( hPolicy,
					TSS_TSPATTRIB_POLICY_CALLBACK_HMAC,
					0, &ES );
	if ( result != TSS_SUCCESS )
	{
		print_error( function1, result );
		exitCode1 = 1;
	}
	else
	{
		if( ES != 0 )
		{
			print_error( function1, result );
			exitCode1 = 1;
		}
		else
		{
			print_success( function1, result);
		}
	}
	fprintf( stderr, "\t\tAddress of callback hmac: %x\n", ES );

	result = Tspi_GetAttribUint32( hPolicy,
					TSS_TSPATTRIB_POLICY_CALLBACK_XOR_ENC,
					0, &ES );
	if ( result != TSS_SUCCESS )
	{
		print_error( function1, result );
		exitCode1 = 1;
	}
	else
	{
		if( ES != 0 )
		{
			print_error( function1, result );
			exitCode1 = 1;
		}
		else
		{
			print_success( function1, result);
		}
	}
	fprintf( stderr, "\t\tAddress of callback xor enc: %x\n", ES );

	result = Tspi_GetAttribUint32( hPolicy,
				TSS_TSPATTRIB_POLICY_CALLBACK_TAKEOWNERSHIP,
				0, &ES );
	if ( result != TSS_SUCCESS )
	{
		print_error( function1, result );
		exitCode1 = 1;
	}
	else
	{
		if( ES != 0 )
		{
			print_error( function1, result );
			exitCode1 = 1;
		}
		else
		{
			print_success( function1, result);
			exitCode1 = 0;
		}
	}
	fprintf( stderr, "\t\tAddress of callback take ownership: %x\n", ES );

	result = Tspi_GetAttribUint32( hPolicy,
				TSS_TSPATTRIB_POLICY_CALLBACK_CHANGEAUTHASYM,
				0, &ES );
	if ( result != TSS_SUCCESS )
	{
		print_error( function1, result );
		exitCode1 = 1;
	}
	else
	{
		if( ES != 0 )
		{
			print_error( function1, result );
			exitCode1 = 1;
		}
		else
		{
			print_success( function1, result);
		}
	}
	fprintf( stderr, "\t\tAddress of callback change auth asym: %x\n", ES );

	result = Tspi_GetAttribUint32( hPolicy,
			TSS_TSPATTRIB_POLICY_SECRET_LIFETIME,
			TSS_TSPATTRIB_POLICYSECRET_LIFETIME_ALWAYS, &ES );
	if ( result != TSS_SUCCESS )
	{
		print_error( function1, result );
		exitCode1 = 1;
	}
	else
	{
		/* flag not set by default */
		if( ES == FALSE )
		{
			print_error( function1, result );
			exitCode1 = 1;
		}
		else
		{
			print_success( function1, result);
		}
	}
	fprintf( stderr, "\t\tFlag set in policy object?: %x\n", ES );

	if( exitCode1 == 0 )
		print_success( function, result );
	else
		print_error( function, result );


	result = Tspi_GetAttribUint32( hPolicy,
			TSS_TSPATTRIB_POLICY_SECRET_LIFETIME,
			TSS_TSPATTRIB_POLICYSECRET_LIFETIME_COUNTER, &ES );
	/* TSS_TSPATTRIB_POLICY_SECRET_LIFETIME Not set by default so 
	   TSS_TSPATTRIB_POLICYSECRET_LIFETIME_COUNTER should return error */
	if ( result == TSS_SUCCESS )
	{
		print_error( function1, result );
		fprintf( stderr, "\t\tCounter value: %x\n", ES );
		exitCode1 = 1;
	}
	else
	{
		print_success( function1, result); 
	}

	result = Tspi_GetAttribUint32( hPolicy,
			TSS_TSPATTRIB_POLICY_SECRET_LIFETIME,
			TSS_TSPATTRIB_POLICYSECRET_LIFETIME_TIMER, &ES );
	/* TSS_TSPATTRIB_POLICY_SECRET_LIFETIME Not set by default so 
	   TSS_TSPATTRIB_POLICYSECRET_LIFETIME_TIMER should return error */
	if ( result == TSS_SUCCESS )
	{
		print_error( function1, result );
		fprintf( stderr, "\t\tTime value in seconds: %x\n", ES );
		exitCode1 = 1;
	}
	else
	{
		print_success( function1, result );
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( exitCode1 );
}
int
main_v1_1( void )
{
	char		*function = "key_auth_chain02";
	TSS_HCONTEXT    hContext;
	TSS_HKEY        hSRK;
	TSS_HKEY	hKey0, hKey1, hKey2;
	TSS_RESULT	result;
	TSS_HENCDATA	hEncData;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy;
	int temp;

	print_begin_test( function );

	srand(time(0));

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		goto done;
	}

		//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						uuid_2, &hKey2 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		goto done;
	}

	result = Tspi_Context_CreateObject( hContext,
						TSS_OBJECT_TYPE_ENCDATA,
						TSS_ENCDATA_BIND, &hEncData );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hEncData)", result );
		goto done;
	}

		// Data Bind
	result = Tspi_Data_Bind( hEncData, hKey2, ulDataLength, rgbDataToBind );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Data_Bind", result );
		goto done;
	}


	result = Tspi_Data_Unbind( hEncData, hKey2, &pulDataLength, &prgbDataToUnBind );
	if ( result != TSS_SUCCESS )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			goto done;
		}
		else
		{
			print_error_nonapi( function, result );
			goto done;
		}
	}
	else
	{
		if ((pulDataLength == ulDataLength) &&
			!memcmp(prgbDataToUnBind, rgbDataToBind, pulDataLength))
			print_success( function, result );
		else{
			printf("Data doesn't match");
			print_error("Data doestn't match: Tspi_Data_Unbind", TSS_E_FAIL);
		}
		
	}


#if 0
#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject( hSRK, TSS_POLICY_USAGE,
					&srkUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		goto done;
	}

		//Set Secret
	result = Tspi_Policy_SetSecret( srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
				TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		goto done;
	}
#endif

	/* ######## Start Key 0 ######## */
	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_RSAKEY,
						TSS_KEY_SIZE_2048 |
						TSS_KEY_TYPE_STORAGE |
						TSS_KEY_NO_AUTHORIZATION,
						&hKey0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (key 0)", result );
		goto done;
	}

	result = Tspi_Key_CreateKey( hKey0, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (key 0)", result );
		goto done;
	}

	result = Tspi_Context_RegisterKey( hContext, hKey0,
						TSS_PS_TYPE_SYSTEM,
						uuid_0,
						TSS_PS_TYPE_SYSTEM,
						SRK_UUID );
	if ( (result != TSS_SUCCESS) )
	{
		print_error( "Tspi_Context_RegisterKey (uuid_0)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}
	fprintf( stderr, "\t\tKey 0 Finished\n" );
	/* ######## End Key 0 ######## */

	/* ######## Start Key 1 ######## */
	result = Tspi_Context_CreateObject( hContext,
						TSS_OBJECT_TYPE_RSAKEY,
						TSS_KEY_AUTHORIZATION,
						&hKey1 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (key 1)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}

	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
                                                uuid_0, &hKey0 );
        if ( result != TSS_SUCCESS )
        {
                print_error( "Tspi_Context_LoadKeyByUUID (hKey0)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
        }

	result = Tspi_GetPolicyObject( hKey1, TSS_POLICY_USAGE,
					&keyUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}

		//Set Secret
	result = Tspi_Policy_SetSecret( keyUsagePolicy, TESTSUITE_KEY_SECRET_MODE,
				TESTSUITE_KEY_SECRET_LEN, TESTSUITE_KEY_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}

	result = Tspi_Key_CreateKey( hKey1, hKey0, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (key 1)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}

	result = Tspi_Context_RegisterKey( hContext, hKey1,
						TSS_PS_TYPE_SYSTEM,
						uuid_1,
						TSS_PS_TYPE_SYSTEM,
						uuid_0 );
	if ( (result != TSS_SUCCESS) )
	{
		print_error( "Tspi_Context_RegisterKey (uuid_1)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		goto done;
	}
	fprintf( stderr, "\t\tKey 1 Finished\n" );
	/* ######## End Key 1 ######## */

	/* ######## Start Key 2 ######## */
	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_RSAKEY,
						TSS_KEY_NO_AUTHORIZATION,
						&hKey2 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (key 2)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_1, &hKey1);
		goto done;
	}

	result = Tspi_Key_LoadKey( hKey1, hKey0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hKey1)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_1, &hKey1);
		goto done;
	}

	result = Tspi_Key_CreateKey( hKey2, hKey1, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (key 2)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_1, &hKey1);
		goto done;
	}

	result = Tspi_Context_RegisterKey( hContext, hKey2,
						TSS_PS_TYPE_SYSTEM,
						uuid_2,
						TSS_PS_TYPE_SYSTEM,
						uuid_1 );
	if ( (result != TSS_SUCCESS) )
	{
		print_error( "Tspi_Context_RegisterKey (uuid_2)", result );
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_0, &hKey0);
		Tspi_Context_UnregisterKey(hContext, TSS_PS_TYPE_SYSTEM, uuid_1, &hKey1);
		goto done;
	}
	fprintf( stderr, "\t\tKey 2 Finished\n" );
	/* ######## End Key 2 ######## */
#endif

	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( result );

done:
	print_error_exit( function, err_string(result) );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( result );
}
main_v1_2(char version){

	char		*nameOfFunction = "Load KEY";
	TSS_HCONTEXT	hContext;
	TSS_HTPM	hTPM;
	TSS_FLAG	initFlags;
	TSS_HKEY 	hKey1, hKey2, hKey3, hKey4;
	TSS_HKEY	hSRK;
	TSS_RESULT	result;
	TSS_UUID	uuid;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy, keyMigPolicy, hKeyPolicy;

	print_begin_test(nameOfFunction);
	printf("KEY structure:\n%s\n", key_structure);

	//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
	//Connect Context
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		
	//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID(hContext,
				TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	printf("Load SRK sucessed!\n");
#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set Secret
	result = Tspi_Policy_SetSecret(srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
			TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
#endif
	// -------------------------------- load k1 ---------------------------------
	printf("Loading K1...\n");
	result = Tspi_Context_GetKeyByUUID(hContext,
					    TSS_PS_TYPE_SYSTEM, UUID_K1,
					    &hKey1);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	
	//验证使用测略
	result = Tspi_Key_LoadKey(hKey1, hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	printf("Load UserK1 sucessed!\n");

	// -------------------------------- load k2 ---------------------------------
	//Load Key By UUID   --K2
	printf("Loading K2...\n");
	result = Tspi_Context_GetKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						UUID_K2,
						&hKey2 );
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//验证使用测略	
	result = set_popup_secret(hContext, hKey1, TSS_POLICY_USAGE, "Input K1's pin", 0);
	if (TSS_SUCCESS != result) {
		print_error("set_popup_secret", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Key_LoadKey(hKey2, hKey1);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	printf("Load UserK2 sucessed!\n");

	// 给SRK授权
	#ifndef TESTSUITE_NOAUTH_SRK
	//Get Policy Object
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Set Secret
	result = Tspi_Policy_SetSecret(srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
			TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	#endif

	// -------------------------------- load k3 ---------------------------------
	printf("Loading K3...\n");
	result = Tspi_Context_LoadKeyByUUID(hContext,
					    TSS_PS_TYPE_SYSTEM, UUID_K3,
					    &hKey3);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_GetKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	
	printf("Load UserK3 sucessed!\n");

	// -------------------------------- load k4 ---------------------------------
	printf("Loading K4...\n");
	result = Tspi_Context_GetKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						UUID_K4,
						&hKey4 );
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//验证使用测略
	result = set_popup_secret(hContext, hKey3, TSS_POLICY_USAGE, "Input K3's pin", 0);
	if (TSS_SUCCESS != result) {
		print_error("set_popup_secret", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Key_LoadKey(hKey4, hKey3);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	printf("Load UserK4 sucessed!\n");
	
	print_success(nameOfFunction, result);
	print_end_test(nameOfFunction);
	Tspi_Context_FreeMemory(hContext, NULL);
	Tspi_Context_Close(hContext);
	exit(0);	
}
main_v1_1(void)
{

	char *nameOfFunction = "Tspi_Key_CreateKey03";
	TSS_HCONTEXT hContext;
	TSS_HTPM hTPM;
	TSS_FLAG initFlags;
	TSS_HKEY hKey;
	TSS_HKEY hSRK;
	TSS_RESULT result;
	TSS_UUID uuid;
	BYTE *randomData;
	TSS_HPOLICY srkUsagePolicy, keyUsagePolicy;
	initFlags = TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048 |
	    TSS_KEY_VOLATILE | TSS_KEY_NO_AUTHORIZATION |
	    TSS_KEY_NOT_MIGRATABLE;
	BYTE well_known_secret[20] = TSS_WELL_KNOWN_SECRET;

	print_begin_test(nameOfFunction);

	//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
	//Connect Context
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Get TPM Object
	result = Tspi_Context_GetTpmObject(hContext, &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_GetTpmObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Create Object
	result =
	    Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
				      initFlags, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID(hContext,
					    TSS_PS_TYPE_SYSTEM, SRK_UUID,
					    &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#ifndef TESTSUITE_NOAUTH_SRK
	//Get Policy Object
	result =
	    Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Set Secret
	result =
	    Tspi_Policy_SetSecret(srkUsagePolicy,
				  TESTSUITE_SRK_SECRET_MODE,
				  TESTSUITE_SRK_SECRET_LEN,
				  TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#endif
	//Get Policy Object
	result =
	    Tspi_GetPolicyObject(hKey, TSS_POLICY_USAGE, &keyUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Set Secret
	result = Tspi_Policy_SetSecret(keyUsagePolicy,
				       TSS_SECRET_MODE_SHA1, 20,
				       well_known_secret);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
	//Create Key
	result = Tspi_Key_CreateKey(-1, hSRK, 0);
	if (TSS_ERROR_CODE(result) != TSS_E_INVALID_HANDLE) {
		if (!checkNonAPI(result)) {
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		} else {
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
	} else {
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(0);
	}
}
main_v1_1(void){
	char		*nameOfFunction = "dump_event_log";
	TSS_HCONTEXT	hContext;
	TSS_RESULT	result;
	TSS_HTPM	hTPM;
	UINT32		ulEventNumber, i, j;
	TSS_PCR_EVENT*	PCREvents;

	print_begin_test(nameOfFunction);

		//Create Result
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create ", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Connect Result
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect ", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Get TPM Object
	result = Tspi_Context_GetTpmObject(hContext,  &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_GetTpmObject ", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Get Event
	result = Tspi_TPM_GetEventLog(hTPM, &ulEventNumber,
				&PCREvents);
	if (result != TSS_SUCCESS) {
		if(!checkNonAPI(result)){
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
		}
		else{
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
		}
	}
	printf("There are %d events:\n", ulEventNumber);
	printf("PCR SHA1\t\t\t\t     Type Name\n");

	for (i = 0; i < ulEventNumber; i++) {
		printf("%*d ", 3, PCREvents[i].ulPcrIndex);
		for (j=0; j<PCREvents[i].ulPcrValueLength; j++)
			printf("%02x", PCREvents[i].rgbPcrValue[j] & 0xff);
		if (j < 20)
			while (j < 20) {
				printf(" ");
				j++;
			}

		printf(" %*d ", 4, PCREvents[i].eventType);
		if (PCREvents[i].ulEventLength == 0)
			printf("NONE\n");
		else
			for (j=0; j<PCREvents[i].ulEventLength; j++)
				printf("%c", PCREvents[i].rgbEvent[j] & 0xff);

			printf("\n");
	}
}
int
main_v1_1( void )
{
	char			*function = "Tspi_PolicyPopup02";
	char			*hashData = "09876543210987654321";
	TSS_RESULT		result;
	TSS_HKEY		hSRK, hKey;
	TSS_UUID		SRKUUID	= {0,0,0,0,0,0,0,0,0,0,1};
	TSS_HPOLICY		hPolicy;
	TSS_HCONTEXT		hContext;
	TSS_HHASH		hHash;
	BYTE			*popupMsg = NULL;
	BYTE			*msg = "Enter a password for a new key:";
	UINT32			msg_len;
	TSS_HPOLICY		srkUsagePolicy;
	TSS_FLAG		initFlags = TSS_KEY_TYPE_SIGNING |
						TSS_KEY_SIZE_2048 |
						TSS_KEY_VOLATILE |
						TSS_KEY_AUTHORIZATION |
						TSS_KEY_NOT_MIGRATABLE;
	UINT32			ulSignatureLen;
	BYTE			*signature;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						SRK_UUID, &hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

#ifndef TESTSUITE_NOAUTH_SRK
	result = Tspi_GetPolicyObject( hSRK, TSS_POLICY_USAGE,
					&srkUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Policy_SetSecret( srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
					TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#endif

	result = Tspi_Context_CreateObject ( hContext,
						TSS_OBJECT_TYPE_RSAKEY,
						initFlags, &hKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_GetPolicyObject ( hKey, TSS_POLICY_USAGE, &hPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	popupMsg = char_to_unicode(msg, &msg_len);
	result = Tspi_SetAttribData( hPolicy,
					TSS_TSPATTRIB_POLICY_POPUPSTRING,
					0, msg_len, popupMsg );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_SetAttribData", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	free(popupMsg);

	result = Tspi_Policy_SetSecret( hPolicy,
					TSS_SECRET_MODE_POPUP,
					0, NULL );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_CreateKey( hKey, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_LoadKey( hKey, hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_LaodKey", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#if 0
	result = Tspi_Policy_FlushSecret( hPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	popupMsg = char_to_unicode("Re-enter the new key's password:"******"Tspi_SetAttribData", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	free(popupMsg);

	result = Tspi_Policy_SetSecret( hPolicy,
					TSS_SECRET_MODE_POPUP,
					0, NULL );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#endif
	/* now sign some data to test the key's auth data */
	result = Tspi_Context_CreateObject ( hContext,
						TSS_OBJECT_TYPE_HASH,
						TSS_HASH_SHA1, &hHash );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}


	result = Tspi_Hash_SetHashValue ( hHash, strlen(hashData), hashData );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Hash_SetHashValue", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Hash_Sign ( hHash, hKey, &ulSignatureLen, &signature );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Hash_Sign", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	print_success( function, result );
	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( 0 );
}
main_v1_1(void){

	char		*nameOfFunction = "Tspi_Key_CreateMigrationBlob03";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK;
	TSS_HKEY	hKey;
	UINT32		TargetPubKeyLength;
	BYTE		*TargetPublicKeyData;
	BYTE		*MigTicket;
	UINT32		TicketLength;
	BYTE		*randomData;
	UINT32		randomLength;
	UINT32		migBlobLength;
	BYTE		*migBlob;
	UINT32		blobLength;
	TSS_RESULT	result;
	TSS_HTPM	hTPM;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy, keyMigPolicy,
			tpmUsagePolicy;

	print_begin_test(nameOfFunction);

			//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create ", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Connect Context
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Get TPM Object
	result = Tspi_Context_GetTpmObject(hContext, &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_GetTpmObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID(hContext,
			TSS_PS_TYPE_SYSTEM,
			SRK_UUID, &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID for hSRK", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set Secret
	result = Tspi_Policy_SetSecret(srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
					TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
#endif
		//Get Policy Object
	result = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &tpmUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set Secret
	result = Tspi_Policy_SetSecret(tpmUsagePolicy, TESTSUITE_OWNER_SECRET_MODE,
					TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Create Object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
				TSS_KEY_TYPE_BIND | TSS_KEY_SIZE_2048 |
				TSS_KEY_NO_AUTHORIZATION | TSS_KEY_NOT_MIGRATABLE,
				&hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Create Signing Key
	result = Tspi_Key_CreateKey(hKey, hSRK, 0);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Key_CreateKey", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		Tspi_Context_CloseObject(hContext, hKey);
		exit(result);
	}
		//Authorize Migration Ticket
	result = Tspi_TPM_AuthorizeMigrationTicket(hTPM, hKey,
			TSS_MS_REWRAP, &TicketLength, &MigTicket);
	if (result != TSS_SUCCESS)
	{
		print_error("Tpsi_TPM_AuthorizeMigrationTicket ", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		Tspi_Context_CloseObject(hContext, hKey);
		exit(result);
	}
		//Create Migration Blob
	result = Tspi_Key_CreateMigrationBlob(-1, hSRK,
				TicketLength, MigTicket, &randomLength,
				&randomData, &migBlobLength, &migBlob);
	if (TSS_ERROR_CODE(result) != TSS_E_INVALID_HANDLE) {
		if(!checkNonAPI(result)){
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
		else{
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
	}
	else{
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(0);
	}
}
main_v1_1(void){

	char		*nameOfFunction = "Tspi_Key_LoadKey01";
	TSS_HCONTEXT	hContext;
	TSS_HTPM	hTPM;
	TSS_FLAG	initFlags;
	TSS_HKEY	hKey;
	TSS_HKEY	hSRK;
	TSS_RESULT	result;
	TSS_UUID	uuid;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy, keyMigPolicy;
	initFlags	= TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048  |
			TSS_KEY_VOLATILE | TSS_KEY_NO_AUTHORIZATION |
			TSS_KEY_NOT_MIGRATABLE;

	print_begin_test(nameOfFunction);

		//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Connect Context
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Create Object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
				initFlags, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID(hContext,
				TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set Secret
	result = Tspi_Policy_SetSecret(srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
			TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#endif
		//Create Key
	result = Tspi_Key_CreateKey(hKey, hSRK, 0);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_Create Key", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Load Key (hKey)
	result = Tspi_Key_LoadKey(hKey, hSRK);
	if (result != TSS_SUCCESS){
		if(!checkNonAPI(result)){
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
		else{
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
	}
	else{
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(0);
	}
}
int
main_v1_1( void )
{
	char		*function = "Tspi_PcrComposite_SetPcrValue02";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK;
	TSS_HPCRS	hPcrComposite;
	TSS_HPCRS	whPcrComposite = -1;
	BYTE		rgbPcrValue[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	TSS_RESULT	result;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_PCRS,
					0, &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( whPcrComposite, 8, 20,
						rgbPcrValue );
	if ( TSS_ERROR_CODE(result) != TSS_E_INVALID_HANDLE )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
		print_end_test( function );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( 0 );
	}
}
int
main_v1_2(char version)
{
        char         *nameOfFunction    = "Tspi_Nv_DefineSpace12";
	
	TSS_HCONTEXT hContext           = NULL_HCONTEXT;
	TSS_HNVSTORE hNVStore           = 0;//NULL_HNVSTORE
	TSS_HPOLICY  hPolicy            = NULL_HPOLICY;
	TSS_HTPM     hTPM               = NULL_HTPM;
	TSS_RESULT   result;

	print_begin_test(nameOfFunction);

		//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create ", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Connect Context
	result = Tspi_Context_Connect(hContext,NULL);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);                
		Tspi_Context_Close(hContext);
		exit(result);
	}

	    	/* Create TPM NV object */
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_NV, 0,&hNVStore);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/* Set the index to be defined. */
	result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_INDEX, 0,0x0000d);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_SetAttribUint32 for setting NV index", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}


	/* Set the permission for the index. */
	result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_PERMISSIONS, 0, 0x2000);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_SetAttribUint32 for setting permission", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);	
       }


	/* Set the data size to be defined. */
	result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_DATASIZE, 0, 0xa);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_SetAttribUint32 for setting data size", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
     	}

      /*Define NV space*/
	result = Tspi_NV_DefineSpace(hNVStore, 0, 0);
      
#ifdef NV_LOCKED	
         if (TSS_ERROR_CODE(result)== TPM_E_OWNER_SET||TSS_ERROR_CODE(result)==TPM_E_BAD_PRESENCE )
        {              
        	print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(0);
       }
       else{
		print_error("Tspi_NV_DefineSpace", result);
	  	print_end_test(nameOfFunction);
                if ( result == TSS_SUCCESS ) {
#ifdef CLEAR_TEST_INDEX
			Tspi_Context_GetTpmObject(hContext, &hTPM);
			Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hPolicy);
			Tspi_Policy_SetSecret(hPolicy, TESTSUITE_OWNER_SECRET_MODE,
							TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);
			Tspi_NV_ReleaseSpace(hNVStore);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_Close(hContext);
#endif
                	exit(-1);
		}
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
     	}

#else
       if (result== TSS_SUCCESS)
       {
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);

	#ifdef CLEAR_TEST_INDEX
		Tspi_Context_GetTpmObject(hContext, &hTPM);
		Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hPolicy);
		Tspi_Policy_SetSecret(hPolicy, TESTSUITE_OWNER_SECRET_MODE,
					TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);
		Tspi_NV_ReleaseSpace(hNVStore);
	#endif

		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(0);
       }
       else{
		print_error("Tspi_NV_DefineSpace", result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
     	}
#endif
}
int
main_v1_1( void )
{
	char		*function = "Tspi_PcrComposite_SetPcrValue03";
	TSS_HCONTEXT	hContext;
	TSS_HPCRS	hPcrComposite;
	BYTE		rgbPcrValue[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	TSS_RESULT	result;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_PCRS,
					0, &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 5, 0,
							rgbPcrValue );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 5, 20,
							NULL );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( (TSS_ERROR_CODE(result) == TSS_E_INVALID_HANDLE) ||
				(TSS_ERROR_CODE(result) == TSS_E_INTERNAL_ERROR) ||
				(result == TSS_SUCCESS) ||
				(TSS_ERROR_CODE(result) == TSS_E_FAIL) ||
				(TSS_ERROR_CODE(result) == TSS_E_NOTIMPL) ||
				(TSS_ERROR_CODE(result) == TSS_E_PS_KEY_NOTFOUND) ||
				(TSS_ERROR_CODE(result) == TSS_E_KEY_ALREADY_REGISTERED) ||
				(TSS_ERROR_CODE(result) == TSS_E_CANCELED) ||
				(TSS_ERROR_CODE(result) == TSS_E_TIMEOUT) ||
				(TSS_ERROR_CODE(result) == TSS_E_OUTOFMEMORY) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNEXPECTED) ||
				(TSS_ERROR_CODE(result) == TSS_E_COMM_FAILURE) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNSUPPORTED_FEATURE) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 8, 20,
							NULL );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( (TSS_ERROR_CODE(result) == TSS_E_INVALID_HANDLE) ||
				(TSS_ERROR_CODE(result) == TSS_E_INTERNAL_ERROR) ||
				(result == TSS_SUCCESS) ||
				(TSS_ERROR_CODE(result) == TSS_E_FAIL) ||
				(TSS_ERROR_CODE(result) == TSS_E_NOTIMPL) ||
				(TSS_ERROR_CODE(result) == TSS_E_PS_KEY_NOTFOUND) ||
				(TSS_ERROR_CODE(result) == TSS_E_KEY_ALREADY_REGISTERED) ||
				(TSS_ERROR_CODE(result) == TSS_E_CANCELED) ||
				(TSS_ERROR_CODE(result) == TSS_E_TIMEOUT) ||
				(TSS_ERROR_CODE(result) == TSS_E_OUTOFMEMORY) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNEXPECTED) ||
				(TSS_ERROR_CODE(result) == TSS_E_COMM_FAILURE) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNSUPPORTED_FEATURE) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
		print_end_test( function );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( 0 );
	}

}
main_v1_2(char version)
{
	TSS_HCONTEXT hContext;
	TSS_HKEY hSRK;
	TSS_HTPM hTPM;
	TSS_HPOLICY hTpmUsagePolicy;
	TSS_FLAG initFlags;
	TSS_HKEY hSrcKey;
	TSS_HKEY hDestKey;
	TSS_HKEY hMaKey[MA_KEY_COUNT];
	TSS_HKEY hCmkKey;
	TSS_HKEY hNewKey;
	TSS_HMIGDATA hMigData;
	TSS_HHASH hHash;
	UINT32 blobSize;
	BYTE *blob;
	UINT32 randomSize;
	BYTE *random;
	int i;
	TSS_RESULT result;


	print_begin_test(nameOfFunction);

	result = connect_load_all(&hContext, &hSRK, &hTPM);
	if ( result != TSS_SUCCESS )
	{
		print_error( "connect_load_all", result );
		exit(result);
	}

	//Get TPM Policy Object
	result = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hTpmUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Set Secret
	result = Tspi_Policy_SetSecret(hTpmUsagePolicy, TESTSUITE_OWNER_SECRET_MODE,
			TESTSUITE_OWNER_SECRET_LEN, TESTSUITE_OWNER_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/*****  Create Overall Source Parent key *****/
	initFlags = TSS_KEY_STRUCT_KEY12 | TSS_KEY_TYPE_STORAGE | TSS_KEY_SIZE_2048 |
			TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION;
	tc_create_object(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hSrcKey);
	tc_create_key(hContext, hSrcKey, hSRK, initFlags);
	tc_load_key(hContext, hSrcKey, hSRK);

	/*****  Create Overall Destination Parent key *****/
	initFlags = TSS_KEY_STRUCT_KEY12 | TSS_KEY_TYPE_STORAGE | TSS_KEY_SIZE_2048 |
			TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION;
	tc_create_object(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hDestKey);
	tc_create_key(hContext, hDestKey, hSRK, initFlags);
	tc_load_key(hContext, hDestKey, hSRK);

	/*****  Create MAs and MSA list *****/
	//Create MigData Object
	tc_create_object(hContext, TSS_OBJECT_TYPE_MIGDATA, 0, &hMigData);

	for (i = 0; i < MA_KEY_COUNT; i++) {
		//Create Key Object
		initFlags = TSS_KEY_STRUCT_KEY12 | TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048 |
				TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION;
		tc_create_object(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hMaKey[i]);
		tc_create_key(hContext, hMaKey[i], hSrcKey, initFlags);

		//Get PubKey Blob
		tc_get_attribdata(hContext, hMaKey[i], TSS_TSPATTRIB_KEY_BLOB,
			TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blobSize, &blob);

		//Add PubKey Blob to the MSA list
		tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIGRATIONBLOB,
			TSS_MIGATTRIB_MIG_MSALIST_PUBKEY_BLOB, blobSize, blob);
	}

	//Grant Owner Approval of MAs
	result = Tspi_TPM_CMKApproveMA(hTPM, hMigData);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_CMKApproveMA", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/*****  Create a CMK  ****/
	initFlags = TSS_KEY_STRUCT_KEY12 | TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048 |
			TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION |
			TSS_KEY_MIGRATABLE | TSS_KEY_CERTIFIED_MIGRATABLE;
	tc_create_object(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hCmkKey);

	//Get and Assign MA/MSA information
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_AUTHORITY_DATA,
		TSS_MIGATTRIB_AUTHORITY_DIGEST, &blobSize, &blob);
	tc_set_attribdata(hContext, hCmkKey, TSS_TSPATTRIB_KEY_CMKINFO,
		TSS_TSPATTRIB_KEYINFO_CMK_MA_DIGEST, blobSize, blob);
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_AUTHORITY_DATA,
		TSS_MIGATTRIB_AUTHORITY_APPROVAL_HMAC, &blobSize, &blob);
	tc_set_attribdata(hContext, hCmkKey, TSS_TSPATTRIB_KEY_CMKINFO,
		TSS_TSPATTRIB_KEYINFO_CMK_MA_APPROVAL, blobSize, blob);

	tc_create_key(hContext, hCmkKey, hSrcKey, initFlags);

	/*****  Authorize migration to the Dest key *****/
	//Authorize Migration Ticket
	result = Tspi_TPM_AuthorizeMigrationTicket(hTPM, hDestKey, TSS_MS_RESTRICT_APPROVE_DOUBLE,
			&blobSize, &blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_AuthorizeMigrationTicket", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Save Ticket
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIGRATIONTICKET, 0, blobSize, blob);

	/*****  Sign the migration ticket *****/
	//Get PubKey Blob of CMK
	tc_get_attribdata(hContext, hCmkKey, TSS_TSPATTRIB_KEY_BLOB,
		TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blobSize, &blob);
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIGRATIONBLOB,
		TSS_MIGATTRIB_MIG_SOURCE_PUBKEY_BLOB, blobSize, blob);

	//Get PubKey Blob of destination CMK parent
	tc_get_attribdata(hContext, hDestKey, TSS_TSPATTRIB_KEY_BLOB,
		TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blobSize, &blob);
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIGRATIONBLOB,
		TSS_MIGATTRIB_MIG_DESTINATION_PUBKEY_BLOB, blobSize, blob);

	//Get PubKey Blob of MA
	tc_get_attribdata(hContext, hMaKey[0], TSS_TSPATTRIB_KEY_BLOB,
		TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blobSize, &blob);
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIGRATIONBLOB,
		TSS_MIGATTRIB_MIG_AUTHORITY_PUBKEY_BLOB, blobSize, blob);

	//Get Ticket Signature Data
	tc_create_object(hContext, TSS_OBJECT_TYPE_HASH, TSS_HASH_SHA1, &hHash);
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_AUTHORITY_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_DESTINATION_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_SOURCE_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	result = Tspi_Hash_GetHashValue(hHash, &blobSize, &blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_GetHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Load Verify Key
	tc_load_key(hContext, hMaKey[0], hSrcKey);

	//Generate Ticket Signature
	result = Tspi_Hash_Sign(hHash, hMaKey[0], &blobSize, &blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_Sign", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Save Ticket Signature
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_TICKET_DATA,
		TSS_MIGATTRIB_TICKET_SIG_VALUE, blobSize, blob);

	//Create Ticket
	result = Tspi_TPM_CMKCreateTicket(hTPM, hMaKey[0], hMigData);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_CMKCreateTicket", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/*****  Create a migration blob *****/
	//Create Blob
	result = Tspi_Key_CMKCreateBlob(hCmkKey, hSrcKey, hMigData, &randomSize, &random);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_CMKCreateBlob", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/*****  Create a CMK ticket for the destination TPM *****/
	//We are using the same TPM so the next steps are not necessary, but are
	//done for procedural info
	//Get Ticket Signature Data
	tc_create_object(hContext, TSS_OBJECT_TYPE_HASH, TSS_HASH_SHA1, &hHash);
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_AUTHORITY_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_DESTINATION_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	tc_get_attribdata(hContext, hMigData, TSS_MIGATTRIB_MIG_AUTH_DATA,
		TSS_MIGATTRIB_MIG_AUTH_SOURCE_DIGEST, &blobSize, &blob);
	result = Tspi_Hash_UpdateHashValue(hHash, blobSize, blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_UpdateHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}
	result = Tspi_Hash_GetHashValue(hHash, &blobSize, &blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_GetHashValue", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Load Verify Key
	tc_load_key(hContext, hMaKey[0], hSrcKey);

	//Generate Ticket Signature
	result = Tspi_Hash_Sign(hHash, hMaKey[0], &blobSize, &blob);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Hash_Sign", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	//Save Ticket Signature
	tc_set_attribdata(hContext, hMigData, TSS_MIGATTRIB_TICKET_DATA,
		TSS_MIGATTRIB_TICKET_SIG_VALUE, blobSize, blob);

	//Create Ticket
	result = Tspi_TPM_CMKCreateTicket(hTPM, hMaKey[0], hMigData);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_CMKCreateTicket", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/***** Migrate the key *****/
	initFlags = TSS_KEY_STRUCT_KEY12 | TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048 |
			TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION |
			TSS_KEY_MIGRATABLE | TSS_KEY_CERTIFIED_MIGRATABLE;
	tc_create_object(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hNewKey);

	result = Tspi_Key_CMKConvertMigration(hNewKey, hDestKey, hMigData, randomSize, random);
	if (result != TSS_SUCCESS) {
		if (!checkNonAPI(result)) {
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_Close(hContext);
			exit(result);
		} else {
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_Close(hContext);
			exit(result);
		}
	}

	tc_load_key(hContext, hNewKey, hDestKey);

	print_success(nameOfFunction, result);
	print_end_test(nameOfFunction);
	Tspi_Context_Close(hContext);
	exit(0);
}
int
main_v1_1( void )
{
	char		*function = "Tspi_Key_GetPubKey01";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK;
	TSS_HKEY	hKey;
	TSS_UUID	SRKUUID			= {0,0,0,0,0,0,0,0,0,0,1};
	TSS_UUID	migratableSignUUID	= {1,2,3,4,5,6,7,8,9,10,2};
	TSS_UUID	uuid;
	TSS_RESULT	result;
	UINT32		ulPubKeyLength;
	BYTE		*rgbPubKey;
	TSS_HPOLICY	srkUsagePolicy;
	TSS_FLAG	initFlags = TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048  |
				TSS_KEY_VOLATILE | TSS_KEY_NO_AUTHORIZATION |
				TSS_KEY_NOT_MIGRATABLE;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, get_server(GLOBALSERVER) );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
						SRK_UUID, &hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

#ifndef TESTSUITE_NOAUTH_SRK
		//Get Policy Object
	result = Tspi_GetPolicyObject( hSRK, TSS_POLICY_USAGE,
					&srkUsagePolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Set Secret
	result = Tspi_Policy_SetSecret( srkUsagePolicy, TESTSUITE_SRK_SECRET_MODE,
				TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#endif
		//Create Signing Key
	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_RSAKEY,
						initFlags, &hKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_CreateKey( hKey, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_LoadKey( hKey, hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_LoadKey (hKey)", result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Get Pub Key
	result = Tspi_Key_GetPubKey( hKey, &ulPubKeyLength, &rgbPubKey );
	if ( result != TSS_SUCCESS )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
		}
		else
		{
			print_error_nonapi( function, result );
		}
	}
	else
	{
		result = Tspi_Context_FreeMemory(hContext, rgbPubKey);
		if (result != TSS_SUCCESS) {
			print_error("Tspi_Context_FreeMemory ", result);
			print_error_exit(function, err_string(result));
			Tspi_Context_Close(hContext);
			exit(result);
		}
		print_success( function, result );
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( result );
}
int
main_v1_2(char version)
{
	char         *nameOfFunction    = "Tspi_Nv_WriteValue04";
	 
	TSS_HCONTEXT hContext           = NULL_HCONTEXT;
	TSS_HNVSTORE hNVStore           = 0;//NULL_HNVSTORE
	TSS_HOBJECT  hPolObject         = NULL_HOBJECT;
      	BYTE         *auth              = "123456";
	UINT32       auth_length        = 6;
	BYTE         *data              = "1234567890";
	TSS_RESULT   result;


	print_begin_test(nameOfFunction);

		//Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create ", result);
		print_error_exit(nameOfFunction, err_string(result));
		exit(result);
	}
		//Connect Context
	result = Tspi_Context_Connect(hContext,NULL);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	    	/* Create TPM NV object */
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_NV, 0,&hNVStore);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		/* Create policy object for the NV object*/
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &hPolObject);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Context_CreateObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		/* Set password */
	result = Tspi_Policy_SetSecret(hPolObject, TSS_SECRET_MODE_PLAIN, auth_length, auth);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Policy_SetSecret", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		/* Set password */
	result = Tspi_Policy_AssignToObject(hPolObject, hNVStore);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_Policy_AssignToObject", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	
	/* Set the index to be defined. */
	result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_INDEX, 0,0x00050001);
	if (result != TSS_SUCCESS)
	{
		print_error("Tspi_SetAttribUint32 for setting NV index", result);
		print_error_exit(nameOfFunction, err_string(result));
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_NV_WriteValue(hNVStore, /*offset*/0,/*datalength*/10, data);  
      
#ifdef NV_LOCKED	
       if (TSS_ERROR_CODE(result)== TPM_E_BADINDEX)
       {              
        	print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(0);
       }
       else{
		print_error("Tspi_NV_WriteValue", result);
	  	print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
                if ( result == TSS_SUCCESS )
                    exit(-1);
		exit(result);
     	}

#else
       if (TSS_ERROR_CODE(result)== TPM_E_BADINDEX)
       {
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(0);
       }
       else{
		print_error("Tspi_NV_WriteValue", result);
		print_end_test(nameOfFunction);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
                if ( result == TSS_SUCCESS )
                    exit(-1);
		exit(result);
     	}
#endif
}
Beispiel #27
0
int main(int argc, char **argv){
    if(argc != 4){
        printf("usage: ./barber <number of clients> <number of waiting room seats> <number of hair cuts per client>");
        exit(EXIT_FAILURE);
    }

    number_of_clients = atoi(argv[1]);
    number_of_seats = atoi(argv[2]);
    number_of_cuts = atoi(argv[3]);
    number_of_free_seats = number_of_seats;
    number_of_out_of_salon_seats = number_of_clients;

    if( (pthread_mutex_init(&draw_out, NULL)) !=0 )
        print_error_exit(errno, "error in pthread_mutex_init");

    sem_init(&barber_ready, 0, 0);
    sem_init(&access_waiting_room_seats, 0, 1);
    sem_init(&customer_ready, 0, 0);

    int i;
    int r_id[number_of_clients];
    for(i = 0; i < number_of_clients; i++){
        r_id[i]=i;
    }

    haircut_salon= malloc(sizeof(struct bench));
    haircut_chairs = malloc(sizeof(struct coordinates) * 2);
    haircut_colors = malloc(sizeof(int) * 2);
    haircut_labels = malloc(sizeof(struct label) * 2);

    barber_room= malloc(sizeof(struct bench));
    barber_chairs = malloc(sizeof(struct coordinates));
    barber_colors = malloc(sizeof(int));
    barber_labels = malloc(sizeof(struct label));

    waiting_room= malloc(sizeof(struct bench));
    waiting_chairs = malloc(sizeof(struct coordinates) * number_of_seats);
    waiting_colors = malloc(sizeof(int) * number_of_seats);
    waiting_labels = malloc(sizeof(struct label) * number_of_seats);

    out_of_salon_room= malloc(sizeof(struct bench));
    out_of_salon_chairs = malloc(sizeof(struct coordinates) * number_of_out_of_salon_seats);
    out_of_salon_colors = malloc(sizeof(int) * number_of_out_of_salon_seats);
    out_of_salon_labels = malloc(sizeof(struct label) * number_of_out_of_salon_seats);

    for(i = 0; i < 2; i++){
        haircut_colors[i] = 6;
        haircut_labels[i].label = malloc(sizeof(char)* MAX_LABEL_SIZE);
        haircut_labels[i].label = " ";
    }

        barber_colors[0] = 6;
        barber_labels[0].label = malloc(sizeof(char)* MAX_LABEL_SIZE);
        barber_labels[0].label = " ";


    for(i = 0; i < number_of_seats; i++){
        waiting_colors[i] = 6;
        waiting_labels[i].label = malloc(sizeof(char)* MAX_LABEL_SIZE);
        waiting_labels[i].label = " ";
    }

    for(i = 0; i < number_of_out_of_salon_seats; i++){
        out_of_salon_colors[i] = 6;
        out_of_salon_labels[i].label = malloc(sizeof(char)* MAX_LABEL_SIZE);
        out_of_salon_labels[i].label = " ";
    }

    draw_bench(haircut_salon, LEFT, MIDDLE, HORIZONTAL, 2, haircut_chairs, "HAIRCUT SALON", haircut_labels);
    draw_bench(barber_room, LEFT, UP, HORIZONTAL, 1, barber_chairs, "BARBER CHAIR", barber_labels);
    draw_bench(waiting_room, LEFT, BOTTOM, HORIZONTAL, number_of_seats, waiting_chairs, "WAITING ROOM", waiting_labels);
    draw_bench(out_of_salon_room, RIGHT, UP, VERTICAL, number_of_out_of_salon_seats, out_of_salon_chairs, "OUT OF SALON", out_of_salon_labels);

    clients = malloc(sizeof(pthread_t) *number_of_clients);

    for(i = 0; i < number_of_clients; i++){
        if(pthread_create(&clients[i], NULL, client_thread, r_id + i) == -1)
            print_error(errno, "pthread_create error");
    }

    if(pthread_create(&barber, NULL, barber_thread, r_id) == -1)
        print_error(errno, "pthread_create error");


    for(i = 0; i < number_of_clients; i++){
        if(pthread_join(clients[i], NULL) == -1)
            print_error(errno, "error in pthread_join");
    }
    if(pthread_join(barber, NULL) == -1)
        print_error(errno, "error in pthread_join");

    return 0;
}