• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef HUKS_HDI_TEMPLATE_H
17 #define HUKS_HDI_TEMPLATE_H
18 
19 #include "huks_sa_type.h"
20 
21 typedef struct HksBlob TargetBlob;
22 typedef struct HksParamSet TargetParamSet;
23 
24 #define HDI_ADAPTER_PARAM(oldParamPtr, newParamPtr) ((oldParamPtr) == NULL ?  NULL : (newParamPtr))
25 
26 
27 #define HDI_CONVERTER_PARAM_IN_BLOB(fromHuksBlobPtr, toHksBlobPtr)  \
28     if ((fromHuksBlobPtr) != NULL && (toHksBlobPtr) != NULL) {      \
29         (toHksBlobPtr)->data = (fromHuksBlobPtr)->data;             \
30         (toHksBlobPtr)->size = (fromHuksBlobPtr)->dataLen;          \
31     }
32 
33 #define HDI_CONVERTER_PARAM_IN_PARAMSET(fromHuksParamSetPtr, toHksParamSetPtr)   \
34     if ((fromHuksParamSetPtr) != NULL && (fromHuksParamSetPtr)->data != NULL &&  \
35         (fromHuksParamSetPtr)->dataLen >= sizeof(TargetParamSet)) {              \
36         (toHksParamSetPtr) = (TargetParamSet *)(fromHuksParamSetPtr)->data;      \
37     }
38 
39 #define HDI_CONVERTER_PARAM_OUT_BLOB(fromHksBlobPtr, toHuksBlobPtr) \
40     if ((fromHksBlobPtr) != NULL && (toHuksBlobPtr) != NULL) {      \
41         (toHuksBlobPtr)->data = (fromHksBlobPtr)->data;             \
42         (toHuksBlobPtr)->dataLen = (fromHksBlobPtr)->size;          \
43     }
44 
45 #define HDI_CONVERTER_FUNC_GENERATEKEY(keyAlias, paramSet, keyIn, keyOut, ret, func) \
46     TargetBlob keyAliasCore = {0};  \
47     TargetParamSet *paramSetCore = NULL;  \
48     TargetBlob keyInCore = {0};  \
49     TargetBlob keyOutCore = {0};  \
50     HDI_CONVERTER_PARAM_IN_BLOB(keyAlias, &keyAliasCore)  \
51     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
52     HDI_CONVERTER_PARAM_IN_BLOB(keyIn, &keyInCore)  \
53     HDI_CONVERTER_PARAM_IN_BLOB(keyOut, &keyOutCore)  \
54     ret = (func)(HDI_ADAPTER_PARAM(keyAlias, &keyAliasCore),  \
55                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
56                  HDI_ADAPTER_PARAM(keyIn, &keyInCore),        \
57                  HDI_ADAPTER_PARAM(keyOut, &keyOutCore));  \
58     HDI_CONVERTER_PARAM_OUT_BLOB(&keyOutCore, keyOut)
59 
60 #define HDI_CONVERTER_FUNC_IMPORTKEY(keyAlias, key, paramSet, keyOut, ret, func) \
61     TargetBlob keyAliasCore = {0};   \
62     TargetParamSet *paramSetCore = NULL;   \
63     TargetBlob keyCore = {0};   \
64     TargetBlob keyOutCore = {0};   \
65     HDI_CONVERTER_PARAM_IN_BLOB(keyAlias, &keyAliasCore)   \
66     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)   \
67     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)   \
68     HDI_CONVERTER_PARAM_IN_BLOB(keyOut, &keyOutCore)   \
69     ret = (func)(HDI_ADAPTER_PARAM(keyAlias, &keyAliasCore), \
70                  HDI_ADAPTER_PARAM(key, &keyCore),   \
71                  HDI_ADAPTER_PARAM(paramSet, paramSetCore), \
72                  HDI_ADAPTER_PARAM(keyOut, &keyOutCore));   \
73     HDI_CONVERTER_PARAM_OUT_BLOB(&keyOutCore, keyOut)
74 
75 #define HDI_CONVERTER_FUNC_IMPORTWRAPPEDKEY(wrappedKeyAlias, key, wrappedKeyData, paramSet, keyOut, ret, func)   \
76     TargetBlob wrappingKeyAliasCore = {0};  \
77     TargetBlob keyCore = {0};  \
78     TargetBlob wrappedKeyDataCore = {0};  \
79     TargetParamSet *paramSetCore = NULL;  \
80     TargetBlob keyOutCore = {0};  \
81     HDI_CONVERTER_PARAM_IN_BLOB(wrappingKeyAlias, &wrappingKeyAliasCore)  \
82     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
83     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
84     HDI_CONVERTER_PARAM_IN_BLOB(wrappedKeyData, &wrappedKeyDataCore)  \
85     HDI_CONVERTER_PARAM_IN_BLOB(keyOut, &keyOutCore)  \
86     ret = (func)(HDI_ADAPTER_PARAM(wrappingKeyAlias, &wrappedKeyDataCore),  \
87                  HDI_ADAPTER_PARAM(key, &keyCore),  \
88                  HDI_ADAPTER_PARAM(wrappedKeyData, &wrappedKeyDataCore),  \
89                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
90                  HDI_ADAPTER_PARAM(keyOut, &keyOutCore));  \
91     HDI_CONVERTER_PARAM_OUT_BLOB(&keyOutCore, keyOut)
92 
93 #define HDI_CONVERTER_FUNC_EXPORTPUBLICKEY(key, paramSet, keyOut, ret, func)  \
94     TargetBlob keyCore = {0};  \
95     TargetParamSet *paramSetCore = NULL;  \
96     TargetBlob keyOutCore = {0};  \
97     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
98     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
99     HDI_CONVERTER_PARAM_IN_BLOB(keyOut, &keyOutCore)  \
100     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
101                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
102                  HDI_ADAPTER_PARAM(keyOut, &keyOutCore));  \
103     HDI_CONVERTER_PARAM_OUT_BLOB(&keyOutCore, keyOut)
104 
105 #define HDI_CONVERTER_FUNC_INIT(key, paramSet, handle, token, ret, func)  \
106     TargetBlob keyCore = {0};  \
107     TargetParamSet *paramSetCore = NULL;  \
108     TargetBlob handleCore = {0};  \
109     TargetBlob tokenCore = {0};  \
110     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
111     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
112     HDI_CONVERTER_PARAM_IN_BLOB(handle, &handleCore)  \
113     HDI_CONVERTER_PARAM_IN_BLOB(token, &tokenCore)  \
114     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
115                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
116                  HDI_ADAPTER_PARAM(handle, &handleCore),  \
117                  HDI_ADAPTER_PARAM(token, &tokenCore));  \
118     HDI_CONVERTER_PARAM_OUT_BLOB(&handleCore, handle)  \
119     HDI_CONVERTER_PARAM_OUT_BLOB(&tokenCore, token)
120 
121 #define HDI_CONVERTER_FUNC_UPDATE(handle, paramSet, inData, outData, ret, func)  \
122     TargetBlob handleCore = {0};  \
123     TargetParamSet *paramSetCore = NULL;  \
124     TargetBlob inDataCore = {0};  \
125     TargetBlob outDataCore = {0};  \
126     HDI_CONVERTER_PARAM_IN_BLOB(handle, &handleCore)  \
127     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
128     HDI_CONVERTER_PARAM_IN_BLOB(inData, &inDataCore)  \
129     HDI_CONVERTER_PARAM_IN_BLOB(outData, &outDataCore)  \
130     ret = (func)(HDI_ADAPTER_PARAM(handle, &handleCore),  \
131                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
132                  HDI_ADAPTER_PARAM(inData, &inDataCore),  \
133                  HDI_ADAPTER_PARAM(outData, &outDataCore));  \
134     HDI_CONVERTER_PARAM_OUT_BLOB(&outDataCore, outData)
135 
136 #define HDI_CONVERTER_FUNC_FINISH(handle, paramSet, inData, outData, ret, func)  \
137     TargetBlob handleCore = {0};  \
138     TargetParamSet *paramSetCore = NULL;  \
139     TargetBlob inDataCore = {0};  \
140     TargetBlob outDataCore = {0};  \
141     HDI_CONVERTER_PARAM_IN_BLOB(handle, &handleCore)  \
142     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
143     HDI_CONVERTER_PARAM_IN_BLOB(inData, &inDataCore)  \
144     HDI_CONVERTER_PARAM_IN_BLOB(outData, &outDataCore)  \
145     ret = (func)(HDI_ADAPTER_PARAM(handle, &handleCore),  \
146                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
147                  HDI_ADAPTER_PARAM(inData, &inDataCore),  \
148                  HDI_ADAPTER_PARAM(outData, &outDataCore));  \
149     HDI_CONVERTER_PARAM_OUT_BLOB(&outDataCore, outData)
150 
151 #define HDI_CONVERTER_FUNC_ABORT(handle, paramSet, ret, func)  \
152     TargetBlob handleCore = {0};  \
153     TargetParamSet *paramSetCore = NULL;  \
154     HDI_CONVERTER_PARAM_IN_BLOB(handle, &handleCore)  \
155     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
156     ret = (func)(HDI_ADAPTER_PARAM(handle, &handleCore),  \
157                  HDI_ADAPTER_PARAM(paramSet, paramSetCore));
158 
159 #define HDI_CONVERTER_FUNC_CHECKKEYVALIDITY(paramSet, key, ret, func)  \
160     TargetBlob keyCore = {0};  \
161     TargetParamSet *paramSetCore = NULL;  \
162     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
163     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
164     ret = (func)(HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
165                  HDI_ADAPTER_PARAM(key, &keyCore));
166 
167 #define HDI_CONVERTER_FUNC_ATTESTKEY(key, paramSet, certChain, ret, func)  \
168     TargetBlob keyCore = {0};  \
169     TargetParamSet *paramSetCore = NULL;  \
170     TargetBlob certChainCore = {0};  \
171     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
172     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
173     HDI_CONVERTER_PARAM_IN_BLOB(certChain, &certChainCore)  \
174     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
175                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
176                  HDI_ADAPTER_PARAM(certChain, &certChainCore));  \
177     HDI_CONVERTER_PARAM_OUT_BLOB(&certChainCore, certChain)
178 
179 #define HDI_CONVERTER_FUNC_GENERATERANDOM(paramSet, random, ret, func)  \
180     TargetParamSet *paramSetCore = NULL;  \
181     TargetBlob randomCore = {0};  \
182     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
183     HDI_CONVERTER_PARAM_IN_BLOB(random, &randomCore)  \
184     ret = (func)(HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
185                  HDI_ADAPTER_PARAM(random, &randomCore));  \
186     HDI_CONVERTER_PARAM_OUT_BLOB(&randomCore, random)
187 
188 #define HDI_CONVERTER_FUNC_SIGN(key, paramSet, srcData, signature, ret, func) \
189     TargetBlob keyCore = {0}; \
190     TargetParamSet *paramSetCore = NULL; \
191     TargetBlob srcDataCore = {0}; \
192     TargetBlob signatureCore = {0}; \
193     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore) \
194     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore) \
195     HDI_CONVERTER_PARAM_IN_BLOB(srcData, &srcDataCore) \
196     HDI_CONVERTER_PARAM_IN_BLOB(signature, &signatureCore) \
197     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore), \
198                  HDI_ADAPTER_PARAM(paramSet, paramSetCore), \
199                  HDI_ADAPTER_PARAM(srcData, &srcDataCore), \
200                  HDI_ADAPTER_PARAM(signature, &signatureCore)); \
201     HDI_CONVERTER_PARAM_OUT_BLOB(&signatureCore, signature)
202 
203 #define HDI_CONVERTER_FUNC_VERIFY(key, paramSet, srcData, signature, ret, func)  \
204     TargetBlob keyCore = {0};  \
205     TargetParamSet *paramSetCore = NULL;  \
206     TargetBlob srcDataCore = {0};  \
207     TargetBlob signatureCore = {0};  \
208     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
209     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
210     HDI_CONVERTER_PARAM_IN_BLOB(srcData, &srcDataCore)  \
211     HDI_CONVERTER_PARAM_IN_BLOB(signature, &signatureCore)  \
212     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
213                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
214                  HDI_ADAPTER_PARAM(srcData, &srcDataCore),  \
215                  HDI_ADAPTER_PARAM(signature, &signatureCore));
216 
217 #define HDI_CONVERTER_FUNC_ENCRYPT(key, paramSet, plainText, cipherText, ret, func)  \
218     TargetBlob keyCore = {0};  \
219     TargetParamSet *paramSetCore = NULL;  \
220     TargetBlob plainTextCore = {0};  \
221     TargetBlob cipherTextCore = {0};  \
222     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
223     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
224     HDI_CONVERTER_PARAM_IN_BLOB(plainText, &plainTextCore)  \
225     HDI_CONVERTER_PARAM_IN_BLOB(cipherText, &cipherTextCore)  \
226     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
227                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
228                  HDI_ADAPTER_PARAM(plainText, &plainTextCore),  \
229                  HDI_ADAPTER_PARAM(cipherText, &cipherTextCore));  \
230     HDI_CONVERTER_PARAM_OUT_BLOB(&cipherTextCore, cipherText)
231 
232 #define HDI_CONVERTER_FUNC_DECRYPT(key, paramSet, cipherText, plainText, ret, func)  \
233     TargetBlob keyCore = {0};  \
234     TargetParamSet *paramSetCore = NULL;  \
235     TargetBlob cipherTextCore = {0};  \
236     TargetBlob plainTextCore = {0};  \
237     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
238     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
239     HDI_CONVERTER_PARAM_IN_BLOB(cipherText, &cipherTextCore)  \
240     HDI_CONVERTER_PARAM_IN_BLOB(plainText, &plainTextCore)  \
241     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
242                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
243                  HDI_ADAPTER_PARAM(cipherText, &cipherTextCore),  \
244                  HDI_ADAPTER_PARAM(plainText, &plainTextCore));  \
245     HDI_CONVERTER_PARAM_OUT_BLOB(&plainTextCore, plainText)
246 
247 #define HDI_CONVERTER_FUNC_AGREEKEY(paramSet, privateKey, peerPublicKey, agreedKey, ret, func)  \
248     TargetParamSet *paramSetCore = NULL;  \
249     TargetBlob privateKeyCore = {0};  \
250     TargetBlob peerPublicKeyCore = {0};  \
251     TargetBlob agreedKeyCore = {0};  \
252     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
253     HDI_CONVERTER_PARAM_IN_BLOB(privateKey, &privateKeyCore)  \
254     HDI_CONVERTER_PARAM_IN_BLOB(peerPublicKey, &peerPublicKeyCore)  \
255     HDI_CONVERTER_PARAM_IN_BLOB(agreedKey, &agreedKeyCore)  \
256     ret = (func)(HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
257                  HDI_ADAPTER_PARAM(privateKey, &privateKeyCore),  \
258                  HDI_ADAPTER_PARAM(peerPublicKey, &peerPublicKeyCore),  \
259                  HDI_ADAPTER_PARAM(agreedKey, &agreedKeyCore));  \
260     HDI_CONVERTER_PARAM_OUT_BLOB(&agreedKeyCore, agreedKey)
261 
262 
263 #define HDI_CONVERTER_FUNC_DERIVEKEY(paramSet, kdfKey, derivedKey, ret, func)  \
264     TargetParamSet *paramSetCore = NULL;  \
265     TargetBlob kdfKeyCore = {0};  \
266     TargetBlob derivedKeyCore = {0};  \
267     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
268     HDI_CONVERTER_PARAM_IN_BLOB(kdfKey, &kdfKeyCore)  \
269     HDI_CONVERTER_PARAM_IN_BLOB(derivedKey, &derivedKeyCore)  \
270     ret = (func)(HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
271                  HDI_ADAPTER_PARAM(kdfKey, &kdfKeyCore),  \
272                  HDI_ADAPTER_PARAM(derivedKey, &derivedKeyCore));  \
273     HDI_CONVERTER_PARAM_OUT_BLOB(&derivedKeyCore, derivedKey)
274 
275 #define HDI_CONVERTER_FUNC_MAC(key, paramSet, srcData, mac, ret, func)  \
276     TargetParamSet *paramSetCore = NULL;  \
277     TargetBlob keyCore = {0};  \
278     TargetBlob srcDataCore = {0};  \
279     TargetBlob macCore = {0};  \
280     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
281     HDI_CONVERTER_PARAM_IN_BLOB(key, &keyCore)  \
282     HDI_CONVERTER_PARAM_IN_BLOB(srcData, &srcDataCore)  \
283     HDI_CONVERTER_PARAM_IN_BLOB(mac, &macCore)  \
284     ret = (func)(HDI_ADAPTER_PARAM(key, &keyCore),  \
285                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
286                  HDI_ADAPTER_PARAM(srcData, &srcDataCore),  \
287                  HDI_ADAPTER_PARAM(mac, &macCore));  \
288     HDI_CONVERTER_PARAM_OUT_BLOB(&macCore, mac)
289 
290 #define HDI_CONVERTER_FUNC_UPGRADEKEY(oldKey, paramSet, newKey, ret, func)  \
291     TargetParamSet *paramSetCore = NULL;  \
292     TargetBlob oldKeyCore = {0};  \
293     TargetBlob newKeyCore = {0};  \
294     HDI_CONVERTER_PARAM_IN_PARAMSET(paramSet, paramSetCore)  \
295     HDI_CONVERTER_PARAM_IN_BLOB(oldKey, &oldKeyCore)  \
296     HDI_CONVERTER_PARAM_IN_BLOB(newKey, &newKeyCore)  \
297     ret = (func)(HDI_ADAPTER_PARAM(oldKey, &oldKeyCore),  \
298                  HDI_ADAPTER_PARAM(paramSet, paramSetCore),  \
299                  HDI_ADAPTER_PARAM(newKey, &newKeyCore));  \
300     HDI_CONVERTER_PARAM_OUT_BLOB(&newKeyCore, newKey)
301 
302 #define HDI_CONVERTER_FUNC_EXPORTCHIPSETPLATFORMPUBLICKEY(salt, scene, publicKey, ret, func)  \
303     TargetBlob saltCore = {0};  \
304     TargetBlob publicKeyCore = {0};  \
305     uint32_t sceneInt = (uint32_t) scene;  \
306     HDI_CONVERTER_PARAM_IN_BLOB(salt, &saltCore)  \
307     HDI_CONVERTER_PARAM_IN_BLOB(publicKey, &publicKeyCore)  \
308     ret = (func)(HDI_ADAPTER_PARAM(salt, &saltCore),  \
309               sceneInt,  \
310               HDI_ADAPTER_PARAM(publicKey, &publicKeyCore));  \
311     HDI_CONVERTER_PARAM_OUT_BLOB(&publicKeyCore, publicKey)
312 
313 
314 #undef HUKS_NULL_POINTER
315 
316 #ifdef __cplusplus
317 #define HUKS_NULL_POINTER nullptr
318 #else
319 #define HUKS_NULL_POINTER NULL
320 #endif
321 
322 #define HUKS_HDI_IF_NOT_SUCC_RETURN(RESULT, ERROR_CODE) \
323 if ((RESULT) != HUKS_SUCCESS) { \
324     return (ERROR_CODE); \
325 }
326 
327 #define HUKS_HDI_IF_NULL_LOGE_RETURN(OBJECT, ERROR_CODE, LOG_MESSAGE, ...) \
328 if ((OBJECT) == HUKS_NULL_POINTER) { \
329     HDF_LOGE(LOG_MESSAGE, ##__VA_ARGS__); \
330     return (ERROR_CODE); \
331 }
332 
333 #endif
334