27
27
// --------------------------------------------------------------------+
28
28
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
29
29
// --------------------------------------------------------------------+
30
- #define EDIV_INVALID 0xFFFF
30
+ #ifdef NRF_CRYPTOCELL
31
+ #define LESC_SUPPORTED 1
32
+ #else
33
+ #define LESC_SUPPORTED 0
34
+ #endif
31
35
32
36
// ------------- IMPLEMENTATION -------------//
33
37
@@ -46,26 +50,24 @@ static void swap_endian(uint8_t data[], uint32_t nbytes)
46
50
}
47
51
}
48
52
49
- BLEPairing::BLEPairing (void )
53
+ // default is Just Work
54
+ static const ble_gap_sec_params_t _sec_param_default =
50
55
{
51
- _sec_param = ((ble_gap_sec_params_t )
52
- {
53
- .bond = 1 ,
54
- .mitm = 0 ,
55
- .lesc = 0 ,
56
- .keypress = 0 ,
57
- .io_caps = BLE_GAP_IO_CAPS_NONE,
58
- .oob = 0 ,
59
- .min_key_size = 7 ,
60
- .max_key_size = 16 ,
61
- .kdist_own = { .enc = 1 , .id = 1 },
62
- .kdist_peer = { .enc = 1 , .id = 1 },
63
- });
64
-
65
- #ifdef NRF_CRYPTOCELL
66
- // _sec_param.lesc = 1; // enable LESC if CryptoCell is present
67
- #endif
56
+ .bond = 1 ,
57
+ .mitm = 0 ,
58
+ .lesc = LESC_SUPPORTED,
59
+ .keypress = 0 ,
60
+ .io_caps = BLE_GAP_IO_CAPS_NONE,
61
+ .oob = 0 ,
62
+ .min_key_size = 7 ,
63
+ .max_key_size = 16 ,
64
+ .kdist_own = { .enc = 1 , .id = 1 },
65
+ .kdist_peer = { .enc = 1 , .id = 1 }
66
+ };
68
67
68
+ BLEPairing::BLEPairing (void )
69
+ {
70
+ _sec_param = _sec_param_default;
69
71
_passkey_cb = NULL ;
70
72
_complete_cb = NULL ;
71
73
}
@@ -100,6 +102,39 @@ bool BLEPairing::begin(void)
100
102
return true ;
101
103
}
102
104
105
+ void BLEPairing::setIOCaps (bool display, bool keyboard, bool yes_no)
106
+ {
107
+ uint8_t io_caps = BLE_GAP_IO_CAPS_NONE;
108
+
109
+ if (display)
110
+ {
111
+ if (keyboard)
112
+ {
113
+ io_caps = BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY;
114
+ }
115
+ else if (yes_no)
116
+ {
117
+ io_caps = BLE_GAP_IO_CAPS_DISPLAY_YESNO;
118
+ }else
119
+ {
120
+ io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
121
+ }
122
+ }else
123
+ {
124
+ if (keyboard) io_caps = BLE_GAP_IO_CAPS_KEYBOARD_ONLY;
125
+ }
126
+
127
+ _sec_param.io_caps = io_caps;
128
+
129
+ // also set Man in the middle protection if we have some IO caps
130
+ if (io_caps != BLE_GAP_IO_CAPS_NONE) _sec_param.mitm = 1 ;
131
+ }
132
+
133
+ void BLEPairing::setMITM (bool enabled)
134
+ {
135
+ _sec_param.mitm = (enabled ? 1 : 0 );
136
+ }
137
+
103
138
/* Resolvable Address = Hash (24 bit) | Random (24 bit)
104
139
* in which
105
140
* - Hash = AES(random) using IRK
@@ -136,16 +171,13 @@ bool BLEPairing::resolveAddress(ble_gap_addr_t const * p_addr, ble_gap_irk_t con
136
171
// Use Legacy SC static Passkey
137
172
bool BLEPairing::setPIN (const char * pin)
138
173
{
139
- // back to open mode
140
174
if (pin == NULL )
141
175
{
142
- _sec_param.bond = 1 ;
143
- _sec_param.mitm = 0 ;
144
- _sec_param.lesc = 0 ; // TODO NRF_CRYPTOCELL
145
- _sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
176
+ // back to default mode
177
+ _sec_param = _sec_param_default;
146
178
}else
147
179
{
148
- VERIFY ( strlen (pin) == BLE_GAP_PASSKEY_LEN );
180
+ VERIFY ( strlen (pin) == BLE_GAP_PASSKEY_LEN);
149
181
150
182
// Static Passkey requires using
151
183
// - Legacy SC
@@ -158,7 +190,7 @@ bool BLEPairing::setPIN(const char* pin)
158
190
159
191
ble_opt_t opt;
160
192
opt.gap_opt .passkey .p_passkey = (const uint8_t *) pin;
161
- VERIFY_STATUS ( sd_ble_opt_set (BLE_GAP_OPT_PASSKEY, &opt), false );
193
+ VERIFY_STATUS (sd_ble_opt_set (BLE_GAP_OPT_PASSKEY, &opt), false );
162
194
}
163
195
164
196
return true ;
@@ -171,20 +203,14 @@ bool BLEPairing::setPasskeyCallback(pair_passkey_cb_t fp)
171
203
172
204
if ( fp == NULL )
173
205
{
174
- // TODO callback clear
206
+ // back to default mode
207
+ _sec_param = _sec_param_default;
175
208
}else
176
209
{
177
210
_sec_param.bond = 1 ;
178
211
_sec_param.mitm = 1 ;
179
-
180
- // TODO NRF_CRYPTOCELL
181
- // _sec_param.lesc = 0;
182
- _sec_param.lesc = 1 ;
212
+ _sec_param.lesc = LESC_SUPPORTED;
183
213
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
184
-
185
- ble_opt_t opt;
186
- opt.gap_opt .passkey .p_passkey = NULL ; // generate Passkey randomly
187
- VERIFY_STATUS ( sd_ble_opt_set (BLE_GAP_OPT_PASSKEY, &opt), false );
188
214
}
189
215
190
216
return true ;
0 commit comments