1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <assert.h>
17
18 #include "hks_test_api_performance.h"
19 #include "hks_api.h"
20 #include "hks_param.h"
21 #include "hks_test_log.h"
22
23 #include "securec.h"
24
25 #define USEC_MINT 1000
26 #define USEC_UINT 1000000
27
HksGenerateKeyRun(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,struct HksParamSet * paramSetOut,uint32_t performTimes)28 int32_t HksGenerateKeyRun(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
29 struct HksParamSet *paramSetOut, uint32_t performTimes)
30 {
31 int32_t ret = 0;
32 /* if keyAlias exist, the key will be generated and refreshed */
33 for (uint32_t i = 0; i < performTimes; ++i) {
34 ret = HksGenerateKey(keyAlias, paramSetIn, paramSetOut);
35 if (ret != HKS_SUCCESS) {
36 return ret;
37 }
38 }
39 return HKS_SUCCESS;
40 }
41
HksEncryptRun(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * plainText,struct HksBlob * cipherText,uint32_t performTimes)42 int32_t HksEncryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet,
43 const struct HksBlob *plainText, struct HksBlob *cipherText, uint32_t performTimes)
44 {
45 int32_t ret = 0;
46 uint32_t oriCipherTestSize = 0;
47 if (cipherText != NULL) {
48 oriCipherTestSize = cipherText->size;
49 }
50
51 for (uint32_t i = 0; i < performTimes; ++i) {
52 if (cipherText != NULL) {
53 (void)memset_s(cipherText->data, oriCipherTestSize, 0, oriCipherTestSize);
54 cipherText->size = oriCipherTestSize;
55 }
56
57 ret = HksEncrypt(key, paramSet, plainText, cipherText);
58 if (ret != HKS_SUCCESS) {
59 return ret;
60 }
61 }
62 return HKS_SUCCESS;
63 }
64
HksDecryptRun(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * cipherText,struct HksBlob * plainText,uint32_t performTimes)65 int32_t HksDecryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet,
66 const struct HksBlob *cipherText, struct HksBlob *plainText, uint32_t performTimes)
67 {
68 int32_t ret = 0;
69 uint32_t oriPlainTextSize = 0;
70 if (plainText != NULL) {
71 oriPlainTextSize = plainText->size;
72 }
73
74 for (uint32_t i = 0; i < performTimes; ++i) {
75 if (plainText != NULL) {
76 (void)memset_s(plainText->data, oriPlainTextSize, 0, oriPlainTextSize);
77 plainText->size = oriPlainTextSize;
78 }
79 ret = HksDecrypt(key, paramSet, cipherText, plainText);
80 if (ret != HKS_SUCCESS) {
81 return ret;
82 }
83 }
84 return HKS_SUCCESS;
85 }
86
HksMacRun(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * mac,uint32_t performTimes)87 int32_t HksMacRun(const struct HksBlob *key, const struct HksParamSet *paramSet,
88 const struct HksBlob *srcData, struct HksBlob *mac, uint32_t performTimes)
89 {
90 int32_t ret = 0;
91 uint32_t oriMacSize = 0;
92 if (mac != NULL) {
93 oriMacSize = mac->size;
94 }
95
96 for (uint32_t i = 0; i < performTimes; ++i) {
97 if (mac != NULL) {
98 (void)memset_s(mac->data, oriMacSize, 0, oriMacSize);
99 mac->size = oriMacSize;
100 }
101 ret = HksMac(key, paramSet, srcData, mac);
102 if (ret != HKS_SUCCESS) {
103 return ret;
104 }
105 }
106 return HKS_SUCCESS;
107 }
108
HksDeriveKeyRun(const struct HksParamSet * paramSet,const struct HksBlob * masterKey,struct HksBlob * derivedKey,uint32_t performTimes)109 int32_t HksDeriveKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *masterKey,
110 struct HksBlob *derivedKey, uint32_t performTimes)
111 {
112 int32_t ret = 0;
113 uint32_t oriDerivedKeySize = 0;
114 if (derivedKey != NULL) {
115 oriDerivedKeySize = derivedKey->size;
116 }
117
118 for (uint32_t i = 0; i < performTimes; ++i) {
119 if (derivedKey != NULL) {
120 (void)memset_s(derivedKey->data, oriDerivedKeySize, 0, oriDerivedKeySize);
121 derivedKey->size = oriDerivedKeySize;
122 }
123 ret = HksDeriveKey(paramSet, masterKey, derivedKey);
124 if (ret != HKS_SUCCESS) {
125 return ret;
126 }
127 }
128 return HKS_SUCCESS;
129 }
130
HksDeleteKeyRun(const struct HksBlob * keyAlias,uint32_t performTimes)131 int32_t HksDeleteKeyRun(const struct HksBlob *keyAlias, uint32_t performTimes)
132 {
133 (void)performTimes;
134 return HksDeleteKey(keyAlias, NULL);
135 }
136
HksKeyExistRun(const struct HksBlob * keyAlias,uint32_t performTimes)137 int32_t HksKeyExistRun(const struct HksBlob *keyAlias, uint32_t performTimes)
138 {
139 int32_t ret = 0;
140 for (uint32_t i = 0; i < performTimes; ++i) {
141 ret = HksKeyExist(keyAlias, NULL);
142 if (ret != HKS_SUCCESS) {
143 return ret;
144 }
145 }
146 return HKS_SUCCESS;
147 }
148
HksGenerateRandomRun(struct HksBlob * random,uint32_t performTimes)149 int32_t HksGenerateRandomRun(struct HksBlob *random, uint32_t performTimes)
150 {
151 int32_t ret = 0;
152 uint32_t oriRandomSize = 0;
153 if (random != NULL) {
154 oriRandomSize = random->size;
155 }
156
157 for (uint32_t i = 0; i < performTimes; ++i) {
158 if (random != NULL) {
159 (void)memset_s(random->data, oriRandomSize, 0, oriRandomSize);
160 random->size = oriRandomSize;
161 }
162 ret = HksGenerateRandom(NULL, random);
163 if (ret != HKS_SUCCESS) {
164 return ret;
165 }
166 }
167 return HKS_SUCCESS;
168 }
169
HksAgreeKeyRun(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,struct HksBlob * agreedKey,uint32_t performTimes)170 int32_t HksAgreeKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
171 const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey, uint32_t performTimes)
172 {
173 int32_t ret = 0;
174 uint32_t oriAgreedKeySize = 0;
175 if (agreedKey != NULL) {
176 oriAgreedKeySize = agreedKey->size;
177 }
178
179 for (uint32_t i = 0; i < performTimes; ++i) {
180 if (agreedKey != NULL) {
181 (void)memset_s(agreedKey->data, oriAgreedKeySize, 0, oriAgreedKeySize);
182 agreedKey->size = oriAgreedKeySize;
183 }
184 ret = HksAgreeKey(paramSet, privateKey, peerPublicKey, agreedKey);
185 if (ret != HKS_SUCCESS) {
186 return ret;
187 }
188 }
189 return HKS_SUCCESS;
190 }
191
HksHashRun(const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * hash,uint32_t performTimes)192 int32_t HksHashRun(const struct HksParamSet *paramSet,
193 const struct HksBlob *srcData, struct HksBlob *hash, uint32_t performTimes)
194 {
195 int32_t ret = 0;
196 uint32_t oriHashSize = 0;
197 if (hash != NULL) {
198 oriHashSize = hash->size;
199 }
200
201 for (uint32_t i = 0; i < performTimes; ++i) {
202 if (hash != NULL) {
203 (void)memset_s(hash->data, oriHashSize, 0, oriHashSize);
204 hash->size = oriHashSize;
205 }
206
207 ret = HksHash(paramSet, srcData, hash);
208 if (ret != HKS_SUCCESS) {
209 return ret;
210 }
211 }
212 return HKS_SUCCESS;
213 }
214
HksBnExpModRun(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n,uint32_t performTimes)215 int32_t HksBnExpModRun(struct HksBlob *x, const struct HksBlob *a,
216 const struct HksBlob *e, const struct HksBlob *n, uint32_t performTimes)
217 {
218 int32_t ret = 0;
219 uint32_t oriXSize = 0;
220 if (x != NULL) {
221 oriXSize = x->size;
222 }
223
224 for (uint32_t i = 0; i < performTimes; ++i) {
225 if (x != NULL) {
226 (void)memset_s(x->data, oriXSize, 0, oriXSize);
227 x->size = oriXSize;
228 }
229 ret = HksBnExpMod(x, a, e, n);
230 if (ret != HKS_SUCCESS) {
231 return ret;
232 }
233 }
234 return HKS_SUCCESS;
235 }
236