static TEEC_Result run_test_with_args(enum storage_benchmark_cmd cmd,
		uint32_t arg0, uint32_t arg1, uint32_t arg2,
		uint32_t arg3, uint32_t *out0, uint32_t *out1)
{
	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
	TEEC_Result res;
	TEEC_Session sess;
	uint32_t orig;

	res = xtest_teec_open_session(&sess, &storage_benchmark_ta_uuid, NULL, &orig);
	if (res != TEEC_SUCCESS)
		return res;

	op.params[0].value.a = arg0;
	op.params[0].value.b = arg1;
	op.params[1].value.a = arg2;
	op.params[1].value.b = arg3;

	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
			TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT, TEEC_NONE);

	res = TEEC_InvokeCommand(&sess, cmd, &op, &orig);

	if (out0)
		*out0 = op.params[2].value.a;
	if (out1)
		*out1 = op.params[2].value.b;

	TEEC_CloseSession(&sess);

	return res;
}
static int smcl_common_invoke_secwld(uint8_t cmd, TEEC_Operation *op)
{
    TEEC_Context ctx = 0;
    TEEC_ErrorOrigin org = TEEC_ORIGIN_API;
    TEEC_Result result = TEEC_ERROR_GENERIC;
    TEEC_Session session;
    TEEC_UUID uuid = SMCL_TA_UUID;
    uint32_t ret = SMCL_GENERAL_FAILURE;

    memset(&session, 0, sizeof(TEEC_Session));

    result = TEEC_InitializeContext(NULL, &ctx);
    if (result != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_InitializeContext failed\n");
        goto out;
    }

    result = TEEC_OpenSession(&ctx, &session, &uuid, TEEC_LOGIN_PUBLIC, NULL,
                              NULL, &org);
    if (result != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_OpenSession failed\n");
        goto out_finalize;
    }

    op->memRefs[3].buffer = (void *)(&ret);
    op->memRefs[3].size = sizeof(uint32_t);
    op->memRefs[3].flags = TEEC_MEM_OUTPUT;
    op->flags |= TEEC_MEMREF_3_USED;

    result = TEEC_InvokeCommand(&session, cmd, op, &org);
    if (result != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_InvokeCommand failed\n");
    }

    result = TEEC_CloseSession(&session);
    if (result != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_CloseSession failed\n");
    }

out_finalize:
    if (TEEC_FinalizeContext(&ctx) != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_FinalizeContext failed\n");
        /*
         * Special handling since there is a risk of overwriting old failing
         * result code from previous calls to tee.
         */
        if (result == TEEC_SUCCESS) {
            result = TEEC_ERROR_GENERIC;
        }
    }
out:
    if (result != TEEC_SUCCESS) {
        return SMCL_TEE_FAILURE;
    }

    return ret;
}
SST_ERROR SST_EXPORT_API SSTTerminate(void)
{
   stubMutexLock();
   if (g_bSSTInitialized)
   {
      TEEC_CloseSession(&g_SSTSession);
      stubFinalizeContext();
      g_bSSTInitialized = false;
   }
   /* else if not intialized => success too */
   stubMutexUnlock();
   return SST_SUCCESS;
}
Beispiel #4
0
int main()
{
	TEEC_Context context;
	TEEC_Session session;
	TEEC_Result ret;
	TEEC_Operation operation = {0};
	uint32_t connection_method = TEEC_LOGIN_PUBLIC;

	printf("START: services test app\n");

	/* Initialize context */
	printf("Initializing context: ");
	ret = TEEC_InitializeContext(NULL, &context);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_InitializeContext failed: 0x%x\n", ret);
		goto end_1;
	} else {
		printf("initialized\n");
	}

	operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);

	/* Open session */
	printf("Opening session: ");
	ret = TEEC_OpenSession(&context, &session, &uuid, connection_method,
			       NULL, &operation, NULL);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_OpenSession failed: 0x%x\n", ret);
		goto end_2;
	} else {
		printf("opened\n");
	}

	printf("Initialization complete\n");

	/* Invoke commands */
	printf("----- Counter tests begin -----\n");
	ret = do_counter_tests(&session, &context);
	if (ret != TEEC_SUCCESS) {
		printf("Counter test failed: 0x%x\n", ret);
		goto end_3;
	} else {
		printf("Counter test ok\n");
	}

	printf("----- Counter tests end -----\n");

