BLE-Stack APIs  3.00.00
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file sm.h
34  * @brief This file contains the interface to the SM.
35  */
36 
37 #ifndef SM_H
38 #define SM_H
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 /*-------------------------------------------------------------------
46  * INCLUDES
47  */
48 #include "bcomdef.h"
49 #include "osal.h"
50 
51 #include "hci.h"
52 
53 /*-------------------------------------------------------------------
54  * MACROS
55  */
56 
57 /*-------------------------------------------------------------------
58  * CONSTANTS
59  */
67 #define DISPLAY_ONLY 0x00
68 #define DISPLAY_YES_NO 0x01
69 #define KEYBOARD_ONLY 0x02
70 #define NO_INPUT_NO_OUTPUT 0x03
71 #define KEYBOARD_DISPLAY 0x04
72 
74 #define SM_AUTH_MITM_MASK(a) (((a) & 0x04) >> 2)
75 
79 #define SM_PASSKEY_TYPE_INPUT 0x01
80 #define SM_PASSKEY_TYPE_DISPLAY 0x02
81 
87 #define SM_AUTH_REQ_NO_BONDING 0x00
88 #define SM_AUTH_REQ_BONDING 0x01
89 
91 #define PASSKEY_LEN 6
92 
93 #define SM_AUTH_STATE_AUTHENTICATED 0x04
94 #define SM_AUTH_STATE_BONDING 0x01
95 #define SM_AUTH_STATE_SECURECONNECTION 0x08
96 
97 #define SM_ECC_KEY_LEN 32 // ECC Key length in bytes
98 
99 /* SM private/public key regeneration policy */
100 #define SM_ECC_KEYS_NOT_AVAILABLE 0xFF // Initial state of recycled keys before they exist.
101 #define SM_ECC_KEYS_REGNENERATE_NEVER 0xFF // Never regenerate the keys.
102 #define SM_ECC_KEYS_REGENERATE_ALWAYS 0x00 // Always regenerate the keys.
103 
107 #define SM_ECC_KEYS_EVENT 0x00 // ECC Keys
108 #define SM_DH_KEY_EVENT 0x01 // Diffie-Hellman key
109  // end of GAPBondMgr_Constants
112 
113 /*-------------------------------------------------------------------
114  * General TYPEDEFS
115  */
116 
121 typedef struct
122 {
124  uint8 newKey[KEYLEN];
126 
130 typedef struct
131 {
132  osal_event_hdr_t hdr;
133  uint8 opcode;
134 } smEventHdr_t;
135 
140 typedef struct
141 {
143  uint8 opcode;
144  uint8 privateKey[SM_ECC_KEY_LEN];
145  uint8 publicKeyX[SM_ECC_KEY_LEN];
146  uint8 publicKeyY[SM_ECC_KEY_LEN];
148 
153 typedef struct
154 {
156  uint8 opcode;
157  uint8 dhKey[SM_ECC_KEY_LEN];
158 } smDhKeyEvt_t;
159 
163 typedef struct
164 {
165  unsigned int sEncKey:1;
166  unsigned int sIdKey:1;
167  unsigned int sSign:1;
168  unsigned int sLinkKey:1;
169  unsigned int sReserved:4;
170  unsigned int mEncKey:1;
171  unsigned int mIdKey:1;
172  unsigned int mSign:1;
173  unsigned int mLinkKey:1;
174  unsigned int mReserved:4;
175 } keyDist_t;
176 
180 typedef struct
181 {
182  uint8 isUsed;
183  uint8 sK[SM_ECC_KEY_LEN];
184  uint8 pK_x[SM_ECC_KEY_LEN];
185  uint8 pK_y[SM_ECC_KEY_LEN];
186 } smEccKeys_t;
187 
191 typedef struct
192 {
193  uint8 ioCaps;
194  uint8 oobAvailable;
195  uint8 oob[KEYLEN];
196  uint8 oobConfirm[KEYLEN];
198  uint8 localOob[KEYLEN];
199  uint8 isSCOnlyMode;
201  uint8 authReq;
205 
209 typedef struct
210 {
211  uint8 ltk[KEYLEN];
212  uint16 div;
213  uint8 rand[B_RANDOM_NUM_SIZE];
214  uint8 keySize;
216 
220 typedef struct
221 {
222  uint8 irk[KEYLEN];
223  uint8 addrType;
224  uint8 bd_addr[B_ADDR_LEN];
226 
230 typedef struct
231 {
232  uint8 srk[KEYLEN];
233  uint32 signCounter;
235 
239 typedef struct
240 {
241  unsigned int bonding:2;
242  unsigned int mitm:1;
243  unsigned int sc:1;
244  unsigned int kp:1;
245  unsigned int reserved:3;
246 } authReq_t;
247 
251 typedef void (*smCB_t)
252 (
253 uint8 *secretKey,
254 uint8 *publicKeyX,
255 uint8 *publicKeyY
256 );
257 
258 /*-------------------------------------------------------------------
259  * GLOBAL VARIABLES
260  */
261 
262 /*-------------------------------------------------------------------
263  * FUNCTIONS - APPLICATION API - Functions the application may call
264  */
265 
276 extern void SM_RegisterTask( uint8 taskID );
277 
285 extern bStatus_t SM_GetEccKeys( void );
286 
287 /*********************************************************************
288  * @brief Request a Diffie-Hellman key from the ECC engine. Key is returned
289  * through the callback registered in SM_RegisterCb, in the first
290  * parameter.
291  *
292  * @param secretKey - the local private key
293  * @param publicKey1 - the remote public key, X-Coordinate.
294  * @param publicKey2 - the remote public key, Y-Coordinate.
295  *
296  * @return SUCCESS if processing, FAILURE if SM is pairing, or
297  * HCI_ERROR_CODE_MEM_CAP_EXCEEDED if out of heap memory.
298  */
299 extern bStatus_t SM_GetDHKey( uint8 *secretKey, uint8 *publicKeyX,
300  uint8 *publicKeyY );
301 
302 /*********************************************************************
303  * @brief Generate a confirm value for OOB data.
304  *
305  * @param publicKey - the local public key X-coordinate
306  * @param oob - the random string to be sent to the remote device via
307  * oob
308  * @param pOut - pointer to the generated confirm value. The calling
309  * application must copy this data into its own buffer.
310  *
311  * @return SUCCESS, INVALIDPARAMETER or bleMemAllocError
312  */
313 extern bStatus_t SM_GetScConfirmOob( uint8 *publicKey, uint8 *oob, uint8 *pOut );
314 
315 /*-------------------------------------------------------------------
316  * FUNCTIONS - MASTER API - Only use these in a master device
317  */
318 
324 extern bStatus_t SM_InitiatorInit( void );
325 
341 extern bStatus_t SM_StartPairing( uint8 initiator,
342  uint8 taskID,
343  uint16 connectionHandle,
344  smLinkSecurityReq_t *pSecReqs );
345 
359 extern bStatus_t SM_StartEncryption( uint16 connHandle, uint8 *pLTK,
360  uint16 div, uint8 *pRandNum,
361  uint8 keyLen );
362 
363 
364 /*-------------------------------------------------------------------
365  * FUNCTIONS - SLAVE API - Only use these in a slave device
366  */
367 
373 extern bStatus_t SM_ResponderInit( void );
374 
375 /*-------------------------------------------------------------------
376  * FUNCTIONS - GENERAL API - both master and slave
377  */
378 
389 extern bStatus_t SM_NewRandKey( uint8 taskID );
390 
391 #if !defined (BLE_V42_FEATURES) || !(BLE_V42_FEATURES & PRIVACY_1_2_CFG)
392 
401 extern bStatus_t SM_CalcRandomAddr( uint8 *pIRK, uint8 *pNewAddr );
402 
413 extern bStatus_t SM_ResolveRandomAddrs( uint8 *pIRK, uint8 *pAddr );
414 #endif // ! BLE_V42_FEATURES | ! PRIVACY_1_2_CFG
415 
428 extern bStatus_t SM_Encrypt( uint8 *pKey, uint8 *pPlainText, uint8 *pResult );
429 
441 extern bStatus_t SM_GenerateAuthenSig( uint8 *pData, uint16 len,
442  uint8 *pAuthenSig );
443 
459 extern bStatus_t SM_VerifyAuthenSig( uint16 connHandle,
460  uint8 authentication,
461  uint8 *pData,
462  uint16 len,
463  uint8 *pAuthenSig );
464 
475 extern bStatus_t SM_PasskeyUpdate( uint8 *pPasskey, uint16 connectionHandle );
476 
477 
485 extern void SM_SetECCRegenerationCount(uint8 count);
486 
487 /*-------------------------------------------------------------------
488  * TASK API - These functions must only be called by OSAL.
489  */
490 
500  extern void SM_Init( uint8 task_id );
501 
512  extern uint16 SM_ProcessEvent( uint8 task_id, uint16 events );
513 
530  extern void SM_p256KeyCB( hciEvt_BLEReadP256PublicKeyComplete_t *pK,
531  uint8 *privateKey );
532 
549  extern void SM_dhKeyCB( hciEvt_BLEGenDHKeyComplete_t *pDhKey );
550 
551 /*-------------------------------------------------------------------
552 -------------------------------------------------------------------*/
553 
554 #ifdef __cplusplus
555 }
556 #endif
557 
558 #endif /* SM_H */
uint8 opcode
SM_DH_KEY_EVENT.
Definition: sm.h:156
bStatus_t SM_PasskeyUpdate(uint8 *pPasskey, uint16 connectionHandle)
Update the passkey for the pairing process.
This API allows the software components in the Z-Stack to be written independently of the specifics o...
bStatus_t SM_ResolveRandomAddrs(uint8 *pIRK, uint8 *pAddr)
Resolve a Private Resolvable Address.
Definition: sm.h:180
Definition: sm.h:209
#define B_RANDOM_NUM_SIZE
BLE Random Number Size.
Definition: bcomdef.h:149
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.
bStatus_t SM_NewRandKey(uint8 taskID)
Generate a key with a random value.
Definition: sm.h:140
Definition: sm.h:130
uint8 addrType
Address type for BD_ADDR.
Definition: sm.h:223
void SM_SetECCRegenerationCount(uint8 count)
Update the regeneration count for ECC key re-usage.
bStatus_t SM_ResponderInit(void)
Initialize SM Responder on a slave device.
bStatus_t SM_GetEccKeys(void)
Get ECC private and public keys. Keys are returned through the callback registered in SM_RegisterCb...
void(* smCB_t)(uint8 *secretKey, uint8 *publicKeyX, uint8 *publicKeyY)
Definition: sm.h:252
Definition: sm.h:121
Definition: sm.h:163
uint8 keySize
LTK Key Size (7-16 bytes)
Definition: sm.h:214
Definition: sm.h:220
osal_event_hdr_t hdr
SM_MSG_EVENT and status.
Definition: sm.h:142
LE Generate DHKey Complete Event.
Definition: hci.h:1168
uint8 isUsed
FALSE if not used. USE_PKEYS if public keys only. USE_ALL_KEYS if public and private keys...
Definition: sm.h:182
Type definitions and macros for BLE stack.
uint8 opcode
SM_ECC_KEYS_EVENT.
Definition: sm.h:143
Definition: sm.h:230
OSAL Event Header.
Definition: osal.h:110
uint16 div
LTK Diversifier.
Definition: sm.h:212
bStatus_t SM_CalcRandomAddr(uint8 *pIRK, uint8 *pNewAddr)
Calculate a new Private Resolvable address.
HCI layer interface.
Definition: sm.h:153
osal_event_hdr_t hdr
SM_MSG_EVENT and status.
Definition: sm.h:155
#define B_ADDR_LEN
Default Public and Random Address Length.
Definition: bcomdef.h:131
bStatus_t SM_StartEncryption(uint16 connHandle, uint8 *pLTK, uint16 div, uint8 *pRandNum, uint8 keyLen)
Send Start Encrypt through HCI.
bStatus_t SM_InitiatorInit(void)
Initialize SM Initiator on a master device.
Status_t bStatus_t
BLE Generic Status return.
Definition: bcomdef.h:243
bStatus_t SM_GenerateAuthenSig(uint8 *pData, uint16 len, uint8 *pAuthenSig)
Generate an outgoing Authentication Signature.
uint32 signCounter
Sign Counter.
Definition: sm.h:233
LE Read P256 Public Key Complete Event.
Definition: hci.h:1159
Definition: sm.h:239
bStatus_t SM_VerifyAuthenSig(uint16 connHandle, uint8 authentication, uint8 *pData, uint16 len, uint8 *pAuthenSig)
Verify an Authentication Signature.
#define KEYLEN
Default key length.
Definition: bcomdef.h:134
osal_event_hdr_t hdr
SM_NEW_RAND_KEY_EVENT and status.
Definition: sm.h:123
bStatus_t SM_Encrypt(uint8 *pKey, uint8 *pPlainText, uint8 *pResult)
Encrypt the plain text data with the key..
Copyright 2016, Texas Instruments Incorporated