blob: 1fec7e66cbf0a0c5981ba636f260f3e9e3bc25f5 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Satya Calloji444a8da2015-03-06 10:38:22 -08003 * Copyright (C) 1999-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
Satya Calloji444a8da2015-03-06 10:38:22 -080021 * This file contains security manager protocol utility functions
The Android Open Source Project5738f832012-12-12 16:00:35 -080022 *
23 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080024#include "bt_target.h"
25
Marie Janssend19e0782016-07-15 12:48:27 -070026#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -080027 #include <stdio.h>
28#endif
29#include <string.h>
30#include "bt_utils.h"
31#include "btm_ble_api.h"
32#include "smp_int.h"
33#include "btm_int.h"
34#include "btm_ble_int.h"
35#include "hcimsgs.h"
36#include "aes.h"
37#include "p_256_ecc_pp.h"
38#include "device/include/controller.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070039#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040
Satya Calloji444a8da2015-03-06 10:38:22 -080041#ifndef SMP_MAX_ENC_REPEAT
42 #define SMP_MAX_ENC_REPEAT 3
43#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080044
45static void smp_rand_back(tBTM_RAND_ENC *p);
Satya Calloji444a8da2015-03-06 10:38:22 -080046static void smp_generate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
47static void smp_generate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -080048static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p);
49static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p);
50static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p);
51static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p);
52static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p);
53static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p);
54static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p);
Marie Janssend19e0782016-07-15 12:48:27 -070055static bool smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output);
Satya Calloji444a8da2015-03-06 10:38:22 -080056static void smp_continue_private_key_creation(tSMP_CB *p_cb, tBTM_RAND_ENC *p);
57static void smp_process_private_key(tSMP_CB *p_cb);
58static void smp_finish_nonce_generation(tSMP_CB *p_cb);
59static void smp_process_new_nonce(tSMP_CB *p_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -080060
Satya Calloji444a8da2015-03-06 10:38:22 -080061#define SMP_PASSKEY_MASK 0xfff00000
The Android Open Source Project5738f832012-12-12 16:00:35 -080062
Marie Janssend19e0782016-07-15 12:48:27 -070063void smp_debug_print_nbyte_little_endian(uint8_t *p, const uint8_t *key_name, uint8_t len)
The Android Open Source Project5738f832012-12-12 16:00:35 -080064{
Marie Janssend19e0782016-07-15 12:48:27 -070065#if (SMP_DEBUG == TRUE)
Jakub Pawlowskice832512016-07-28 06:11:36 -070066 int ind;
Satya Calloji444a8da2015-03-06 10:38:22 -080067 int col_count = 32;
68 int row_count;
Marie Janssend19e0782016-07-15 12:48:27 -070069 uint8_t p_buf[512];
The Android Open Source Project5738f832012-12-12 16:00:35 -080070
Satya Calloji444a8da2015-03-06 10:38:22 -080071 SMP_TRACE_WARNING("%s(LSB ~ MSB):", key_name);
72 memset(p_buf, 0, sizeof(p_buf));
73 row_count = len % col_count ? len / col_count + 1: len / col_count;
74
75 ind = 0;
76 for (int row = 0; row < row_count; row++)
The Android Open Source Project5738f832012-12-12 16:00:35 -080077 {
Satya Calloji444a8da2015-03-06 10:38:22 -080078 for (int column = 0, x = 0; (ind < len) && (column < col_count); column++, ind++)
79 {
George Burgess IV80d7f602016-03-02 14:00:19 -080080 x += snprintf((char *)&p_buf[x], sizeof(p_buf) - x, "%02x ", p[ind]);
Satya Calloji444a8da2015-03-06 10:38:22 -080081 }
82 SMP_TRACE_WARNING(" [%03d]: %s", row * col_count, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -080083 }
Satya Calloji444a8da2015-03-06 10:38:22 -080084#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080085}
Satya Calloji444a8da2015-03-06 10:38:22 -080086
Marie Janssend19e0782016-07-15 12:48:27 -070087void smp_debug_print_nbyte_big_endian (uint8_t *p, const uint8_t *key_name, uint8_t len)
Satya Calloji444a8da2015-03-06 10:38:22 -080088{
Marie Janssend19e0782016-07-15 12:48:27 -070089#if (SMP_DEBUG == TRUE)
90 uint8_t p_buf[512];
Satya Calloji444a8da2015-03-06 10:38:22 -080091
92 SMP_TRACE_WARNING("%s(MSB ~ LSB):", key_name);
93 memset(p_buf, 0, sizeof(p_buf));
Satya Calloji444a8da2015-03-06 10:38:22 -080094
95 int ind = 0;
96 int ncols = 32; /* num entries in one line */
97 int nrows; /* num lines */
Satya Calloji444a8da2015-03-06 10:38:22 -080098
Subramanian Srinivasan33bab322016-01-08 17:37:28 -080099 nrows = len % ncols ? len / ncols + 1: len / ncols;
Satya Calloji444a8da2015-03-06 10:38:22 -0800100 for (int row = 0; row < nrows; row++)
101 {
102 for (int col = 0, x = 0; (ind < len) && (col < ncols); col++, ind++)
103 {
George Burgess IV80d7f602016-03-02 14:00:19 -0800104 x += snprintf ((char *)&p_buf[len-x-1], sizeof(p_buf) - (len-x-1), "%02x ", p[ind]);
Satya Calloji444a8da2015-03-06 10:38:22 -0800105 }
106 SMP_TRACE_WARNING("[%03d]: %s", row * ncols, p_buf);
107 }
108#endif
109}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110
111/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800112 *
113 * Function smp_encrypt_data
114 *
115 * Description This function is called to encrypt data.
116 * It uses AES-128 encryption algorithm.
117 * Plain_text is encrypted using key, the result is at p_out.
118 *
119 * Returns void
120 *
121 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700122bool smp_encrypt_data (uint8_t *key, uint8_t key_len,
123 uint8_t *plain_text, uint8_t pt_len,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124 tSMP_ENC *p_out)
125{
Satya Calloji444a8da2015-03-06 10:38:22 -0800126 aes_context ctx;
Marie Janssend19e0782016-07-15 12:48:27 -0700127 uint8_t *p_start = NULL;
128 uint8_t *p = NULL;
129 uint8_t *p_rev_data = NULL; /* input data in big endilan format */
130 uint8_t *p_rev_key = NULL; /* input key in big endilan format */
131 uint8_t *p_rev_output = NULL; /* encrypted output in big endilan format */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132
Satya Calloji444a8da2015-03-06 10:38:22 -0800133 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800134 if ( (p_out == NULL ) || (key_len != SMP_ENCRYT_KEY_SIZE) )
135 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800136 SMP_TRACE_ERROR ("%s failed", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -0700137 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138 }
139
Marie Janssend19e0782016-07-15 12:48:27 -0700140 p_start = (uint8_t *)osi_calloc(SMP_ENCRYT_DATA_SIZE * 4);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141
142 if (pt_len > SMP_ENCRYT_DATA_SIZE)
143 pt_len = SMP_ENCRYT_DATA_SIZE;
144
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145 p = p_start;
146 ARRAY_TO_STREAM (p, plain_text, pt_len); /* byte 0 to byte 15 */
147 p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE; /* start at byte 16 */
148 REVERSE_ARRAY_TO_STREAM (p, p_start, SMP_ENCRYT_DATA_SIZE); /* byte 16 to byte 31 */
149 p_rev_key = p; /* start at byte 32 */
150 REVERSE_ARRAY_TO_STREAM (p, key, SMP_ENCRYT_KEY_SIZE); /* byte 32 to byte 47 */
151
Marie Janssend19e0782016-07-15 12:48:27 -0700152#if (SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE)
153 smp_debug_print_nbyte_little_endian(key, (const uint8_t *)"Key", SMP_ENCRYT_KEY_SIZE);
154 smp_debug_print_nbyte_little_endian(p_start, (const uint8_t *)"Plain text", SMP_ENCRYT_DATA_SIZE);
Satya Calloji444a8da2015-03-06 10:38:22 -0800155#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156 p_rev_output = p;
157 aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
158 aes_encrypt(p_rev_data, p, &ctx); /* outputs in byte 48 to byte 63 */
159
160 p = p_out->param_buf;
161 REVERSE_ARRAY_TO_STREAM (p, p_rev_output, SMP_ENCRYT_DATA_SIZE);
Marie Janssend19e0782016-07-15 12:48:27 -0700162#if (SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE)
163 smp_debug_print_nbyte_little_endian(p_out->param_buf, (const uint8_t *)"Encrypted text", SMP_ENCRYT_KEY_SIZE);
Satya Calloji444a8da2015-03-06 10:38:22 -0800164#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165
166 p_out->param_len = SMP_ENCRYT_KEY_SIZE;
167 p_out->status = HCI_SUCCESS;
168 p_out->opcode = HCI_BLE_ENCRYPT;
169
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800170 osi_free(p_start);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800171
Marie Janssend19e0782016-07-15 12:48:27 -0700172 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800173}
174
The Android Open Source Project5738f832012-12-12 16:00:35 -0800175/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800176 *
177 * Function smp_generate_passkey
178 *
179 * Description This function is called to generate passkey.
180 *
181 * Returns void
182 *
183 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700184void smp_generate_passkey(tSMP_CB *p_cb,
185 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186{
Satya Calloji444a8da2015-03-06 10:38:22 -0800187 SMP_TRACE_DEBUG ("%s", __func__);
188 p_cb->rand_enc_proc_state = SMP_GEN_TK;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800189
190 /* generate MRand or SRand */
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700191 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800192}
Satya Calloji444a8da2015-03-06 10:38:22 -0800193
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800195 *
196 * Function smp_proc_passkey
197 *
198 * Description This function is called to process a passkey.
199 *
200 * Returns void
201 *
202 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203void smp_proc_passkey(tSMP_CB *p_cb , tBTM_RAND_ENC *p)
204{
Marie Janssend19e0782016-07-15 12:48:27 -0700205 uint8_t *tt = p_cb->tk;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206 tSMP_KEY key;
Marie Janssend19e0782016-07-15 12:48:27 -0700207 uint32_t passkey; /* 19655 test number; */
208 uint8_t *pp = p->param_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800209
Satya Calloji444a8da2015-03-06 10:38:22 -0800210 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800211 STREAM_TO_UINT32(passkey, pp);
212 passkey &= ~SMP_PASSKEY_MASK;
213
214 /* truncate by maximum value */
215 while (passkey > BTM_MAX_PASSKEY_VAL)
216 passkey >>= 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800217
218 /* save the TK */
219 memset(p_cb->tk, 0, BT_OCTET16_LEN);
220 UINT32_TO_STREAM(tt, passkey);
221
222 key.key_type = SMP_KEY_TYPE_TK;
223 key.p_data = p_cb->tk;
224
225 if (p_cb->p_callback)
226 {
227 (*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
228 }
229
Satya Calloji444a8da2015-03-06 10:38:22 -0800230 if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_PASSKEY_DISP)
231 {
232 smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &passkey);
233 }
234 else
235 {
236 smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
237 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238}
239
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800241 *
242 * Function smp_generate_stk
243 *
244 * Description This function is called to generate STK calculated by running
245 * AES with the TK value as key and a concatenation of the random
246 * values.
247 *
248 * Returns void
249 *
250 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700251void smp_generate_stk(tSMP_CB *p_cb,
252 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800253{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254
Satya Calloji444a8da2015-03-06 10:38:22 -0800255 tSMP_ENC output;
256 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800257
Satya Calloji444a8da2015-03-06 10:38:22 -0800258 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259
Satya Calloji444a8da2015-03-06 10:38:22 -0800260 if (p_cb->le_secure_connections_mode_is_used)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800262 SMP_TRACE_WARNING ("FOR LE SC LTK IS USED INSTEAD OF STK");
263 output.param_len = SMP_ENCRYT_KEY_SIZE;
264 output.status = HCI_SUCCESS;
265 output.opcode = HCI_BLE_ENCRYPT;
266 memcpy(output.param_buf, p_cb->ltk, SMP_ENCRYT_DATA_SIZE);
267 }
268 else if (!smp_calculate_legacy_short_term_key(p_cb, &output))
269 {
270 SMP_TRACE_ERROR("%s failed", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
Satya Calloji444a8da2015-03-06 10:38:22 -0800272 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273 }
274
Satya Calloji444a8da2015-03-06 10:38:22 -0800275 smp_process_stk(p_cb, &output);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276}
Satya Calloji444a8da2015-03-06 10:38:22 -0800277
The Android Open Source Project5738f832012-12-12 16:00:35 -0800278/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800279 *
280 * Function smp_generate_srand_mrand_confirm
281 *
282 * Description This function is called to start the second pairing phase by
283 * start generating random number.
284 *
285 *
286 * Returns void
287 *
288 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700289void smp_generate_srand_mrand_confirm(tSMP_CB *p_cb,
290 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291{
Satya Calloji444a8da2015-03-06 10:38:22 -0800292 SMP_TRACE_DEBUG ("%s", __func__);
293 p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294 /* generate MRand or SRand */
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700295 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296}
Satya Calloji444a8da2015-03-06 10:38:22 -0800297
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800299 *
300 * Function smp_generate_rand_cont
301 *
302 * Description This function is called to generate another 64 bits random for
303 * MRand or Srand.
304 *
305 * Returns void
306 *
307 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700308void smp_generate_rand_cont(tSMP_CB *p_cb,
309 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310{
Satya Calloji444a8da2015-03-06 10:38:22 -0800311 SMP_TRACE_DEBUG ("%s", __func__);
312 p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND_CONT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800313 /* generate 64 MSB of MRand or SRand */
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700314 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315}
Satya Calloji444a8da2015-03-06 10:38:22 -0800316
The Android Open Source Project5738f832012-12-12 16:00:35 -0800317/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800318 *
319 * Function smp_generate_ltk
320 *
321 * Description This function is called:
322 * - in legacy pairing - to calculate LTK, starting with DIV
323 * generation;
324 * - in LE Secure Connections pairing over LE transport - to process LTK
325 * already generated to encrypt LE link;
326 * - in LE Secure Connections pairing over BR/EDR transport - to start
327 * BR/EDR Link Key processing.
328 *
329 * Returns void
330 *
331 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700332void smp_generate_ltk(tSMP_CB *p_cb,
333 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800334{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335
Marie Janssend19e0782016-07-15 12:48:27 -0700336 bool div_status;
337 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800338 if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING)
339 {
340 smp_br_process_link_key(p_cb, NULL);
341 return;
342 }
343 else if (p_cb->le_secure_connections_mode_is_used)
344 {
345 smp_process_secure_connection_long_term_key();
346 return;
347 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800348
349 div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
350
351 if (div_status)
352 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800353 smp_generate_ltk_cont(p_cb, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800354 }
355 else
356 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700357 SMP_TRACE_DEBUG ("Generate DIV for LTK");
Satya Calloji444a8da2015-03-06 10:38:22 -0800358 p_cb->rand_enc_proc_state = SMP_GEN_DIV_LTK;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359 /* generate MRand or SRand */
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700360 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361 }
362}
363
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800365 *
366 * Function smp_compute_csrk
367 *
368 * Description This function is called to calculate CSRK
369 *
370 *
371 * Returns void
372 *
373 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700374void smp_compute_csrk(tSMP_CB *p_cb,
375 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376{
Satya Calloji444a8da2015-03-06 10:38:22 -0800377
The Android Open Source Project5738f832012-12-12 16:00:35 -0800378 BT_OCTET16 er;
Marie Janssend19e0782016-07-15 12:48:27 -0700379 uint8_t buffer[4]; /* for (r || DIV) r=1*/
380 uint16_t r=1;
381 uint8_t *p=buffer;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382 tSMP_ENC output;
383 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
384
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700385 SMP_TRACE_DEBUG ("smp_compute_csrk div=%x", p_cb->div);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800386 BTM_GetDeviceEncRoot(er);
387 /* CSRK = d1(ER, DIV, 1) */
388 UINT16_TO_STREAM(p, p_cb->div);
389 UINT16_TO_STREAM(p, r);
390
391 if (!SMP_Encrypt(er, BT_OCTET16_LEN, buffer, 4, &output))
392 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700393 SMP_TRACE_ERROR("smp_generate_csrk failed");
Satya Calloji444a8da2015-03-06 10:38:22 -0800394 if (p_cb->smp_over_br)
395 {
396 smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &status);
397 }
398 else
399 {
400 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
401 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 }
403 else
404 {
405 memcpy((void *)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
406 smp_send_csrk_info(p_cb, NULL);
407 }
408}
409
410/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800411 *
412 * Function smp_generate_csrk
413 *
414 * Description This function is called to calculate CSRK, starting with DIV
415 * generation.
416 *
417 *
418 * Returns void
419 *
420 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700421void smp_generate_csrk(tSMP_CB *p_cb,
422 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424
Marie Janssend19e0782016-07-15 12:48:27 -0700425 bool div_status;
Satya Calloji444a8da2015-03-06 10:38:22 -0800426
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700427 SMP_TRACE_DEBUG ("smp_generate_csrk");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428
429 div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
430 if (div_status)
431 {
432 smp_compute_csrk(p_cb, NULL);
433 }
434 else
435 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700436 SMP_TRACE_DEBUG ("Generate DIV for CSRK");
Satya Calloji444a8da2015-03-06 10:38:22 -0800437 p_cb->rand_enc_proc_state = SMP_GEN_DIV_CSRK;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700438 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800439 }
440}
441
The Android Open Source Project5738f832012-12-12 16:00:35 -0800442/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800443 * Function smp_concatenate_peer
444 * add pairing command sent from local device into p1.
445 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700446void smp_concatenate_local( tSMP_CB *p_cb, uint8_t **p_data, uint8_t op_code)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800447{
Marie Janssend19e0782016-07-15 12:48:27 -0700448 uint8_t *p = *p_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449
Satya Calloji444a8da2015-03-06 10:38:22 -0800450 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800451 UINT8_TO_STREAM(p, op_code);
Satya Calloji444a8da2015-03-06 10:38:22 -0800452 UINT8_TO_STREAM(p, p_cb->local_io_capability);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453 UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
454 UINT8_TO_STREAM(p, p_cb->loc_auth_req);
455 UINT8_TO_STREAM(p, p_cb->loc_enc_size);
Satya Calloji444a8da2015-03-06 10:38:22 -0800456 UINT8_TO_STREAM(p, p_cb->local_i_key);
457 UINT8_TO_STREAM(p, p_cb->local_r_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
459 *p_data = p;
460}
Satya Calloji444a8da2015-03-06 10:38:22 -0800461
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800463 * Function smp_concatenate_peer
464 * add pairing command received from peer device into p1.
465 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700466void smp_concatenate_peer( tSMP_CB *p_cb, uint8_t **p_data, uint8_t op_code)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467{
Marie Janssend19e0782016-07-15 12:48:27 -0700468 uint8_t *p = *p_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700470 SMP_TRACE_DEBUG ("smp_concatenate_peer ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471 UINT8_TO_STREAM(p, op_code);
472 UINT8_TO_STREAM(p, p_cb->peer_io_caps);
473 UINT8_TO_STREAM(p, p_cb->peer_oob_flag);
474 UINT8_TO_STREAM(p, p_cb->peer_auth_req);
475 UINT8_TO_STREAM(p, p_cb->peer_enc_size);
476 UINT8_TO_STREAM(p, p_cb->peer_i_key);
477 UINT8_TO_STREAM(p, p_cb->peer_r_key);
478
479 *p_data = p;
480}
Satya Calloji444a8da2015-03-06 10:38:22 -0800481
The Android Open Source Project5738f832012-12-12 16:00:35 -0800482/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800483 *
484 * Function smp_gen_p1_4_confirm
485 *
486 * Description Generate Confirm/Compare Step1:
487 * p1 = pres || preq || rat' || iat'
488 *
489 * Returns void
490 *
491 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800492void smp_gen_p1_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p1)
493{
Marie Janssend19e0782016-07-15 12:48:27 -0700494 uint8_t *p = (uint8_t *)p1;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800495 tBLE_ADDR_TYPE addr_type = 0;
496 BD_ADDR remote_bda;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700498 SMP_TRACE_DEBUG ("smp_gen_p1_4_confirm");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800499
500 if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700502 SMP_TRACE_ERROR("can not generate confirm for unknown device");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503 return;
504 }
505
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800506 BTM_ReadConnectionAddr( p_cb->pairing_bda, p_cb->local_bda, &p_cb->addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507
508 if (p_cb->role == HCI_ROLE_MASTER)
509 {
510 /* LSB : rat': initiator's(local) address type */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800511 UINT8_TO_STREAM(p, p_cb->addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512 /* LSB : iat': responder's address type */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800513 UINT8_TO_STREAM(p, addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514 /* concatinate preq */
515 smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
516 /* concatinate pres */
517 smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
518 }
519 else
520 {
521 /* LSB : iat': initiator's address type */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800522 UINT8_TO_STREAM(p, addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523 /* LSB : rat': responder's(local) address type */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800524 UINT8_TO_STREAM(p, p_cb->addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525 /* concatinate preq */
526 smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
527 /* concatinate pres */
528 smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
529 }
Marie Janssend19e0782016-07-15 12:48:27 -0700530#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700531 SMP_TRACE_DEBUG("p1 = pres || preq || rat' || iat'");
Marie Janssend19e0782016-07-15 12:48:27 -0700532 smp_debug_print_nbyte_little_endian ((uint8_t *)p1, (const uint8_t *)"P1", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533#endif
534}
Satya Calloji444a8da2015-03-06 10:38:22 -0800535
The Android Open Source Project5738f832012-12-12 16:00:35 -0800536/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800537 *
538 * Function smp_gen_p2_4_confirm
539 *
540 * Description Generate Confirm/Compare Step2:
541 * p2 = padding || ia || ra
542 *
543 * Returns void
544 *
545 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546void smp_gen_p2_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p2)
547{
Marie Janssend19e0782016-07-15 12:48:27 -0700548 uint8_t *p = (uint8_t *)p2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800549 BD_ADDR remote_bda;
550 tBLE_ADDR_TYPE addr_type = 0;
551
552 if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type))
553 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700554 SMP_TRACE_ERROR("can not generate confirm p2 for unknown device");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800555 return;
556 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800557
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700558 SMP_TRACE_DEBUG ("smp_gen_p2_4_confirm");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800559
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 memset(p, 0, sizeof(BT_OCTET16));
561
562 if (p_cb->role == HCI_ROLE_MASTER)
563 {
564 /* LSB ra */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800565 BDADDR_TO_STREAM(p, remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800566 /* ia */
567 BDADDR_TO_STREAM(p, p_cb->local_bda);
568 }
569 else
570 {
571 /* LSB ra */
572 BDADDR_TO_STREAM(p, p_cb->local_bda);
573 /* ia */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800574 BDADDR_TO_STREAM(p, remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575 }
Marie Janssend19e0782016-07-15 12:48:27 -0700576#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700577 SMP_TRACE_DEBUG("p2 = padding || ia || ra");
Marie Janssend19e0782016-07-15 12:48:27 -0700578 smp_debug_print_nbyte_little_endian(p2, (const uint8_t *)"p2", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800579#endif
580}
Satya Calloji444a8da2015-03-06 10:38:22 -0800581
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800583 *
584 * Function smp_calculate_comfirm
585 *
586 * Description This function is called to calculate Confirm value.
587 *
588 * Returns void
589 *
590 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700591void smp_calculate_comfirm (tSMP_CB *p_cb, BT_OCTET16 rand,
592 UNUSED_ATTR BD_ADDR bda)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800593{
Satya Calloji444a8da2015-03-06 10:38:22 -0800594
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595 BT_OCTET16 p1;
596 tSMP_ENC output;
597 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
598
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700599 SMP_TRACE_DEBUG ("smp_calculate_comfirm ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600 /* generate p1 = pres || preq || rat' || iat' */
601 smp_gen_p1_4_confirm(p_cb, p1);
602
603 /* p1 = rand XOR p1 */
604 smp_xor_128(p1, rand);
605
Marie Janssend19e0782016-07-15 12:48:27 -0700606 smp_debug_print_nbyte_little_endian ((uint8_t *)p1, (const uint8_t *)"P1' = r XOR p1", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607
608 /* calculate e(k, r XOR p1), where k = TK */
609 if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output))
610 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700611 SMP_TRACE_ERROR("smp_generate_csrk failed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
613 }
614 else
615 {
616 smp_calculate_comfirm_cont(p_cb, &output);
617 }
618}
Satya Calloji444a8da2015-03-06 10:38:22 -0800619
The Android Open Source Project5738f832012-12-12 16:00:35 -0800620/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800621 *
622 * Function smp_calculate_comfirm_cont
623 *
624 * Description This function is called when SConfirm/MConfirm is generated
625 * proceed to send the Confirm request/response to peer device.
626 *
627 * Returns void
628 *
629 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p)
631{
632 BT_OCTET16 p2;
633 tSMP_ENC output;
634 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
635
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700636 SMP_TRACE_DEBUG ("smp_calculate_comfirm_cont ");
Marie Janssend19e0782016-07-15 12:48:27 -0700637#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700638 SMP_TRACE_DEBUG("Confirm step 1 p1' = e(k, r XOR p1) Generated");
Marie Janssend19e0782016-07-15 12:48:27 -0700639 smp_debug_print_nbyte_little_endian (p->param_buf, (const uint8_t *)"C1", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640#endif
641
642 smp_gen_p2_4_confirm(p_cb, p2);
643
644 /* calculate p2 = (p1' XOR p2) */
645 smp_xor_128(p2, p->param_buf);
Marie Janssend19e0782016-07-15 12:48:27 -0700646 smp_debug_print_nbyte_little_endian ((uint8_t *)p2, (const uint8_t *)"p2' = C1 xor p2", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647
648 /* calculate: Confirm = E(k, p1' XOR p2) */
649 if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p2, BT_OCTET16_LEN, &output))
650 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700651 SMP_TRACE_ERROR("smp_calculate_comfirm_cont failed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
653 }
654 else
655 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800656 switch (p_cb->rand_enc_proc_state)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800657 {
658 case SMP_GEN_CONFIRM:
659 smp_process_confirm(p_cb, &output);
660 break;
661
662 case SMP_GEN_COMPARE:
663 smp_process_compare(p_cb, &output);
664 break;
665 }
666 }
667}
Satya Calloji444a8da2015-03-06 10:38:22 -0800668
The Android Open Source Project5738f832012-12-12 16:00:35 -0800669/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800670 *
671 * Function smp_generate_confirm
672 *
673 * Description This function is called when a 48 bits random number is generated
674 * as SRand or MRand, continue to calculate Sconfirm or MConfirm.
675 *
676 * Returns void
677 *
678 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700679static void smp_generate_confirm(tSMP_CB *p_cb,
680 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681{
Satya Calloji444a8da2015-03-06 10:38:22 -0800682 SMP_TRACE_DEBUG ("%s", __func__);
683 p_cb->rand_enc_proc_state = SMP_GEN_CONFIRM;
Marie Janssend19e0782016-07-15 12:48:27 -0700684 smp_debug_print_nbyte_little_endian ((uint8_t *)p_cb->rand, (const uint8_t *)"local rand", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800685 smp_calculate_comfirm(p_cb, p_cb->rand, p_cb->pairing_bda);
686}
Satya Calloji444a8da2015-03-06 10:38:22 -0800687
The Android Open Source Project5738f832012-12-12 16:00:35 -0800688/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800689 *
690 * Function smp_generate_compare
691 *
692 * Description This function is called to generate SConfirm for Slave device,
693 * or MSlave for Master device. This function can be also used for
694 * generating Compare number for confirm value check.
695 *
696 * Returns void
697 *
698 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700699void smp_generate_compare (tSMP_CB *p_cb,
700 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800701{
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700702 SMP_TRACE_DEBUG ("smp_generate_compare ");
Satya Calloji444a8da2015-03-06 10:38:22 -0800703 p_cb->rand_enc_proc_state = SMP_GEN_COMPARE;
Marie Janssend19e0782016-07-15 12:48:27 -0700704 smp_debug_print_nbyte_little_endian ((uint8_t *)p_cb->rrand, (const uint8_t *)"peer rand", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705 smp_calculate_comfirm(p_cb, p_cb->rrand, p_cb->local_bda);
706}
Satya Calloji444a8da2015-03-06 10:38:22 -0800707
The Android Open Source Project5738f832012-12-12 16:00:35 -0800708/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800709 *
710 * Function smp_process_confirm
711 *
712 * Description This function is called when SConfirm/MConfirm is generated
713 * proceed to send the Confirm request/response to peer device.
714 *
715 * Returns void
716 *
717 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p)
719{
720 tSMP_KEY key;
721
Marie Janssend19e0782016-07-15 12:48:27 -0700722 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800723 memcpy(p_cb->confirm, p->param_buf, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800724
725#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700726 SMP_TRACE_DEBUG("Confirm Generated");
Marie Janssend19e0782016-07-15 12:48:27 -0700727 smp_debug_print_nbyte_little_endian ((uint8_t *)p_cb->confirm, (const uint8_t *)"Confirm", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800728#endif
729
730 key.key_type = SMP_KEY_TYPE_CFM;
731 key.p_data = p->param_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732 smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
733}
Satya Calloji444a8da2015-03-06 10:38:22 -0800734
The Android Open Source Project5738f832012-12-12 16:00:35 -0800735/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800736 *
737 * Function smp_process_compare
738 *
739 * Description This function is called when Compare is generated using the
740 * RRand and local BDA, TK information.
741 *
742 * Returns void
743 *
744 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800745static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p)
746{
747 tSMP_KEY key;
748
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700749 SMP_TRACE_DEBUG ("smp_process_compare ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800750#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700751 SMP_TRACE_DEBUG("Compare Generated");
Marie Janssend19e0782016-07-15 12:48:27 -0700752 smp_debug_print_nbyte_little_endian (p->param_buf, (const uint8_t *)"Compare", 16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800753#endif
754 key.key_type = SMP_KEY_TYPE_CMP;
755 key.p_data = p->param_buf;
756
757 smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
758}
759
760/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800761 *
762 * Function smp_process_stk
763 *
764 * Description This function is called when STK is generated
765 * proceed to send the encrypt the link using STK.
766 *
767 * Returns void
768 *
769 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800770static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p)
771{
772 tSMP_KEY key;
773
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700774 SMP_TRACE_DEBUG ("smp_process_stk ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800775#if (SMP_DEBUG == TRUE)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700776 SMP_TRACE_ERROR("STK Generated");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800777#endif
778 smp_mask_enc_key(p_cb->loc_enc_size, p->param_buf);
779
780 key.key_type = SMP_KEY_TYPE_STK;
781 key.p_data = p->param_buf;
782
783 smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
784}
785
786/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800787 *
788 * Function smp_generate_ltk_cont
789 *
790 * Description This function is to calculate LTK = d1(ER, DIV, 0)= e(ER, DIV)
791 *
792 * Returns void
793 *
794 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700795static void smp_generate_ltk_cont(tSMP_CB *p_cb,
796 UNUSED_ATTR tSMP_INT_DATA *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797{
Satya Calloji444a8da2015-03-06 10:38:22 -0800798
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799 BT_OCTET16 er;
800 tSMP_ENC output;
801 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
802
Satya Calloji444a8da2015-03-06 10:38:22 -0800803 SMP_TRACE_DEBUG ("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800804 BTM_GetDeviceEncRoot(er);
805
806 /* LTK = d1(ER, DIV, 0)= e(ER, DIV)*/
Marie Janssend19e0782016-07-15 12:48:27 -0700807 if (!SMP_Encrypt(er, BT_OCTET16_LEN, (uint8_t *)&p_cb->div,
808 sizeof(uint16_t), &output))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800809 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800810 SMP_TRACE_ERROR("%s failed", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800811 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
812 }
813 else
814 {
815 /* mask the LTK */
816 smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
817 memcpy((void *)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
818 smp_generate_rand_vector(p_cb, NULL);
819 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800820}
821
822/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800823 *
824 * Function smp_generate_y
825 *
826 * Description This function is to proceed generate Y = E(DHK, Rand)
827 *
828 * Returns void
829 *
830 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700831static void smp_generate_y(tSMP_CB *p_cb,
832 UNUSED_ATTR tSMP_INT_DATA *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833{
Satya Calloji444a8da2015-03-06 10:38:22 -0800834
The Android Open Source Project5738f832012-12-12 16:00:35 -0800835 BT_OCTET16 dhk;
836 tSMP_ENC output;
837 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
Satya Calloji444a8da2015-03-06 10:38:22 -0800838
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700840 SMP_TRACE_DEBUG ("smp_generate_y ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800841 BTM_GetDeviceDHK(dhk);
842
843 if (!SMP_Encrypt(dhk, BT_OCTET16_LEN, p_cb->enc_rand,
844 BT_OCTET8_LEN, &output))
845 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700846 SMP_TRACE_ERROR("smp_generate_y failed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
848 }
849 else
850 {
851 smp_process_ediv(p_cb, &output);
852 }
853}
Satya Calloji444a8da2015-03-06 10:38:22 -0800854
The Android Open Source Project5738f832012-12-12 16:00:35 -0800855/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800856 *
857 * Function smp_generate_rand_vector
858 *
859 * Description This function is called when LTK is generated, send state machine
860 * event to SMP.
861 *
862 * Returns void
863 *
864 ******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700865static void smp_generate_rand_vector (tSMP_CB *p_cb,
866 UNUSED_ATTR tSMP_INT_DATA *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868 /* generate EDIV and rand now */
869 /* generate random vector */
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700870 SMP_TRACE_DEBUG ("smp_generate_rand_vector ");
Satya Calloji444a8da2015-03-06 10:38:22 -0800871 p_cb->rand_enc_proc_state = SMP_GEN_RAND_V;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700872 btsnd_hcic_ble_rand((void *)smp_rand_back);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873}
Satya Calloji444a8da2015-03-06 10:38:22 -0800874
The Android Open Source Project5738f832012-12-12 16:00:35 -0800875/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800876 *
877 * Function smp_process_ediv
878 *
879 * Description This function is to calculate EDIV = Y xor DIV
880 *
881 * Returns void
882 *
883 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800884static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p)
885{
886 tSMP_KEY key;
Marie Janssend19e0782016-07-15 12:48:27 -0700887 uint8_t *pp= p->param_buf;
888 uint16_t y;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800889
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700890 SMP_TRACE_DEBUG ("smp_process_ediv ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800891 STREAM_TO_UINT16(y, pp);
892
893 /* EDIV = Y xor DIV */
894 p_cb->ediv = p_cb->div ^ y;
895 /* send LTK ready */
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700896 SMP_TRACE_ERROR("LTK ready");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800897 key.key_type = SMP_KEY_TYPE_LTK;
898 key.p_data = p->param_buf;
899
900 smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
901}
902
903/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800904 *
905 * Function smp_calculate_legacy_short_term_key
906 *
907 * Description The function calculates legacy STK.
908 *
909 * Returns false if out of resources, true in other cases.
910 *
911 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700912bool smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output)
Satya Calloji444a8da2015-03-06 10:38:22 -0800913{
914 BT_OCTET16 ptext;
Marie Janssend19e0782016-07-15 12:48:27 -0700915 uint8_t *p = ptext;
Satya Calloji444a8da2015-03-06 10:38:22 -0800916
917 SMP_TRACE_DEBUG ("%s", __func__);
918 memset(p, 0, BT_OCTET16_LEN);
919 if (p_cb->role == HCI_ROLE_MASTER)
920 {
921 memcpy(p, p_cb->rand, BT_OCTET8_LEN);
922 memcpy(&p[BT_OCTET8_LEN], p_cb->rrand, BT_OCTET8_LEN);
923 }
924 else
925 {
926 memcpy(p, p_cb->rrand, BT_OCTET8_LEN);
927 memcpy(&p[BT_OCTET8_LEN], p_cb->rand, BT_OCTET8_LEN);
928 }
929
Marie Janssend19e0782016-07-15 12:48:27 -0700930 bool encrypted;
Satya Calloji444a8da2015-03-06 10:38:22 -0800931 /* generate STK = Etk(rand|rrand)*/
932 encrypted = SMP_Encrypt( p_cb->tk, BT_OCTET16_LEN, ptext, BT_OCTET16_LEN, output);
933 if (!encrypted)
934 {
935 SMP_TRACE_ERROR("%s failed", __func__);
936 }
937 return encrypted;
938}
939
940/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800941 *
942 * Function smp_create_private_key
943 *
944 * Description This function is called to create private key used to
945 * calculate public key and DHKey.
946 * The function starts private key creation requesting controller
947 * to generate [0-7] octets of private key.
948 *
949 * Returns void
950 *
951 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800952void smp_create_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
953{
Marie Janssend19e0782016-07-15 12:48:27 -0700954 SMP_TRACE_DEBUG ("%s",__func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800955 p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_0_7;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -0700956 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -0800957}
958
959/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800960 *
961 * Function smp_use_oob_private_key
962 *
963 * Description This function is called
964 * - to save the secret key used to calculate the public key used
965 * in calculations of commitment sent OOB to a peer
966 * - to use this secret key to recalculate the public key and
967 * start the process of sending this public key to the peer
968 * if secret/public keys have to be reused.
969 * If the keys aren't supposed to be reused, continue from the
970 * point from which request for OOB data was issued.
971 *
972 * Returns void
973 *
974 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800975void smp_use_oob_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
976{
977 SMP_TRACE_DEBUG ("%s req_oob_type: %d, role: %d",
978 __func__, p_cb->req_oob_type, p_cb->role);
979
980 switch (p_cb->req_oob_type)
981 {
982 case SMP_OOB_BOTH:
983 case SMP_OOB_LOCAL:
984 SMP_TRACE_DEBUG("%s restore secret key", __func__)
985 memcpy(p_cb->private_key, p_cb->sc_oob_data.loc_oob_data.private_key_used, BT_OCTET32_LEN);
986 smp_process_private_key(p_cb);
987 break;
988 default:
989 SMP_TRACE_DEBUG("%s create secret key anew", __func__);
990 smp_set_state(SMP_STATE_PAIR_REQ_RSP);
991 smp_decide_association_model(p_cb, NULL);
992 break;
993 }
994}
995
996/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800997 *
998 * Function smp_continue_private_key_creation
999 *
1000 * Description This function is used to continue private key creation.
1001 *
1002 * Returns void
1003 *
1004 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001005void smp_continue_private_key_creation (tSMP_CB *p_cb, tBTM_RAND_ENC *p)
1006{
Marie Janssend19e0782016-07-15 12:48:27 -07001007 uint8_t state = p_cb->rand_enc_proc_state & ~0x80;
Satya Calloji444a8da2015-03-06 10:38:22 -08001008 SMP_TRACE_DEBUG ("%s state=0x%x", __func__, state);
1009
1010 switch (state)
1011 {
1012 case SMP_GENERATE_PRIVATE_KEY_0_7:
1013 memcpy((void *)p_cb->private_key, p->param_buf, p->param_len);
1014 p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_8_15;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07001015 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -08001016 break;
1017
1018 case SMP_GENERATE_PRIVATE_KEY_8_15:
1019 memcpy((void *)&p_cb->private_key[8], p->param_buf, p->param_len);
1020 p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_16_23;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07001021 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -08001022 break;
1023
1024 case SMP_GENERATE_PRIVATE_KEY_16_23:
1025 memcpy((void *)&p_cb->private_key[16], p->param_buf, p->param_len);
1026 p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_24_31;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07001027 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -08001028 break;
1029
1030 case SMP_GENERATE_PRIVATE_KEY_24_31:
1031 memcpy((void *)&p_cb->private_key[24], p->param_buf, p->param_len);
1032 smp_process_private_key (p_cb);
1033 break;
1034
1035 default:
1036 break;
1037 }
1038
1039 return;
1040}
1041
1042/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001043 *
1044 * Function smp_process_private_key
1045 *
1046 * Description This function processes private key.
1047 * It calculates public key and notifies SM that private key /
1048 * public key pair is created.
1049 *
1050 * Returns void
1051 *
1052 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001053void smp_process_private_key(tSMP_CB *p_cb)
1054{
1055 Point public_key;
1056 BT_OCTET32 private_key;
1057
Marie Janssend19e0782016-07-15 12:48:27 -07001058 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001059
1060 memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
Jakub Pawlowski28b20ff2016-11-03 14:18:55 -07001061 ECC_PointMult(&public_key, &(curve_p256.G), (uint32_t*) private_key, KEY_LENGTH_DWORDS_P256);
Satya Calloji444a8da2015-03-06 10:38:22 -08001062 memcpy(p_cb->loc_publ_key.x, public_key.x, BT_OCTET32_LEN);
1063 memcpy(p_cb->loc_publ_key.y, public_key.y, BT_OCTET32_LEN);
1064
Marie Janssend19e0782016-07-15 12:48:27 -07001065 smp_debug_print_nbyte_little_endian (p_cb->private_key, (const uint8_t *)"private",
Satya Calloji444a8da2015-03-06 10:38:22 -08001066 BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001067 smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.x, (const uint8_t *)"local public(x)",
Satya Calloji444a8da2015-03-06 10:38:22 -08001068 BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001069 smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.y, (const uint8_t *)"local public(y)",
Satya Calloji444a8da2015-03-06 10:38:22 -08001070 BT_OCTET32_LEN);
1071 p_cb->flags |= SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY;
1072 smp_sm_event(p_cb, SMP_LOC_PUBL_KEY_CRTD_EVT, NULL);
1073}
1074
1075/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001076 *
1077 * Function smp_compute_dhkey
1078 *
1079 * Description The function:
1080 * - calculates a new public key using as input local private
1081 * key and peer public key;
1082 * - saves the new public key x-coordinate as DHKey.
1083 *
1084 * Returns void
1085 *
1086 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001087void smp_compute_dhkey (tSMP_CB *p_cb)
1088{
1089 Point peer_publ_key, new_publ_key;
1090 BT_OCTET32 private_key;
1091
Marie Janssend19e0782016-07-15 12:48:27 -07001092 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001093
1094 memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
1095 memcpy(peer_publ_key.x, p_cb->peer_publ_key.x, BT_OCTET32_LEN);
1096 memcpy(peer_publ_key.y, p_cb->peer_publ_key.y, BT_OCTET32_LEN);
1097
Jakub Pawlowski28b20ff2016-11-03 14:18:55 -07001098 ECC_PointMult(&new_publ_key, &peer_publ_key, (uint32_t*) private_key, KEY_LENGTH_DWORDS_P256);
Satya Calloji444a8da2015-03-06 10:38:22 -08001099
1100 memcpy(p_cb->dhkey, new_publ_key.x, BT_OCTET32_LEN);
1101
Marie Janssend19e0782016-07-15 12:48:27 -07001102 smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const uint8_t *)"Old DHKey",
Satya Calloji444a8da2015-03-06 10:38:22 -08001103 BT_OCTET32_LEN);
1104
Marie Janssend19e0782016-07-15 12:48:27 -07001105 smp_debug_print_nbyte_little_endian (p_cb->private_key, (const uint8_t *)"private",
Satya Calloji444a8da2015-03-06 10:38:22 -08001106 BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001107 smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.x, (const uint8_t *)"rem public(x)",
Satya Calloji444a8da2015-03-06 10:38:22 -08001108 BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001109 smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.y, (const uint8_t *)"rem public(y)",
Satya Calloji444a8da2015-03-06 10:38:22 -08001110 BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001111 smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const uint8_t *)"Reverted DHKey",
Satya Calloji444a8da2015-03-06 10:38:22 -08001112 BT_OCTET32_LEN);
1113}
1114
1115/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001116 *
1117 * Function smp_calculate_local_commitment
1118 *
1119 * Description The function calculates and saves local commmitment in CB.
1120 *
1121 * Returns void
1122 *
1123 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001124void smp_calculate_local_commitment(tSMP_CB *p_cb)
1125{
Marie Janssend19e0782016-07-15 12:48:27 -07001126 uint8_t random_input;
Satya Calloji444a8da2015-03-06 10:38:22 -08001127
Marie Janssend19e0782016-07-15 12:48:27 -07001128 SMP_TRACE_DEBUG("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001129
1130 switch (p_cb->selected_association_model)
1131 {
1132 case SMP_MODEL_SEC_CONN_JUSTWORKS:
1133 case SMP_MODEL_SEC_CONN_NUM_COMP:
1134 if (p_cb->role == HCI_ROLE_MASTER)
1135 SMP_TRACE_WARNING ("local commitment calc on master is not expected \
1136 for Just Works/Numeric Comparison models");
1137 smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand, 0,
1138 p_cb->commitment);
1139 break;
1140 case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1141 case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1142 random_input = smp_calculate_random_input(p_cb->local_random, p_cb->round);
1143 smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
1144 random_input, p_cb->commitment);
1145 break;
1146 case SMP_MODEL_SEC_CONN_OOB:
1147 SMP_TRACE_WARNING ("local commitment calc is expected for OOB model BEFORE pairing");
1148 smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->loc_publ_key.x, p_cb->local_random, 0,
1149 p_cb->commitment);
1150 break;
1151 default:
1152 SMP_TRACE_ERROR("Association Model = %d is not used in LE SC",
1153 p_cb->selected_association_model);
1154 return;
1155 }
1156
1157 SMP_TRACE_EVENT ("local commitment calculation is completed");
1158}
1159
1160/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001161 *
1162 * Function smp_calculate_peer_commitment
1163 *
1164 * Description The function calculates and saves peer commmitment at the
1165 * provided output buffer.
1166 *
1167 * Returns void
1168 *
1169 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001170void smp_calculate_peer_commitment(tSMP_CB *p_cb, BT_OCTET16 output_buf)
1171{
Marie Janssend19e0782016-07-15 12:48:27 -07001172 uint8_t ri;
Satya Calloji444a8da2015-03-06 10:38:22 -08001173
Marie Janssend19e0782016-07-15 12:48:27 -07001174 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001175
1176 switch (p_cb->selected_association_model)
1177 {
1178 case SMP_MODEL_SEC_CONN_JUSTWORKS:
1179 case SMP_MODEL_SEC_CONN_NUM_COMP:
1180 if (p_cb->role == HCI_ROLE_SLAVE)
1181 SMP_TRACE_WARNING ("peer commitment calc on slave is not expected \
1182 for Just Works/Numeric Comparison models");
1183 smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, 0,
1184 output_buf);
1185 break;
1186 case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1187 case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1188 ri = smp_calculate_random_input(p_cb->peer_random, p_cb->round);
1189 smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, ri,
1190 output_buf);
1191 break;
1192 case SMP_MODEL_SEC_CONN_OOB:
1193 smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->peer_publ_key.x, p_cb->peer_random, 0,
1194 output_buf);
1195 break;
1196 default:
1197 SMP_TRACE_ERROR("Association Model = %d is not used in LE SC",
1198 p_cb->selected_association_model);
1199 return;
1200 }
1201
1202 SMP_TRACE_EVENT ("peer commitment calculation is completed");
1203}
1204
1205/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001206 *
1207 * Function smp_calculate_f4
1208 *
1209 * Description The function calculates
1210 * C = f4(U, V, X, Z) = AES-CMAC (U||V||Z)
1211 * X
1212 * where
1213 * input: U is 256 bit,
1214 * V is 256 bit,
1215 * X is 128 bit,
1216 * Z is 8 bit,
1217 * output: C is 128 bit.
1218 *
1219 * Returns void
1220 *
1221 * Note The LSB is the first octet, the MSB is the last octet of
1222 * the AES-CMAC input/output stream.
1223 *
1224 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001225void smp_calculate_f4(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t z, uint8_t *c)
Satya Calloji444a8da2015-03-06 10:38:22 -08001226{
Marie Janssend19e0782016-07-15 12:48:27 -07001227 uint8_t msg_len = BT_OCTET32_LEN /* U size */ + BT_OCTET32_LEN /* V size */ + 1 /* Z size */;
1228 uint8_t msg[BT_OCTET32_LEN + BT_OCTET32_LEN + 1];
1229 uint8_t key[BT_OCTET16_LEN];
1230 uint8_t cmac[BT_OCTET16_LEN];
1231 uint8_t *p = NULL;
1232#if (SMP_DEBUG == TRUE)
1233 uint8_t *p_prnt = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001234#endif
1235
Marie Janssend19e0782016-07-15 12:48:27 -07001236 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001237
Marie Janssend19e0782016-07-15 12:48:27 -07001238#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001239 p_prnt = u;
Marie Janssend19e0782016-07-15 12:48:27 -07001240 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"U", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001241 p_prnt = v;
Marie Janssend19e0782016-07-15 12:48:27 -07001242 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"V", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001243 p_prnt = x;
Marie Janssend19e0782016-07-15 12:48:27 -07001244 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"X", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001245 p_prnt = &z;
Marie Janssend19e0782016-07-15 12:48:27 -07001246 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"Z", 1);
Satya Calloji444a8da2015-03-06 10:38:22 -08001247#endif
1248
1249 p = msg;
1250 UINT8_TO_STREAM(p, z);
1251 ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
1252 ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001253#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001254 p_prnt = msg;
Marie Janssend19e0782016-07-15 12:48:27 -07001255 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"M", msg_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08001256#endif
1257
1258 p = key;
1259 ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001260#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001261 p_prnt = key;
Marie Janssend19e0782016-07-15 12:48:27 -07001262 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001263#endif
1264
1265 aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
Marie Janssend19e0782016-07-15 12:48:27 -07001266#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001267 p_prnt = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001268 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"AES_CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001269#endif
1270
1271 p = c;
1272 ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
1273}
1274
1275/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001276 *
1277 * Function smp_calculate_numeric_comparison_display_number
1278 *
1279 * Description The function calculates and saves number to display in numeric
1280 * comparison association mode.
1281 *
1282 * Returns void
1283 *
1284 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001285void smp_calculate_numeric_comparison_display_number(tSMP_CB *p_cb,
1286 tSMP_INT_DATA *p_data)
1287{
1288 SMP_TRACE_DEBUG ("%s", __func__);
1289
1290 if (p_cb->role == HCI_ROLE_MASTER)
1291 {
1292 p_cb->number_to_display =
1293 smp_calculate_g2(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
1294 p_cb->rrand);
1295 }
1296 else
1297 {
1298 p_cb->number_to_display =
1299 smp_calculate_g2(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand,
1300 p_cb->rand);
1301 }
1302
1303 if (p_cb->number_to_display >= (BTM_MAX_PASSKEY_VAL + 1))
1304 {
Marie Janssend19e0782016-07-15 12:48:27 -07001305 uint8_t reason;
Satya Calloji444a8da2015-03-06 10:38:22 -08001306 reason = p_cb->failure = SMP_PAIR_FAIL_UNKNOWN;
1307 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1308 return;
1309 }
1310
1311 SMP_TRACE_EVENT("Number to display in numeric comparison = %d", p_cb->number_to_display);
1312 p_cb->cb_evt = SMP_NC_REQ_EVT;
1313 smp_sm_event(p_cb, SMP_SC_DSPL_NC_EVT, &p_cb->number_to_display);
1314 return;
1315}
1316
1317/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001318 *
1319 * Function smp_calculate_g2
1320 *
1321 * Description The function calculates
1322 * g2(U, V, X, Y) = AES-CMAC (U||V||Y) mod 2**32 mod 10**6
1323 * X
1324 * and
1325 * Vres = g2(U, V, X, Y) mod 10**6
1326 * where
1327 * input: U is 256 bit,
1328 * V is 256 bit,
1329 * X is 128 bit,
1330 * Y is 128 bit,
1331 *
1332 * Returns Vres.
1333 * Expected value has to be in the range [0 - 999999] i.e. [0 - 0xF423F].
1334 * Vres = 1000000 means that the calculation fails.
1335 *
1336 * Note The LSB is the first octet, the MSB is the last octet of
1337 * the AES-CMAC input/output stream.
1338 *
1339 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001340uint32_t smp_calculate_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y)
Satya Calloji444a8da2015-03-06 10:38:22 -08001341{
Marie Janssend19e0782016-07-15 12:48:27 -07001342 uint8_t msg_len = BT_OCTET32_LEN /* U size */ + BT_OCTET32_LEN /* V size */
Satya Calloji444a8da2015-03-06 10:38:22 -08001343 + BT_OCTET16_LEN /* Y size */;
Marie Janssend19e0782016-07-15 12:48:27 -07001344 uint8_t msg[BT_OCTET32_LEN + BT_OCTET32_LEN + BT_OCTET16_LEN];
1345 uint8_t key[BT_OCTET16_LEN];
1346 uint8_t cmac[BT_OCTET16_LEN];
1347 uint8_t *p = NULL;
1348 uint32_t vres;
1349#if (SMP_DEBUG == TRUE)
1350 uint8_t *p_prnt = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001351#endif
1352
Marie Janssend19e0782016-07-15 12:48:27 -07001353 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001354
1355 p = msg;
1356 ARRAY_TO_STREAM(p, y, BT_OCTET16_LEN);
1357 ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
1358 ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001359#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001360 p_prnt = u;
Marie Janssend19e0782016-07-15 12:48:27 -07001361 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"U", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001362 p_prnt = v;
Marie Janssend19e0782016-07-15 12:48:27 -07001363 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"V", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001364 p_prnt = x;
Marie Janssend19e0782016-07-15 12:48:27 -07001365 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"X", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001366 p_prnt = y;
Marie Janssend19e0782016-07-15 12:48:27 -07001367 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"Y", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001368#endif
1369
1370 p = key;
1371 ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001372#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001373 p_prnt = key;
Marie Janssend19e0782016-07-15 12:48:27 -07001374 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001375#endif
1376
1377 if(!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac))
1378 {
Marie Janssend19e0782016-07-15 12:48:27 -07001379 SMP_TRACE_ERROR("%s failed",__func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001380 return (BTM_MAX_PASSKEY_VAL + 1);
1381 }
1382
Marie Janssend19e0782016-07-15 12:48:27 -07001383#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001384 p_prnt = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001385 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"AES-CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001386#endif
1387
1388 /* vres = cmac mod 2**32 mod 10**6 */
1389 p = &cmac[0];
1390 STREAM_TO_UINT32(vres, p);
Marie Janssend19e0782016-07-15 12:48:27 -07001391#if (SMP_DEBUG == TRUE)
1392 p_prnt = (uint8_t *) &vres;
1393 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"cmac mod 2**32", 4);
Satya Calloji444a8da2015-03-06 10:38:22 -08001394#endif
1395
1396 while (vres > BTM_MAX_PASSKEY_VAL)
1397 vres -= (BTM_MAX_PASSKEY_VAL + 1);
Marie Janssend19e0782016-07-15 12:48:27 -07001398#if (SMP_DEBUG == TRUE)
1399 p_prnt = (uint8_t *) &vres;
1400 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"cmac mod 2**32 mod 10**6", 4);
Satya Calloji444a8da2015-03-06 10:38:22 -08001401#endif
1402
1403 SMP_TRACE_ERROR("Value for numeric comparison = %d", vres);
1404 return vres;
1405}
1406
1407/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001408 *
1409 * Function smp_calculate_f5
1410 *
1411 * Description The function provides two AES-CMAC that are supposed to be used as
1412 * - MacKey (MacKey is used in pairing DHKey check calculation);
1413 * - LTK (LTK is used to ecrypt the link after completion of Phase 2
1414 * and on reconnection, to derive BR/EDR LK).
1415 * The function inputs are W, N1, N2, A1, A2.
1416 * F5 rules:
1417 * - the value used as key in MacKey/LTK (T) is calculated
1418 * (function smp_calculate_f5_key(...));
1419 * The formula is:
1420 * T = AES-CMAC (W)
1421 * salt
1422 * where salt is internal parameter of smp_calculate_f5_key(...).
1423 * - MacKey and LTK are calculated as AES-MAC values received with the
1424 * key T calculated in the previous step and the plaintext message
1425 * built from the external parameters N1, N2, A1, A2 and the internal
1426 * parameters counter, keyID, length.
1427 * The function smp_calculate_f5_mackey_or_long_term_key(...) is used in the
1428 * calculations.
1429 * The same formula is used in calculation of MacKey and LTK and the
1430 * same parameter values except the value of the internal parameter
1431 * counter:
1432 * - in MacKey calculations the value is 0;
1433 * - in LTK calculations the value is 1.
1434 * MacKey = AES-CMAC (Counter=0||keyID||N1||N2||A1||A2||Length=256)
1435 * T
1436 * LTK = AES-CMAC (Counter=1||keyID||N1||N2||A1||A2||Length=256)
1437 * T
1438 * The parameters are
1439 * input:
1440 * W is 256 bits,
1441 * N1 is 128 bits,
1442 * N2 is 128 bits,
1443 * A1 is 56 bit,
1444 * A2 is 56 bit.
1445 * internal:
1446 * Counter is 8 bits, its value is 0 for MacKey,
1447 * 1 for LTK;
1448 * KeyId is 32 bits, its value is
1449 * 0x62746c65 (MSB~LSB);
1450 * Length is 16 bits, its value is 0x0100
1451 * (MSB~LSB).
1452 * output:
1453 * MacKey is 128 bits;
1454 * LTK is 128 bits
1455 *
1456 * Returns false if out of resources, true in other cases.
1457 *
1458 * Note The LSB is the first octet, the MSB is the last octet of
1459 * the AES-CMAC input/output stream.
1460 *
1461 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001462bool smp_calculate_f5(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t *a1, uint8_t *a2,
1463 uint8_t *mac_key, uint8_t *ltk)
Satya Calloji444a8da2015-03-06 10:38:22 -08001464{
1465 BT_OCTET16 t; /* AES-CMAC output in smp_calculate_f5_key(...), key in */
1466 /* smp_calculate_f5_mackey_or_long_term_key(...) */
Marie Janssend19e0782016-07-15 12:48:27 -07001467#if (SMP_DEBUG == TRUE)
1468 uint8_t *p_prnt = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001469#endif
1470 /* internal parameters: */
1471
1472 /*
1473 counter is 0 for MacKey,
1474 is 1 for LTK
1475 */
Marie Janssend19e0782016-07-15 12:48:27 -07001476 uint8_t counter_mac_key[1] = {0};
1477 uint8_t counter_ltk[1] = {1};
Satya Calloji444a8da2015-03-06 10:38:22 -08001478 /*
1479 keyID 62746c65
1480 */
Marie Janssend19e0782016-07-15 12:48:27 -07001481 uint8_t key_id[4] = {0x65, 0x6c, 0x74, 0x62};
Satya Calloji444a8da2015-03-06 10:38:22 -08001482 /*
1483 length 0100
1484 */
Marie Janssend19e0782016-07-15 12:48:27 -07001485 uint8_t length[2] = {0x00, 0x01};
Satya Calloji444a8da2015-03-06 10:38:22 -08001486
Marie Janssend19e0782016-07-15 12:48:27 -07001487 SMP_TRACE_DEBUG ("%s", __func__);
1488#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001489 p_prnt = w;
Marie Janssend19e0782016-07-15 12:48:27 -07001490 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"W", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001491 p_prnt = n1;
Marie Janssend19e0782016-07-15 12:48:27 -07001492 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"N1", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001493 p_prnt = n2;
Marie Janssend19e0782016-07-15 12:48:27 -07001494 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"N2", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001495 p_prnt = a1;
Marie Janssend19e0782016-07-15 12:48:27 -07001496 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"A1", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001497 p_prnt = a2;
Marie Janssend19e0782016-07-15 12:48:27 -07001498 smp_debug_print_nbyte_little_endian (p_prnt,(const uint8_t *) "A2", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001499#endif
1500
1501 if (!smp_calculate_f5_key(w, t))
1502 {
Marie Janssend19e0782016-07-15 12:48:27 -07001503 SMP_TRACE_ERROR("%s failed to calc T",__func__);
1504 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001505 }
Marie Janssend19e0782016-07-15 12:48:27 -07001506#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001507 p_prnt = t;
Marie Janssend19e0782016-07-15 12:48:27 -07001508 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"T", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001509#endif
1510
1511 if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_mac_key, key_id, n1, n2, a1, a2,
1512 length, mac_key))
1513 {
Marie Janssend19e0782016-07-15 12:48:27 -07001514 SMP_TRACE_ERROR("%s failed to calc MacKey", __func__);
1515 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001516 }
Marie Janssend19e0782016-07-15 12:48:27 -07001517#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001518 p_prnt = mac_key;
Marie Janssend19e0782016-07-15 12:48:27 -07001519 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"MacKey", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001520#endif
1521
1522 if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_ltk, key_id, n1, n2, a1, a2,
1523 length, ltk))
1524 {
Marie Janssend19e0782016-07-15 12:48:27 -07001525 SMP_TRACE_ERROR("%s failed to calc LTK",__func__);
1526 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001527 }
Marie Janssend19e0782016-07-15 12:48:27 -07001528#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001529 p_prnt = ltk;
Marie Janssend19e0782016-07-15 12:48:27 -07001530 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"LTK", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001531#endif
1532
Marie Janssend19e0782016-07-15 12:48:27 -07001533 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001534}
1535
1536/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001537 *
1538 * Function smp_calculate_f5_mackey_or_long_term_key
1539 *
1540 * Description The function calculates the value of MacKey or LTK by the rules
1541 * defined for f5 function.
1542 * At the moment exactly the same formula is used to calculate
1543 * LTK and MacKey.
1544 * The difference is the value of input parameter Counter:
1545 * - in MacKey calculations the value is 0;
1546 * - in LTK calculations the value is 1.
1547 * The formula:
1548 * mac = AES-CMAC (Counter||keyID||N1||N2||A1||A2||Length)
1549 * T
1550 * where
1551 * input: T is 256 bits;
1552 * Counter is 8 bits, its value is 0 for MacKey,
1553 * 1 for LTK;
1554 * keyID is 32 bits, its value is 0x62746c65;
1555 * N1 is 128 bits;
1556 * N2 is 128 bits;
1557 * A1 is 56 bits;
1558 * A2 is 56 bits;
1559 * Length is 16 bits, its value is 0x0100
1560 * output: LTK is 128 bit.
1561 *
1562 * Returns false if out of resources, true in other cases.
1563 *
1564 * Note The LSB is the first octet, the MSB is the last octet of
1565 * the AES-CMAC input/output stream.
1566 *
1567 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001568bool smp_calculate_f5_mackey_or_long_term_key(uint8_t *t, uint8_t *counter,
1569 uint8_t *key_id, uint8_t *n1, uint8_t *n2, uint8_t *a1, uint8_t *a2,
1570 uint8_t *length, uint8_t *mac)
Satya Calloji444a8da2015-03-06 10:38:22 -08001571{
Marie Janssend19e0782016-07-15 12:48:27 -07001572 uint8_t *p = NULL;
1573 uint8_t cmac[BT_OCTET16_LEN];
1574 uint8_t key[BT_OCTET16_LEN];
1575 uint8_t msg_len = 1 /* Counter size */ + 4 /* keyID size */ +
Satya Calloji444a8da2015-03-06 10:38:22 -08001576 BT_OCTET16_LEN /* N1 size */ + BT_OCTET16_LEN /* N2 size */ +
1577 7 /* A1 size*/ + 7 /* A2 size*/ + 2 /* Length size */;
Marie Janssend19e0782016-07-15 12:48:27 -07001578 uint8_t msg[1 + 4 + BT_OCTET16_LEN + BT_OCTET16_LEN + 7 + 7 + 2];
1579 bool ret = true;
1580#if (SMP_DEBUG == TRUE)
1581 uint8_t *p_prnt = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001582#endif
1583
Marie Janssend19e0782016-07-15 12:48:27 -07001584 SMP_TRACE_DEBUG ("%s", __func__);
1585#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001586 p_prnt = t;
Marie Janssend19e0782016-07-15 12:48:27 -07001587 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"T", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001588 p_prnt = counter;
Marie Janssend19e0782016-07-15 12:48:27 -07001589 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"Counter", 1);
Satya Calloji444a8da2015-03-06 10:38:22 -08001590 p_prnt = key_id;
Marie Janssend19e0782016-07-15 12:48:27 -07001591 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"KeyID", 4);
Satya Calloji444a8da2015-03-06 10:38:22 -08001592 p_prnt = n1;
Marie Janssend19e0782016-07-15 12:48:27 -07001593 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"N1", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001594 p_prnt = n2;
Marie Janssend19e0782016-07-15 12:48:27 -07001595 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"N2", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001596 p_prnt = a1;
Marie Janssend19e0782016-07-15 12:48:27 -07001597 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"A1", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001598 p_prnt = a2;
Marie Janssend19e0782016-07-15 12:48:27 -07001599 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"A2", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001600 p_prnt = length;
Marie Janssend19e0782016-07-15 12:48:27 -07001601 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"Length", 2);
Satya Calloji444a8da2015-03-06 10:38:22 -08001602#endif
1603
1604 p = key;
1605 ARRAY_TO_STREAM(p, t, BT_OCTET16_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001606#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001607 p_prnt = key;
Marie Janssend19e0782016-07-15 12:48:27 -07001608 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001609#endif
1610 p = msg;
1611 ARRAY_TO_STREAM(p, length, 2);
1612 ARRAY_TO_STREAM(p, a2, 7);
1613 ARRAY_TO_STREAM(p, a1, 7);
1614 ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
1615 ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
1616 ARRAY_TO_STREAM(p, key_id, 4);
1617 ARRAY_TO_STREAM(p, counter, 1);
Marie Janssend19e0782016-07-15 12:48:27 -07001618#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001619 p_prnt = msg;
Marie Janssend19e0782016-07-15 12:48:27 -07001620 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"M", msg_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08001621#endif
1622
1623 if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac))
1624 {
Marie Janssend19e0782016-07-15 12:48:27 -07001625 SMP_TRACE_ERROR("%s failed", __func__);
1626 ret = false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001627 }
1628
Marie Janssend19e0782016-07-15 12:48:27 -07001629#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001630 p_prnt = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001631 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"AES-CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001632#endif
1633
1634 p = mac;
1635 ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
1636 return ret;
1637}
1638
1639/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001640 *
1641 * Function smp_calculate_f5_key
1642 *
1643 * Description The function calculates key T used in calculation of
1644 * MacKey and LTK (f5 output is defined as MacKey || LTK).
1645 * T = AES-CMAC (W)
1646 * salt
1647 * where
1648 * Internal: salt is 128 bit.
1649 * input: W is 256 bit.
1650 * Output: T is 128 bit.
1651 *
1652 * Returns false if out of resources, true in other cases.
1653 *
1654 * Note The LSB is the first octet, the MSB is the last octet of
1655 * the AES-CMAC input/output stream.
1656 *
1657 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001658bool smp_calculate_f5_key(uint8_t *w, uint8_t *t)
Satya Calloji444a8da2015-03-06 10:38:22 -08001659{
Marie Janssend19e0782016-07-15 12:48:27 -07001660 uint8_t *p = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001661 /* Please see 2.2.7 LE Secure Connections Key Generation Function f5 */
1662 /*
1663 salt: 6C88 8391 AAF5 A538 6037 0BDB 5A60 83BE
1664 */
1665 BT_OCTET16 salt = {
1666 0xBE, 0x83, 0x60, 0x5A, 0xDB, 0x0B, 0x37, 0x60,
1667 0x38, 0xA5, 0xF5, 0xAA, 0x91, 0x83, 0x88, 0x6C
1668 };
Marie Janssend19e0782016-07-15 12:48:27 -07001669#if (SMP_DEBUG == TRUE)
1670 uint8_t *p_prnt = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001671#endif
1672
Marie Janssend19e0782016-07-15 12:48:27 -07001673 SMP_TRACE_DEBUG ("%s", __func__);
1674#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001675 p_prnt = salt;
Marie Janssend19e0782016-07-15 12:48:27 -07001676 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"salt", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001677 p_prnt = w;
Marie Janssend19e0782016-07-15 12:48:27 -07001678 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"W", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001679#endif
1680
1681 BT_OCTET16 key;
1682 BT_OCTET32 msg;
1683
1684 p = key;
1685 ARRAY_TO_STREAM(p, salt, BT_OCTET16_LEN);
1686 p = msg;
1687 ARRAY_TO_STREAM(p, w, BT_OCTET32_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001688#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001689 p_prnt = key;
Marie Janssend19e0782016-07-15 12:48:27 -07001690 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001691 p_prnt = msg;
Marie Janssend19e0782016-07-15 12:48:27 -07001692 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"M", BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001693#endif
1694
1695 BT_OCTET16 cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001696 bool ret = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001697 if (!aes_cipher_msg_auth_code(key, msg, BT_OCTET32_LEN, BT_OCTET16_LEN, cmac))
1698 {
Marie Janssend19e0782016-07-15 12:48:27 -07001699 SMP_TRACE_ERROR("%s failed", __func__);
1700 ret = false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001701 }
1702
Marie Janssend19e0782016-07-15 12:48:27 -07001703#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001704 p_prnt = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001705 smp_debug_print_nbyte_little_endian (p_prnt, (const uint8_t *)"AES-CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001706#endif
1707
1708 p = t;
1709 ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
1710 return ret;
1711}
1712
1713/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001714 *
1715 * Function smp_calculate_local_dhkey_check
1716 *
1717 * Description The function calculates and saves local device DHKey check
1718 * value in CB.
1719 * Before doing this it calls smp_calculate_f5_mackey_and_long_term_key(...).
1720 * to calculate MacKey and LTK.
1721 * MacKey is used in dhkey calculation.
1722 *
1723 * Returns void
1724 *
1725 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001726void smp_calculate_local_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1727{
Marie Janssend19e0782016-07-15 12:48:27 -07001728 uint8_t iocap[3], a[7], b[7];
Satya Calloji444a8da2015-03-06 10:38:22 -08001729
Marie Janssend19e0782016-07-15 12:48:27 -07001730 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001731
1732 smp_calculate_f5_mackey_and_long_term_key(p_cb);
1733
1734 smp_collect_local_io_capabilities(iocap, p_cb);
1735
1736 smp_collect_local_ble_address(a, p_cb);
1737 smp_collect_peer_ble_address(b, p_cb);
1738 smp_calculate_f6(p_cb->mac_key, p_cb->rand, p_cb->rrand, p_cb->peer_random, iocap, a, b,
1739 p_cb->dhkey_check);
1740
1741 SMP_TRACE_EVENT ("local DHKey check calculation is completed");
1742}
1743
1744/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001745 *
1746 * Function smp_calculate_peer_dhkey_check
1747 *
1748 * Description The function calculates peer device DHKey check value.
1749 *
1750 * Returns void
1751 *
1752 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001753void smp_calculate_peer_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1754{
Marie Janssend19e0782016-07-15 12:48:27 -07001755 uint8_t iocap[3], a[7], b[7];
Satya Calloji444a8da2015-03-06 10:38:22 -08001756 BT_OCTET16 param_buf;
Marie Janssend19e0782016-07-15 12:48:27 -07001757 bool ret;
Satya Calloji444a8da2015-03-06 10:38:22 -08001758 tSMP_KEY key;
1759 tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
1760
Marie Janssend19e0782016-07-15 12:48:27 -07001761 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001762
1763 smp_collect_peer_io_capabilities(iocap, p_cb);
1764
1765 smp_collect_local_ble_address(a, p_cb);
1766 smp_collect_peer_ble_address(b, p_cb);
1767 ret = smp_calculate_f6(p_cb->mac_key, p_cb->rrand, p_cb->rand, p_cb->local_random, iocap,
1768 b, a, param_buf);
1769
1770 if (ret)
1771 {
1772 SMP_TRACE_EVENT ("peer DHKey check calculation is completed");
1773#if (SMP_DEBUG == TRUE)
Marie Janssend19e0782016-07-15 12:48:27 -07001774 smp_debug_print_nbyte_little_endian (param_buf, (const uint8_t *)"peer DHKey check",
Satya Calloji444a8da2015-03-06 10:38:22 -08001775 BT_OCTET16_LEN);
1776#endif
1777 key.key_type = SMP_KEY_TYPE_PEER_DHK_CHCK;
1778 key.p_data = param_buf;
1779 smp_sm_event(p_cb, SMP_SC_KEY_READY_EVT, &key);
1780 }
1781 else
1782 {
1783 SMP_TRACE_EVENT ("peer DHKey check calculation failed");
1784 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
1785 }
1786}
1787
1788/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001789 *
1790 * Function smp_calculate_f6
1791 *
1792 * Description The function calculates
1793 * C = f6(W, N1, N2, R, IOcap, A1, A2) = AES-CMAC (N1||N2||R||IOcap||A1||A2)
1794 * W
1795 * where
1796 * input: W is 128 bit,
1797 * N1 is 128 bit,
1798 * N2 is 128 bit,
1799 * R is 128 bit,
1800 * IOcap is 24 bit,
1801 * A1 is 56 bit,
1802 * A2 is 56 bit,
1803 * output: C is 128 bit.
1804 *
1805 * Returns false if out of resources, true in other cases.
1806 *
1807 * Note The LSB is the first octet, the MSB is the last octet of
1808 * the AES-CMAC input/output stream.
1809 *
1810 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001811bool smp_calculate_f6(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t *r, uint8_t *iocap, uint8_t *a1,
1812 uint8_t *a2, uint8_t *c)
Satya Calloji444a8da2015-03-06 10:38:22 -08001813{
Marie Janssend19e0782016-07-15 12:48:27 -07001814 uint8_t *p = NULL;
1815 uint8_t msg_len = BT_OCTET16_LEN /* N1 size */ + BT_OCTET16_LEN /* N2 size */ +
Satya Calloji444a8da2015-03-06 10:38:22 -08001816 BT_OCTET16_LEN /* R size */ + 3 /* IOcap size */ + 7 /* A1 size*/
1817 + 7 /* A2 size*/;
Marie Janssend19e0782016-07-15 12:48:27 -07001818 uint8_t msg[BT_OCTET16_LEN + BT_OCTET16_LEN + BT_OCTET16_LEN + 3 + 7 + 7];
1819#if (SMP_DEBUG == TRUE)
1820 uint8_t *p_print = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08001821#endif
1822
Marie Janssend19e0782016-07-15 12:48:27 -07001823 SMP_TRACE_DEBUG ("%s", __func__);
1824#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001825 p_print = w;
Marie Janssend19e0782016-07-15 12:48:27 -07001826 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"W", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001827 p_print = n1;
Marie Janssend19e0782016-07-15 12:48:27 -07001828 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"N1", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001829 p_print = n2;
Marie Janssend19e0782016-07-15 12:48:27 -07001830 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"N2", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001831 p_print = r;
Marie Janssend19e0782016-07-15 12:48:27 -07001832 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"R", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001833 p_print = iocap;
Marie Janssend19e0782016-07-15 12:48:27 -07001834 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"IOcap", 3);
Satya Calloji444a8da2015-03-06 10:38:22 -08001835 p_print = a1;
Marie Janssend19e0782016-07-15 12:48:27 -07001836 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"A1", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001837 p_print = a2;
Marie Janssend19e0782016-07-15 12:48:27 -07001838 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"A2", 7);
Satya Calloji444a8da2015-03-06 10:38:22 -08001839#endif
1840
Marie Janssend19e0782016-07-15 12:48:27 -07001841 uint8_t cmac[BT_OCTET16_LEN];
1842 uint8_t key[BT_OCTET16_LEN];
Satya Calloji444a8da2015-03-06 10:38:22 -08001843
1844 p = key;
1845 ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001846#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001847 p_print = key;
Marie Janssend19e0782016-07-15 12:48:27 -07001848 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001849#endif
1850
1851 p = msg;
1852 ARRAY_TO_STREAM(p, a2, 7);
1853 ARRAY_TO_STREAM(p, a1, 7);
1854 ARRAY_TO_STREAM(p, iocap, 3);
1855 ARRAY_TO_STREAM(p, r, BT_OCTET16_LEN);
1856 ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
1857 ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
Marie Janssend19e0782016-07-15 12:48:27 -07001858#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001859 p_print = msg;
Marie Janssend19e0782016-07-15 12:48:27 -07001860 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"M", msg_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08001861#endif
1862
Marie Janssend19e0782016-07-15 12:48:27 -07001863 bool ret = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001864 if(!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac))
1865 {
Marie Janssend19e0782016-07-15 12:48:27 -07001866 SMP_TRACE_ERROR("%s failed", __func__);
1867 ret = false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001868 }
1869
Marie Janssend19e0782016-07-15 12:48:27 -07001870#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08001871 p_print = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07001872 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"AES-CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08001873#endif
1874
1875 p = c;
1876 ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
1877 return ret;
1878}
1879
1880/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001881 *
1882 * Function smp_calculate_link_key_from_long_term_key
1883 *
1884 * Description The function calculates and saves BR/EDR link key derived from
1885 * LE SC LTK.
1886 *
1887 * Returns false if out of resources, true in other cases.
1888 *
1889 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001890bool smp_calculate_link_key_from_long_term_key(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001891{
1892 tBTM_SEC_DEV_REC *p_dev_rec;
Chaojing Sune2805532015-04-22 13:40:21 -07001893 BD_ADDR bda_for_lk;
1894 tBLE_ADDR_TYPE conn_addr_type;
Satya Calloji444a8da2015-03-06 10:38:22 -08001895
1896 SMP_TRACE_DEBUG ("%s", __func__);
1897
Chaojing Sune2805532015-04-22 13:40:21 -07001898 if (p_cb->id_addr_rcvd && p_cb->id_addr_type == BLE_ADDR_PUBLIC)
1899 {
1900 SMP_TRACE_DEBUG ("Use rcvd identity address as BD_ADDR of LK rcvd identity address");
1901 memcpy(bda_for_lk, p_cb->id_addr, BD_ADDR_LEN);
1902 }
1903 else if ((BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda_for_lk, &conn_addr_type)) &&
1904 conn_addr_type == BLE_ADDR_PUBLIC)
1905 {
1906 SMP_TRACE_DEBUG ("Use rcvd connection address as BD_ADDR of LK");
1907 }
1908 else
1909 {
1910 SMP_TRACE_WARNING ("Don't have peer public address to associate with LK");
Marie Janssend19e0782016-07-15 12:48:27 -07001911 return false;
Chaojing Sune2805532015-04-22 13:40:21 -07001912 }
1913
Marie Janssenf33b6f42016-11-22 15:01:42 -08001914 p_dev_rec = btm_find_dev(p_cb->pairing_bda);
1915 if (p_dev_rec == NULL)
Satya Calloji444a8da2015-03-06 10:38:22 -08001916 {
1917 SMP_TRACE_ERROR("%s failed to find Security Record", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -07001918 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001919 }
1920
1921 BT_OCTET16 intermediate_link_key;
Marie Janssend19e0782016-07-15 12:48:27 -07001922 bool ret = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001923
Marie Janssend19e0782016-07-15 12:48:27 -07001924 ret = smp_calculate_h6(p_cb->ltk, (uint8_t *)"1pmt" /* reversed "tmp1" */,intermediate_link_key);
Satya Calloji444a8da2015-03-06 10:38:22 -08001925 if (!ret)
1926 {
1927 SMP_TRACE_ERROR("%s failed to derive intermediate_link_key", __func__);
1928 return ret;
1929 }
1930
1931 BT_OCTET16 link_key;
Marie Janssend19e0782016-07-15 12:48:27 -07001932 ret = smp_calculate_h6(intermediate_link_key, (uint8_t *) "rbel" /* reversed "lebr" */, link_key);
Satya Calloji444a8da2015-03-06 10:38:22 -08001933 if (!ret)
1934 {
1935 SMP_TRACE_ERROR("%s failed", __func__);
1936 }
1937 else
1938 {
Marie Janssend19e0782016-07-15 12:48:27 -07001939 uint8_t link_key_type;
Satya Calloji444a8da2015-03-06 10:38:22 -08001940 if (btm_cb.security_mode == BTM_SEC_MODE_SC)
1941 {
1942 /* Secure Connections Only Mode */
1943 link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
1944 }
1945 else if (controller_get_interface()->supports_secure_connections())
1946 {
1947 /* both transports are SC capable */
1948 if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
1949 link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
1950 else
1951 link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB_P_256;
1952 }
1953 else if (btm_cb.security_mode == BTM_SEC_MODE_SP)
1954 {
1955 /* BR/EDR transport is SSP capable */
1956 if (p_cb->sec_level == SMP_SEC_AUTHENTICATED)
1957 link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
1958 else
1959 link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
1960 }
1961 else
1962 {
1963 SMP_TRACE_ERROR ("%s failed to update link_key. Sec Mode = %d, sm4 = 0x%02x",
1964 __func__, btm_cb.security_mode, p_dev_rec->sm4);
Marie Janssend19e0782016-07-15 12:48:27 -07001965 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001966 }
1967
1968 link_key_type += BTM_LTK_DERIVED_LKEY_OFFSET;
1969
Marie Janssend19e0782016-07-15 12:48:27 -07001970 uint8_t *p;
Satya Calloji444a8da2015-03-06 10:38:22 -08001971 BT_OCTET16 notif_link_key;
1972 p = notif_link_key;
1973 ARRAY16_TO_STREAM(p, link_key);
1974
Chaojing Sune2805532015-04-22 13:40:21 -07001975 btm_sec_link_key_notification (bda_for_lk, notif_link_key, link_key_type);
Satya Calloji444a8da2015-03-06 10:38:22 -08001976
1977 SMP_TRACE_EVENT ("%s is completed", __func__);
1978 }
1979
1980 return ret;
1981}
1982
1983/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001984 *
1985 * Function smp_calculate_long_term_key_from_link_key
1986 *
1987 * Description The function calculates and saves SC LTK derived from BR/EDR
1988 * link key.
1989 *
1990 * Returns false if out of resources, true in other cases.
1991 *
1992 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001993bool smp_calculate_long_term_key_from_link_key(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001994{
Marie Janssend19e0782016-07-15 12:48:27 -07001995 bool ret = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001996 tBTM_SEC_DEV_REC *p_dev_rec;
Marie Janssend19e0782016-07-15 12:48:27 -07001997 uint8_t rev_link_key[16];
Satya Calloji444a8da2015-03-06 10:38:22 -08001998
Marie Janssend19e0782016-07-15 12:48:27 -07001999 SMP_TRACE_DEBUG ("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002000
Marie Janssenf33b6f42016-11-22 15:01:42 -08002001 p_dev_rec = btm_find_dev(p_cb->pairing_bda);
2002 if (p_dev_rec == NULL)
Satya Calloji444a8da2015-03-06 10:38:22 -08002003 {
Marie Janssend19e0782016-07-15 12:48:27 -07002004 SMP_TRACE_ERROR("%s failed to find Security Record",__func__);
2005 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08002006 }
2007
Marie Janssend19e0782016-07-15 12:48:27 -07002008 uint8_t br_link_key_type;
Marie Janssenf33b6f42016-11-22 15:01:42 -08002009 br_link_key_type = BTM_SecGetDeviceLinkKeyType(p_cb->pairing_bda);
2010 if (br_link_key_type == BTM_LKEY_TYPE_IGNORE)
Satya Calloji444a8da2015-03-06 10:38:22 -08002011 {
Marie Janssend19e0782016-07-15 12:48:27 -07002012 SMP_TRACE_ERROR("%s failed to retrieve BR link type",__func__);
2013 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08002014 }
2015
2016 if ((br_link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256) &&
2017 (br_link_key_type != BTM_LKEY_TYPE_UNAUTH_COMB_P_256))
2018 {
2019 SMP_TRACE_ERROR("%s LE SC LTK can't be derived from LK %d",
Marie Janssend19e0782016-07-15 12:48:27 -07002020 __func__, br_link_key_type);
2021 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08002022 }
2023
Marie Janssend19e0782016-07-15 12:48:27 -07002024 uint8_t *p1;
2025 uint8_t *p2;
Satya Calloji444a8da2015-03-06 10:38:22 -08002026 p1 = rev_link_key;
2027 p2 = p_dev_rec->link_key;
2028 REVERSE_ARRAY_TO_STREAM(p1, p2, 16);
2029
2030 BT_OCTET16 intermediate_long_term_key;
2031 /* "tmp2" obtained from the spec */
Marie Janssend19e0782016-07-15 12:48:27 -07002032 ret = smp_calculate_h6(rev_link_key, (uint8_t *) "2pmt" /* reversed "tmp2" */,
Satya Calloji444a8da2015-03-06 10:38:22 -08002033 intermediate_long_term_key);
2034
2035 if (!ret)
2036 {
Marie Janssend19e0782016-07-15 12:48:27 -07002037 SMP_TRACE_ERROR("%s failed to derive intermediate_long_term_key",__func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002038 return ret;
2039 }
2040
2041 /* "brle" obtained from the spec */
Marie Janssend19e0782016-07-15 12:48:27 -07002042 ret = smp_calculate_h6(intermediate_long_term_key, (uint8_t *) "elrb" /* reversed "brle" */,
Satya Calloji444a8da2015-03-06 10:38:22 -08002043 p_cb->ltk);
2044
2045 if (!ret)
2046 {
Marie Janssend19e0782016-07-15 12:48:27 -07002047 SMP_TRACE_ERROR("%s failed",__func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002048 }
2049 else
2050 {
2051 p_cb->sec_level = (br_link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)
2052 ? SMP_SEC_AUTHENTICATED : SMP_SEC_UNAUTHENTICATE;
Marie Janssend19e0782016-07-15 12:48:27 -07002053 SMP_TRACE_EVENT ("%s is completed",__func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002054 }
2055
2056 return ret;
2057}
2058
2059/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002060 *
2061 * Function smp_calculate_h6
2062 *
2063 * Description The function calculates
2064 * C = h6(W, KeyID) = AES-CMAC (KeyID)
2065 * W
2066 * where
2067 * input: W is 128 bit,
2068 * KeyId is 32 bit,
2069 * output: C is 128 bit.
2070 *
2071 * Returns false if out of resources, true in other cases.
2072 *
2073 * Note The LSB is the first octet, the MSB is the last octet of
2074 * the AES-CMAC input/output stream.
2075 *
2076 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07002077bool smp_calculate_h6(uint8_t *w, uint8_t *keyid, uint8_t *c)
Satya Calloji444a8da2015-03-06 10:38:22 -08002078{
Marie Janssend19e0782016-07-15 12:48:27 -07002079#if (SMP_DEBUG == TRUE)
2080 uint8_t *p_print = NULL;
Satya Calloji444a8da2015-03-06 10:38:22 -08002081#endif
2082
Marie Janssend19e0782016-07-15 12:48:27 -07002083 SMP_TRACE_DEBUG ("%s",__func__);
2084#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08002085 p_print = w;
Marie Janssend19e0782016-07-15 12:48:27 -07002086 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"W", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08002087 p_print = keyid;
Marie Janssend19e0782016-07-15 12:48:27 -07002088 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"keyID", 4);
Satya Calloji444a8da2015-03-06 10:38:22 -08002089#endif
2090
Marie Janssend19e0782016-07-15 12:48:27 -07002091 uint8_t *p = NULL;
2092 uint8_t key[BT_OCTET16_LEN];
Satya Calloji444a8da2015-03-06 10:38:22 -08002093
2094 p = key;
2095 ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
2096
Marie Janssend19e0782016-07-15 12:48:27 -07002097#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08002098 p_print = key;
Marie Janssend19e0782016-07-15 12:48:27 -07002099 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"K", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08002100#endif
2101
Marie Janssend19e0782016-07-15 12:48:27 -07002102 uint8_t msg_len = 4 /* KeyID size */;
2103 uint8_t msg[4];
Satya Calloji444a8da2015-03-06 10:38:22 -08002104
2105 p = msg;
2106 ARRAY_TO_STREAM(p, keyid, 4);
2107
Marie Janssend19e0782016-07-15 12:48:27 -07002108#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08002109 p_print = msg;
Marie Janssend19e0782016-07-15 12:48:27 -07002110 smp_debug_print_nbyte_little_endian (p_print,(const uint8_t *) "M", msg_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08002111#endif
2112
Marie Janssend19e0782016-07-15 12:48:27 -07002113 bool ret = true;
2114 uint8_t cmac[BT_OCTET16_LEN];
Satya Calloji444a8da2015-03-06 10:38:22 -08002115 if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac))
2116 {
Marie Janssend19e0782016-07-15 12:48:27 -07002117 SMP_TRACE_ERROR("%s failed",__func__);
2118 ret = false;
Satya Calloji444a8da2015-03-06 10:38:22 -08002119 }
2120
Marie Janssend19e0782016-07-15 12:48:27 -07002121#if (SMP_DEBUG == TRUE)
Satya Calloji444a8da2015-03-06 10:38:22 -08002122 p_print = cmac;
Marie Janssend19e0782016-07-15 12:48:27 -07002123 smp_debug_print_nbyte_little_endian (p_print, (const uint8_t *)"AES-CMAC", BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08002124#endif
2125
2126 p = c;
2127 ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
2128 return ret;
2129}
2130
2131/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002132 *
2133 * Function smp_start_nonce_generation
2134 *
2135 * Description This function starts nonce generation.
2136 *
2137 * Returns void
2138 *
2139 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08002140void smp_start_nonce_generation(tSMP_CB *p_cb)
2141{
Marie Janssend19e0782016-07-15 12:48:27 -07002142 SMP_TRACE_DEBUG("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002143 p_cb->rand_enc_proc_state = SMP_GEN_NONCE_0_7;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002144 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -08002145}
2146
2147/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002148 *
2149 * Function smp_finish_nonce_generation
2150 *
2151 * Description This function finishes nonce generation.
2152 *
2153 * Returns void
2154 *
2155 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08002156void smp_finish_nonce_generation(tSMP_CB *p_cb)
2157{
Marie Janssend19e0782016-07-15 12:48:27 -07002158 SMP_TRACE_DEBUG("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08002159 p_cb->rand_enc_proc_state = SMP_GEN_NONCE_8_15;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002160 btsnd_hcic_ble_rand((void *)smp_rand_back);
Satya Calloji444a8da2015-03-06 10:38:22 -08002161}
2162
2163/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002164 *
2165 * Function smp_process_new_nonce
2166 *
2167 * Description This function notifies SM that it has new nonce.
2168 *
2169 * Returns void
2170 *
2171 ******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08002172void smp_process_new_nonce(tSMP_CB *p_cb)
2173{
Marie Janssend19e0782016-07-15 12:48:27 -07002174 SMP_TRACE_DEBUG ("%s round %d", __func__, p_cb->round);
Satya Calloji444a8da2015-03-06 10:38:22 -08002175 smp_sm_event(p_cb, SMP_HAVE_LOC_NONCE_EVT, NULL);
2176}
2177
2178/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002179 *
2180 * Function smp_rand_back
2181 *
2182 * Description This function is to process the rand command finished,
2183 * process the random/encrypted number for further action.
2184 *
2185 * Returns void
2186 *
2187 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002188static void smp_rand_back(tBTM_RAND_ENC *p)
2189{
2190 tSMP_CB *p_cb = &smp_cb;
Marie Janssend19e0782016-07-15 12:48:27 -07002191 uint8_t *pp = p->param_buf;
2192 uint8_t failure = SMP_PAIR_FAIL_UNKNOWN;
2193 uint8_t state = p_cb->rand_enc_proc_state & ~0x80;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002194
Marie Janssend19e0782016-07-15 12:48:27 -07002195 SMP_TRACE_DEBUG ("%s state=0x%x", __func__, state);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002196 if (p && p->status == HCI_SUCCESS)
2197 {
2198 switch (state)
2199 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08002200 case SMP_GEN_SRAND_MRAND:
2201 memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08002202 smp_generate_rand_cont(p_cb, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002203 break;
2204
2205 case SMP_GEN_SRAND_MRAND_CONT:
2206 memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
Satya Calloji444a8da2015-03-06 10:38:22 -08002207 smp_generate_confirm(p_cb, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002208 break;
2209
2210 case SMP_GEN_DIV_LTK:
2211 STREAM_TO_UINT16(p_cb->div, pp);
Satya Calloji444a8da2015-03-06 10:38:22 -08002212 smp_generate_ltk_cont(p_cb, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002213 break;
2214
2215 case SMP_GEN_DIV_CSRK:
2216 STREAM_TO_UINT16(p_cb->div, pp);
2217 smp_compute_csrk(p_cb, NULL);
2218 break;
2219
2220 case SMP_GEN_TK:
2221 smp_proc_passkey(p_cb, p);
2222 break;
2223
2224 case SMP_GEN_RAND_V:
2225 memcpy(p_cb->enc_rand, p->param_buf, BT_OCTET8_LEN);
2226 smp_generate_y(p_cb, NULL);
2227 break;
2228
Satya Calloji444a8da2015-03-06 10:38:22 -08002229 case SMP_GENERATE_PRIVATE_KEY_0_7:
2230 case SMP_GENERATE_PRIVATE_KEY_8_15:
2231 case SMP_GENERATE_PRIVATE_KEY_16_23:
2232 case SMP_GENERATE_PRIVATE_KEY_24_31:
2233 smp_continue_private_key_creation(p_cb, p);
2234 break;
2235
2236 case SMP_GEN_NONCE_0_7:
2237 memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
2238 smp_finish_nonce_generation(p_cb);
2239 break;
2240
2241 case SMP_GEN_NONCE_8_15:
2242 memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
2243 smp_process_new_nonce(p_cb);
2244 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002245 }
2246
2247 return;
2248 }
2249
Marie Janssend19e0782016-07-15 12:48:27 -07002250 SMP_TRACE_ERROR("%s key generation failed: (%d)", __func__, p_cb->rand_enc_proc_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002251 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002252}