/* Cleanup */
end_3:
	printf("Closing session: ");
	TEEC_CloseSession(&session);
	printf("closed\n");

end_2:
	printf("Finalizing context: ");
	TEEC_FinalizeContext(&context);
	printf("finalized\n");

end_1:
	printf("END: services test app\n");
	exit(ret);
}
Beispiel #5
0
int main(int argc, char *argv[])
{
	TEEC_Result res;
	TEEC_Context ctx;
	TEEC_Session sess;
	TEEC_Operation op;
	TEEC_UUID uuid_ta = TA_SE_API_TEST_UUID;
	TEEC_UUID uuid_sta = TA_SE_API_SELF_TEST_UUID;
	TEEC_UUID *uuid;
	uint32_t err_origin;
	bool self_test = false;
	int c;

	while ((c = getopt(argc, argv, "s")) != -1)
	{
		switch (c)
		{
		case 's':
			self_test = true;
			break;
		case '?':
			exit(1);
		}
	}

	/* Initialize a context connecting us to the TEE */
	res = TEEC_InitializeContext(NULL, &ctx);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_InitializeContext failed with code 0x%x", res);

	if (self_test)
		uuid = &uuid_sta;
	else
		uuid = &uuid_ta;

	/*
	 * Open a session to the "hello world" TA, the TA will print "hello
	 * world!" in the log when the session is created.
	 */
	res = TEEC_OpenSession(&ctx, &sess, uuid,
			       TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
			res, err_origin);

	/*
	 * Execute a function in the TA by invoking it, in this case
	 * we're incrementing a number.
	 *
	 * The value of command ID part and how the parameters are
	 * interpreted is part of the interface provided by the TA.
	 */

	/*
	 * Prepare the argument. Pass a value in the first parameter,
	 * the remaining three parameters are unused.
	 */
	memset(&op, 0, sizeof(op));

	res = TEEC_InvokeCommand(&sess, CMD_SELF_TESTS, &op,
				 &err_origin);
	if (res != TEEC_SUCCESS)
		printf("failed\n");
	else
		printf("success\n");

	/*
	 * We're done with the TA, close the session and
	 * destroy the context.
	 *
	 * The TA will print "Goodbye!" in the log when the
	 * session is closed.
	 */

	TEEC_CloseSession(&sess);

	TEEC_FinalizeContext(&ctx);

	return 0;
}
Beispiel #6
0
int main(void){
 	TEEC_Result res;
	TEEC_Context ctx;
	TEEC_Session sess;
	TEEC_Operation op,init_vals;
	TEEC_UUID uuid = TA_QUIZ_UUID;
	char question[DEF_QUES_SIZE] = {0};
	uint32_t err_origin;
	//strt file read
	FILE *infile;
	infile = fopen("/bin/test.txt","r");
	int num = 0;
	int i;			
	ssize_t read;
	size_t len =0;
	char *line;
	line = (char *)malloc(sizeof(char) * 120);
	while((read = getline(&line,&len,infile)) != -1){
		 // printf("%s\n",line );
		num++;
	}
	printf("%d\n",num);
	struct question ques[num/2];

	fseek(infile,0,SEEK_SET);
	for(i =0 ; i < num;++i){
		read = getline(&line,&len,infile);
		// printf("%s\n",line );
		line[strlen(line)] = '\0';
			
		if(i%2 == 0){
			strcpy(ques[i/2].q,line);

		}else{
			strcpy(ques[i/2].answer,line);
			

		}
	}
	free(line);

	num = num/2;
	/*
	FILE *infile;
	infile = fopen("/bin/test.txt","r");
	int num = 0;			
	struct question temp;
	int i;
	while(fread(&temp,sizeof(struct question),1,infile) != 0){
		// printf("%s \n %s \n",temp.q,temp.answer);
		num++;
	}
	fseek(infile,0,SEEK_SET);	
	struct question ques[num];
	for(i =0 ; i < num;++i){
		fread(&ques[i],sizeof(struct question),1,infile);
	}
	*/
	//end of read from file
	memset(&init_vals,0,sizeof(init_vals));
	init_vals.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,TEEC_MEMREF_TEMP_INPUT,TEEC_NONE,TEEC_NONE);
	init_vals.params[0].value.a = num;
	init_vals.params[1].tmpref.buffer = ques;
	init_vals.params[1].tmpref.size = sizeof(ques[0])*num;

	res = TEEC_InitializeContext(NULL, &ctx); // This basically checks if a TEE is present in the system
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_InitializeContext failed with code 0x%x", res);   
	/*
	 * Open a session to the  TA
	 */
	res = TEEC_OpenSession(&ctx, &sess, &uuid,TEEC_LOGIN_PUBLIC, NULL, &init_vals, &err_origin);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",res, err_origin);

	/*
	 * Prepare the argument. Pass a value in the first parameter,
	 * the remaining three parameters are unused.
	 */

	memset(&op, 0, sizeof(op));
	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_MEMREF_TEMP_OUTPUT,
					 TEEC_NONE, TEEC_NONE);
	op.params[1].tmpref.buffer = question;
	op.params[1].tmpref.size  = sizeof(question);
	
	printf("********************** QUIZ **************************\n");
	while(1){
		res = TEEC_InvokeCommand(&sess, TA_QUIZ_CMD_START_QUIZ, &op,&err_origin);
		if (res != TEEC_SUCCESS)
			errx(1, "TEEC_Get_Question failed with code 0x%x origin 0x%x",res, err_origin);	
		if(op.params[0].value.a == 0){
			break;
		}else{
			printf("\n----------------------- \n Question:\n %s \n",question );
			printf(" Enter your anwer:\n");
			char answer[DEF_QUES_SIZE];
			fgets(answer,DEF_QUES_SIZE,stdin);
			if ((strlen(answer)>0) && (answer[strlen (answer) - 1] == '\n'))
        		answer[strlen (answer) - 1] = '\0';
        	send_answer(sess,answer,err_origin);

		}
		
	}	
	printf("Quiz ended\n");
	/* 
	 * Close the session 
	 */
	TEEC_CloseSession(&sess);
	TEEC_FinalizeContext(&ctx);


 	return 0;
}
Beispiel #7
0
int main()
{
	TEEC_Context context;
	TEEC_Session session;
	TEEC_Operation operation;
	TEEC_SharedMemory in_mem;
	TEEC_SharedMemory out_mem;
	TEEC_Result ret;
	uint32_t return_origin;
	uint32_t connection_method = TEEC_LOGIN_PUBLIC;
	char data[DATA_SIZE];
	uint8_t sha1[SHA1_SIZE];
	int i;

	printf("START: example SHA1 calc app\n");

	memset((void *)&in_mem, 0, sizeof(in_mem));
	memset((void *)&out_mem, 0, sizeof(out_mem));
	memset((void *)&operation, 0, sizeof(operation));
	memset(data, 'y', DATA_SIZE);
	memset(sha1, 0, SHA1_SIZE);

	/* Initialize context */
	printf("Initializing context: ");
	ret = TEEC_InitializeContext(NULL, &context);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_InitializeContext failed: 0x%x\n", ret);
		goto end_1;
	} else {
		printf("initiliazed\n");
	}

	/* Open session is expecting HASH algorithm */
	operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
	operation.params[0].value.a = HASH_SHA1;

	/* Open session */
	printf("Openning session: ");
	ret = TEEC_OpenSession(&context, &session, &uuid, connection_method,
			       NULL, &operation, &return_origin);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_OpenSession failed: 0x%x\n", ret);
		goto end_2;
	} else {
		printf("opened\n");
	}

	/* Register shared memory for initial hash */

	/* Data */
	in_mem.buffer = data;
	in_mem.size = DATA_SIZE;
	in_mem.flags = TEEC_MEM_INPUT;

	ret = TEEC_RegisterSharedMemory(&context, &in_mem);
	if (ret != TEE_SUCCESS) {
		printf("Failed to register DATA shared memory\n");
		goto end_3;
	}
	printf("Registered in mem..\n");

	/* Fill operation parameters */
	operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
	operation.params[0].memref.parent = &in_mem;

	/* Invoke command */
	printf("Invoking command: Update sha1: ");
	ret = TEEC_InvokeCommand(&session, HASH_UPDATE, &operation, &return_origin);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_InvokeCommand failed: 0x%x\n", ret);
		goto end_3;
	} else {
		printf("done\n");
	}

	/* register a shared memory region to hold the output of the sha1 operation */
	out_mem.buffer = sha1;
	out_mem.size = SHA1_SIZE;
	out_mem.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;

	ret = TEEC_RegisterSharedMemory(&context, &out_mem);
	if (ret != TEE_SUCCESS) {
		printf("Failed to allocate SHA1 shared memory\n");
		goto end_3;
	}
	printf("Registered out mem..\n");

	/*
	 * Send some more data to calculate the hash over, this will be added to the origional hash
	 * .  This is not strictly needed it is a test for passing 2 memref params in a single
	 * operation
	 */
	memset(data, 'Z', DATA_SIZE);

	/* Fill operation parameters */
	operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_MEMREF_WHOLE,
						TEEC_NONE, TEEC_NONE);
	/*
	 * reuse the origional input shared memory, because we have just updated the contents
	 * of the buffer
	 */
	operation.params[0].memref.parent = &in_mem;
	operation.params[1].memref.parent = &out_mem;

	/* Invoke command */
	printf("Invoking command: Do final sha1: ");
	ret = TEEC_InvokeCommand(&session, HASH_DO_FINAL, &operation, &return_origin);
	if (ret != TEEC_SUCCESS) {
		printf("TEEC_InvokeCommand failed: 0x%x\n", ret);
		goto end_4;
	} else {
		printf("done\n");
	}

	/* Printf sha1 buf */
	printf("Calculated sha1: ");
	for (i = 0; i < SHA1_SIZE; i++)
		printf("%02x", sha1[i]);
	printf("\n");

	/* Cleanup used connection/resources */

