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_sha_perf.h"
13 #include "ta_sha_perf_priv.h"
14 
15 #define CHECK(res, name, action) do {			\
16 		if ((res) != TEE_SUCCESS) {		\
17 			DMSG(name ": 0x%08x", (res));	\
18 			action				\
19 		}					\
20 	} while(0)
21 
22 static TEE_OperationHandle digest_op = NULL;
23 
cmd_process(uint32_t param_types,TEE_Param params[4])24 TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4])
25 {
26 	TEE_Result res = TEE_ERROR_GENERIC;
27 	int n = 0;
28 	void *in = NULL;
29 	void *out = NULL;
30 	uint32_t insz = 0;
31 	uint32_t outsz = 0;
32 	uint32_t offset = 0;
33 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
34 						   TEE_PARAM_TYPE_MEMREF_OUTPUT,
35 						   TEE_PARAM_TYPE_VALUE_INPUT,
36 						   TEE_PARAM_TYPE_NONE);
37 
38 	if (param_types != exp_param_types)
39 		return TEE_ERROR_BAD_PARAMETERS;
40 
41 	offset = params[2].value.b;
42 	in = (uint8_t *)params[0].memref.buffer + offset;
43 	insz = params[0].memref.size - offset;
44 	out = params[1].memref.buffer;
45 	outsz = params[1].memref.size;
46 	n = params[2].value.a;
47 
48 	while (n--) {
49 		res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
50 		CHECK(res, "TEE_DigestDoFinal", return res;);
51 	}
52 	return TEE_SUCCESS;
53 }
54 
cmd_prepare_op(uint32_t param_types,TEE_Param params[4])55 TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4])
56 {
57 	TEE_Result res = TEE_ERROR_GENERIC;
58 	uint32_t algo = 0;
59 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
60 						   TEE_PARAM_TYPE_NONE,
61 						   TEE_PARAM_TYPE_NONE,
62 						   TEE_PARAM_TYPE_NONE);
63 
64 	if (param_types != exp_param_types)
65 		return TEE_ERROR_BAD_PARAMETERS;
66 
67 	switch (params[0].value.a) {
68 	case TA_SHA_SHA1:
69 		algo = TEE_ALG_SHA1;
70 		break;
71 	case TA_SHA_SHA224:
72 		algo = TEE_ALG_SHA224;
73 		break;
74 	case TA_SHA_SHA256:
75 		algo = TEE_ALG_SHA256;
76 		break;
77 	case TA_SHA_SHA384:
78 		algo = TEE_ALG_SHA384;
79 		break;
80 	case TA_SHA_SHA512:
81 		algo = TEE_ALG_SHA512;
82 		break;
83 	default:
84 		return TEE_ERROR_BAD_PARAMETERS;
85 	}
86 
87 	if (digest_op)
88 		TEE_FreeOperation(digest_op);
89 
90 	res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_DIGEST, 0);
91 	CHECK(res, "TEE_AllocateOperation", return res;);
92 
93 	return TEE_SUCCESS;
94 }
95 
96 
cmd_clean_res(void)97 void cmd_clean_res(void)
98 {
99 	if (digest_op)
100 		TEE_FreeOperation(digest_op);
101 }
102