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 "standard_exchange_message_util.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "json_utils.h"
21 #include "protocol_common.h"
22 #include "pake_base_cur_task.h"
23 #include "string_util.h"
24
PackageNonceAndCipherToJson(const Uint8Buff * nonce,const Uint8Buff * cipher,CJson * data,const char * key)25 int32_t PackageNonceAndCipherToJson(const Uint8Buff *nonce, const Uint8Buff *cipher, CJson *data, const char *key)
26 {
27 int32_t res = HC_SUCCESS;
28 uint32_t exAuthInfoLen = nonce->length + cipher->length;
29 uint8_t *exAuthInfoVal = (uint8_t *)HcMalloc(exAuthInfoLen, 0);
30 if (exAuthInfoVal == NULL) {
31 LOGE("malloc exAuthInfoVal failed.");
32 res = HC_ERR_ALLOC_MEMORY;
33 goto ERR;
34 }
35 if (memcpy_s(exAuthInfoVal, exAuthInfoLen, nonce->val, nonce->length) != EOK) {
36 LOGE("memcpy nonce failed");
37 res = HC_ERR_MEMORY_COPY;
38 goto ERR;
39 }
40 if (memcpy_s(exAuthInfoVal + nonce->length, exAuthInfoLen - nonce->length,
41 cipher->val, cipher->length) != EOK) {
42 LOGE("memcpy exInfoCipher failed");
43 res = HC_ERR_MEMORY_COPY;
44 goto ERR;
45 }
46 GOTO_ERR_AND_SET_RET(AddByteToJson(data, key, exAuthInfoVal, exAuthInfoLen), res);
47 ERR:
48 HcFree(exAuthInfoVal);
49 return res;
50 }
51
ParseNonceAndCipherFromJson(Uint8Buff * nonce,Uint8Buff * cipher,const CJson * in,const char * key)52 int32_t ParseNonceAndCipherFromJson(Uint8Buff *nonce, Uint8Buff *cipher, const CJson *in, const char *key)
53 {
54 int32_t res = HC_SUCCESS;
55 uint8_t *exAuthInfoVal = NULL;
56 const char *exAuthInfoStr = GetStringFromJson(in, key);
57 if (exAuthInfoStr == NULL) {
58 LOGE("get exAuthInfoStr failed.");
59 res = HC_ERR_JSON_GET;
60 goto ERR;
61 }
62 int32_t exAuthInfoLen = (int32_t)HcStrlen(exAuthInfoStr) / BYTE_TO_HEX_OPER_LENGTH;
63 exAuthInfoVal = (uint8_t *)HcMalloc(exAuthInfoLen, 0);
64 if (exAuthInfoVal == NULL) {
65 LOGE("Malloc exAuthInfoVal failed.");
66 res = HC_ERR_ALLOC_MEMORY;
67 goto ERR;
68 }
69 res = HexStringToByte(exAuthInfoStr, exAuthInfoVal, exAuthInfoLen);
70 if (res != HC_SUCCESS) {
71 LOGE("Convert exAuthInfo from hex string to byte failed.");
72 goto ERR;
73 }
74 if (memcpy_s(nonce->val, nonce->length, exAuthInfoVal, nonce->length) != EOK) {
75 LOGE("copy nonce failed!");
76 res = HC_ERR_MEMORY_COPY;
77 goto ERR;
78 }
79
80 res = InitSingleParam(cipher, exAuthInfoLen - nonce->length);
81 if (res != HC_SUCCESS) {
82 LOGE("init exInfoCipher failed");
83 goto ERR;
84 }
85 if (memcpy_s(cipher->val, cipher->length, exAuthInfoVal + nonce->length,
86 exAuthInfoLen - nonce->length) != EOK) {
87 LOGE("copy exInfoCipher failed!");
88 res = HC_ERR_MEMORY_COPY;
89 goto ERR;
90 }
91 ERR:
92 HcFree(exAuthInfoVal);
93 return res;
94 }
95
GenerateSelfChallenge(PakeParams * params)96 int32_t GenerateSelfChallenge(PakeParams *params)
97 {
98 int res = InitSingleParam(&(params->baseParams.challengeSelf), PAKE_CHALLENGE_LEN);
99 if (res != HC_SUCCESS) {
100 LOGE("InitSingleParam for challengeSelf failed, res: %d.", res);
101 return res;
102 }
103 res = params->baseParams.loader->generateRandom(&(params->baseParams.challengeSelf));
104 if (res != HC_SUCCESS) {
105 LOGE("GenerateRandom for challengeSelf failed, res: %d.", res);
106 return res;
107 }
108 return res;
109 }
110
GetPeerChallenge(PakeParams * params,const CJson * in)111 int32_t GetPeerChallenge(PakeParams *params, const CJson *in)
112 {
113 int res = InitSingleParam(&(params->baseParams.challengePeer), PAKE_CHALLENGE_LEN);
114 if (res != HC_SUCCESS) {
115 LOGE("InitSingleParam for challengePeer failed, res: %d.", res);
116 return res;
117 }
118 res = GetByteFromJson(in, FIELD_CHALLENGE,
119 params->baseParams.challengePeer.val, params->baseParams.challengePeer.length);
120 if (res != HC_SUCCESS) {
121 LOGE("Get challengePeer failed, res: %d.", res);
122 return res;
123 }
124 return res;
125 }
126