end_4:

	printf("Releasing shared out memory..\n");
	TEEC_ReleaseSharedMemory(&out_mem);

end_3:
	printf("Releasing shared in memory..\n");
	TEEC_ReleaseSharedMemory(&in_mem);
	printf("Closing session..\n");
	TEEC_CloseSession(&session);

end_2:

	printf("Finalizing ctx..\n");
	TEEC_FinalizeContext(&context);
end_1:

	printf("END: example SHA1 calc app\n");
	exit(ret);
}
int main(int argc, char *argv[])
{
	TEEC_Result res;
	TEEC_Context ctx;
	TEEC_Session sess;
	TEEC_Operation op;
	TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
	uint32_t err_origin;

	/* Initialize a context connecting us to the TEE */
	res = TEEC_InitializeContext(NULL, &ctx);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_InitializeContext failed with code 0x%x", res);

	/*
	 * Open a session to the "hello world" TA, the TA will print "hello
	 * world!" in the log when the session is created.
	 */
	res = TEEC_OpenSession(&ctx, &sess, &uuid,
			       TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
			res, err_origin);

	/*
	 * Execute a function in the TA by invoking it, in this case
	 * we're incrementing a number.
	 *
	 * The value of command ID part and how the parameters are
	 * interpreted is part of the interface provided by the TA.
	 */

	/*
	 * Prepare the argument. Pass a value in the first parameter,
	 * the remaining three parameters are unused.
	 */
	memset(&op, 0, sizeof(op));
	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,
					 TEEC_NONE, TEEC_NONE);
	op.params[0].value.a = 42;

	printf("Invoking TA to increment %d\n", op.params[0].value.a);
	res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,
				 &err_origin);
	if (res != TEEC_SUCCESS)
		errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
			res, err_origin);
	printf("TA incremented value to %d\n", op.params[0].value.a);

	/*
	 * We're done with the TA, close the session and
	 * destroy the context.
	 *
	 * The TA will print "Goodbye!" in the log when the
	 * session is closed.
	 */

	TEEC_CloseSession(&sess);

	TEEC_FinalizeContext(&ctx);

	return 0;
}
int verify_start_modem(void *access_mem_start,
                       size_t shared_mem_size,
                       size_t access_private_mem_size,
                       struct access_image_descr *access_image_descr)
{
    int ret = 0;

