• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "iso_task_common.h"
17 #include <time.h>
18 #include "das_task_common.h"
19 #include "das_module_defines.h"
20 #include "hc_log.h"
21 #include "hc_types.h"
22 #include "iso_protocol_common.h"
23 #include "protocol_common.h"
24 
GenerateReturnKey(IsoParams * params,uint8_t * returnKey,uint32_t returnKeyLen)25 static int GenerateReturnKey(IsoParams *params, uint8_t *returnKey, uint32_t returnKeyLen)
26 {
27     int hkdfSaltLen = params->baseParams.randPeer.length + params->baseParams.randPeer.length;
28     int res;
29     uint8_t *hkdfSalt = (uint8_t *)HcMalloc(hkdfSaltLen, 0);
30     if (hkdfSalt == NULL) {
31         return HC_ERR_ALLOC_MEMORY;
32     }
33     if (params->isClient) {
34         if (memcpy_s(hkdfSalt, hkdfSaltLen, params->baseParams.randSelf.val,
35             params->baseParams.randSelf.length) != EOK) {
36             LOGE("Copy randSelf failed.");
37             res = HC_ERR_MEMORY_COPY;
38             goto ERR;
39         }
40         if (memcpy_s(hkdfSalt + params->baseParams.randSelf.length, hkdfSaltLen - params->baseParams.randSelf.length,
41             params->baseParams.randPeer.val, params->baseParams.randPeer.length) != EOK) {
42             LOGE("Copy randPeer failed.");
43             res = HC_ERR_MEMORY_COPY;
44             goto ERR;
45         }
46     } else {
47         if (memcpy_s(hkdfSalt, hkdfSaltLen, params->baseParams.randPeer.val,
48             params->baseParams.randPeer.length) != EOK) {
49             LOGE("Copy randPeer failed.");
50             res = HC_ERR_MEMORY_COPY;
51             goto ERR;
52         }
53         if (memcpy_s(hkdfSalt + params->baseParams.randPeer.length, hkdfSaltLen - params->baseParams.randPeer.length,
54             params->baseParams.randSelf.val, params->baseParams.randSelf.length) != EOK) {
55             LOGE("Copy randSelf failed.");
56             res = HC_ERR_MEMORY_COPY;
57             goto ERR;
58         }
59     }
60     Uint8Buff hkdfSaltBuf = { hkdfSalt, hkdfSaltLen };
61     Uint8Buff keyInfoBuf = { (uint8_t *)GENERATE_RETURN_KEY_STR, (uint32_t)strlen(GENERATE_RETURN_KEY_STR) };
62     Uint8Buff returnKeyBuf = { returnKey, returnKeyLen };
63     res = params->baseParams.loader->computeHkdf(&(params->baseParams.sessionKey), &hkdfSaltBuf, &keyInfoBuf,
64         &returnKeyBuf, false);
65     if (res != HC_SUCCESS) {
66         LOGE("computeHkdf for returnKey failed.");
67         goto ERR;
68     }
69 ERR:
70     FreeAndCleanKey(&(params->baseParams.sessionKey));
71     HcFree(hkdfSalt);
72     return res;
73 }
74 
GenerateEncResult(const IsoParams * params,int message,CJson * sendToPeer,const char * aad)75 int GenerateEncResult(const IsoParams *params, int message, CJson *sendToPeer, const char *aad)
76 {
77     CJson *payload = NULL;
78     uint8_t *out = NULL;
79     uint8_t nonce[NONCE_SIZE] = { 0 };
80     Uint8Buff nonceBuf = { nonce, sizeof(nonce) };
81     int ret = params->baseParams.loader->generateRandom(&nonceBuf);
82     if (ret != 0) {
83         LOGE("Generate nonce failed, res: %x.", ret);
84         return ret;
85     }
86 
87     int result = 0;
88     Uint8Buff plainBuf = { (uint8_t *)&result, sizeof(int) };
89     GcmParam encryptInfo;
90     encryptInfo.nonce = nonce;
91     encryptInfo.nonceLen = NONCE_SIZE;
92     encryptInfo.aad = (uint8_t *)aad;
93     encryptInfo.aadLen = (uint32_t)strlen(aad);
94     out = (uint8_t *)HcMalloc((sizeof(int) + TAG_LEN), 0);
95     if (out == NULL) {
96         ret = HC_ERR_ALLOC_MEMORY;
97         goto ERR;
98     }
99     Uint8Buff outBuf = { out, sizeof(int) + TAG_LEN };
100     ret = params->baseParams.loader->aesGcmEncrypt(&params->baseParams.sessionKey, &plainBuf,
101         &encryptInfo, false, &outBuf);
102     if (ret != HC_SUCCESS) {
103         goto ERR;
104     }
105     payload = CreateJson();
106     if (payload == NULL) {
107         ret = HC_ERR_ALLOC_MEMORY;
108         goto ERR;
109     }
110     GOTO_ERR_AND_SET_RET(AddByteToJson(payload, FIELD_NONCE, nonce, sizeof(nonce)), ret);
111     GOTO_ERR_AND_SET_RET(AddByteToJson(payload, FIELD_ENC_RESULT, out, sizeof(int) + TAG_LEN), ret);
112     GOTO_ERR_AND_SET_RET(AddIntToJson(payload, FIELD_OPERATION_CODE, params->opCode), ret);
113     GOTO_ERR_AND_SET_RET(AddObjToJson(sendToPeer, FIELD_PAYLOAD, payload), ret);
114     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToPeer, FIELD_MESSAGE, message), ret);
115     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToPeer, FIELD_AUTH_FORM, AUTH_FORM_ACCOUNT_UNRELATED), ret);
116 ERR:
117     FreeJson(payload);
118     HcFree(out);
119     return ret;
120 }
121 
SendResultToFinalSelf(IsoParams * params,CJson * out,bool isNeedReturnKey)122 int SendResultToFinalSelf(IsoParams *params, CJson *out, bool isNeedReturnKey)
123 {
124     CJson *sendToSelf = CreateJson();
125     if (sendToSelf == NULL) {
126         LOGE("Create sendToSelf json failed.");
127         return HC_ERR_JSON_CREATE;
128     }
129     uint8_t *returnSessionKey = NULL;
130     int res = 0;
131     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToSelf, FIELD_OPERATION_CODE, OP_BIND), res);
132     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToSelf, FIELD_AUTH_FORM, AUTH_FORM_ACCOUNT_UNRELATED), res);
133     if (isNeedReturnKey) {
134         returnSessionKey = (uint8_t *)HcMalloc(params->keyLen, 0);
135         if (returnSessionKey == NULL) {
136             LOGE("Malloc for returnSessionKey failed.");
137             res = HC_ERR_ALLOC_MEMORY;
138             goto ERR;
139         }
140         res = GenerateReturnKey(params, returnSessionKey, params->keyLen);
141         if (res != 0) {
142             LOGE("gen return key failed, res:%d", res);
143             goto ERR;
144         }
145         GOTO_ERR_AND_SET_RET(AddByteToJson(sendToSelf, FIELD_SESSION_KEY, returnSessionKey, params->keyLen), res);
146     }
147     GOTO_ERR_AND_SET_RET(AddObjToJson(out, FIELD_SEND_TO_SELF, sendToSelf), res);
148 ERR:
149     ClearSensitiveStringInJson(sendToSelf, FIELD_SESSION_KEY);
150     FreeJson(sendToSelf);
151     if (returnSessionKey != NULL) {
152         (void)memset_s(returnSessionKey, params->keyLen, 0, params->keyLen);
153     }
154     HcFree(returnSessionKey);
155     return res;
156 }
157 
GenEncResult(IsoParams * params,int message,CJson * out,const char * aad,bool isNeedReturnKey)158 int GenEncResult(IsoParams *params, int message, CJson *out, const char *aad, bool isNeedReturnKey)
159 {
160     CJson *sendToSelf = CreateJson();
161     if (sendToSelf == NULL) {
162         LOGE("Create sendToSelf json failed.");
163         return HC_ERR_JSON_CREATE;
164     }
165     CJson *sendToPeer = CreateJson();
166     if (sendToPeer == NULL) {
167         LOGE("Create sendToPeer json failed.");
168         FreeJson(sendToSelf);
169         return HC_ERR_JSON_CREATE;
170     }
171 
172     uint8_t *returnKey = NULL;
173     int res = GenerateEncResult(params, message, sendToPeer, aad);
174     if (res != 0) {
175         goto ERR;
176     }
177     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToSelf, FIELD_AUTH_FORM, AUTH_FORM_ACCOUNT_UNRELATED), res);
178     if (isNeedReturnKey) {
179         returnKey = (uint8_t *)HcMalloc(params->keyLen, 0);
180         if (returnKey == NULL) {
181             res = HC_ERR_ALLOC_MEMORY;
182             goto ERR;
183         }
184         res = GenerateReturnKey(params, returnKey, params->keyLen);
185         if (res != 0) {
186             LOGE("gen return key failed, res:%d", res);
187             goto ERR;
188         }
189         GOTO_ERR_AND_SET_RET(AddByteToJson(sendToSelf, FIELD_SESSION_KEY, returnKey,
190             params->keyLen), res);
191     }
192     GOTO_ERR_AND_SET_RET(AddIntToJson(sendToSelf, FIELD_OPERATION_CODE, params->opCode), res);
193     GOTO_ERR_AND_SET_RET(AddObjToJson(out, FIELD_SEND_TO_PEER, sendToPeer), res);
194     GOTO_ERR_AND_SET_RET(AddObjToJson(out, FIELD_SEND_TO_SELF, sendToSelf), res);
195 ERR:
196     ClearSensitiveStringInJson(sendToSelf, FIELD_SESSION_KEY);
197     FreeJson(sendToPeer);
198     FreeJson(sendToSelf);
199     if (returnKey != NULL) {
200         (void)memset_s(returnKey, params->keyLen, 0, params->keyLen);
201     }
202     HcFree(returnKey);
203     return res;
204 }
205 
CheckEncResult(IsoParams * params,const CJson * in,const char * aad)206 int CheckEncResult(IsoParams *params, const CJson *in, const char *aad)
207 {
208     int result = 0;
209     int res;
210     uint8_t *nonce = NULL;
211     uint8_t *encResult = NULL;
212 
213     nonce = (uint8_t *)HcMalloc(NONCE_SIZE, 0);
214     if (nonce == NULL) {
215         res = HC_ERR_ALLOC_MEMORY;
216         goto ERR;
217     }
218     GOTO_ERR_AND_SET_RET(GetByteFromJson(in, FIELD_NONCE, nonce, NONCE_SIZE), res);
219     encResult = (uint8_t *)HcMalloc(sizeof(int) + TAG_LEN, 0);
220     if (encResult == NULL) {
221         res = HC_ERR_ALLOC_MEMORY;
222         goto ERR;
223     }
224     GOTO_ERR_AND_SET_RET(GetByteFromJson(in, FIELD_ENC_RESULT, encResult, sizeof(int) + TAG_LEN), res);
225     Uint8Buff outBuf = { (uint8_t *)&result, sizeof(int) };
226     Uint8Buff encResultBuf = { encResult, sizeof(int) + TAG_LEN };
227     GcmParam gcmParam;
228     gcmParam.aad = (uint8_t *)aad;
229     gcmParam.aadLen = (uint32_t)strlen(aad);
230     gcmParam.nonce = nonce;
231     gcmParam.nonceLen = NONCE_SIZE;
232 
233     res = params->baseParams.loader->aesGcmDecrypt(&params->baseParams.sessionKey, &encResultBuf, &gcmParam, false,
234         &outBuf);
235     if (res != 0) {
236         LOGE("decrypt result failed, res:%d", res);
237         goto ERR;
238     }
239 ERR:
240     HcFree(nonce);
241     HcFree(encResult);
242     return res;
243 }
244 
DeleteAuthCode(const IsoParams * params)245 void DeleteAuthCode(const IsoParams *params)
246 {
247     uint8_t *keyAlias = (uint8_t *)HcMalloc(ISO_KEY_ALIAS_LEN, 0);
248     if (keyAlias == NULL) {
249         return;
250     }
251     int res = GenerateKeyAliasInIso(params, keyAlias, ISO_KEY_ALIAS_LEN, true);
252     if (res != 0) {
253         LOGE("GenerateKeyAliasInIso failed, res:%d", res);
254         goto ERR;
255     }
256     LOGI("AuthCode alias: %x%x%x%x****.", keyAlias[0], keyAlias[1], keyAlias[2], keyAlias[3]);
257     Uint8Buff outKeyAlias = { keyAlias, ISO_KEY_ALIAS_LEN };
258     params->baseParams.loader->deleteKey(&outKeyAlias);
259 ERR:
260     HcFree(keyAlias);
261     return;
262 }
263 
DestroyIsoParams(IsoParams * params)264 void DestroyIsoParams(IsoParams *params)
265 {
266     if (params == NULL) {
267         return;
268     }
269 
270     DestroyIsoBaseParams(&params->baseParams);
271 
272     if (params->packageName != NULL) {
273         HcFree(params->packageName);
274         params->packageName = NULL;
275     }
276     if (params->serviceType != NULL) {
277         HcFree(params->serviceType);
278         params->serviceType = NULL;
279     }
280     if (params->seed.val != NULL) {
281         HcFree(params->seed.val);
282         params->seed.val = NULL;
283     }
284     if (params->pinCodeString != NULL) {
285         (void)memset_s(params->pinCodeString, strlen(params->pinCodeString), 0, strlen(params->pinCodeString));
286         HcFree(params->pinCodeString);
287         params->pinCodeString = NULL;
288     }
289     (void)memset_s(params, sizeof(IsoParams), 0, sizeof(IsoParams));
290 }
291 
FillAuthId(IsoParams * params,const CJson * in)292 static int FillAuthId(IsoParams *params, const CJson *in)
293 {
294     const char *authId = GetStringFromJson(in, FIELD_SELF_AUTH_ID);
295     if (authId == NULL) {
296         LOGE("get self authId failed");
297         return HC_ERROR;
298     }
299     uint32_t authIdLen = strlen(authId);
300     if (authIdLen == 0 || authIdLen > MAX_AUTH_ID_LEN) {
301         LOGE("Invalid authIdSelfLen: %d.", authIdLen);
302         return HC_ERR_INVALID_PARAMS;
303     }
304     params->baseParams.authIdSelf.length = authIdLen;
305     params->baseParams.authIdSelf.val = (uint8_t *)HcMalloc(params->baseParams.authIdSelf.length, 0);
306     if (params->baseParams.authIdSelf.val == NULL) {
307         LOGE("malloc authIdSelf failed");
308         return HC_ERROR;
309     }
310     if (memcpy_s(params->baseParams.authIdSelf.val, params->baseParams.authIdSelf.length,
311         authId, strlen(authId)) != EOK) {
312         LOGE("Memcpy authIdSelf failed.");
313         return HC_ERR_MEMORY_COPY;
314     }
315 
316     if (params->opCode == OP_BIND) {
317         params->baseParams.authIdPeer.length = 0;
318         params->baseParams.authIdPeer.val = NULL;
319     } else {
320         authId = GetStringFromJson(in, FIELD_PEER_AUTH_ID);
321         if (authId == NULL) {
322             LOGE("get peer authId failed");
323             return HC_ERROR;
324         }
325         authIdLen = strlen(authId);
326         if (authIdLen == 0 || authIdLen > MAX_AUTH_ID_LEN) {
327             LOGE("Invalid authIdPeerLen %d.", authIdLen);
328             return HC_ERR_INVALID_PARAMS;
329         }
330         params->baseParams.authIdPeer.length = authIdLen;
331         params->baseParams.authIdPeer.val = (uint8_t *)HcMalloc(params->baseParams.authIdPeer.length, 0);
332         if (params->baseParams.authIdPeer.val == NULL) {
333             LOGE("malloc authIdPeer failed");
334             return HC_ERROR;
335         }
336         if (memcpy_s(params->baseParams.authIdPeer.val, params->baseParams.authIdPeer.length,
337             authId, strlen(authId)) != EOK) {
338             LOGE("Memcpy authIdPeer failed.");
339             return HC_ERR_MEMORY_COPY;
340         }
341     }
342 
343     return HC_SUCCESS;
344 }
345 
FillPkgNameAndServiceType(IsoParams * params,const CJson * in)346 static int FillPkgNameAndServiceType(IsoParams *params, const CJson *in)
347 {
348     const char *serviceType = GetStringFromJson(in, FIELD_SERVICE_TYPE);
349     if (serviceType == NULL) {
350         LOGE("get serviceType failed");
351         return HC_ERROR;
352     }
353     params->serviceType = (char *)HcMalloc((uint32_t)(strlen(serviceType) + 1), 0);
354     if (params->serviceType == NULL) {
355         LOGE("malloc serviceType failed");
356         return HC_ERR_ALLOC_MEMORY;
357     }
358     if (memcpy_s(params->serviceType, strlen(serviceType) + 1, serviceType, strlen(serviceType)) != EOK) {
359         LOGE("memcpy serviceType failed.");
360         return HC_ERR_MEMORY_COPY;
361     }
362     const char *packageName = GetStringFromJson(in, FIELD_PKG_NAME);
363     if (packageName == NULL) {
364         LOGE("get packageName failed");
365         return HC_ERROR;
366     }
367     params->packageName = (char *)HcMalloc((uint32_t)(strlen(packageName) + 1), 0);
368     if (params->packageName == NULL) {
369         LOGE("malloc packageName failed");
370         return HC_ERROR;
371     }
372     if (memcpy_s(params->packageName, strlen(packageName) + 1, packageName, strlen(packageName)) != EOK) {
373         LOGE("memcpy packageName failed.");
374         return HC_ERR_MEMORY_COPY;
375     }
376     return HC_SUCCESS;
377 }
378 
FillPin(IsoParams * params,const CJson * in)379 static int FillPin(IsoParams *params, const CJson *in)
380 {
381     if (params->opCode == OP_BIND) {
382         const char *pinString = GetStringFromJson(in, FIELD_PIN_CODE);
383         if (pinString == NULL) {
384             LOGE("Get pin failed.");
385             return HC_ERROR;
386         }
387         if (strlen(pinString) < MIN_PIN_LEN || strlen(pinString) > MAX_PIN_LEN) {
388             LOGE("Pin is too short.");
389             return HC_ERR_INVALID_PARAMS;
390         }
391         params->pinCodeString = (char *)HcMalloc(strlen(pinString) + 1, 0);
392         if (params->pinCodeString == NULL) {
393             LOGE("malloc pinCode failed.");
394             return HC_ERR_ALLOC_MEMORY;
395         }
396         if (memcpy_s(params->pinCodeString, strlen(pinString) + 1, pinString, strlen(pinString)) != EOK) {
397             LOGE("memcpy pinCodeString failed.");
398             (void)memset_s(params->pinCodeString, strlen(pinString) + 1, 0, strlen(pinString) + 1);
399             return HC_ERR_MEMORY_COPY;
400         }
401     }
402     return HC_SUCCESS;
403 }
404 
AllocSeed(IsoParams * params)405 static int AllocSeed(IsoParams *params)
406 {
407     params->seed.val = (uint8_t *)HcMalloc(SEED_LEN, 0);
408     if (params->seed.val == NULL) {
409         LOGE("Malloc for seed failed.");
410         return HC_ERR_ALLOC_MEMORY;
411     }
412     params->seed.length = SEED_LEN;
413     return HC_SUCCESS;
414 }
415 
GetUserType(IsoParams * params,const CJson * in)416 static int GetUserType(IsoParams *params, const CJson *in)
417 {
418     int res = GetIntFromJson(in, FIELD_SELF_TYPE, &(params->selfUserType));
419     if (res != 0) {
420         LOGE("get userType failed: %d", res);
421         return res;
422     }
423 
424     res = GetIntFromJson(in, FIELD_PEER_USER_TYPE, &(params->peerUserType));
425     if (res != 0) {
426         LOGD("get peer Type failed use default, res: %d", res);
427         params->peerUserType = 0; /* fill default value */
428         res = HC_SUCCESS;
429     }
430     return res;
431 }
432 
GetKeyLength(IsoParams * params,const CJson * in)433 static int32_t GetKeyLength(IsoParams *params, const CJson *in)
434 {
435     if (params->opCode == OP_UNBIND || params->opCode == OP_BIND) {
436         params->keyLen = 0;
437         return HC_SUCCESS;
438     }
439 
440     if (GetIntFromJson(in, FIELD_KEY_LENGTH, (int32_t *)&(params->keyLen)) != 0) {
441         LOGD("Get key length failed, use default.");
442         params->keyLen = DEFAULT_RETURN_KEY_LENGTH;
443     }
444     if (params->keyLen < MIN_OUTPUT_KEY_LEN || params->keyLen > MAX_OUTPUT_KEY_LEN) {
445         LOGE("Output key length is invalid, keyLen: %d.", params->keyLen);
446         return HC_ERR_INVALID_LEN;
447     }
448     return HC_SUCCESS;
449 }
450 
InitIsoParams(IsoParams * params,const CJson * in)451 int InitIsoParams(IsoParams *params, const CJson *in)
452 {
453     int res;
454     if (GetIntFromJson(in, FIELD_OPERATION_CODE, &(params->opCode)) != 0) {
455         LOGD("Get opCode failed, use default.");
456         params->opCode = AUTHENTICATE;
457     }
458     if (params->opCode != OP_BIND && params->opCode != OP_UNBIND && params->opCode != AUTHENTICATE) {
459         LOGE("Unsupported opCode: %d.", params->opCode);
460         res = HC_ERR_NOT_SUPPORT;
461         goto ERR;
462     }
463     if (GetBoolFromJson(in, FIELD_IS_CLIENT, &(params->isClient)) != 0) {
464         LOGE("get isClient failed");
465         res = HC_ERR_JSON_GET;
466         goto ERR;
467     }
468     res = InitIsoBaseParams(&params->baseParams);
469     if (res != HC_SUCCESS) {
470         LOGE("InitIsoBaseParams failed, res: %x.", res);
471         goto ERR;
472     }
473     res = GetKeyLength(params, in);
474     if (res != HC_SUCCESS) {
475         goto ERR;
476     }
477     res = GetUserType(params, in);
478     if (res != HC_SUCCESS) {
479         goto ERR;
480     }
481     res = FillAuthId(params, in);
482     if (res != HC_SUCCESS) {
483         goto ERR;
484     }
485     res = FillPkgNameAndServiceType(params, in);
486     if (res != HC_SUCCESS) {
487         goto ERR;
488     }
489     res = FillPin(params, in);
490     if (res != HC_SUCCESS) {
491         goto ERR;
492     }
493     res = AllocSeed(params);
494     if (res != HC_SUCCESS) {
495         goto ERR;
496     }
497     return HC_SUCCESS;
498 ERR:
499     DestroyIsoParams(params);
500     return res;
501 }
502 
AuthGeneratePsk(const Uint8Buff * seed,IsoParams * params)503 static int AuthGeneratePsk(const Uint8Buff *seed, IsoParams *params)
504 {
505     uint8_t keyAlias[ISO_KEY_ALIAS_LEN] = { 0 };
506     int res = GenerateKeyAliasInIso(params, keyAlias, sizeof(keyAlias), true);
507     if (res != 0) {
508         return res;
509     }
510 
511     LOGI("AuthCode alias: %x%x%x%x****.", keyAlias[0], keyAlias[1], keyAlias[2], keyAlias[3]);
512     Uint8Buff keyAliasBuf = { keyAlias, sizeof(keyAlias) };
513     Uint8Buff pskBuf = { params->baseParams.psk, sizeof(params->baseParams.psk) };
514     return params->baseParams.loader->computeHmac(&keyAliasBuf, seed, &pskBuf, true);
515 }
516 
AuthGeneratePskUsePin(const Uint8Buff * seed,IsoParams * params,const char * pinString)517 static int AuthGeneratePskUsePin(const Uint8Buff *seed, IsoParams *params, const char *pinString)
518 {
519     Uint8Buff messageBuf = { (uint8_t *)pinString, (uint32_t)strlen(pinString) };
520     Uint8Buff pskBuf = { params->baseParams.psk, sizeof(params->baseParams.psk) };
521     uint8_t hash[SHA256_LEN] = { 0 };
522     Uint8Buff hashBuf = { hash, sizeof(hash) };
523     int res = params->baseParams.loader->sha256(&messageBuf, &hashBuf);
524     if (res != 0) {
525         LOGE("sha256 failed, res:%d", res);
526         return res;
527     }
528     return params->baseParams.loader->computeHmac(&hashBuf, seed, &pskBuf, false);
529 }
530 
GenerateKeyAliasInIso(const IsoParams * params,uint8_t * keyAlias,uint32_t keyAliasLen,bool useOpposite)531 int GenerateKeyAliasInIso(const IsoParams *params, uint8_t *keyAlias, uint32_t keyAliasLen, bool useOpposite)
532 {
533     if (params == NULL || keyAlias == NULL || keyAliasLen == 0) {
534         return HC_ERR_INVALID_PARAMS;
535     }
536     Uint8Buff pkgName = { (uint8_t *)params->packageName, (uint32_t)strlen(params->packageName) };
537     Uint8Buff serviceType = { (uint8_t *)params->serviceType, (uint32_t)strlen(params->serviceType) };
538     Uint8Buff authId = { NULL, 0 };
539     if (useOpposite) {
540         authId.val = params->baseParams.authIdPeer.val;
541         authId.length = params->baseParams.authIdPeer.length;
542     } else {
543         authId.val = params->baseParams.authIdSelf.val;
544         authId.length = params->baseParams.authIdSelf.length;
545     }
546     Uint8Buff outKeyAlias = { keyAlias, keyAliasLen };
547     return GenerateKeyAlias(&pkgName, &serviceType, KEY_ALIAS_AUTH_TOKEN, &authId,
548         &outKeyAlias);
549 }
550 
GeneratePsk(const CJson * in,IsoParams * params)551 int GeneratePsk(const CJson *in, IsoParams *params)
552 {
553     if (!params->isClient) {
554         if (GetByteFromJson(in, FIELD_SEED, params->seed.val, params->seed.length) != 0) {
555             LOGE("Get seed failed.");
556             return HC_ERR_JSON_GET;
557         }
558     }
559     int res;
560     if (params->opCode == AUTHENTICATE || params->opCode == OP_UNBIND) {
561         res = AuthGeneratePsk(&params->seed, params);
562     } else {
563         res = AuthGeneratePskUsePin(&params->seed, params, params->pinCodeString);
564         if (params->pinCodeString != NULL) {
565             (void)memset_s(params->pinCodeString, HcStrlen(params->pinCodeString), 0, HcStrlen(params->pinCodeString));
566         }
567     }
568     if (res != HC_SUCCESS) {
569         LOGE("Generate psk failed, res: %x.", res);
570         goto ERR;
571     }
572     return res;
573 ERR:
574     (void)memset_s(params->baseParams.psk, sizeof(params->baseParams.psk), 0, sizeof(params->baseParams.psk));
575     return res;
576 }
577 
GenerateSeed(IsoParams * params)578 int GenerateSeed(IsoParams *params)
579 {
580     uint8_t *random = (uint8_t *)HcMalloc(SEED_LEN, 0);
581     if (random == NULL) {
582         LOGE("malloc random failed");
583         return HC_ERR_ALLOC_MEMORY;
584     }
585     Uint8Buff randomBuf = { random, SEED_LEN };
586     int res = params->baseParams.loader->generateRandom(&randomBuf);
587     if (res != 0) {
588         LOGE("generate random failed, res:%d", res);
589         HcFree(random);
590         return res;
591     }
592     clock_t times = 0;
593     uint8_t *input = (uint8_t *)HcMalloc(SEED_LEN + sizeof(clock_t), 0);
594     if (input == NULL) {
595         LOGE("malloc failed");
596         HcFree(random);
597         return HC_ERR_ALLOC_MEMORY;
598     }
599     if (memcpy_s(input, SEED_LEN + sizeof(clock_t), random, SEED_LEN) != EOK) {
600         LOGE("memcpy seed failed.");
601         res = HC_ERR_MEMORY_COPY;
602         goto ERR;
603     }
604     if (memcpy_s(input + SEED_LEN, sizeof(clock_t), &times, sizeof(clock_t)) != EOK) {
605         LOGE("memcpy times failed.");
606         res = HC_ERR_MEMORY_COPY;
607         goto ERR;
608     }
609     Uint8Buff inputBuf = { input, SEED_LEN + sizeof(clock_t) };
610     res = params->baseParams.loader->sha256(&inputBuf, &params->seed);
611     if (res != HC_SUCCESS) {
612         LOGE("sha256 failed.");
613         goto ERR;
614     }
615 ERR:
616     HcFree(random);
617     HcFree(input);
618     return res;
619 }
620