1 #ifndef SMPC_H_
2 #define SMPC_H_
3 
4 /**
5  ****************************************************************************************
6  * @addtogroup SMP Security Manager Protocol
7  * @ingroup HOST
8  * @brief Security Manager Protocol.
9  *
10  * The SMP is responsible for the over-all security policies of BLE.
11  * It defines methods for pairing and key distribution, handles encryption,
12  * data signing and privacy features such as random addressing generation and resolution.
13  *
14  * Pairing is performed to exchange pairing features and generate a short term
15  * key for link encryption.
16  * A transport specific key distribution is performed to
17  * share the keys that can be used to encrypt the link in the future
18  * reconnection process, signed data verification and random address
19  * resolution.
20  *
21  * There exist 3 phases in the complete security procedure:
22  * 1. Feature exchange (IO capabilities, OOB flags, Authentication Requirements, Key distributions)
23  * 2. Short Term Key generation
24  *    Generation method depends on exchanged features:
25  *     - Just Works - use Temporary key = 0
26  *     - PassKey Entry - use Temporary Key = 6-digit provided by user
27  *     - Out of Band (OOB) - use Temporary Key = 16-octet key, available form OOB source
28  * 3. Transport Specific Key Distribution (TKDP)(LTK+EDIV+RAND_NB, IRK+ADDR, CSRK)
29  *---------------------------------------------------------------------
30  * @addtogroup SMPC Security Manager Protocol Controller
31  * @ingroup SMP
32  * @brief Security Manager Protocol Controller.
33  *
34  * This block handles control of SM procedures for several possible existing connections,
35  * for which the security procedure may be conducted simultaneously.
36  *
37  * It allows flow control for HCI access to encryption and random number generation, used
38  * at different moments in the procedure.
39  *
40  * It handles PDU creation and sending through L2CAP, also their reception from L2CAP
41  * and interpretation.
42  *
43  * Other small utilities such as maximum key size determination and TKDP organization are
44  * implemented in SMPC.
45  * @{
46  *
47  ****************************************************************************************
48  */
49 
50 
51 /*
52  * INCLUDE FILES
53  ****************************************************************************************
54  */
55 #include "rwip_config.h"
56 
57 #if (BLE_SMPC)
58 #include "co_bt.h"
59 #include "gap.h"
60 #include "gapc_task.h"
61 #include "ke_task.h"
62 
63 /*
64  * DEFINES
65  ****************************************************************************************
66  */
67 
68 /// MAC length
69 #define SMPC_SIGN_MAC_LEN                       (8)
70 /// SignCounter length
71 #define SMPC_SIGN_COUNTER_LEN                   (4)
72 /// Signature length
73 #define SMPC_SIGN_LEN                           (SMPC_SIGN_MAC_LEN + SMPC_SIGN_COUNTER_LEN)
74 
75 /**
76  * Repeated Attempts Timer Configuration
77  */
78 /// Repeated Attempts Timer default value (x10ms)
79 #define SMPC_REP_ATTEMPTS_TIMER_DEF_VAL         (200)      //2s
80 /// Repeated Attempts Timer max value (x10ms)
81 #define SMPC_REP_ATTEMPTS_TIMER_MAX_VAL         (3000)     //30s
82 /// Repeated Attempts Timer multiplier
83 #define SMPC_REP_ATTEMPTS_TIMER_MULT            (2)
84 
85 /**
86  * Timeout Timer Configuration
87  */
88 #define SMPC_TIMEOUT_TIMER_DURATION             (3000)     //30s
89 
90 #define SMPC_PUBLIC_KEY_256_COORD_LEN            0x20
91 /*
92  * ENUMERATIONS
93  ****************************************************************************************
94  */
95 
96 
97 /// Information source.
98 enum smpc_addr_src
99 {
100     /// Local info.
101     SMPC_INFO_LOCAL,
102     /// Peer info.
103     SMPC_INFO_PEER,
104     /// Maximum info source.
105     SMPC_INFO_MAX
106 };
107 
108 /*
109  * STRUCTURES DEFINITION
110  ****************************************************************************************
111  */
112 
113 /// Master ID Information Structure
114 struct smpc_mst_id_info
115 {
116     // Encryption Diversifier
117     uint16_t ediv;
118 
119     // Random Number
120     uint8_t randnb[GAP_RAND_NB_LEN];
121 };
122 
123 #if (SECURE_CONNECTIONS)
124 struct smp_aes_cmac
125 {
126     uint8_t* M; // pointer to memory allocated by calling function
127     uint8_t  M_len;
128     uint8_t  M_last[16];
129     uint8_t  X[16];
130     uint8_t  Y[16];
131     uint8_t*  K; //[16];
132     uint8_t  K1[16];
133     uint8_t  K2[16];
134     uint8_t  next_block;
135     uint8_t  num_blocks;
136     uint8_t  state;  // Only 3 States - Idle, SubKey Generation, Block AES
137 };
138 
139 struct smp_f4
140 {
141     uint8_t M[65];
142     uint8_t X[16]; // The Key
143 };
144 
145 struct smp_f5
146 {
147     uint8_t M[53];
148     uint8_t* W;
149     uint8_t T[16];
150     uint8_t SALT[16];
151 };
152 
153 
154 struct smp_f6
155 {
156     uint8_t W[16];
157     uint8_t M[65];
158 };
159 
160 struct smp_g2
161 {
162     uint8_t X[16];
163     uint8_t M[80];
164 };
165 
166 struct gapc_public_key
167 {
168     uint8_t x[GAP_P256_KEY_LEN];
169     uint8_t y[GAP_P256_KEY_LEN];
170 };
171 
172 #endif // (SECURE_CONNECTIONS)
173 /// Pairing Information
174 struct smpc_pair_info
175 {
176     /// TK during Phase 2, LTK or IRK during Phase 3
177     struct gap_sec_key key;
178     /// Pairing request command
179     struct gapc_pairing pair_req_feat;
180     /// Pairing response feature
181     struct gapc_pairing pair_rsp_feat;
182     /// Random number value
183     uint8_t rand[RAND_VAL_LEN];
184     /// Remote random number value
185     uint8_t rem_rand[RAND_VAL_LEN];
186     /// Confirm value to check
187     uint8_t conf_value[GAP_KEY_LEN];
188     /// Pairing Method
189     uint8_t pair_method;
190     /// Authentication level
191     uint8_t auth;
192     /// check that LTK exchanged during pairing
193     bool ltk_exchanged;
194     /// Key to be exchanged (transmitted or to be received)
195     uint8_t keys_dist;
196 
197 
198     #if (SECURE_CONNECTIONS)
199     // AES_CMAC Info
200     struct smp_aes_cmac* aes_cmac;
201     // Structure for Secure Connections Crypto functions
202     struct smp_f4*  f4_info;
203     struct smp_f5*  f5_info;
204     struct smp_f6*  f6_info;
205     struct smp_g2*  g2_info;
206 
207     bool dh_key_calculation_complete;
208 
209     uint8_t MacKey[GAP_KEY_LEN];
210     uint8_t dh_key_check_peer[DHKEY_CHECK_LEN];
211     uint8_t dh_key_local[DH_KEY_LEN];
212 
213     uint8_t dh_key_check_local[DHKEY_CHECK_LEN];
214     bool dh_key_check_received_from_peer;
215 
216     public_key_t peer_public_key;
217 
218     uint8_t passkey_bit_count;
219     uint32_t passkey;
220 
221     // Required for OOB
222     uint8_t peer_r[GAP_KEY_LEN];
223     uint8_t local_r[GAP_KEY_LEN];
224     bool peer_rand_received;
225     bool peer_confirm_received;
226     #endif // (SECURE_CONNECTIONS)
227 };
228 
229 /// Signing Information
230 struct smpc_sign_info
231 {
232     /// Operation requester task id
233     ke_task_id_t requester;
234 
235     /// Message offset
236     uint16_t msg_offset;
237     /// Number of block
238     uint8_t block_nb;
239     /// Cn-1 value -> Need to kept this value to retrieve it after L generation
240     uint8_t cn1[GAP_KEY_LEN];
241 };
242 
243 /// SMPC environment structure
244 struct smpc_env
245 {
246     /// SMPC temporary information
247     union smpc_info
248     {
249         /**
250          * Pairing Information - This structure is allocated at the beginning of a pairing
251          * or procedure. It is freed when a disconnection occurs or at the end of
252          * the pairing procedure. If not enough memory can be found, the procedure will fail
253          *  with an "Unspecified Reason" error
254          */
255         struct smpc_pair_info *pair;
256 
257         /**
258          * Signature Procedure Information - This structure is allocated at the beginning of a
259          * signing procedure. It is freed when a disconnection occurs or at the end of
260          * the signing procedure. If not enough memory can be found, the procedure will fail
261          *  with an "Unspecified Reason" error.
262          */
263         struct smpc_sign_info *sign;
264     } info;
265 
266     /// CSRK values (Local and remote)
267     struct gap_sec_key csrk[SMPC_INFO_MAX];
268 
269     /// signature counter values (Local and remote)
270     uint32_t sign_counter[SMPC_INFO_MAX];
271 
272     /// Repeated Attempt Timer value
273     uint16_t rep_att_timer_val;
274 
275     /// Encryption key size
276     uint8_t key_size;
277 
278     /**
279      * Contains the current state of the two timers needed in the SMPC task
280      *      Bit 0 - Is Timeout Timer running
281      *      Bit 1 - Is Repeated Attempt Timer running
282      *      Bit 2 - Has task reached a SMP Timeout
283      */
284     uint8_t timer_state;
285 
286     /// State of the current procedure
287     uint8_t state;
288 
289     #if (SECURE_CONNECTIONS)
290     bool secure_connections_enabled;
291     #endif // (SECURE_CONNECTIONS)
292 };
293 
294 /*
295  * GLOBAL VARIABLES DEFINITION
296  ****************************************************************************************
297  */
298 
299 
300 /*
301  * MACROS
302  ****************************************************************************************
303  */
304 
305 /*
306  * FUNCTION DECLARATIONS
307  ****************************************************************************************
308  */
309 
310 
311 #endif //(BLE_SMPC)
312 #endif //SMPC_H_
313 
314 /// @} SMPC
315