    TEEC_Result teec_ret = TEEC_ERROR_GENERIC;
    TEEC_Context ctx;
    TEEC_SharedMemory shm;
    TEEC_ErrorOrigin org;
    TEEC_Session session;
    TEEC_Operation operation;
    TEEC_UUID ta_static_uuid = STATIC_TA_UUID;

    (void)access_mem_start;
    (void)shared_mem_size;
    (void)access_private_mem_size;

    if (NULL == access_image_descr ||
            NULL == access_image_descr->elf_hdr) {
        ret = -1;
        dprintf(ERROR, "NULL pointer error");
        goto out;
    }

    teec_ret = TEEC_InitializeContext(NULL, &ctx);

    if (teec_ret != TEEC_SUCCESS) {
        goto out;
    }

    teec_ret = TEEC_OpenSession(&ctx, &session, &ta_static_uuid,
                                TEEC_LOGIN_PUBLIC, NULL, NULL, &org);

    if (teec_ret != TEEC_SUCCESS) {
        goto out_finalize;
    }

    shm.size = 4;
    shm.flags = TEEC_MEM_INPUT;
    teec_ret = TEEC_AllocateSharedMemory(&ctx, &shm);

    if (teec_ret != TEEC_SUCCESS) {
        goto out_close;
    }

    operation.memRefs[0] = shm;
    operation.flags = TEEC_MEMREF_0_USED;

    /* Point to entry. */
    *((unsigned int *)shm.buffer) =
        (uintptr_t)((uint8_t *) access_image_descr->elf_hdr +
                       ELF_ENTRY_OFFSET);

    teec_ret = TEEC_InvokeCommand(&session, BASS_APP_VERITY_START_MODEM,
                                  &operation, &org);

    if (teec_ret != TEEC_SUCCESS) {
        goto out_release;
    }

out_release:
    TEEC_ReleaseSharedMemory(&shm);

out_close:
    TEEC_CloseSession(&session);

out_finalize:
    TEEC_FinalizeContext(&ctx);

out:

    if (teec_ret != TEEC_SUCCESS) {
        ret = -1;
    }

    return ret;
}
bass_return_code teec_invoke_secworld(TEEC_Operation *const operation,
                                      const uint32_t command_id)
{
    bass_return_code ret_code = BASS_RC_FAILURE;
    TEEC_Result result = TEEC_ERROR_GENERIC;
    TEEC_Session session;
    TEEC_Context context;
    TEEC_ErrorOrigin errorOrigin;
    TEEC_UUID ta_static_uuid = STATIC_TA_UUID;

    result = TEEC_InitializeContext(NULL, &context);

    if (TEEC_SUCCESS != result) {
        dprintf(ERROR, "TEEC_InitializeContext error\n");
        goto function_exit;
    }

    result = TEEC_OpenSession(&context, &session, &ta_static_uuid,
                              TEEC_LOGIN_PUBLIC, NULL, NULL, &errorOrigin);

    if (TEEC_SUCCESS != result) {
        dprintf(ERROR, "TEEC_OpenSession error\n");
        goto finalize;
    }

    result = TEEC_InvokeCommand(&session, command_id,
                                operation, &errorOrigin);

    if (TEEC_SUCCESS != result) {
        dprintf(ERROR, "TEEC_InvokeCommand error\n");
        goto close;
    }

close:
    if (TEEC_CloseSession(&session) != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_CloseSession error\n");
    }

finalize:
    if (TEEC_FinalizeContext(&context) != TEEC_SUCCESS) {
        dprintf(ERROR, "TEEC_FinalizeContext error\n");
    }

function_exit:

    if (TEEC_SUCCESS != result) {
        dprintf(ERROR, "result = 0x%X, errorOrigin = %d\n", result,
                errorOrigin);

        switch (errorOrigin) {
        case TEEC_ORIGIN_API:
            ret_code = BASS_RC_ERROR_TEE_API;
            break;
        case TEEC_ORIGIN_COMMS:
            ret_code = BASS_RC_ERROR_TEE_COMMS;
            break;
        case TEEC_ORIGIN_TEE:
            ret_code = BASS_RC_ERROR_TEE_CORE;
            break;
        case TEEC_ORIGIN_TRUSTED_APP:
            ret_code = BASS_RC_ERROR_TEE_TRUSTED_APP;
            break;
        default:
            ret_code = BASS_RC_ERROR_UNKNOWN;
            break;

        }
    } else {
        ret_code = BASS_RC_SUCCESS;
    }

    return ret_code;
}