1/******************************************************************************
2 *
3 *  Copyright (C) 2008-2012 Broadcom Corporation
4 *
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 *
21 *  This file contains the implementation of the SMP utility functions used
22 *  by SMP.
23 *
24 ******************************************************************************/
25
26#include "bt_target.h"
27
28#if SMP_INCLUDED == TRUE
29    #if SMP_DEBUG == TRUE
30        #include <stdio.h>
31    #endif
32    #include <string.h>
33
34    #include "btm_ble_api.h"
35    #include "smp_int.h"
36    #include "btm_int.h"
37    #include "btm_ble_int.h"
38    #include "hcimsgs.h"
39    #include "aes.h"
40    #ifndef SMP_MAX_ENC_REPEAT
41        #define SMP_MAX_ENC_REPEAT      3
42    #endif
43
44static void smp_rand_back(tBTM_RAND_ENC *p);
45static void smp_genenrate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
46static void smp_genenrate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
47static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p);
48static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p);
49static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p);
50static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p);
51static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p);
52static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p);
53static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p);
54
55static const tSMP_ACT smp_encrypt_action[] =
56{
57    smp_generate_compare,           /* SMP_GEN_COMPARE */
58    smp_genenrate_confirm,          /* SMP_GEN_CONFIRM*/
59    smp_generate_stk,               /* SMP_GEN_STK*/
60    smp_genenrate_ltk_cont,          /* SMP_GEN_LTK */
61    smp_generate_ltk,               /* SMP_GEN_DIV_LTK */
62    smp_generate_rand_vector,        /* SMP_GEN_RAND_V */
63    smp_generate_y,                  /* SMP_GEN_EDIV */
64    smp_generate_passkey,           /* SMP_GEN_TK */
65    smp_generate_confirm,           /* SMP_GEN_SRAND_MRAND */
66    smp_genenrate_rand_cont         /* SMP_GEN_SRAND_MRAND_CONT */
67};
68
69
70    #define SMP_PASSKEY_MASK    0xfff00000
71
72    #if SMP_DEBUG == TRUE
73static void smp_debug_print_nbyte_little_endian (UINT8 *p, const UINT8 *key_name, UINT8 len)
74{
75    int     i, x = 0;
76    UINT8   p_buf[100];
77    memset(p_buf, 0, 100);
78
79    for (i = 0; i < len; i ++)
80    {
81        x += sprintf ((char *)&p_buf[x], "%02x ", p[i]);
82    }
83    SMP_TRACE_WARNING2("%s(LSB ~ MSB) = %s", key_name, p_buf);
84}
85    #else
86        #define smp_debug_print_nbyte_little_endian(p, key_name, len)
87    #endif
88
89/*******************************************************************************
90**
91** Function         smp_encrypt_data
92**
93** Description      This function is called to generate passkey.
94**
95** Returns          void
96**
97*******************************************************************************/
98BOOLEAN smp_encrypt_data (UINT8 *key, UINT8 key_len,
99                          UINT8 *plain_text, UINT8 pt_len,
100                          tSMP_ENC *p_out)
101{
102    aes_context     ctx;
103    UINT8           *p_start = NULL;
104    UINT8           *p = NULL;
105    UINT8           *p_rev_data = NULL;    /* input data in big endilan format */
106    UINT8           *p_rev_key = NULL;     /* input key in big endilan format */
107    UINT8           *p_rev_output = NULL;  /* encrypted output in big endilan format */
108
109    SMP_TRACE_DEBUG0 ("smp_encrypt_data");
110    if ( (p_out == NULL ) || (key_len != SMP_ENCRYT_KEY_SIZE) )
111    {
112        BTM_TRACE_ERROR0 ("smp_encrypt_data Failed");
113        return(FALSE);
114    }
115
116    if ((p_start = (UINT8 *)GKI_getbuf((SMP_ENCRYT_DATA_SIZE*4))) == NULL)
117    {
118        BTM_TRACE_ERROR0 ("smp_encrypt_data Failed unable to allocate buffer");
119        return(FALSE);
120    }
121
122    if (pt_len > SMP_ENCRYT_DATA_SIZE)
123        pt_len = SMP_ENCRYT_DATA_SIZE;
124
125    memset(p_start, 0, SMP_ENCRYT_DATA_SIZE * 4);
126    p = p_start;
127    ARRAY_TO_STREAM (p, plain_text, pt_len); /* byte 0 to byte 15 */
128    p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE; /* start at byte 16 */
129    REVERSE_ARRAY_TO_STREAM (p, p_start, SMP_ENCRYT_DATA_SIZE);  /* byte 16 to byte 31 */
130    p_rev_key = p; /* start at byte 32 */
131    REVERSE_ARRAY_TO_STREAM (p, key, SMP_ENCRYT_KEY_SIZE); /* byte 32 to byte 47 */
132
133    smp_debug_print_nbyte_little_endian(key, (const UINT8 *)"Key", SMP_ENCRYT_KEY_SIZE);
134    smp_debug_print_nbyte_little_endian(p_start, (const UINT8 *)"Plain text", SMP_ENCRYT_DATA_SIZE);
135    p_rev_output = p;
136    aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
137    aes_encrypt(p_rev_data, p, &ctx);  /* outputs in byte 48 to byte 63 */
138
139    p = p_out->param_buf;
140    REVERSE_ARRAY_TO_STREAM (p, p_rev_output, SMP_ENCRYT_DATA_SIZE);
141    smp_debug_print_nbyte_little_endian(p_out->param_buf, (const UINT8 *)"Encrypted text", SMP_ENCRYT_KEY_SIZE);
142
143    p_out->param_len = SMP_ENCRYT_KEY_SIZE;
144    p_out->status = HCI_SUCCESS;
145    p_out->opcode =  HCI_BLE_ENCRYPT;
146
147    GKI_freebuf(p_start);
148
149    return(TRUE);
150}
151
152
153/*******************************************************************************
154**
155** Function         smp_generate_passkey
156**
157** Description      This function is called to generate passkey.
158**
159** Returns          void
160**
161*******************************************************************************/
162void smp_generate_passkey(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
163{
164    SMP_TRACE_DEBUG0 ("smp_generate_passkey");
165    p_cb->rand_enc_proc = SMP_GEN_TK;
166
167    /* generate MRand or SRand */
168    if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
169        smp_rand_back(NULL);
170}
171/*******************************************************************************
172**
173** Function         smp_proc_passkey
174**
175** Description      This function is called to process a passkey.
176**
177** Returns          void
178**
179*******************************************************************************/
180void smp_proc_passkey(tSMP_CB *p_cb , tBTM_RAND_ENC *p)
181{
182    UINT8   *tt = p_cb->tk;
183    tSMP_KEY    key;
184    UINT32  passkey; //= 19655 test number; */
185    UINT8 *pp = p->param_buf;
186
187    SMP_TRACE_DEBUG0 ("smp_proc_passkey ");
188    STREAM_TO_UINT32(passkey, pp);
189    passkey &= ~SMP_PASSKEY_MASK;
190
191    /* truncate by maximum value */
192    while (passkey > BTM_MAX_PASSKEY_VAL)
193        passkey >>= 1;
194    SMP_TRACE_ERROR1("Passkey generated = %d", passkey);
195
196    /* save the TK */
197    memset(p_cb->tk, 0, BT_OCTET16_LEN);
198    UINT32_TO_STREAM(tt, passkey);
199
200    key.key_type = SMP_KEY_TYPE_TK;
201    key.p_data  = p_cb->tk;
202
203    if (p_cb->p_callback)
204    {
205        (*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
206    }
207
208    smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
209}
210
211
212/*******************************************************************************
213**
214** Function         smp_generate_stk
215**
216** Description      This function is called to generate STK calculated by running
217**                  AES with the TK value as key and a concatenation of the random
218**                  values.
219**
220** Returns          void
221**
222*******************************************************************************/
223void smp_generate_stk (tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
224{
225    BT_OCTET16      ptext;
226    UINT8           *p = ptext;
227    tSMP_ENC        output;
228    tSMP_STATUS     status = SMP_PAIR_FAIL_UNKNOWN;
229
230    SMP_TRACE_DEBUG0 ("smp_generate_stk ");
231
232    memset(p, 0, BT_OCTET16_LEN);
233    if (p_cb->role == HCI_ROLE_MASTER)
234    {
235        memcpy(p, p_cb->rand, BT_OCTET8_LEN);
236        memcpy(&p[BT_OCTET8_LEN], p_cb->rrand, BT_OCTET8_LEN);
237    }
238    else
239    {
240        memcpy(p, p_cb->rrand, BT_OCTET8_LEN);
241        memcpy(&p[BT_OCTET8_LEN], p_cb->rand, BT_OCTET8_LEN);
242    }
243
244    /* generate STK = Etk(rand|rrand)*/
245    if (!SMP_Encrypt( p_cb->tk, BT_OCTET16_LEN, ptext, BT_OCTET16_LEN, &output))
246    {
247        SMP_TRACE_ERROR0("smp_generate_stk failed");
248        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
249    }
250    else
251    {
252        smp_process_stk(p_cb, &output);
253    }
254
255}
256/*******************************************************************************
257**
258** Function         smp_generate_confirm
259**
260** Description      This function is called to start the second pairing phase by
261**                  start generating initializer random number.
262**
263**
264** Returns          void
265**
266*******************************************************************************/
267void smp_generate_confirm (tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
268{
269    SMP_TRACE_DEBUG0 ("smp_generate_confirm");
270    p_cb->rand_enc_proc = SMP_GEN_SRAND_MRAND;
271    /* generate MRand or SRand */
272    if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
273        smp_rand_back(NULL);
274}
275/*******************************************************************************
276**
277** Function         smp_genenrate_rand_cont
278**
279** Description      This function is called to generate another 64 bits random for
280**                  MRand or Srand.
281**
282** Returns          void
283**
284*******************************************************************************/
285void smp_genenrate_rand_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
286{
287    SMP_TRACE_DEBUG0 ("smp_genenrate_rand_cont ");
288    p_cb->rand_enc_proc = SMP_GEN_SRAND_MRAND_CONT;
289    /* generate 64 MSB of MRand or SRand */
290
291    if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
292        smp_rand_back(NULL);
293}
294/*******************************************************************************
295**
296** Function         smp_generate_ltk
297**
298** Description      This function is called to calculate LTK, starting with DIV
299**                  generation.
300**
301**
302** Returns          void
303**
304*******************************************************************************/
305void smp_generate_ltk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
306{
307    BOOLEAN     div_status;
308
309    SMP_TRACE_DEBUG0 ("smp_generate_ltk ");
310
311    div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
312
313    if (div_status)
314    {
315        smp_genenrate_ltk_cont(p_cb, NULL);
316    }
317    else
318    {
319        SMP_TRACE_DEBUG0 ("Generate DIV for LTK");
320        p_cb->rand_enc_proc = SMP_GEN_DIV_LTK;
321        /* generate MRand or SRand */
322        if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
323            smp_rand_back(NULL);
324    }
325}
326
327
328/*******************************************************************************
329**
330** Function         smp_compute_csrk
331**
332** Description      This function is called to calculate CSRK
333**
334**
335** Returns          void
336**
337*******************************************************************************/
338void smp_compute_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
339{
340    BT_OCTET16  er;
341    UINT8       buffer[4]; /* for (r || DIV)  r=1*/
342    UINT16      r=1;
343    UINT8       *p=buffer;
344    tSMP_ENC    output;
345    tSMP_STATUS   status = SMP_PAIR_FAIL_UNKNOWN;
346
347    SMP_TRACE_DEBUG1 ("smp_compute_csrk div=%x", p_cb->div);
348    BTM_GetDeviceEncRoot(er);
349    /* CSRK = d1(ER, DIV, 1) */
350    UINT16_TO_STREAM(p, p_cb->div);
351    UINT16_TO_STREAM(p, r);
352
353    if (!SMP_Encrypt(er, BT_OCTET16_LEN, buffer, 4, &output))
354    {
355        SMP_TRACE_ERROR0("smp_generate_csrk failed");
356        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
357    }
358    else
359    {
360        memcpy((void *)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
361        smp_send_csrk_info(p_cb, NULL);
362    }
363}
364
365/*******************************************************************************
366**
367** Function         smp_generate_csrk
368**
369** Description      This function is called to calculate LTK, starting with DIV
370**                  generation.
371**
372**
373** Returns          void
374**
375*******************************************************************************/
376void smp_generate_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
377{
378    BOOLEAN     div_status;
379
380    SMP_TRACE_DEBUG0 ("smp_generate_csrk");
381
382    div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
383    if (div_status)
384    {
385        smp_compute_csrk(p_cb, NULL);
386    }
387    else
388    {
389        SMP_TRACE_DEBUG0 ("Generate DIV for CSRK");
390        p_cb->rand_enc_proc = SMP_GEN_DIV_CSRK;
391        if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
392            smp_rand_back(NULL);
393    }
394}
395
396
397/*******************************************************************************
398** Function         smp_concatenate_peer
399**                  add pairing command sent from local device into p1.
400*******************************************************************************/
401void smp_concatenate_local( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
402{
403    UINT8   *p = *p_data;
404
405    SMP_TRACE_DEBUG0 ("smp_concatenate_local ");
406    UINT8_TO_STREAM(p, op_code);
407    UINT8_TO_STREAM(p, p_cb->loc_io_caps);
408    UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
409    UINT8_TO_STREAM(p, p_cb->loc_auth_req);
410    UINT8_TO_STREAM(p, p_cb->loc_enc_size);
411    UINT8_TO_STREAM(p, p_cb->loc_i_key);
412    UINT8_TO_STREAM(p, p_cb->loc_r_key);
413
414    *p_data = p;
415}
416/*******************************************************************************
417** Function         smp_concatenate_peer
418**                  add pairing command received from peer device into p1.
419*******************************************************************************/
420void smp_concatenate_peer( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
421{
422    UINT8   *p = *p_data;
423
424    SMP_TRACE_DEBUG0 ("smp_concatenate_peer ");
425    UINT8_TO_STREAM(p, op_code);
426    UINT8_TO_STREAM(p, p_cb->peer_io_caps);
427    UINT8_TO_STREAM(p, p_cb->peer_oob_flag);
428    UINT8_TO_STREAM(p, p_cb->peer_auth_req);
429    UINT8_TO_STREAM(p, p_cb->peer_enc_size);
430    UINT8_TO_STREAM(p, p_cb->peer_i_key);
431    UINT8_TO_STREAM(p, p_cb->peer_r_key);
432
433    *p_data = p;
434}
435/*******************************************************************************
436**
437** Function         smp_gen_p1_4_confirm
438**
439** Description      Generate Confirm/Compare Step1:
440**                  p1 = pres || preq || rat' || iat'
441**
442** Returns          void
443**
444*******************************************************************************/
445void smp_gen_p1_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p1)
446{
447    UINT8 *p = (UINT8 *)p1;
448    tBTM_SEC_DEV_REC  *p_dev_rec;
449
450    SMP_TRACE_DEBUG0 ("smp_gen_p1_4_confirm");
451    if ((p_dev_rec = btm_find_dev (p_cb->pairing_bda)) == NULL)
452    {
453        SMP_TRACE_ERROR0("can not generate confirm for unknown device");
454        return;
455    }
456
457    BTM_ReadConnectionAddr(p_cb->local_bda);
458
459    if (p_cb->role == HCI_ROLE_MASTER)
460    {
461        /* LSB : rat': initiator's(local) address type */
462        UINT8_TO_STREAM(p, 0);
463        /* LSB : iat': responder's address type */
464        UINT8_TO_STREAM(p, p_dev_rec->ble.ble_addr_type);
465        /* concatinate preq */
466        smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
467        /* concatinate pres */
468        smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
469    }
470    else
471    {
472        /* LSB : iat': initiator's address type */
473        UINT8_TO_STREAM(p, p_dev_rec->ble.ble_addr_type);
474        /* LSB : rat': responder's(local) address type */
475        UINT8_TO_STREAM(p, 0);
476        /* concatinate preq */
477        smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
478        /* concatinate pres */
479        smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
480    }
481#if SMP_DEBUG == TRUE
482    SMP_TRACE_DEBUG0("p1 = pres || preq || rat' || iat'");
483    smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1", 16);
484#endif
485}
486/*******************************************************************************
487**
488** Function         smp_gen_p2_4_confirm
489**
490** Description      Generate Confirm/Compare Step2:
491**                  p2 = padding || ia || ra
492**
493** Returns          void
494**
495*******************************************************************************/
496void smp_gen_p2_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p2)
497{
498    UINT8   *p = (UINT8 *)p2;
499
500    SMP_TRACE_DEBUG0 ("smp_gen_p2_4_confirm");
501    memset(p, 0, sizeof(BT_OCTET16));
502
503    if (p_cb->role == HCI_ROLE_MASTER)
504    {
505        /* LSB ra */
506        BDADDR_TO_STREAM(p, p_cb->pairing_bda);
507        /* ia */
508        BDADDR_TO_STREAM(p, p_cb->local_bda);
509    }
510    else
511    {
512        /* LSB ra */
513        BDADDR_TO_STREAM(p, p_cb->local_bda);
514        /* ia */
515        BDADDR_TO_STREAM(p, p_cb->pairing_bda);
516    }
517#if SMP_DEBUG == TRUE
518    SMP_TRACE_DEBUG0("p2 = padding || ia || ra");
519    smp_debug_print_nbyte_little_endian(p2, (const UINT8 *)"p2", 16);
520#endif
521}
522/*******************************************************************************
523**
524** Function         smp_calculate_comfirm
525**
526** Description      This function is called to calculate Confirm value.
527**
528** Returns          void
529**
530*******************************************************************************/
531void smp_calculate_comfirm (tSMP_CB *p_cb, BT_OCTET16 rand, BD_ADDR bda)
532{
533    BT_OCTET16      p1;
534    tSMP_ENC       output;
535    tSMP_STATUS     status = SMP_PAIR_FAIL_UNKNOWN;
536
537    SMP_TRACE_DEBUG0 ("smp_calculate_comfirm ");
538    /* generate p1 = pres || preq || rat' || iat' */
539    smp_gen_p1_4_confirm(p_cb, p1);
540
541    /* p1 = rand XOR p1 */
542    smp_xor_128(p1, rand);
543
544    smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1' = r XOR p1", 16);
545
546    /* calculate e(k, r XOR p1), where k = TK */
547    if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output))
548    {
549        SMP_TRACE_ERROR0("smp_generate_csrk failed");
550        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
551    }
552    else
553    {
554        smp_calculate_comfirm_cont(p_cb, &output);
555    }
556}
557/*******************************************************************************
558**
559** Function         smp_calculate_comfirm_cont
560**
561** Description      This function is called when SConfirm/MConfirm is generated
562**                  proceed to send the Confirm request/response to peer device.
563**
564** Returns          void
565**
566*******************************************************************************/
567static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p)
568{
569    BT_OCTET16    p2;
570    tSMP_ENC      output;
571    tSMP_STATUS     status = SMP_PAIR_FAIL_UNKNOWN;
572
573    SMP_TRACE_DEBUG0 ("smp_calculate_comfirm_cont ");
574#if SMP_DEBUG == TRUE
575    SMP_TRACE_DEBUG0("Confirm step 1 p1' = e(k, r XOR p1)  Generated");
576    smp_debug_print_nbyte_little_endian (p->param_buf, (const UINT8 *)"C1", 16);
577#endif
578
579    smp_gen_p2_4_confirm(p_cb, p2);
580
581    /* calculate p2 = (p1' XOR p2) */
582    smp_xor_128(p2, p->param_buf);
583    smp_debug_print_nbyte_little_endian ((UINT8 *)p2, (const UINT8 *)"p2' = C1 xor p2", 16);
584
585    /* calculate: Confirm = E(k, p1' XOR p2) */
586    if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p2, BT_OCTET16_LEN, &output))
587    {
588        SMP_TRACE_ERROR0("smp_calculate_comfirm_cont failed");
589        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
590    }
591    else
592    {
593        switch (p_cb->rand_enc_proc)
594        {
595            case SMP_GEN_CONFIRM:
596                smp_process_confirm(p_cb, &output);
597                break;
598
599            case SMP_GEN_COMPARE:
600                smp_process_compare(p_cb, &output);
601                break;
602        }
603    }
604}
605/*******************************************************************************
606**
607** Function         smp_genenrate_confirm
608**
609** Description      This function is called when a 48 bits random number is generated
610**                  as SRand or MRand, continue to calculate Sconfirm or MConfirm.
611**
612** Returns          void
613**
614*******************************************************************************/
615static void smp_genenrate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
616{
617    SMP_TRACE_DEBUG0 ("smp_genenrate_confirm ");
618    p_cb->rand_enc_proc = SMP_GEN_CONFIRM;
619
620    smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rand,  (const UINT8 *)"local rand", 16);
621
622    smp_calculate_comfirm(p_cb, p_cb->rand, p_cb->pairing_bda);
623}
624/*******************************************************************************
625**
626** Function         smp_generate_compare
627**
628** Description      This function is called to generate SConfirm for Slave device,
629**                  or MSlave for Master device. This function can be also used for
630**                  generating Compare number for confirm value check.
631**
632** Returns          void
633**
634*******************************************************************************/
635void smp_generate_compare (tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
636{
637    SMP_TRACE_DEBUG0 ("smp_generate_compare ");
638    p_cb->rand_enc_proc = SMP_GEN_COMPARE;
639
640    smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rrand,  (const UINT8 *)"peer rand", 16);
641
642    smp_calculate_comfirm(p_cb, p_cb->rrand, p_cb->local_bda);
643}
644/*******************************************************************************
645**
646** Function         smp_process_confirm
647**
648** Description      This function is called when SConfirm/MConfirm is generated
649**                  proceed to send the Confirm request/response to peer device.
650**
651** Returns          void
652**
653*******************************************************************************/
654static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p)
655{
656    tSMP_KEY    key;
657
658    SMP_TRACE_DEBUG0 ("smp_process_confirm ");
659#if SMP_CONFORMANCE_TESTING == TRUE
660    if (p_cb->enable_test_confirm_val)
661    {
662        BTM_TRACE_DEBUG0 ("Use confirm value from script");
663        memcpy(p_cb->confirm, p_cb->test_confirm, BT_OCTET16_LEN);
664    }
665    else
666        memcpy(p_cb->confirm, p->param_buf, BT_OCTET16_LEN);
667#else
668    memcpy(p_cb->confirm, p->param_buf, BT_OCTET16_LEN);
669#endif
670
671
672#if (SMP_DEBUG == TRUE)
673    SMP_TRACE_DEBUG0("Confirm  Generated");
674    smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->confirm,  (const UINT8 *)"Confirm", 16);
675#endif
676
677    key.key_type = SMP_KEY_TYPE_CFM;
678    key.p_data = p->param_buf;
679
680    smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
681}
682/*******************************************************************************
683**
684** Function         smp_process_compare
685**
686** Description      This function is called when Compare is generated using the
687**                  RRand and local BDA, TK information.
688**
689** Returns          void
690**
691*******************************************************************************/
692static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p)
693{
694    tSMP_KEY    key;
695
696    SMP_TRACE_DEBUG0 ("smp_process_compare ");
697#if (SMP_DEBUG == TRUE)
698    SMP_TRACE_DEBUG0("Compare Generated");
699    smp_debug_print_nbyte_little_endian (p->param_buf,  (const UINT8 *)"Compare", 16);
700#endif
701    key.key_type = SMP_KEY_TYPE_CMP;
702    key.p_data   = p->param_buf;
703
704    smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
705}
706
707/*******************************************************************************
708**
709** Function         smp_process_stk
710**
711** Description      This function is called when STK is generated
712**                  proceed to send the encrypt the link using STK.
713**
714** Returns          void
715**
716*******************************************************************************/
717static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p)
718{
719    tSMP_KEY    key;
720
721    SMP_TRACE_DEBUG0 ("smp_process_stk ");
722#if (SMP_DEBUG == TRUE)
723    SMP_TRACE_ERROR0("STK Generated");
724#endif
725    smp_mask_enc_key(p_cb->loc_enc_size, p->param_buf);
726
727    key.key_type = SMP_KEY_TYPE_STK;
728    key.p_data   = p->param_buf;
729
730    smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
731}
732
733/*******************************************************************************
734**
735** Function         smp_genenrate_ltk_cont
736**
737** Description      This function is to calculate LTK = d1(ER, DIV, 0)= e(ER, DIV)
738**
739** Returns          void
740**
741*******************************************************************************/
742static void smp_genenrate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
743{
744    BT_OCTET16  er;
745    tSMP_ENC    output;
746    tSMP_STATUS     status = SMP_PAIR_FAIL_UNKNOWN;
747
748    SMP_TRACE_DEBUG0 ("smp_genenrate_ltk_cont ");
749    BTM_GetDeviceEncRoot(er);
750
751    /* LTK = d1(ER, DIV, 0)= e(ER, DIV)*/
752    if (!SMP_Encrypt(er, BT_OCTET16_LEN, (UINT8 *)&p_cb->div,
753                     sizeof(UINT16), &output))
754    {
755        SMP_TRACE_ERROR0("smp_genenrate_ltk_cont failed");
756        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
757    }
758    else
759    {
760        /* mask the LTK */
761        smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
762        memcpy((void *)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
763        smp_generate_rand_vector(p_cb, NULL);
764    }
765
766}
767
768/*******************************************************************************
769**
770** Function         smp_generate_y
771**
772** Description      This function is to proceed generate Y = E(DHK, Rand)
773**
774** Returns          void
775**
776*******************************************************************************/
777static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p)
778{
779    BT_OCTET16  dhk;
780    tSMP_ENC   output;
781    tSMP_STATUS     status = SMP_PAIR_FAIL_UNKNOWN;
782
783
784    SMP_TRACE_DEBUG0 ("smp_generate_y ");
785    BTM_GetDeviceDHK(dhk);
786
787    if (!SMP_Encrypt(dhk, BT_OCTET16_LEN, p_cb->enc_rand,
788                     BT_OCTET8_LEN, &output))
789    {
790        SMP_TRACE_ERROR0("smp_generate_y failed");
791        smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
792    }
793    else
794    {
795        smp_process_ediv(p_cb, &output);
796    }
797}
798/*******************************************************************************
799**
800** Function         smp_generate_rand_vector
801**
802** Description      This function is called when LTK is generated, send state machine
803**                  event to SMP.
804**
805** Returns          void
806**
807*******************************************************************************/
808static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p)
809{
810    /* generate EDIV and rand now */
811    /* generate random vector */
812    SMP_TRACE_DEBUG0 ("smp_generate_rand_vector ");
813    p_cb->rand_enc_proc = SMP_GEN_RAND_V;
814    if (!btsnd_hcic_ble_rand((void *)smp_rand_back))
815        smp_rand_back(NULL);
816
817}
818/*******************************************************************************
819**
820** Function         smp_genenrate_smp_process_edivltk_cont
821**
822** Description      This function is to calculate EDIV = Y xor DIV
823**
824** Returns          void
825**
826*******************************************************************************/
827static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p)
828{
829    tSMP_KEY    key;
830    UINT8 *pp= p->param_buf;
831    UINT16  y;
832
833    SMP_TRACE_DEBUG0 ("smp_process_ediv ");
834    STREAM_TO_UINT16(y, pp);
835
836    /* EDIV = Y xor DIV */
837    p_cb->ediv = p_cb->div ^ y;
838    /* send LTK ready */
839    SMP_TRACE_ERROR0("LTK ready");
840    key.key_type = SMP_KEY_TYPE_LTK;
841    key.p_data   = p->param_buf;
842
843    smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
844}
845
846/*******************************************************************************
847**
848** Function         smp_rand_back
849**
850** Description      This function is to process the rand command finished,
851**                  process the random/encrypted number for further action.
852**
853** Returns          void
854**
855*******************************************************************************/
856static void smp_rand_back(tBTM_RAND_ENC *p)
857{
858    tSMP_CB *p_cb = &smp_cb;
859    UINT8   *pp = p->param_buf;
860    UINT8   failure = SMP_PAIR_FAIL_UNKNOWN;
861    UINT8   state = p_cb->rand_enc_proc & ~0x80;
862
863    SMP_TRACE_DEBUG1 ("smp_rand_back state=0x%x", state);
864    if (p && p->status == HCI_SUCCESS)
865    {
866        switch (state)
867        {
868
869            case SMP_GEN_SRAND_MRAND:
870                memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
871                smp_genenrate_rand_cont(p_cb, NULL);
872                break;
873
874            case SMP_GEN_SRAND_MRAND_CONT:
875                memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
876                smp_genenrate_confirm(p_cb, NULL);
877                break;
878
879            case SMP_GEN_DIV_LTK:
880                STREAM_TO_UINT16(p_cb->div, pp);
881                smp_genenrate_ltk_cont(p_cb, NULL);
882                break;
883
884            case SMP_GEN_DIV_CSRK:
885                STREAM_TO_UINT16(p_cb->div, pp);
886                smp_compute_csrk(p_cb, NULL);
887                break;
888
889            case SMP_GEN_TK:
890                smp_proc_passkey(p_cb, p);
891                break;
892
893            case SMP_GEN_RAND_V:
894                memcpy(p_cb->enc_rand, p->param_buf, BT_OCTET8_LEN);
895                smp_generate_y(p_cb, NULL);
896                break;
897
898        }
899
900        return;
901    }
902
903    SMP_TRACE_ERROR1("smp_rand_back Key generation failed: (%d)", p_cb->rand_enc_proc);
904
905    smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
906
907}
908#endif
909
910