1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  * All rights reserved.
5  */
6 
7 #include <tee_internal_api.h>
8 #include <tee_ta_api.h>
9 #include <string.h>
10 #include <trace.h>
11 
12 #include "ta_hash_perf.h"
13 #include "ta_hash_perf_priv.h"
14 
15 #define CHECK(res, name, action) do {			 \
16 		if ((res) != TEE_SUCCESS) {		 \
17 			DMSG(name ": %#08"PRIx32, (res));\
18 			action				 \
19 		}					 \
20 	} while(0)
21 
22 static TEE_OperationHandle digest_op;
23 
is_mac(uint32_t algo)24 static bool is_mac(uint32_t algo)
25 {
26 	switch (algo) {
27 	case TEE_ALG_HMAC_SHA1:
28 	case TEE_ALG_HMAC_SHA224:
29 	case TEE_ALG_HMAC_SHA256:
30 	case TEE_ALG_HMAC_SHA384:
31 	case TEE_ALG_HMAC_SHA512:
32 	case TEE_ALG_HMAC_SM3:
33 		return true;
34 	default:
35 		return false;
36 	}
37 }
38 
cmd_process(uint32_t param_types,TEE_Param params[4])39 TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4])
40 {
41 	TEE_Result res = TEE_ERROR_GENERIC;
42 	TEE_OperationInfo info = { };
43 	int n = 0;
44 	void *in = NULL;
45 	void *out = NULL;
46 	uint32_t insz = 0;
47 	uint32_t outsz = 0;
48 	uint32_t offset = 0;
49 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
50 						   TEE_PARAM_TYPE_MEMREF_OUTPUT,
51 						   TEE_PARAM_TYPE_VALUE_INPUT,
52 						   TEE_PARAM_TYPE_NONE);
53 
54 	if (param_types != exp_param_types)
55 		return TEE_ERROR_BAD_PARAMETERS;
56 
57 	offset = params[2].value.b;
58 	in = (uint8_t *)params[0].memref.buffer + offset;
59 	insz = params[0].memref.size - offset;
60 	out = params[1].memref.buffer;
61 	outsz = params[1].memref.size;
62 	n = params[2].value.a;
63 
64 	TEE_GetOperationInfo(digest_op, &info);
65 
66 	if (is_mac(info.algorithm)) {
67 		while (n--) {
68 			TEE_MACInit(digest_op, NULL, 0);
69 			res = TEE_MACComputeFinal(digest_op, in, insz, out, &outsz);
70 			CHECK(res, "TEE_MACComputeFinal", return res;);
71 		}
72 	} else {
73 		while (n--) {
74 			res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
75 			CHECK(res, "TEE_DigestDoFinal", return res;);
76 		}
77 	}
78 
79 	return TEE_SUCCESS;
80 }
81 
cmd_prepare_op(uint32_t param_types,TEE_Param params[4])82 TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4])
83 {
84 	TEE_ObjectHandle hkey = TEE_HANDLE_NULL;
85 	TEE_Result res = TEE_ERROR_GENERIC;
86 	TEE_Attribute attr = { };
87 	uint32_t key_type = TEE_TYPE_HMAC_SHA1;
88 	uint32_t mac_key_size = 512;
89 	uint32_t max_key_size = 0;
90 	uint32_t algo = 0;
91 	static uint8_t mac_key[] = {
92 		0x00, 0x01, 0x02, 0x03,
93 		0x04, 0x05, 0x06, 0x07,
94 		0x08, 0x09, 0x0A, 0x0B,
95 		0x0C, 0x0D, 0x0E, 0x0F,
96 		0x10, 0x11, 0x12, 0x13,
97 		0x14, 0x15, 0x16, 0x17,
98 		0x18, 0x19, 0x1A, 0x1B,
99 		0x1C, 0x1D, 0x1E, 0x1F,
100 		0x20, 0x21, 0x22, 0x23,
101 		0x24, 0x25, 0x26, 0x27,
102 		0x28, 0x29, 0x2A, 0x2B,
103 		0x2C, 0x2D, 0x2E, 0x2F,
104 		0x30, 0x31, 0x32, 0x33,
105 		0x34, 0x35, 0x36, 0x37,
106 		0x38, 0x39, 0x3A, 0x3B,
107 		0x3C, 0x3D, 0x3E, 0x3F
108 	};
109 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
110 						   TEE_PARAM_TYPE_NONE,
111 						   TEE_PARAM_TYPE_NONE,
112 						   TEE_PARAM_TYPE_NONE);
113 
114 	if (param_types != exp_param_types)
115 		return TEE_ERROR_BAD_PARAMETERS;
116 
117 	switch (params[0].value.a) {
118 	case TA_SHA_SHA1:
119 		algo = TEE_ALG_SHA1;
120 		break;
121 	case TA_SHA_SHA224:
122 		algo = TEE_ALG_SHA224;
123 		break;
124 	case TA_SHA_SHA256:
125 		algo = TEE_ALG_SHA256;
126 		break;
127 	case TA_SHA_SHA384:
128 		algo = TEE_ALG_SHA384;
129 		break;
130 	case TA_SHA_SHA512:
131 		algo = TEE_ALG_SHA512;
132 		break;
133 	case TA_SM3:
134 		algo = TEE_ALG_SM3;
135 		break;
136 	case TA_HMAC_SHA1:
137 		key_type = TEE_TYPE_HMAC_SHA1;
138 		algo = TEE_ALG_HMAC_SHA1;
139 		max_key_size = 512;
140 		break;
141 	case TA_HMAC_SHA224:
142 		key_type = TEE_TYPE_HMAC_SHA224;
143 		algo = TEE_ALG_HMAC_SHA224;
144 		max_key_size = 512;
145 		break;
146 	case TA_HMAC_SHA256:
147 		key_type = TEE_TYPE_HMAC_SHA256;
148 		algo = TEE_ALG_HMAC_SHA256;
149 		max_key_size = 512;
150 		break;
151 	case TA_HMAC_SHA384:
152 		key_type = TEE_TYPE_HMAC_SHA384;
153 		algo = TEE_ALG_HMAC_SHA384;
154 		max_key_size = 1024;
155 		break;
156 	case TA_HMAC_SHA512:
157 		key_type = TEE_TYPE_HMAC_SHA512;
158 		algo = TEE_ALG_HMAC_SHA512;
159 		max_key_size = 1024;
160 		break;
161 	case TA_HMAC_SM3:
162 		key_type = TEE_TYPE_HMAC_SM3;
163 		algo = TEE_ALG_HMAC_SM3;
164 		max_key_size = 512;
165 		break;
166 	default:
167 		return TEE_ERROR_BAD_PARAMETERS;
168 	}
169 
170 	if (digest_op)
171 		TEE_FreeOperation(digest_op);
172 
173 	if (is_mac(algo)) {
174 		res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_MAC, max_key_size);
175 		CHECK(res, "TEE_AllocateOperation", return res;);
176 
177 		res = TEE_AllocateTransientObject(key_type, max_key_size, &hkey);
178 		CHECK(res, "TEE_AllocateTransientObject", return res;);
179 
180 		attr.attributeID = TEE_ATTR_SECRET_VALUE;
181 		attr.content.ref.buffer = mac_key;
182 		attr.content.ref.length = mac_key_size / 8;
183 
184 		res = TEE_PopulateTransientObject(hkey, &attr, 1);
185 		CHECK(res, "TEE_PopulateTransientObject", return res;);
186 
187 		res = TEE_SetOperationKey(digest_op, hkey);
188 		CHECK(res, "TEE_SetOperationKey", return res;);
189 
190 		TEE_FreeTransientObject(hkey);
191 	} else {
192 		res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_DIGEST, 0);
193 		CHECK(res, "TEE_AllocateOperation", return res;);
194 	}
195 	return TEE_SUCCESS;
196 }
197 
cmd_clean_res(void)198 void cmd_clean_res(void)
199 {
200 	if (digest_op)
201 		TEE_FreeOperation(digest_op);
202 }
203