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 SMP_TOOL_H
17 #define SMP_TOOL_H
18 
19 #include <stdint.h>
20 
21 #include "platform/include/list.h"
22 #include "platform/include/mutex.h"
23 #include "smp_def.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define IO_COUNTS 5
30 
31 #define CRYPT_AESCMAC_Z_LEN 16
32 #define CRYPT_AESCMAC_TMP_LEN 16
33 #define CRYPT_AESCMAC_X_LEN 16
34 #define CRYPT_AESCMAC_Y_LEN 16
35 #define CRYPT_AESCMAC_PADDED_LEN 16
36 #define CRYPT_AESCMAC_KEY_LEN 16
37 #define CRYPT_AESCMAC_IN_LEN 16
38 #define CRYPT_AESCMAC_OUT_LEN 16
39 #define CRYPT_AESCMAC_NUM 15
40 #define CRYPT_AESCMAC_CONST_RB_LEN 16
41 typedef struct {
42     uint8_t input[CRYPT_AESCMAC_IN_LEN];
43     uint8_t output1[CRYPT_AESCMAC_OUT_LEN];
44     uint8_t output2[CRYPT_AESCMAC_OUT_LEN];
45 } SMP_CryptAesCmacStep1Param;
46 
47 typedef struct {
48     uint8_t input1[CRYPT_AESCMAC_IN_LEN];
49     uint8_t input2[CRYPT_AESCMAC_IN_LEN];
50     const uint8_t *input3;
51     int length;
52     int n;
53     uint8_t output[CRYPT_AESCMAC_OUT_LEN];
54 } SMP_CryptAesCmacStep2Param;
55 
56 typedef struct {
57     uint16_t stepA;
58     uint16_t stepB;
59     const uint8_t *message;
60     uint8_t messageSize;
61     uint8_t aesCmacOutput[CRYPT_AESCMAC_OUT_LEN];
62     uint8_t key[CRYPT_AESCMAC_KEY_LEN];
63     int n;
64     uint32_t signCounter;
65     bool isUsingHwAes128;
66 } SMP_CryptAesCmacStep3Param;
67 
68 typedef struct {
69     uint16_t stepA;
70     uint16_t stepB;
71     SMP_EncCmd *pEncCmdData;
72     uint8_t X[CRYPT_AESCMAC_X_LEN];
73     bool isUsingHwAes128;
74 } SMP_CryptAesCmacStep4Param;
75 
76 #define CRYPT_C1_P1_LEN 16
77 #define CRYPT_C1_P2_LEN 16
78 #define CRYPT_C1_PADDING_LEN 4
79 #define CRYPT_C1_IA_LEN 6
80 #define CRYPT_C1_RA_LEN 6
81 #define CRYPT_C1_IN_LEN 16
82 #define CRYPT_C1_OUT_LEN 16
83 typedef struct {
84     uint8_t r[SMP_RANDOM_DATA_LEN];
85     uint8_t preq[SMP_CMD_PAIR_REQ_DATA_LEN];
86     uint8_t pres[SMP_CMD_PAIR_RSP_DATA_LEN];
87     uint8_t iat;
88     uint8_t rat;
89     uint8_t output[CRYPT_C1_OUT_LEN];
90 } SMP_CryptC1Step1Param;
91 
92 typedef struct {
93     uint8_t ia[CRYPT_C1_IA_LEN];
94     uint8_t ra[CRYPT_C1_RA_LEN];
95     uint8_t input[CRYPT_C1_IN_LEN];
96     uint8_t output[CRYPT_C1_OUT_LEN];
97 } SMP_CryptC1Step2Param;
98 
99 #define CRYPT_F4_OUT_LEN 65
100 typedef struct {
101     uint8_t U[SMP_PUBLICKEY_X_LEN];
102     uint8_t V[SMP_PUBLICKEY_X_LEN];
103     uint8_t Z[1];
104     uint8_t output[CRYPT_F4_OUT_LEN];
105 } SMP_CryptF4Param;
106 
107 #define CRYPT_F5_KEYID_LEN 4
108 #define CRYPT_F5_LENGTH_LEN 2
109 #define CRYPT_F5_A1_LEN 7
110 #define CRYPT_F5_A2_LEN 7
111 #define CRYPT_F5_OUT_LEN 53
112 typedef struct {
113     uint8_t N1[SMP_RANDOM_DATA_LEN];
114     uint8_t N2[SMP_RANDOM_DATA_LEN];
115     uint8_t A1[CRYPT_F5_A1_LEN];
116     uint8_t A2[CRYPT_F5_A2_LEN];
117     uint8_t output[CRYPT_F5_OUT_LEN];
118 } SMP_CryptF5Param;
119 
120 #define CRYPT_F6_A1_LEN 7
121 #define CRYPT_F6_A2_LEN 7
122 #define CRYPT_F6_IO_LEN 3
123 #define CRYPT_F6_OUT_LEN 65
124 typedef struct {
125     uint8_t N1[SMP_RANDOM_DATA_LEN];
126     uint8_t N2[SMP_RANDOM_DATA_LEN];
127     uint8_t R[SMP_RANDOM_DATA_LEN];
128     uint8_t IOcap[CRYPT_F6_IO_LEN];
129     uint8_t A1[CRYPT_F6_A1_LEN];
130     uint8_t A2[CRYPT_F6_A2_LEN];
131     uint8_t output[CRYPT_F6_OUT_LEN];
132 } SMP_CryptF6Param;
133 
134 #define CRYPT_G2_OUT_LEN 80
135 typedef struct {
136     uint8_t U[SMP_PUBLICKEY_X_LEN];
137     uint8_t V[SMP_PUBLICKEY_X_LEN];
138     uint8_t Y[SMP_RANDOM_DATA_LEN];
139     uint8_t output[CRYPT_G2_OUT_LEN];
140 } SMP_CryptG2Param;
141 
142 #define CRYPT_S1_OUT_LEN 16
143 typedef struct {
144     uint8_t r1[SMP_RANDOM_DATA_LEN];
145     uint8_t r2[SMP_RANDOM_DATA_LEN];
146     uint8_t output[CRYPT_S1_OUT_LEN];
147 } SMP_CryptS1Param;
148 
149 void SMP_ConstituteC1Step1Param(uint8_t role, const uint8_t *random, uint8_t randomLen, SMP_CryptC1Step1Param *param);
150 int SMP_CryptographicC1Step1(uint16_t step, SMP_CryptC1Step1Param *param);
151 int SMP_ConstituteC1Step2Param(const SMP_EncData *encData, uint8_t role, SMP_CryptC1Step2Param *param);
152 int SMP_CryptographicC1Step2(uint16_t step, SMP_CryptC1Step2Param *param);
153 void SMP_CryptographicS1(SMP_CryptS1Param *param);
154 void SMP_ConstituteF4Param(bool isPasskey, SMP_CryptF4Param *param);
155 void SMP_CryptographicF4(SMP_CryptF4Param *param);
156 void SMP_ConstituteF5Param(uint8_t role, SMP_CryptF5Param *cryptF5Param);
157 void SMP_CryptographicF5(SMP_CryptF5Param *param);
158 void SMP_ConstituteF6Param(bool isCalculatePeer, SMP_CryptF6Param *cryptF6Param);
159 void SMP_CryptographicF6(SMP_CryptF6Param *param);
160 void SMP_CryptographicG2(SMP_CryptG2Param *param);
161 void SMP_CryptographicAesCmacStep1(SMP_CryptAesCmacStep1Param *param);
162 void SMP_CryptographicAesCmacStep2(SMP_CryptAesCmacStep2Param *param);
163 void SMP_ConstituteAesCmacStep3Param(
164     const SMP_EncData *encData, const uint8_t *data, uint8_t dataLen, SMP_CryptAesCmacStep3Param *param);
165 int SMP_CryptographicAesCmacStep3(const SMP_CryptAesCmacStep3Param *param);
166 int SMP_ConstituteAesCmacStep4Param(
167     const SMP_EncData *encData, uint16_t stepA, uint16_t stepB, uint8_t role, SMP_CryptAesCmacStep4Param *param);
168 int SMP_CryptographicAesCmacStep4(SMP_CryptAesCmacStep4Param *param);
169 void SMP_MemoryReverseCopy(uint8_t *dest, const uint8_t *source, uint16_t length);
170 void SMP_ReverseMemoryOrder(uint8_t *buf, uint16_t length);
171 void SMP_ClearPairState(SMP_PairMng *mng);
172 void SMP_CalculatePairType(SMP_PairMng *mng);
173 void SMP_CalculateEncKeySize(SMP_PairMng *mng);
174 void SMP_CalculatePairMethod(SMP_PairMng *mng);
175 void SMP_CalculateKeyDistribution(SMP_PairMng *mng);
176 int SMP_CheckRemotePairParam(const SMP_PairParam *pairParam, uint8_t *reason);
177 List *SMP_ListCreate(FreeDataCb cb);
178 void SMP_ListDelete(List *list);
179 void SMP_ListAddLast(List *list, void *data);
180 bool SMP_ListRemoveNode(List *list, void *data);
181 ListNode *SMP_ListGetFirstNode(const List *list);
182 void *SMP_ListGetNodeData(const ListNode *node);
183 bool SMP_IsScOobPair(uint8_t pairMethod);
184 bool SMP_IsPasskeyEntryPair(uint8_t pairMethod);
185 bool SMP_IsJustworkOrNumericPair(uint8_t pairMethod);
186 void SMP_GetPublicKeyX(const uint8_t *pubKey, uint8_t *x, uint8_t length);
187 void SMP_LongTermKeyCopy(uint8_t *destLtk, const uint8_t *sourceLtk, uint8_t encKeySize);
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif