TI BLE Stack API Documentation  2.03.09
sm.h
1 /******************************************************************************
2 
3  @file sm.h
4 
5  @brief BLE SM API
6 
7  This file contains the interface to the SM.
8 
9  Group: WCS, BTS
10  $Target Device: DEVICES $
11 
12  ******************************************************************************
13  $License: TISD 2009 $
14  ******************************************************************************
15  $Release Name: PACKAGE NAME $
16  $Release Date: PACKAGE RELEASE DATE $
17  *****************************************************************************/
18 
19 #ifndef SM_H
20 #define SM_H
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
27 /*-------------------------------------------------------------------
28  * INCLUDES
29  */
30 #include "bcomdef.h"
31 #include "osal.h"
32 
33 #include "hci.h"
34 
35 /*-------------------------------------------------------------------
36  * MACROS
37  */
38 
39 /*-------------------------------------------------------------------
40  * CONSTANTS
41  */
45 #define DISPLAY_ONLY 0x00
46 #define DISPLAY_YES_NO 0x01
47 #define KEYBOARD_ONLY 0x02
48 #define NO_INPUT_NO_OUTPUT 0x03
49 #define KEYBOARD_DISPLAY 0x04
50 
52 #define SM_AUTH_MITM_MASK(a) (((a) & 0x04) >> 2)
53 
57 #define SM_PASSKEY_TYPE_INPUT 0x01
58 #define SM_PASSKEY_TYPE_DISPLAY 0x02
59 
65 #define SM_AUTH_REQ_NO_BONDING 0x00
66 #define SM_AUTH_REQ_BONDING 0x01
67 
69 #define PASSKEY_LEN 6
70 
71 #define SM_AUTH_STATE_AUTHENTICATED 0x04
72 #define SM_AUTH_STATE_BONDING 0x01
73 #define SM_AUTH_STATE_SECURECONNECTION 0x08
74 
75 #define SM_ECC_KEY_LEN 32 // ECC Key length in bytes
76 
77 /* SM private/public key regeneration policy */
78 #define SM_ECC_KEYS_NOT_AVAILABLE 0xFF // Initial state of recycled keys before they exist.
79 #define SM_ECC_KEYS_REGNENERATE_NEVER 0xFF // Never regenerate the keys.
80 #define SM_ECC_KEYS_REGENERATE_ALWAYS 0x00 // Always regenerate the keys.
81 
85 #define SM_ECC_KEYS_EVENT 0x00 // ECC Keys
86 #define SM_DH_KEY_EVENT 0x01 // Diffie-Hellman key
87 
88 /*-------------------------------------------------------------------
89  * General TYPEDEFS
90  */
91 
96 typedef struct
97 {
99  uint8 newKey[KEYLEN];
101 
105 typedef struct
106 {
107  osal_event_hdr_t hdr;
108  uint8 opcode;
109 } smEventHdr_t;
110 
115 typedef struct
116 {
118  uint8 opcode;
119  uint8 privateKey[SM_ECC_KEY_LEN];
120  uint8 publicKeyX[SM_ECC_KEY_LEN];
121  uint8 publicKeyY[SM_ECC_KEY_LEN];
123 
128 typedef struct
129 {
131  uint8 opcode;
132  uint8 dhKey[SM_ECC_KEY_LEN];
133 } smDhKeyEvt_t;
134 
138 typedef struct
139 {
140  unsigned int sEncKey:1;
141  unsigned int sIdKey:1;
142  unsigned int sSign:1;
143  unsigned int sLinkKey:1;
144  unsigned int sReserved:4;
145  unsigned int mEncKey:1;
146  unsigned int mIdKey:1;
147  unsigned int mSign:1;
148  unsigned int mLinkKey:1;
149  unsigned int mReserved:4;
150 } keyDist_t;
151 
155 typedef struct
156 {
157  uint8 isUsed;
158  uint8 sK[SM_ECC_KEY_LEN];
159  uint8 pK_x[SM_ECC_KEY_LEN];
160  uint8 pK_y[SM_ECC_KEY_LEN];
161 } smEccKeys_t;
162 
166 typedef struct
167 {
168  uint8 ioCaps;
169  uint8 oobAvailable;
170  uint8 oob[KEYLEN];
171  uint8 oobConfirm[KEYLEN];
173  uint8 localOob[KEYLEN];
174  uint8 isSCOnlyMode;
176  uint8 authReq;
180 
184 typedef struct
185 {
186  uint8 ltk[KEYLEN];
187  uint16 div;
188  uint8 rand[B_RANDOM_NUM_SIZE];
189  uint8 keySize;
191 
195 typedef struct
196 {
197  uint8 irk[KEYLEN];
198  uint8 addrType;
199  uint8 bd_addr[B_ADDR_LEN];
201 
205 typedef struct
206 {
207  uint8 srk[KEYLEN];
208  uint32 signCounter;
210 
214 typedef struct
215 {
216  unsigned int bonding:2;
217  unsigned int mitm:1;
218  unsigned int sc:1;
219  unsigned int kp:1;
220  unsigned int reserved:3;
221 } authReq_t;
222 
226 typedef void (*smCB_t)
227 (
228 uint8 *secretKey,
229 uint8 *publicKeyX,
230 uint8 *publicKeyY
231 );
232 
233 /*-------------------------------------------------------------------
234  * GLOBAL VARIABLES
235  */
236 
243 /*-------------------------------------------------------------------
244  * FUNCTIONS - APPLICATION API - Functions the application may call
245  */
246 
247 /*********************************************************************
248  * @fn SM_RegisterTask
249  *
250  * @brief Register Task with the Security Manager to receive
251  * ECC keys or a ECDH shared secret.
252  *
253  * @params taskID - Application's task ID.
254  *
255  * @return None
256  */
257 extern void SM_RegisterTask( uint8 taskID );
258 
259 /******************************************************************************
260  * @brief Get ECC private and public keys. Keys are returned through the
261  * callback registered in SM_RegisterCb.
262  *
263  * @param None
264  *
265  * @return SUCCESS if processing, FAILURE if SM is pairing or
266  * HCI_ERROR_CODE_MEM_CAP_EXCEEDED if out of heap memory.
267  */
268 extern bStatus_t SM_GetEccKeys( void );
269 
270 /*********************************************************************
271  * @brief Request a Diffie-Hellman key from the ECC engine. Key is returned
272  * through the callback registered in SM_RegisterCb, in the first
273  * parameter.
274  *
275  * @param secretKey - the local private key
276  * @param publicKey1 - the remote public key, X-Coordinate.
277  * @param publicKey2 - the remote public key, Y-Coordinate.
278  *
279  * @return SUCCESS if processing, FAILURE if SM is pairing, or
280  * HCI_ERROR_CODE_MEM_CAP_EXCEEDED if out of heap memory.
281  */
282 extern bStatus_t SM_GetDHKey( uint8 *secretKey, uint8 *publicKeyX,
283  uint8 *publicKeyY );
284 
285 /*********************************************************************
286  * @brief Generate a confirm value for OOB data.
287  *
288  * @param publicKey - the local public key X-coordinate
289  * @param oob - the random string to be sent to the remote device via
290  * oob
291  * @param pOut - pointer to the generated confirm value. The calling
292  * application must copy this data into its own buffer.
293  *
294  * @return SUCCESS, INVALIDPARAMETER or bleMemAllocError
295  */
296 extern bStatus_t SM_GetScConfirmOob( uint8 *publicKey, uint8 *oob, uint8 *pOut );
297 
298 /*-------------------------------------------------------------------
299  * FUNCTIONS - MASTER API - Only use these in a master device
300  */
301 
307 extern bStatus_t SM_InitiatorInit( void );
308 
324 extern bStatus_t SM_StartPairing( uint8 initiator,
325  uint8 taskID,
326  uint16 connectionHandle,
327  smLinkSecurityReq_t *pSecReqs );
328 
342 extern bStatus_t SM_StartEncryption( uint16 connHandle, uint8 *pLTK,
343  uint16 div, uint8 *pRandNum,
344  uint8 keyLen );
345 
346 
347 /*-------------------------------------------------------------------
348  * FUNCTIONS - SLAVE API - Only use these in a slave device
349  */
350 
356 extern bStatus_t SM_ResponderInit( void );
357 
358 /*-------------------------------------------------------------------
359  * FUNCTIONS - GENERAL API - both master and slave
360  */
361 
372 extern bStatus_t SM_NewRandKey( uint8 taskID );
373 
374 #if !defined (BLE_V42_FEATURES) || !(BLE_V42_FEATURES & PRIVACY_1_2_CFG)
375 
384 extern bStatus_t SM_CalcRandomAddr( uint8 *pIRK, uint8 *pNewAddr );
385 
396 extern bStatus_t SM_ResolveRandomAddrs( uint8 *pIRK, uint8 *pAddr );
397 #endif // ! BLE_V42_FEATURES | ! PRIVACY_1_2_CFG
398 
411 extern bStatus_t SM_Encrypt( uint8 *pKey, uint8 *pPlainText, uint8 *pResult );
412 
424 extern bStatus_t SM_GenerateAuthenSig( uint8 *pData, uint16 len,
425  uint8 *pAuthenSig );
426 
442 extern bStatus_t SM_VerifyAuthenSig( uint16 connHandle,
443  uint8 authentication,
444  uint8 *pData,
445  uint16 len,
446  uint8 *pAuthenSig );
447 
458 extern bStatus_t SM_PasskeyUpdate( uint8 *pPasskey, uint16 connectionHandle );
459 
460 
468 extern void SM_SetECCRegenerationCount(uint8 count);
469 
470 
475 /*-------------------------------------------------------------------
476  * TASK API - These functions must only be called by OSAL.
477  */
478 
488  extern void SM_Init( uint8 task_id );
489 
500  extern uint16 SM_ProcessEvent( uint8 task_id, uint16 events );
501 
518  extern void SM_p256KeyCB( hciEvt_BLEReadP256PublicKeyComplete_t *pK,
519  uint8 *privateKey );
520 
537  extern void SM_dhKeyCB( hciEvt_BLEGenDHKeyComplete_t *pDhKey );
538 
539 /*-------------------------------------------------------------------
540 -------------------------------------------------------------------*/
541 
542 #ifdef __cplusplus
543 }
544 #endif
545 
546 #endif /* SM_H */
bStatus_t SM_StartEncryption(uint16 connHandle, uint8 *pLTK, uint16 div, uint8 *pRandNum, uint8 keyLen)
Send Start Encrypt through HCI.
bStatus_t SM_PasskeyUpdate(uint8 *pPasskey, uint16 connectionHandle)
Update the passkey for the pairing process.
LE Read P256 Public Key Complete Event.
Definition: hci.h:1130
void(* smCB_t)(uint8 *secretKey, uint8 *publicKeyX, uint8 *publicKeyY)
Definition: sm.h:227
bStatus_t SM_CalcRandomAddr(uint8 *pIRK, uint8 *pNewAddr)
Calculate a new Private Resolvable address.
uint8 maxEncKeySize
Maximum Encryption Key size (7-16 bytes)
Definition: sm.h:178
uint8 opcode
SM_DH_KEY_EVENT.
Definition: sm.h:131
void SM_Init(uint8 task_id)
Definition: sm.h:166
Definition: sm.h:115
uint8 authReq
Authentication Requirements.
Definition: sm.h:176
Definition: sm.h:105
osal_event_hdr_t hdr
SM_MSG_EVENT and status.
Definition: sm.h:117
#define B_ADDR_LEN
Default Public and Random Address Length.
Definition: bcomdef.h:114
bStatus_t SM_ResolveRandomAddrs(uint8 *pIRK, uint8 *pAddr)
Resolve a Private Resolvable Address.
Definition: sm.h:184
uint32 signCounter
Sign Counter.
Definition: sm.h:208
Definition: sm.h:214
bStatus_t SM_VerifyAuthenSig(uint16 connHandle, uint8 authentication, uint8 *pData, uint16 len, uint8 *pAuthenSig)
Verify an Authentication Signature.
Definition: sm.h:128
LE Generate DHKey Complete Event.
Definition: hci.h:1139
uint8 isSCOnlyMode
TRUE if Secure Connections Only Mode. Secure Connections only.
Definition: sm.h:174
Definition: sm.h:138
uint16 div
LTK Diversifier.
Definition: sm.h:187
osal_event_hdr_t hdr
SM_MSG_EVENT and status.
Definition: sm.h:130
Status_t bStatus_t
BLE Generic Status return: BLE Default BLE Status Values.
Definition: bcomdef.h:244
#define KEYLEN
Default key length.
Definition: bcomdef.h:117
uint8 isUsed
FALSE if not used. USE_PKEYS if public keys only. USE_ALL_KEYS if public and private keys...
Definition: sm.h:157
uint8 keySize
LTK Key Size (7-16 bytes)
Definition: sm.h:189
Definition: sm.h:96
void SM_SetECCRegenerationCount(uint8 count)
Update the regeneration count for ECC key re-usage.
bStatus_t SM_InitiatorInit(void)
Initialize SM Initiator on a master device.
osal_event_hdr_t hdr
SM_NEW_RAND_KEY_EVENT and status.
Definition: sm.h:98
Definition: osal.h:89
bStatus_t SM_NewRandKey(uint8 taskID)
Generate a key with a random value.
uint8 ioCaps
I/O Capabilities (ie.
Definition: sm.h:168
bStatus_t SM_StartPairing(uint8 initiator, uint8 taskID, uint16 connectionHandle, smLinkSecurityReq_t *pSecReqs)
Start the pairing process. This function is also called if the device is already bound.
HCI layer interface.
keyDist_t keyDist
Key Distribution mask.
Definition: sm.h:177
uint8 opcode
SM_ECC_KEYS_EVENT.
Definition: sm.h:118
uint8 addrType
Address type for BD_ADDR.
Definition: sm.h:198
uint8 localOobAvailable
True if local Out-of-band key available. Secure Connections only.
Definition: sm.h:172
bStatus_t SM_Encrypt(uint8 *pKey, uint8 *pPlainText, uint8 *pResult)
Encrypt the plain text data with the key..
bStatus_t SM_GenerateAuthenSig(uint8 *pData, uint16 len, uint8 *pAuthenSig)
Generate an outgoing Authentication Signature.
Definition: sm.h:195
uint8 oobAvailable
True if remote Out-of-band key available.
Definition: sm.h:169
smEccKeys_t eccKeys
Optionally specified ECC keys for pairing. Secure Connections only.
Definition: sm.h:175
#define B_RANDOM_NUM_SIZE
BLE Random Number Size.
Definition: bcomdef.h:132
bStatus_t SM_ResponderInit(void)
Initialize SM Responder on a slave device.
Definition: sm.h:205
Definition: sm.h:155