| Airoha M0 BLE API 1.0.5.4

sm_common.h
1 /******************************************************************************
2 Copyright (c) Airoha 2016 - All rights reserved
3
4 FILE NAME
5 sm_common.h
6 DESCRIPTION
7 NOTES
8 ********************************************************************************/
9
10 /***************************************************************
11 *
12 * Implement SM (Security Manager)
13 * Bluetooth 4.0 - Low Energy
14 *
15 ***************************************************************/
16
17 #ifndef __SM_COMMON_H__
18 #define __SM_COMMON_H__
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include "sm_hal.h"
24
25
26 /*************************************************************************
27 * Macro Definition
28 *************************************************************************/
29
30 #define SM_DEBUG
31
32 #ifdef SM_DEBUG
33 #define SM_PRINT(x) printf x
34 #else
35 #define SM_PRINT(x)
36 #endif
37
38 #ifndef TO_MCU_ENDIAN_U16
39 #define TO_MCU_ENDIAN_U16(xx) ((xx<<8)|((xx>>8)&0X00FF))
40 #endif
41
42 #ifndef TO_LITTLE_ENDIAN_U16
43 #define TO_LITTLE_ENDIAN_U16(xx) ((xx<<8)|((xx>>8)&0X00FF))
44 #endif
45
46 #define UNUSED(x) { do { if(x){}; } while (0); }
47
48 #define SM_ROLE_MASTER 0
49 #define SM_ROLE_SLAVE 1
50 #define SM_NUMBER_OF_ROLES 2
51
52 #define SM_MIN_ENCRYPTION_KEY_SIZE 7
53 #define SM_MAX_ENCRYPTION_KEY_SIZE 16
54
55 #define SM_RAND_NUMBER_LEN 8
56 #define SM_EDIV_LEN 2
57 #define SM_MASTER_ID_LEN 10 //RAND:8 bytes, EDIV:2 bytes
58
59 #define SM_PASSKEY_LEN 3
60
61 #define SM_FEATURE_AUTHREQ_IDX 3
62 #define SM_FEATURE_INITKEYDISTR_IDX 5
63 #define SM_FEATURE_RESPKEYDISTR_IDX 6
64
65 #define SM_ENC_KEY_DISTRIBUTION 0x01
66 #define SM_ID_KEY_DISTRIBUTION 0x02
67 #define SM_SIGN_KEY_DISTRIBUTION 0x04
68
69 /* BD_ADDR TYPE */
70 #define SM_BD_ADDR_TYPE_PUBLIC 0x00
71 #define SM_BD_ADDR_TYPE_RANDOM 0x01
72
73 /* RANDOM BD_ADDR TYPE */
74 #define SM_RAND_ADDR_STATIC 0x00
75 #define SM_RAND_ADDR_NON_RESOLV 0x01
76 #define SM_RAND_ADDR_RESOLV 0x02
77
78 /* SM_STATUS */
79 #define SM_STATUS_OK 0
80 #define SM_STATUS_BUSY 1 // verify or generate signature
81 #define SM_STATUS_PROCESSING 2 // authentication procedure
82 #define SM_STATUS_FAIL 3
83 #define SM_STATUS_NOT_SUPPORT 4
84 #define SM_STATUS_INVALID_PARA 5
85 #define SM_STATUS_INPUT_CANCEL 6
86 #define SM_STATUS_KEY_NOT_EXIST 7
87 #define SM_STATUS_INSUFFICIENT_ENC 8
88 #define SM_STATUS_INSUFFICIENT_AUTH 9
89
90 //for protocol
91 #define SM_STATIC_RANDOM_ADDR_MSB 0x3
92 #define SM_PRIVATE_NONRESOLVABLE_ADDR_MSB 0x00
93 #define SM_PRIVATE_RESOLVABLE_ADDR_MSB 0x01
94 #define SM_ADDR_PRAND_LEN 3
95 #define SM_ADDR_HASH_LEN 3
96 #define SM_ADDR_LAST_BYTE 5
97 //for protocol
98
99 #define SM_P1_PREQ_IDX 2
100 #define SM_P1_PRES_IDX 9
101 #define SM_P1_PRES_INIT_KEY_IDX 14
102 #define SM_P1_PRES_RESP_KEY_IDX 15
103
104 /* SM_PAIRING_STATE */
105 #define SM_STATE_IDLE 0x00
106 #define SM_SLAVE_STATE_PHASE2_WAIT_PASSKEY 0x01
107 #define SM_SLAVE_STATE_PHASE2_GEN_SCONFIRM 0x02
108 #define SM_SLAVE_STATE_PHASE2_WAIT_MCONFIRM 0x03
109 #define SM_SLAVE_STATE_PHASE2_READY_TO_SEND_SCONFIRM 0x03
110 #define SM_SLAVE_STATE_PHASE2_WAIT_MRANDOM 0x04
111 #define SM_SLAVE_STATE_PHASE2_GEN_MCONFIRM 0x05
112 #define SM_SLAVE_STATE_PHASE2_SC_WAIT_PUCLIC_KEY 0x06
113 #define SM_SLAVE_STATE_PHASE2_SC_WAIT_NUMERIC_COMPARISON_VALUE 0x07
114 #define SM_SLAVE_STATE_PHASE2_SC_WAIT_USER_CONFIRM 0x08
115 #define SM_SLAVE_STATE_PHASE2_SC_GEN_EB 0x09
116 #define SM_SLAVE_STATE_PHASE2_SC_WAIT_EA 0x0A
117 #define SM_SLAVE_STATE_PHASE2_SC_READY_TO_CHECK_EA 0x0B //Got Ea, wait for EB
118 #define SM_SLAVE_STATE_PHASE2_SC_CHECKING_EA 0x0C //Checking Ea
119 #define SM_SLAVE_STATE_PHASE2_WAIT_STK_REQ_EVENT 0x0D
120 #define SM_MASTER_STATE_PHASE2_GEN_MCONFIRM 0x13
121 #define SM_MASTER_STATE_PHASE2_WAIT_SCONFIRM 0x14
122 #define SM_MASTER_STATE_PHASE2_WAIT_SRANDOM 0x15
123 #define SM_MASTER_STATE_PHASE2_GEN_SCONFIRM 0x16
124 #define SM_MASTER_STATE_PHASE2_WAIT_HCI_ENCRY_CHANGE_EVENT 0x17
125 #define SM_MASTER_STATE_PHASE2_READY_TO_SEND_MRANDOM 0x18
126 #define SM_MASTER_STATE_PHASE2_SC_GEN_EA 0x19
127
128 #define SM_STATE_PHASE3 0x30
129 #define SM_STATE_PHASE3_WAIT_ENC_INFO 0x31
130 #define SM_STATE_PHASE3_WAIT_ID_INFO 0x32
131 #define SM_STATE_PHASE3_WAIT_SIGNING_INFO 0x33
132
133 /*
134 Mask for smpFlag
135 Bit0: Bonding,
136 Bit1: Valid data, (AB1122 use only),
137 Bit2: MITM,
138 Bit3: useSC,
139 Bit4: KEY_PRESS_NOTI
140 Bit5: Reserved,
141 Bit6: Reserved,
142 Bit7: Role,
143 */
144 //#define SM_BONDING 0x01 //ble_gap_sm.h
145 //#define SM_SMPFLAG_VALID_DATA 0x02 (AB1122 use only)
146 //#define SM_MITM_PROTECTION 0x04 //ble_gap_sm.h
147 //#define SM_SC_ENABLE 0x08 //ble_gap_sm.h
148 //#define SM_KEY_PRESS_NOTI_ENABLE 0x10 //ble_gap_sm.h
149 #define SM_SMPFLAG_ROLE_MASTER 0x00
150 #define SM_SMPFLAG_ROLE_SLAVE 0x80
151
152 typedef struct
153 {
154 uint32_t signCounter;
155 uint32_t peerSignCounter;
156
157 uint8_t peerAddr[6];
158 uint8_t isValidData;
159 uint8_t peerAddrType;
160
161 uint8_t encKeySize;
162 uint8_t smpFlag; //Bit 0:Bonding, Bit 2:MITM, Bit 3:useSC, Bit 4:KEY_PRESS_NOTI, Bit7:Role
163 uint8_t keyDistr; //initKeyDistr
164 uint8_t respKeyDistr; //respKeyDistr
165
166 uint8_t LTK[SM_MAX_ENCRYPTION_KEY_SIZE]; //Temporary key, STK, LTK
167 uint8_t peerLTK[SM_MAX_ENCRYPTION_KEY_SIZE]; //peerLTK, passkey/OOB value(SC, ra of f6)
168
169 uint8_t CSRK[SM_MAX_ENCRYPTION_KEY_SIZE];
170 uint8_t peerCSRK[SM_MAX_ENCRYPTION_KEY_SIZE];
171
172 uint8_t IRK[SM_MAX_ENCRYPTION_KEY_SIZE];
173 uint8_t peerIRK[SM_MAX_ENCRYPTION_KEY_SIZE];
174
175 uint8_t MasterInfo[SM_MASTER_ID_LEN]; //Rand+EDIV
176 uint8_t peerMasterInfo[SM_MASTER_ID_LEN];
177 }SM_PAIRING_RECORD_TYPE;
178
179 typedef struct
180 {
181 uint8_t pke[SM_PASSKEY_LEN];
182 uint8_t isValidData;
183 }SM_PASSKEY_RECORD_TYPE;
184
185 typedef struct
186 {
187 uint8_t pReq[7];
188 uint8_t state;
189 uint8_t pRes[7];
190 uint8_t pairingMethod;
191
192 uint8_t peerRand[SM_MAX_ENCRYPTION_KEY_SIZE];
193 uint8_t rand[SM_MAX_ENCRYPTION_KEY_SIZE];
194
195 uint8_t confirm[SM_MAX_ENCRYPTION_KEY_SIZE]; //peerConfirm, ownConfirm, ownDHCheck
196
197 uint8_t peerPkx[32]; //peerPublicKey(x), peerDHCheck (16 bytes)
198 uint8_t dhKey[32]; //peerPublicKey(y), DHKey
199 uint8_t macKey[SM_MAX_ENCRYPTION_KEY_SIZE]; //macKey, passkeyCount(SC)
200
201 SM_PAIRING_RECORD_TYPE pairingData;
202 }SM_PAIRING_DATA;
203
204 typedef struct
205 {
206 uint16_t secReqFlag;
207 }SM_CTL_TYPE;
208
209 /*uint8_t SM_GetEncKeySize(uint8_t idx);
210 bool SM_IsLinkEncrypted(uint8_t idx);*/
211
212 typedef void (*SM_TKVALUE_TABLE)(uint8_t idx);
213 typedef void(*SM_CommandHandler)(uint8_t, uint8_t*, SM_PAIRING_DATA *);
214
215 bool SM_Common_ValueCompare(uint8_t *ptr1, uint8_t *ptr2, uint8_t len);
216 bool SM_Common_IsSecureConnection(uint8_t idx);
217 SM_PAIRING_DATA *SM_Common_AllocPairingData(uint8_t idx);
218 uint8_t SM_Common_GetRi(uint8_t n, uint8_t *PK);
219 bool sm_hal_flash_load_data(uint8_t *addr, SM_PAIRING_RECORD_TYPE *record); //put here, because cross reference of sm_common.h/sm_hal.h
220
221
222 #endif