1 /*
2  * Copyright (c) 2017, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <tee_internal_api.h>
29 #include <tee_internal_api_extensions.h>
30 
31 #include <random_ta.h>
32 
TA_CreateEntryPoint(void)33 TEE_Result TA_CreateEntryPoint(void)
34 {
35 	return TEE_SUCCESS;
36 }
37 
TA_DestroyEntryPoint(void)38 void TA_DestroyEntryPoint(void)
39 {
40 }
41 
TA_OpenSessionEntryPoint(uint32_t param_types,TEE_Param __maybe_unused params[4],void __maybe_unused ** sess_ctx)42 TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
43 		TEE_Param __maybe_unused params[4],
44 		void __maybe_unused **sess_ctx)
45 {
46 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
47 						   TEE_PARAM_TYPE_NONE,
48 						   TEE_PARAM_TYPE_NONE,
49 						   TEE_PARAM_TYPE_NONE);
50 	if (param_types != exp_param_types)
51 		return TEE_ERROR_BAD_PARAMETERS;
52 
53 	(void)&params;
54 	(void)&sess_ctx;
55 
56 	return TEE_SUCCESS;
57 }
58 
TA_CloseSessionEntryPoint(void __maybe_unused * sess_ctx)59 void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
60 {
61 	(void)&sess_ctx;
62 }
63 
random_number_generate(uint32_t param_types,TEE_Param params[4])64 static TEE_Result random_number_generate(uint32_t param_types,
65 	TEE_Param params[4])
66 {
67 	uint32_t exp_param_types =
68 				TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT,
69 						TEE_PARAM_TYPE_NONE,
70 						TEE_PARAM_TYPE_NONE,
71 						TEE_PARAM_TYPE_NONE);
72 	void *buf = NULL;
73 
74 	DMSG("has been called");
75 	if (param_types != exp_param_types)
76 		return TEE_ERROR_BAD_PARAMETERS;
77 
78 	buf = TEE_Malloc(params[0].memref.size, 0);
79 	if (!buf)
80 		return TEE_ERROR_OUT_OF_MEMORY;
81 	IMSG("Generating random data over %u bytes.", params[0].memref.size);
82 	/*
83 	 * The TEE_GenerateRandom function is a part of TEE Internal Core API,
84 	 * which generates random data
85 	 *
86 	 * Parameters:
87 	 * @ randomBuffer : Reference to generated random data
88 	 * @ randomBufferLen : Byte length of requested random data
89 	 */
90 	TEE_GenerateRandom(buf, params[0].memref.size);
91 	TEE_MemMove(params[0].memref.buffer, buf, params[0].memref.size);
92 	TEE_Free(buf);
93 
94 	return TEE_SUCCESS;
95 }
96 
TA_InvokeCommandEntryPoint(void __maybe_unused * sess_ctx,uint32_t cmd_id,uint32_t param_types,TEE_Param params[4])97 TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx,
98 			uint32_t cmd_id,
99 			uint32_t param_types, TEE_Param params[4])
100 {
101 	(void)&sess_ctx;
102 
103 	switch (cmd_id) {
104 	case TA_RANDOM_CMD_GENERATE:
105 		return random_number_generate(param_types, params);
106 	default:
107 		return TEE_ERROR_BAD_PARAMETERS;
108 	}
109 }
110