blob: 9f41468450efbba02774685e89c64a155dfb3ca5 [file] [log] [blame]
Igor Opaniuk136644a2016-09-13 13:40:56 +03001/*
2 * Copyright (c) 2015, 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_ta_api.h>
30#include <string.h>
31#include <trace.h>
32
33#include "ta_sha_perf.h"
34#include "ta_sha_perf_priv.h"
35
36#define CHECK(res, name, action) do { \
37 if ((res) != TEE_SUCCESS) { \
38 DMSG(name ": 0x%08x", (res)); \
39 action \
40 } \
41 } while(0)
42
43static TEE_OperationHandle digest_op = NULL;
44
45TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4])
46{
Etienne Carriere102092e2019-03-28 15:24:22 +010047 TEE_Result res = TEE_ERROR_GENERIC;
48 int n = 0;
49 void *in = NULL;
50 void *out = NULL;
51 uint32_t insz = 0;
52 uint32_t outsz = 0;
53 uint32_t offset = 0;
Igor Opaniuk136644a2016-09-13 13:40:56 +030054 uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
55 TEE_PARAM_TYPE_MEMREF_OUTPUT,
56 TEE_PARAM_TYPE_VALUE_INPUT,
57 TEE_PARAM_TYPE_NONE);
58
59 if (param_types != exp_param_types)
60 return TEE_ERROR_BAD_PARAMETERS;
61
62 offset = params[2].value.b;
63 in = (uint8_t *)params[0].memref.buffer + offset;
64 insz = params[0].memref.size - offset;
65 out = params[1].memref.buffer;
66 outsz = params[1].memref.size;
67 n = params[2].value.a;
68
69 while (n--) {
70 res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
71 CHECK(res, "TEE_DigestDoFinal", return res;);
72 }
73 return TEE_SUCCESS;
74}
75
76TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4])
77{
Etienne Carriere102092e2019-03-28 15:24:22 +010078 TEE_Result res = TEE_ERROR_GENERIC;
79 uint32_t algo = 0;
Igor Opaniuk136644a2016-09-13 13:40:56 +030080 uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
81 TEE_PARAM_TYPE_NONE,
82 TEE_PARAM_TYPE_NONE,
83 TEE_PARAM_TYPE_NONE);
Etienne Carriere102092e2019-03-28 15:24:22 +010084
Igor Opaniuk136644a2016-09-13 13:40:56 +030085 if (param_types != exp_param_types)
86 return TEE_ERROR_BAD_PARAMETERS;
87
88 switch (params[0].value.a) {
89 case TA_SHA_SHA1:
90 algo = TEE_ALG_SHA1;
91 break;
92 case TA_SHA_SHA224:
93 algo = TEE_ALG_SHA224;
94 break;
95 case TA_SHA_SHA256:
96 algo = TEE_ALG_SHA256;
97 break;
98 case TA_SHA_SHA384:
99 algo = TEE_ALG_SHA384;
100 break;
101 case TA_SHA_SHA512:
102 algo = TEE_ALG_SHA512;
103 break;
104 default:
105 return TEE_ERROR_BAD_PARAMETERS;
106 }
107
108 if (digest_op)
109 TEE_FreeOperation(digest_op);
110
111 res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_DIGEST, 0);
112 CHECK(res, "TEE_AllocateOperation", return res;);
113
114 return TEE_SUCCESS;
115}
116
117
118void cmd_clean_res(void)
119{
120 if (digest_op)
121 TEE_FreeOperation(digest_op);
122}