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 #ifndef ISO_PROTOCOL_COMMON_H
17 #define ISO_PROTOCOL_COMMON_H
18 
19 #include "alg_defs.h"
20 #include "string_util.h"
21 #include "json_utils.h"
22 
23 #define GENERATE_SESSION_KEY_STR "hichain_iso_session_key"
24 
25 #define RAND_BYTE_LEN 16
26 #define PSK_LEN 32
27 #define ISO_SESSION_KEY_LEN 32
28 
29 typedef struct IsoBaseParamsT {
30     Uint8Buff randSelf;
31     Uint8Buff randPeer;
32     Uint8Buff authIdSelf; // need malloc by caller
33     Uint8Buff authIdPeer; // need malloc by caller
34     Uint8Buff sessionKey;
35     uint8_t psk[PSK_LEN];
36     const AlgLoader *loader;
37     int32_t osAccountId;
38 } IsoBaseParams;
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 int32_t InitIsoBaseParams(const CJson *in, IsoBaseParams *params);
45 void DestroyIsoBaseParams(IsoBaseParams *params);
46 
47 int IsoClientGenRandom(IsoBaseParams *params);
48 int IsoClientCheckAndGenToken(IsoBaseParams *params, const Uint8Buff *peerToken, Uint8Buff *selfToken);
49 int IsoClientGenSessionKey(IsoBaseParams *params, int returnResult, const uint8_t *hmac, uint32_t hmacLen);
50 
51 int IsoServerGenRandomAndToken(IsoBaseParams *params, Uint8Buff *selfTokenBuf);
52 int IsoServerGenSessionKeyAndCalToken(IsoBaseParams *params, const Uint8Buff *tokenFromPeer, Uint8Buff *tokenToPeer);
53 
54 #ifdef __cplusplus
55 }
56 #endif
57 #endif