• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 #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(HEX): %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 
379 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
CheckPinLenForStandardIso(const CJson * in,const char * pinCode)380 static bool CheckPinLenForStandardIso(const CJson *in, const char *pinCode)
381 {
382     int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
383     (void)GetIntFromJson(in, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
384     if (protocolExpandVal != LITE_PROTOCOL_STANDARD_MODE) {
385         LOGI("not standard iso, no need to check.");
386         return true;
387     }
388     return HcStrlen(pinCode) >= PIN_CODE_LEN_LONG;
389 }
390 #endif
391 
FillPin(IsoParams * params,const CJson * in)392 static int FillPin(IsoParams *params, const CJson *in)
393 {
394     if (params->opCode == OP_BIND) {
395         const char *pinString = GetStringFromJson(in, FIELD_PIN_CODE);
396         if (pinString == NULL) {
397             LOGE("Get pin failed.");
398             return HC_ERROR;
399         }
400         if (strlen(pinString) < MIN_PIN_LEN || strlen(pinString) > MAX_PIN_LEN) {
401             LOGE("Pin is too short.");
402             return HC_ERR_INVALID_PARAMS;
403         }
404     #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
405         if (!CheckPinLenForStandardIso(in, pinString)) {
406             LOGE("Invalid pin code len!");
407             return HC_ERR_INVALID_LEN;
408         }
409     #endif
410         params->pinCodeString = (char *)HcMalloc(strlen(pinString) + 1, 0);
411         if (params->pinCodeString == NULL) {
412             LOGE("malloc pinCode failed.");
413             return HC_ERR_ALLOC_MEMORY;
414         }
415         if (memcpy_s(params->pinCodeString, strlen(pinString) + 1, pinString, strlen(pinString)) != EOK) {
416             LOGE("memcpy pinCodeString failed.");
417             (void)memset_s(params->pinCodeString, strlen(pinString) + 1, 0, strlen(pinString) + 1);
418             return HC_ERR_MEMORY_COPY;
419         }
420     }
421     return HC_SUCCESS;
422 }
423 
AllocSeed(IsoParams * params)424 static int AllocSeed(IsoParams *params)
425 {
426     params->seed.val = (uint8_t *)HcMalloc(SEED_LEN, 0);
427     if (params->seed.val == NULL) {
428         LOGE("Malloc for seed failed.");
429         return HC_ERR_ALLOC_MEMORY;
430     }
431     params->seed.length = SEED_LEN;
432     return HC_SUCCESS;
433 }
434 
GetUserType(IsoParams * params,const CJson * in)435 static int GetUserType(IsoParams *params, const CJson *in)
436 {
437     int res = GetIntFromJson(in, FIELD_SELF_TYPE, &(params->selfUserType));
438     if (res != 0) {
439         LOGE("get userType failed: %d", res);
440         return res;
441     }
442 
443     res = GetIntFromJson(in, FIELD_PEER_USER_TYPE, &(params->peerUserType));
444     if (res != 0) {
445         LOGD("get peer Type failed use default, res: %d", res);
446         params->peerUserType = 0; /* fill default value */
447         res = HC_SUCCESS;
448     }
449     return res;
450 }
451 
GetKeyLength(IsoParams * params,const CJson * in)452 static int32_t GetKeyLength(IsoParams *params, const CJson *in)
453 {
454     if (params->opCode == OP_UNBIND || params->opCode == OP_BIND) {
455         params->keyLen = 0;
456         return HC_SUCCESS;
457     }
458 
459     if (GetIntFromJson(in, FIELD_KEY_LENGTH, (int32_t *)&(params->keyLen)) != 0) {
460         LOGD("Get key length failed, use default.");
461         params->keyLen = DEFAULT_RETURN_KEY_LENGTH;
462     }
463     if (params->keyLen < MIN_OUTPUT_KEY_LEN || params->keyLen > MAX_OUTPUT_KEY_LEN) {
464         LOGE("Output key length is invalid, keyLen: %d.", params->keyLen);
465         return HC_ERR_INVALID_LEN;
466     }
467     return HC_SUCCESS;
468 }
469 
InitIsoParams(IsoParams * params,const CJson * in)470 int InitIsoParams(IsoParams *params, const CJson *in)
471 {
472     int res;
473     if (GetIntFromJson(in, FIELD_OPERATION_CODE, &(params->opCode)) != 0) {
474         LOGD("Get opCode failed, use default.");
475         params->opCode = AUTHENTICATE;
476     }
477     if (params->opCode != OP_BIND && params->opCode != OP_UNBIND && params->opCode != AUTHENTICATE) {
478         LOGE("Unsupported opCode: %d.", params->opCode);
479         res = HC_ERR_NOT_SUPPORT;
480         goto ERR;
481     }
482     if (GetBoolFromJson(in, FIELD_IS_CLIENT, &(params->isClient)) != 0) {
483         LOGE("get isClient failed");
484         res = HC_ERR_JSON_GET;
485         goto ERR;
486     }
487     res = InitIsoBaseParams(&params->baseParams);
488     if (res != HC_SUCCESS) {
489         LOGE("InitIsoBaseParams failed, res: %x.", res);
490         goto ERR;
491     }
492     res = GetKeyLength(params, in);
493     if (res != HC_SUCCESS) {
494         goto ERR;
495     }
496     res = GetUserType(params, in);
497     if (res != HC_SUCCESS) {
498         goto ERR;
499     }
500     res = FillAuthId(params, in);
501     if (res != HC_SUCCESS) {
502         goto ERR;
503     }
504     res = FillPkgNameAndServiceType(params, in);
505     if (res != HC_SUCCESS) {
506         goto ERR;
507     }
508     res = FillPin(params, in);
509     if (res != HC_SUCCESS) {
510         goto ERR;
511     }
512     res = AllocSeed(params);
513     if (res != HC_SUCCESS) {
514         goto ERR;
515     }
516     return HC_SUCCESS;
517 ERR:
518     DestroyIsoParams(params);
519     return res;
520 }
521 
AuthGeneratePsk(const Uint8Buff * seed,IsoParams * params)522 static int AuthGeneratePsk(const Uint8Buff *seed, IsoParams *params)
523 {
524     uint8_t keyAlias[ISO_KEY_ALIAS_LEN] = { 0 };
525     int res = GenerateKeyAliasInIso(params, keyAlias, sizeof(keyAlias), true);
526     if (res != 0) {
527         return res;
528     }
529 
530     LOGI("AuthCode alias(HEX): %x%x%x%x****.", keyAlias[0], keyAlias[1], keyAlias[2], keyAlias[3]);
531     Uint8Buff keyAliasBuf = { keyAlias, sizeof(keyAlias) };
532     Uint8Buff pskBuf = { params->baseParams.psk, sizeof(params->baseParams.psk) };
533     return params->baseParams.loader->computeHmac(&keyAliasBuf, seed, &pskBuf, true);
534 }
535 
AuthGeneratePskUsePin(const Uint8Buff * seed,IsoParams * params,const char * pinString)536 static int AuthGeneratePskUsePin(const Uint8Buff *seed, IsoParams *params, const char *pinString)
537 {
538     Uint8Buff messageBuf = { (uint8_t *)pinString, (uint32_t)strlen(pinString) };
539     Uint8Buff pskBuf = { params->baseParams.psk, sizeof(params->baseParams.psk) };
540     uint8_t hash[SHA256_LEN] = { 0 };
541     Uint8Buff hashBuf = { hash, sizeof(hash) };
542     int res = params->baseParams.loader->sha256(&messageBuf, &hashBuf);
543     if (res != 0) {
544         LOGE("sha256 failed, res:%d", res);
545         return res;
546     }
547     return params->baseParams.loader->computeHmac(&hashBuf, seed, &pskBuf, false);
548 }
549 
GenerateKeyAliasInIso(const IsoParams * params,uint8_t * keyAlias,uint32_t keyAliasLen,bool useOpposite)550 int GenerateKeyAliasInIso(const IsoParams *params, uint8_t *keyAlias, uint32_t keyAliasLen, bool useOpposite)
551 {
552     if (params == NULL || keyAlias == NULL || keyAliasLen == 0) {
553         return HC_ERR_INVALID_PARAMS;
554     }
555     Uint8Buff pkgName = { (uint8_t *)params->packageName, (uint32_t)strlen(params->packageName) };
556     Uint8Buff serviceType = { (uint8_t *)params->serviceType, (uint32_t)strlen(params->serviceType) };
557     Uint8Buff authId = { NULL, 0 };
558     if (useOpposite) {
559         authId.val = params->baseParams.authIdPeer.val;
560         authId.length = params->baseParams.authIdPeer.length;
561     } else {
562         authId.val = params->baseParams.authIdSelf.val;
563         authId.length = params->baseParams.authIdSelf.length;
564     }
565     Uint8Buff outKeyAlias = { keyAlias, keyAliasLen };
566     return GenerateKeyAlias(&pkgName, &serviceType, KEY_ALIAS_AUTH_TOKEN, &authId,
567         &outKeyAlias);
568 }
569 
GeneratePsk(const CJson * in,IsoParams * params)570 int GeneratePsk(const CJson *in, IsoParams *params)
571 {
572     if (!params->isClient) {
573         if (GetByteFromJson(in, FIELD_SEED, params->seed.val, params->seed.length) != 0) {
574             LOGE("Get seed failed.");
575             return HC_ERR_JSON_GET;
576         }
577     }
578     int res;
579     if (params->opCode == AUTHENTICATE || params->opCode == OP_UNBIND) {
580         res = AuthGeneratePsk(&params->seed, params);
581     } else {
582         res = AuthGeneratePskUsePin(&params->seed, params, params->pinCodeString);
583         if (params->pinCodeString != NULL) {
584             (void)memset_s(params->pinCodeString, HcStrlen(params->pinCodeString), 0, HcStrlen(params->pinCodeString));
585         }
586     }
587     if (res != HC_SUCCESS) {
588         LOGE("Generate psk failed, res: %x.", res);
589         goto ERR;
590     }
591     return res;
592 ERR:
593     (void)memset_s(params->baseParams.psk, sizeof(params->baseParams.psk), 0, sizeof(params->baseParams.psk));
594     return res;
595 }
596 
GenerateSeed(IsoParams * params)597 int GenerateSeed(IsoParams *params)
598 {
599     uint8_t *random = (uint8_t *)HcMalloc(SEED_LEN, 0);
600     if (random == NULL) {
601         LOGE("malloc random failed");
602         return HC_ERR_ALLOC_MEMORY;
603     }
604     Uint8Buff randomBuf = { random, SEED_LEN };
605     int res = params->baseParams.loader->generateRandom(&randomBuf);
606     if (res != 0) {
607         LOGE("generate random failed, res:%d", res);
608         HcFree(random);
609         return res;
610     }
611     clock_t times = 0;
612     uint8_t *input = (uint8_t *)HcMalloc(SEED_LEN + sizeof(clock_t), 0);
613     if (input == NULL) {
614         LOGE("malloc failed");
615         HcFree(random);
616         return HC_ERR_ALLOC_MEMORY;
617     }
618     if (memcpy_s(input, SEED_LEN + sizeof(clock_t), random, SEED_LEN) != EOK) {
619         LOGE("memcpy seed failed.");
620         res = HC_ERR_MEMORY_COPY;
621         goto ERR;
622     }
623     if (memcpy_s(input + SEED_LEN, sizeof(clock_t), &times, sizeof(clock_t)) != EOK) {
624         LOGE("memcpy times failed.");
625         res = HC_ERR_MEMORY_COPY;
626         goto ERR;
627     }
628     Uint8Buff inputBuf = { input, SEED_LEN + sizeof(clock_t) };
629     res = params->baseParams.loader->sha256(&inputBuf, &params->seed);
630     if (res != HC_SUCCESS) {
631         LOGE("sha256 failed.");
632         goto ERR;
633     }
634 ERR:
635     HcFree(random);
636     HcFree(input);
637     return res;
638 }
639