blob: df0d308716e719e67dc381e0ada6e30df655fd68 [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{
47 TEE_Result res;
48 int n;
49 void *in, *out;
50 uint32_t insz;
51 uint32_t outsz;
52 uint32_t offset;
53 uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
54 TEE_PARAM_TYPE_MEMREF_OUTPUT,
55 TEE_PARAM_TYPE_VALUE_INPUT,
56 TEE_PARAM_TYPE_NONE);
57
58 if (param_types != exp_param_types)
59 return TEE_ERROR_BAD_PARAMETERS;
60
61 offset = params[2].value.b;
62 in = (uint8_t *)params[0].memref.buffer + offset;
63 insz = params[0].memref.size - offset;
64 out = params[1].memref.buffer;
65 outsz = params[1].memref.size;
66 n = params[2].value.a;
67
68 while (n--) {
69 res = TEE_DigestDoFinal(digest_op, in, insz, out, &outsz);
70 CHECK(res, "TEE_DigestDoFinal", return res;);
71 }
72 return TEE_SUCCESS;
73}
74
75TEE_Result cmd_prepare_op(uint32_t param_types, TEE_Param params[4])
76{
77 TEE_Result res;
78 uint32_t algo;
79
80 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);
84 if (param_types != exp_param_types)
85 return TEE_ERROR_BAD_PARAMETERS;
86
87 switch (params[0].value.a) {
88 case TA_SHA_SHA1:
89 algo = TEE_ALG_SHA1;
90 break;
91 case TA_SHA_SHA224:
92 algo = TEE_ALG_SHA224;
93 break;
94 case TA_SHA_SHA256:
95 algo = TEE_ALG_SHA256;
96 break;
97 case TA_SHA_SHA384:
98 algo = TEE_ALG_SHA384;
99 break;
100 case TA_SHA_SHA512:
101 algo = TEE_ALG_SHA512;
102 break;
103 default:
104 return TEE_ERROR_BAD_PARAMETERS;
105 }
106
107 if (digest_op)
108 TEE_FreeOperation(digest_op);
109
110 res = TEE_AllocateOperation(&digest_op, algo, TEE_MODE_DIGEST, 0);
111 CHECK(res, "TEE_AllocateOperation", return res;);
112
113 return TEE_SUCCESS;
114}
115
116
117void cmd_clean_res(void)
118{
119 if (digest_op)
120 TEE_FreeOperation(digest_op);
121}