1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
77
78
79
83
84
88
89
94
95
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
174
175
179
180
183
184
185
186
187
188
189
190
191
192
193
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
231
232
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
256
257
258
259
260
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
289
290
291
292
293
294
295
296
297
300
301
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
415
419
420
421
422
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
505
506
507
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
617
618
619
620
621
622
633
634
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
694
695
696
697
698
699
700
701
702
703
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
978
979
980
981
982
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1014
1015
1016
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1080
1088
1096
1101
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1215
1216
1221
1226
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1261
1262
1267
1271
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1331
1332
1333
1334
1335
1336
1338
1339
1340
1341
1342
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1374
1375
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1446
1447
1448
1452
1453
1454
1456
1457
1458
1459
1460
1461
1462
1464
1465
1466
1469
1470
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1547
1548
1549
1552
1553
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1576
1577
1579
1580
1581
1582
1586
1587
1589
1590
1591
1592
1596
1597
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1689
1690
1691
1695
1696
1697
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1732
1736
1737
1738
1739
1740
1741
1742
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1760
1761
1762
1763
1764
1768
1769
1770
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1841
1842
1843
1844
1845
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1924
1925
1926
1927
1931
1932
1933
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1964
1970
1971
1975
1976
1977
1978
1979
1980
1984
1985
1986
1987
1990
1991
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2051
2052
2053
2054
2058
2059
2060
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2081
2082
2083
2084
2085
2086
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2149
2150
2151
2152
2153
2157
2158
2159
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2244
2249
2254
2259
2263
2268
2269
2270
2271
2275
2280
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
/* ... */
/* ... */
#include "common/bt_target.h"
#if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
#if SMP_DEBUG == TRUE
#include <stdio.h>
#endif
#include <string.h>
#include "stack/btm_ble_api.h"
#include "smp_int.h"
#include "btm_int.h"
#include "btm_ble_int.h"
#include "stack/hcimsgs.h"
#include "aes.h"
#include "p_256_ecc_pp.h"
#include "device/controller.h"9 includes
#ifndef SMP_MAX_ENC_REPEAT
#define SMP_MAX_ENC_REPEAT 3
#endif
static void smp_rand_back(tBTM_RAND_ENC *p);
static void smp_generate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
static void smp_generate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p);
static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p);
static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p);
static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p);
static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p);
static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p);
static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p);
static BOOLEAN smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output);
static void smp_continue_private_key_creation(tSMP_CB *p_cb, tBTM_RAND_ENC *p);
static void smp_process_private_key(tSMP_CB *p_cb);
static void smp_finish_nonce_generation(tSMP_CB *p_cb);
static void smp_process_new_nonce(tSMP_CB *p_cb);
static const tSMP_ACT smp_encrypt_action[] = {
smp_generate_compare,
smp_generate_confirm,
smp_generate_stk,
smp_generate_ltk_cont,
smp_generate_ltk,
smp_generate_rand_vector,
smp_generate_y,
smp_generate_passkey,
smp_generate_srand_mrand_confirm,
smp_generate_rand_cont
}{...};
/* ... */
static tSMP_LOC_OOB_DATA saved_local_oob_data = {};
void smp_save_local_oob_data(tSMP_CB *p_cb)
{
memcpy(&saved_local_oob_data, &p_cb->sc_oob_data.loc_oob_data, sizeof(tSMP_LOC_OOB_DATA));
}{ ... }
void smp_clear_local_oob_data(void)
{
memset(&saved_local_oob_data, 0, sizeof(tSMP_LOC_OOB_DATA));
}{ ... }
static BOOLEAN oob_data_is_empty(tSMP_LOC_OOB_DATA *data)
{
tSMP_LOC_OOB_DATA empty_data = {0};
return (memcmp(data, &empty_data, sizeof(tSMP_LOC_OOB_DATA)) == 0);
}{ ... }
tSMP_LOC_OOB_DATA *smp_get_local_oob_data(void)
{
return &saved_local_oob_data;
}{ ... }
void smp_debug_print_nbyte_little_endian(UINT8 *p, const UINT8 *key_name, UINT8 len)
{
#if SMP_DEBUG == TRUE
int ind, x;
int col_count = 32;
int row_count;
UINT8 p_buf[512];
SMP_TRACE_WARNING("%s(LSB ~ MSB):\n", key_name);
memset(p_buf, 0, sizeof(p_buf));
row_count = len % col_count ? len / col_count + 1 : len / col_count;
ind = 0;
for (int row = 0; row < row_count; row++) {
for (int column = 0, x = 0; (ind < len) && (column < col_count); column++, ind++) {
x += sprintf((char *)&p_buf[x], "%02x ", p[ind]);
}{...}
SMP_TRACE_WARNING(" [%03d]: %s", row * col_count, p_buf);
}{...}
#endif/* ... */
}{ ... }
#if 0
void smp_debug_print_nbyte_big_endian (UINT8 *p, const UINT8 *key_name, UINT8 len)
{
#if SMP_DEBUG == TRUE
UINT8 p_buf[512];
SMP_TRACE_WARNING("%s(MSB ~ LSB):", key_name);
memset(p_buf, 0, sizeof(p_buf));
nrows = len % ncols ? len / ncols + 1 : len / ncols;
int ind = 0;
int ncols = 32;
int nrows;
int x;
for (int row = 0; row < nrows; row++) {
for (int col = 0, x = 0; (ind < len) && (col < ncols); col++, ind++) {
x += sprintf ((char *)&p_buf[len - x - 1], "%02x ", p[ind]);
}{...}
SMP_TRACE_WARNING("[%03d]: %s", row * ncols, p_buf);
}{...}
/* ... */#endif
}{...}
/* ... */#endif
/* ... */
BOOLEAN smp_encrypt_data (UINT8 *key, UINT8 key_len,
UINT8 *plain_text, UINT8 pt_len,
tSMP_ENC *p_out)
{
aes_context ctx;
UINT8 *p_start = NULL;
UINT8 *p = NULL;
UINT8 *p_rev_data = NULL;
UINT8 *p_rev_key = NULL;
UINT8 *p_rev_output = NULL;
SMP_TRACE_DEBUG ("%s\n", __func__);
if ( (p_out == NULL ) || (key_len != SMP_ENCRYT_KEY_SIZE) ) {
SMP_TRACE_ERROR ("%s failed\n", __func__);
return FALSE;
}{...}
if ((p_start = (UINT8 *)osi_malloc((SMP_ENCRYT_DATA_SIZE * 4))) == NULL) {
SMP_TRACE_ERROR ("%s failed unable to allocate buffer\n", __func__);
return FALSE;
}{...}
if (pt_len > SMP_ENCRYT_DATA_SIZE) {
pt_len = SMP_ENCRYT_DATA_SIZE;
}{...}
memset(p_start, 0, SMP_ENCRYT_DATA_SIZE * 4);
p = p_start;
ARRAY_TO_STREAM (p, plain_text, pt_len);
p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE;
REVERSE_ARRAY_TO_STREAM (p, p_start, SMP_ENCRYT_DATA_SIZE);
p_rev_key = p;
REVERSE_ARRAY_TO_STREAM (p, key, SMP_ENCRYT_KEY_SIZE);
#if SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE
smp_debug_print_nbyte_little_endian(key, (const UINT8 *)"Key", SMP_ENCRYT_KEY_SIZE);
smp_debug_print_nbyte_little_endian(p_start, (const UINT8 *)"Plain text", SMP_ENCRYT_DATA_SIZE);/* ... */
#endif
p_rev_output = p;
aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
bluedroid_aes_encrypt(p_rev_data, p, &ctx);
p = p_out->param_buf;
REVERSE_ARRAY_TO_STREAM (p, p_rev_output, SMP_ENCRYT_DATA_SIZE);
#if SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE
smp_debug_print_nbyte_little_endian(p_out->param_buf, (const UINT8 *)"Encrypted text", SMP_ENCRYT_KEY_SIZE);
#endif
p_out->param_len = SMP_ENCRYT_KEY_SIZE;
p_out->status = HCI_SUCCESS;
p_out->opcode = HCI_BLE_ENCRYPT;
osi_free(p_start);
return TRUE;
}{ ... }
void smp_use_static_passkey(void)
{
tSMP_CB *p_cb = &smp_cb;
UINT8 *tt = p_cb->tk;
tSMP_KEY key;
UINT32 passkey = p_cb->static_passkey;
memset(p_cb->tk, 0, BT_OCTET16_LEN);
UINT32_TO_STREAM(tt, passkey);
key.key_type = SMP_KEY_TYPE_TK;
key.p_data = p_cb->tk;
if (p_cb->p_callback) {
(*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
}{...}
if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_PASSKEY_DISP) {
smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &passkey);
}{...} else {
smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
}{...}
}{ ... }
/* ... */
void smp_generate_passkey(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
if(p_cb->use_static_passkey) {
SMP_TRACE_DEBUG ("%s use static passkey %6d", __func__, p_cb->static_passkey);
smp_use_static_passkey();
return;
}{...}
SMP_TRACE_DEBUG ("%s generate rand passkey", __func__);
p_cb->rand_enc_proc_state = SMP_GEN_TK;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_proc_passkey(tSMP_CB *p_cb , tBTM_RAND_ENC *p)
{
UINT8 *tt = p_cb->tk;
tSMP_KEY key;
UINT32 passkey;
UINT8 *pp = p->param_buf;
SMP_TRACE_DEBUG ("%s", __func__);
STREAM_TO_UINT32(passkey, pp);
passkey &= ~SMP_PASSKEY_MASK;
while (passkey > BTM_MAX_PASSKEY_VAL) {
passkey >>= 1;
}{...}
memset(p_cb->tk, 0, BT_OCTET16_LEN);
UINT32_TO_STREAM(tt, passkey);
key.key_type = SMP_KEY_TYPE_TK;
key.p_data = p_cb->tk;
if (p_cb->p_callback) {
(*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
}{...}
if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_PASSKEY_DISP) {
smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &passkey);
}{...} else {
smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
}{...}
}{ ... }
/* ... */
void smp_generate_stk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("%s\n", __func__);
if (p_cb->le_secure_connections_mode_is_used) {
SMP_TRACE_WARNING ("FOR LE SC LTK IS USED INSTEAD OF STK");
output.param_len = SMP_ENCRYT_KEY_SIZE;
output.status = HCI_SUCCESS;
output.opcode = HCI_BLE_ENCRYPT;
memcpy(output.param_buf, p_cb->ltk, SMP_ENCRYT_DATA_SIZE);
}{...} else if (!smp_calculate_legacy_short_term_key(p_cb, &output)) {
SMP_TRACE_ERROR("%s failed", __func__);
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
return;
}{...}
smp_process_stk(p_cb, &output);
}{ ... }
/* ... */
void smp_generate_srand_mrand_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
SMP_TRACE_DEBUG ("%s\n", __func__);
p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_generate_rand_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
SMP_TRACE_DEBUG ("%s\n", __func__);
p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND_CONT;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_generate_ltk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
BOOLEAN div_status;
SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
#if (CLASSIC_BT_INCLUDED == TRUE)
if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING) {
smp_br_process_link_key(p_cb, NULL);
return;
}{...}
#endif/* ... */
if (p_cb->le_secure_connections_mode_is_used) {
smp_process_secure_connection_long_term_key();
return;
}{...}
div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
if (div_status) {
smp_generate_ltk_cont(p_cb, NULL);
}{...} else {
SMP_TRACE_DEBUG ("Generate DIV for LTK\n");
p_cb->rand_enc_proc_state = SMP_GEN_DIV_LTK;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{...}
}{ ... }
/* ... */
void smp_compute_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
BT_OCTET16 er;
UINT8 buffer[4];
UINT16 r = 1;
UINT8 *p = buffer;
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("smp_compute_csrk div=%x\n", p_cb->div);
BTM_GetDeviceEncRoot(er);
UINT16_TO_STREAM(p, p_cb->div);
UINT16_TO_STREAM(p, r);
if (!SMP_Encrypt(er, BT_OCTET16_LEN, buffer, 4, &output)) {
SMP_TRACE_ERROR("smp_generate_csrk failed\n");
if (p_cb->smp_over_br) {
#if (CLASSIC_BT_INCLUDED == TRUE)
smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &status);
#endif
}{...} else {
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...}
}{...} else {
memcpy((void *)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
smp_send_csrk_info(p_cb, NULL);
}{...}
}{ ... }
/* ... */
void smp_generate_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
BOOLEAN div_status;
SMP_TRACE_DEBUG ("smp_generate_csrk");
div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
if (div_status) {
smp_compute_csrk(p_cb, NULL);
}{...} else {
SMP_TRACE_DEBUG ("Generate DIV for CSRK");
p_cb->rand_enc_proc_state = SMP_GEN_DIV_CSRK;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{...}
}{ ... }
/* ... */
void smp_concatenate_local( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
{
UINT8 *p = *p_data;
SMP_TRACE_DEBUG ("%s\n", __func__);
UINT8_TO_STREAM(p, op_code);
UINT8_TO_STREAM(p, p_cb->local_io_capability);
UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
UINT8_TO_STREAM(p, p_cb->loc_auth_req);
UINT8_TO_STREAM(p, p_cb->loc_enc_size);
UINT8_TO_STREAM(p, p_cb->local_i_key);
UINT8_TO_STREAM(p, p_cb->local_r_key);
*p_data = p;
}{ ... }
/* ... */
void smp_concatenate_peer( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
{
UINT8 *p = *p_data;
SMP_TRACE_DEBUG ("smp_concatenate_peer \n");
UINT8_TO_STREAM(p, op_code);
UINT8_TO_STREAM(p, p_cb->peer_io_caps);
UINT8_TO_STREAM(p, p_cb->peer_oob_flag);
UINT8_TO_STREAM(p, p_cb->peer_auth_req);
UINT8_TO_STREAM(p, p_cb->peer_enc_size);
UINT8_TO_STREAM(p, p_cb->peer_i_key);
UINT8_TO_STREAM(p, p_cb->peer_r_key);
*p_data = p;
}{ ... }
/* ... */
void smp_gen_p1_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p1)
{
UINT8 *p = (UINT8 *)p1;
tBLE_ADDR_TYPE addr_type = 0;
BD_ADDR remote_bda;
SMP_TRACE_DEBUG ("smp_gen_p1_4_confirm\n");
if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type)) {
SMP_TRACE_ERROR("can not generate confirm for unknown device\n");
return;
}{...}
BTM_ReadConnectionAddr( p_cb->pairing_bda, p_cb->local_bda, &p_cb->addr_type);
if (p_cb->role == HCI_ROLE_MASTER) {
UINT8_TO_STREAM(p, p_cb->addr_type);
UINT8_TO_STREAM(p, addr_type);
smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
}{...} else {
UINT8_TO_STREAM(p, addr_type);
UINT8_TO_STREAM(p, p_cb->addr_type);
smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
}{...}
#if SMP_DEBUG == TRUE
SMP_TRACE_DEBUG("p1 = pres || preq || rat' || iat'\n");
smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1", 16);/* ... */
#endif
}{ ... }
/* ... */
void smp_gen_p2_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p2)
{
UINT8 *p = (UINT8 *)p2;
BD_ADDR remote_bda;
tBLE_ADDR_TYPE addr_type = 0;
SMP_TRACE_DEBUG ("smp_gen_p2_4_confirm\n");
if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type)) {
SMP_TRACE_ERROR("can not generate confirm p2 for unknown device\n");
return;
}{...}
SMP_TRACE_DEBUG ("smp_gen_p2_4_confirm\n");
memset(p, 0, sizeof(BT_OCTET16));
if (p_cb->role == HCI_ROLE_MASTER) {
BDADDR_TO_STREAM(p, remote_bda);
BDADDR_TO_STREAM(p, p_cb->local_bda);
}{...} else {
BDADDR_TO_STREAM(p, p_cb->local_bda);
BDADDR_TO_STREAM(p, remote_bda);
}{...}
#if SMP_DEBUG == TRUE
SMP_TRACE_DEBUG("p2 = padding || ia || ra");
smp_debug_print_nbyte_little_endian(p2, (const UINT8 *)"p2", 16);/* ... */
#endif
}{ ... }
/* ... */
void smp_calculate_comfirm (tSMP_CB *p_cb, BT_OCTET16 rand, BD_ADDR bda)
{
UNUSED(bda);
BT_OCTET16 p1;
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("smp_calculate_comfirm \n");
smp_gen_p1_4_confirm(p_cb, p1);
smp_xor_128(p1, rand);
smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1' = r XOR p1", 16);
if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output)) {
SMP_TRACE_ERROR("smp_generate_csrk failed");
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...} else {
smp_calculate_comfirm_cont(p_cb, &output);
}{...}
}{ ... }
/* ... */
static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p)
{
BT_OCTET16 p2;
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("smp_calculate_comfirm_cont \n");
#if SMP_DEBUG == TRUE
SMP_TRACE_DEBUG("Confirm step 1 p1' = e(k, r XOR p1) Generated\n");
smp_debug_print_nbyte_little_endian (p->param_buf, (const UINT8 *)"C1", 16);/* ... */
#endif
smp_gen_p2_4_confirm(p_cb, p2);
smp_xor_128(p2, p->param_buf);
smp_debug_print_nbyte_little_endian ((UINT8 *)p2, (const UINT8 *)"p2' = C1 xor p2", 16);
if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p2, BT_OCTET16_LEN, &output)) {
SMP_TRACE_ERROR("smp_calculate_comfirm_cont failed\n");
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...} else {
SMP_TRACE_DEBUG("p_cb->rand_enc_proc_state=%d\n", p_cb->rand_enc_proc_state);
switch (p_cb->rand_enc_proc_state) {
case SMP_GEN_CONFIRM:
smp_process_confirm(p_cb, &output);
break;
...
case SMP_GEN_COMPARE:
smp_process_compare(p_cb, &output);
break;...
}{...}
}{...}
}{ ... }
/* ... */
static void smp_generate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
SMP_TRACE_DEBUG ("%s\n", __func__);
p_cb->rand_enc_proc_state = SMP_GEN_CONFIRM;
smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rand, (const UINT8 *)"local rand", 16);
smp_calculate_comfirm(p_cb, p_cb->rand, p_cb->pairing_bda);
}{ ... }
/* ... */
void smp_generate_compare (tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
SMP_TRACE_DEBUG ("smp_generate_compare \n");
p_cb->rand_enc_proc_state = SMP_GEN_COMPARE;
smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rrand, (const UINT8 *)"peer rand", 16);
smp_calculate_comfirm(p_cb, p_cb->rrand, p_cb->local_bda);
}{ ... }
/* ... */
static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p)
{
tSMP_KEY key;
SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
memcpy(p_cb->confirm, p->param_buf, BT_OCTET16_LEN);
#if (SMP_DEBUG == TRUE)
SMP_TRACE_DEBUG("Confirm Generated");
smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->confirm, (const UINT8 *)"Confirm", 16);/* ... */
#endif
key.key_type = SMP_KEY_TYPE_CFM;
key.p_data = p->param_buf;
smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
}{ ... }
/* ... */
static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p)
{
tSMP_KEY key;
SMP_TRACE_DEBUG ("smp_process_compare \n");
#if (SMP_DEBUG == TRUE)
SMP_TRACE_DEBUG("Compare Generated\n");
smp_debug_print_nbyte_little_endian (p->param_buf, (const UINT8 *)"Compare", 16);/* ... */
#endif
key.key_type = SMP_KEY_TYPE_CMP;
key.p_data = p->param_buf;
smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
}{ ... }
/* ... */
static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p)
{
tSMP_KEY key;
SMP_TRACE_DEBUG ("smp_process_stk ");
#if (SMP_DEBUG == TRUE)
SMP_TRACE_ERROR("STK Generated");
#endif
smp_mask_enc_key(p_cb->loc_enc_size, p->param_buf);
key.key_type = SMP_KEY_TYPE_STK;
key.p_data = p->param_buf;
smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
}{ ... }
/* ... */
static void smp_generate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UNUSED(p_data);
BT_OCTET16 er;
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("%s\n", __func__);
BTM_GetDeviceEncRoot(er);
if (!SMP_Encrypt(er, BT_OCTET16_LEN, (UINT8 *)&p_cb->div,
sizeof(UINT16), &output)) {
SMP_TRACE_ERROR("%s failed\n", __func__);
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...} else {
smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
memcpy((void *)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
smp_generate_rand_vector(p_cb, NULL);
}{...}
}{ ... }
/* ... */
static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p)
{
UNUSED(p);
BT_OCTET16 dhk;
tSMP_ENC output;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("smp_generate_y \n");
BTM_GetDeviceDHK(dhk);
if (!SMP_Encrypt(dhk, BT_OCTET16_LEN, p_cb->enc_rand,
BT_OCTET8_LEN, &output)) {
SMP_TRACE_ERROR("smp_generate_y failed");
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...} else {
smp_process_ediv(p_cb, &output);
}{...}
}{ ... }
/* ... */
static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p)
{
UNUSED(p);
SMP_TRACE_DEBUG ("smp_generate_rand_vector\n");
p_cb->rand_enc_proc_state = SMP_GEN_RAND_V;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p)
{
tSMP_KEY key;
UINT8 *pp = p->param_buf;
UINT16 y;
SMP_TRACE_DEBUG ("smp_process_ediv ");
STREAM_TO_UINT16(y, pp);
p_cb->ediv = p_cb->div ^ y;
SMP_TRACE_DEBUG("LTK ready");
key.key_type = SMP_KEY_TYPE_LTK;
key.p_data = p->param_buf;
smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
}{ ... }
/* ... */
BOOLEAN smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output)
{
BT_OCTET16 ptext;
UINT8 *p = ptext;
SMP_TRACE_DEBUG ("%s\n", __func__);
memset(p, 0, BT_OCTET16_LEN);
if (p_cb->role == HCI_ROLE_MASTER) {
memcpy(p, p_cb->rand, BT_OCTET8_LEN);
memcpy(&p[BT_OCTET8_LEN], p_cb->rrand, BT_OCTET8_LEN);
}{...} else {
memcpy(p, p_cb->rrand, BT_OCTET8_LEN);
memcpy(&p[BT_OCTET8_LEN], p_cb->rand, BT_OCTET8_LEN);
}{...}
BOOLEAN encrypted;
encrypted = SMP_Encrypt( p_cb->tk, BT_OCTET16_LEN, ptext, BT_OCTET16_LEN, output);
if (!encrypted) {
SMP_TRACE_ERROR("%s failed\n", __func__);
}{...}
return encrypted;
}{ ... }
/* ... */
void smp_create_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
SMP_TRACE_DEBUG("%s", __func__);
if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
SMP_TRACE_EVENT("OOB Association Model");
if (!oob_data_is_empty(&saved_local_oob_data)) {
SMP_TRACE_EVENT("Found OOB data, loading keys");
memcpy(&p_cb->sc_oob_data.loc_oob_data, &saved_local_oob_data, sizeof(tSMP_LOC_OOB_DATA));
smp_process_private_key(p_cb);
return;
}{...}
SMP_TRACE_EVENT("OOB Association Model with no saved data");
}{...}
p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_0_7;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_use_oob_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
SMP_TRACE_DEBUG ("%s req_oob_type: %d, role: %d\n",
__func__, p_cb->req_oob_type, p_cb->role);
switch (p_cb->req_oob_type) {
case SMP_OOB_BOTH:
case SMP_OOB_LOCAL:
SMP_TRACE_DEBUG("%s restore secret key\n", __func__);
smp_process_private_key(p_cb);
break;...
default:
SMP_TRACE_DEBUG("%s create secret key anew\n", __func__);
smp_set_state(SMP_STATE_PAIR_REQ_RSP);
smp_decide_association_model(p_cb, NULL);
break;...
}{...}
}{ ... }
/* ... */
void smp_continue_private_key_creation (tSMP_CB *p_cb, tBTM_RAND_ENC *p)
{
UINT8 state = p_cb->rand_enc_proc_state & ~0x80;
SMP_TRACE_DEBUG ("%s state=0x%x\n", __func__, state);
switch (state) {
case SMP_GENERATE_PRIVATE_KEY_0_7:
memcpy((void *)p_cb->private_key, p->param_buf, p->param_len);
p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_8_15;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
break;
...
case SMP_GENERATE_PRIVATE_KEY_8_15:
memcpy((void *)&p_cb->private_key[8], p->param_buf, p->param_len);
p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_16_23;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
break;
...
case SMP_GENERATE_PRIVATE_KEY_16_23:
memcpy((void *)&p_cb->private_key[16], p->param_buf, p->param_len);
p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_24_31;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
break;
...
case SMP_GENERATE_PRIVATE_KEY_24_31:
memcpy((void *)&p_cb->private_key[24], p->param_buf, p->param_len);
smp_process_private_key (p_cb);
break;
...
default:
break;...
}{...}
return;
}{ ... }
/* ... */
void smp_process_private_key(tSMP_CB *p_cb)
{
Point public_key;
BT_OCTET32 private_key;
tSMP_LOC_OOB_DATA *p_loc_oob = &p_cb->sc_oob_data.loc_oob_data;
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
if (p_loc_oob->present) {
memcpy(p_cb->private_key, p_loc_oob->private_key_used, BT_OCTET32_LEN);
memcpy(p_cb->loc_publ_key.x, p_loc_oob->publ_key_used.x, BT_OCTET32_LEN);
memcpy(p_cb->loc_publ_key.y, p_loc_oob->publ_key_used.y, BT_OCTET32_LEN);
memcpy(p_cb->local_random, p_loc_oob->randomizer, BT_OCTET16_LEN);
}{...} else {
memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *) private_key, KEY_LENGTH_DWORDS_P256);
memcpy(p_cb->loc_publ_key.x, public_key.x, BT_OCTET32_LEN);
memcpy(p_cb->loc_publ_key.y, public_key.y, BT_OCTET32_LEN);
}{...}
smp_debug_print_nbyte_little_endian (p_cb->private_key, (const UINT8 *)"private",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.x, (const UINT8 *)"local public(x)",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.y, (const UINT8 *)"local public(y)",
BT_OCTET32_LEN);
p_cb->flags |= SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY;
smp_sm_event(p_cb, SMP_LOC_PUBL_KEY_CRTD_EVT, NULL);
}{ ... }
/* ... */
void smp_compute_dhkey (tSMP_CB *p_cb)
{
Point peer_publ_key, new_publ_key;
BT_OCTET32 private_key;
SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
memcpy(peer_publ_key.x, p_cb->peer_publ_key.x, BT_OCTET32_LEN);
memcpy(peer_publ_key.y, p_cb->peer_publ_key.y, BT_OCTET32_LEN);
ECC_PointMult(&new_publ_key, &peer_publ_key, (DWORD *) private_key, KEY_LENGTH_DWORDS_P256);
memcpy(p_cb->dhkey, new_publ_key.x, BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const UINT8 *)"Old DHKey",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->private_key, (const UINT8 *)"private",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.x, (const UINT8 *)"rem public(x)",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.y, (const UINT8 *)"rem public(y)",
BT_OCTET32_LEN);
smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const UINT8 *)"Reverted DHKey",
BT_OCTET32_LEN);
}{ ... }
/* ... */
void smp_calculate_local_commitment(tSMP_CB *p_cb)
{
UINT8 random_input;
SMP_TRACE_DEBUG("%s\n", __FUNCTION__);
switch (p_cb->selected_association_model) {
case SMP_MODEL_SEC_CONN_JUSTWORKS:
case SMP_MODEL_SEC_CONN_NUM_COMP:
if (p_cb->role == HCI_ROLE_MASTER) {
SMP_TRACE_WARNING ("local commitment calc on master is not expected \
for Just Works/Numeric Comparison models\n");
}{...}
smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand, 0,
p_cb->commitment);
break;...
case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
random_input = smp_calculate_random_input(p_cb->local_random, p_cb->round);
smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
random_input, p_cb->commitment);
break;...
case SMP_MODEL_SEC_CONN_OOB:
SMP_TRACE_WARNING ("local commitment calc is expected for OOB model BEFORE pairing\n");
smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->loc_publ_key.x, p_cb->local_random, 0,
p_cb->commitment);
break;...
default:
SMP_TRACE_ERROR("Association Model = %d is not used in LE SC\n",
p_cb->selected_association_model);
return;...
}{...}
SMP_TRACE_EVENT ("local commitment calculation is completed");
}{ ... }
/* ... */
void smp_calculate_peer_commitment(tSMP_CB *p_cb, BT_OCTET16 output_buf)
{
UINT8 ri;
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
switch (p_cb->selected_association_model) {
case SMP_MODEL_SEC_CONN_JUSTWORKS:
case SMP_MODEL_SEC_CONN_NUM_COMP:
if (p_cb->role == HCI_ROLE_SLAVE) {
SMP_TRACE_WARNING ("peer commitment calc on slave is not expected \
for Just Works/Numeric Comparison models\n");
}{...}
smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, 0,
output_buf);
break;...
case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
ri = smp_calculate_random_input(p_cb->peer_random, p_cb->round);
smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, ri,
output_buf);
break;...
case SMP_MODEL_SEC_CONN_OOB:
smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->peer_publ_key.x, p_cb->peer_random, 0,
output_buf);
break;...
default:
SMP_TRACE_ERROR("Association Model = %d is not used in LE SC\n",
p_cb->selected_association_model);
return;...
}{...}
SMP_TRACE_EVENT ("peer commitment calculation is completed\n");
}{ ... }
/* ... */
void smp_calculate_f4(UINT8 *u, UINT8 *v, UINT8 *x, UINT8 z, UINT8 *c)
{
UINT8 msg_len = BT_OCTET32_LEN + BT_OCTET32_LEN + 1 ;
UINT8 msg[BT_OCTET32_LEN + BT_OCTET32_LEN + 1];
UINT8 key[BT_OCTET16_LEN];
UINT8 cmac[BT_OCTET16_LEN];
UINT8 *p = NULL;
#if SMP_DEBUG == TRUE
UINT8 *p_prnt = NULL;
#endif
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_prnt = u;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"U", BT_OCTET32_LEN);
p_prnt = v;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"V", BT_OCTET32_LEN);
p_prnt = x;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"X", BT_OCTET16_LEN);
p_prnt = &z;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Z", 1);/* ... */
#endif
p = msg;
UINT8_TO_STREAM(p, z);
ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
#if SMP_DEBUG == TRUE
p_prnt = msg;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", msg_len);/* ... */
#endif
p = key;
ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_prnt = key;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);/* ... */
#endif
aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
#if SMP_DEBUG == TRUE
p_prnt = cmac;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES_CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = c;
ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
}{ ... }
/* ... */
void smp_calculate_numeric_comparison_display_number(tSMP_CB *p_cb,
tSMP_INT_DATA *p_data)
{
SMP_TRACE_DEBUG ("%s", __func__);
if (p_cb->role == HCI_ROLE_MASTER) {
p_cb->number_to_display =
smp_calculate_g2(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
p_cb->rrand);
}{...} else {
p_cb->number_to_display =
smp_calculate_g2(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand,
p_cb->rand);
}{...}
if (p_cb->number_to_display >= (BTM_MAX_PASSKEY_VAL + 1)) {
UINT8 reason;
reason = p_cb->failure = SMP_PAIR_FAIL_UNKNOWN;
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
return;
}{...}
SMP_TRACE_EVENT("Number to display in numeric comparison = %d", p_cb->number_to_display);
p_cb->cb_evt = SMP_NC_REQ_EVT;
smp_sm_event(p_cb, SMP_SC_DSPL_NC_EVT, &p_cb->number_to_display);
return;
}{ ... }
/* ... */
UINT32 smp_calculate_g2(UINT8 *u, UINT8 *v, UINT8 *x, UINT8 *y)
{
UINT8 msg_len = BT_OCTET32_LEN + BT_OCTET32_LEN
+ BT_OCTET16_LEN ;
UINT8 msg[BT_OCTET32_LEN + BT_OCTET32_LEN + BT_OCTET16_LEN];
UINT8 key[BT_OCTET16_LEN];
UINT8 cmac[BT_OCTET16_LEN];
UINT8 *p = NULL;
UINT32 vres;
#if SMP_DEBUG == TRUE
UINT8 *p_prnt = NULL;
#endif
SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
p = msg;
ARRAY_TO_STREAM(p, y, BT_OCTET16_LEN);
ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
#if SMP_DEBUG == TRUE
p_prnt = u;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"U", BT_OCTET32_LEN);
p_prnt = v;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"V", BT_OCTET32_LEN);
p_prnt = x;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"X", BT_OCTET16_LEN);
p_prnt = y;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Y", BT_OCTET16_LEN);/* ... */
#endif
p = key;
ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_prnt = key;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);/* ... */
#endif
if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
return (BTM_MAX_PASSKEY_VAL + 1);
}{...}
#if SMP_DEBUG == TRUE
p_prnt = cmac;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = &cmac[0];
STREAM_TO_UINT32(vres, p);
#if SMP_DEBUG == TRUE
p_prnt = (UINT8 *) &vres;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"cmac mod 2**32", 4);/* ... */
#endif
while (vres > BTM_MAX_PASSKEY_VAL) {
vres -= (BTM_MAX_PASSKEY_VAL + 1);
}{...}
#if SMP_DEBUG == TRUE
p_prnt = (UINT8 *) &vres;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"cmac mod 2**32 mod 10**6", 4);/* ... */
#endif
SMP_TRACE_ERROR("Value for numeric comparison = %d", vres);
return vres;
}{ ... }
/* ... */
BOOLEAN smp_calculate_f5(UINT8 *w, UINT8 *n1, UINT8 *n2, UINT8 *a1, UINT8 *a2,
UINT8 *mac_key, UINT8 *ltk)
{
BT_OCTET16 t;
#if SMP_DEBUG == TRUE
UINT8 *p_prnt = NULL;
#endif
/* ... */
UINT8 counter_mac_key[1] = {0};
UINT8 counter_ltk[1] = {1};
/* ... */
UINT8 key_id[4] = {0x65, 0x6c, 0x74, 0x62};
/* ... */
UINT8 length[2] = {0x00, 0x01};
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_prnt = w;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"W", BT_OCTET32_LEN);
p_prnt = n1;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N1", BT_OCTET16_LEN);
p_prnt = n2;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N2", BT_OCTET16_LEN);
p_prnt = a1;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A1", 7);
p_prnt = a2;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *) "A2", 7);/* ... */
#endif
if (!smp_calculate_f5_key(w, t)) {
SMP_TRACE_ERROR("%s failed to calc T", __FUNCTION__);
return FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_prnt = t;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"T", BT_OCTET16_LEN);/* ... */
#endif
if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_mac_key, key_id, n1, n2, a1, a2,
length, mac_key)) {
SMP_TRACE_ERROR("%s failed to calc MacKey", __FUNCTION__);
return FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_prnt = mac_key;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"MacKey", BT_OCTET16_LEN);/* ... */
#endif
if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_ltk, key_id, n1, n2, a1, a2,
length, ltk)) {
SMP_TRACE_ERROR("%s failed to calc LTK", __FUNCTION__);
return FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_prnt = ltk;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"LTK", BT_OCTET16_LEN);/* ... */
#endif
return TRUE;
}{ ... }
/* ... */
BOOLEAN smp_calculate_f5_mackey_or_long_term_key(UINT8 *t, UINT8 *counter,
UINT8 *key_id, UINT8 *n1, UINT8 *n2, UINT8 *a1, UINT8 *a2,
UINT8 *length, UINT8 *mac)
{
UINT8 *p = NULL;
UINT8 cmac[BT_OCTET16_LEN];
UINT8 key[BT_OCTET16_LEN];
UINT8 msg_len = 1 + 4 +
BT_OCTET16_LEN + BT_OCTET16_LEN +
7 + 7 + 2 ;
UINT8 msg[1 + 4 + BT_OCTET16_LEN + BT_OCTET16_LEN + 7 + 7 + 2];
BOOLEAN ret = TRUE;
#if SMP_DEBUG == TRUE
UINT8 *p_prnt = NULL;
#endif
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_prnt = t;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"T", BT_OCTET16_LEN);
p_prnt = counter;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Counter", 1);
p_prnt = key_id;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"KeyID", 4);
p_prnt = n1;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N1", BT_OCTET16_LEN);
p_prnt = n2;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N2", BT_OCTET16_LEN);
p_prnt = a1;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A1", 7);
p_prnt = a2;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A2", 7);
p_prnt = length;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Length", 2);/* ... */
#endif
p = key;
ARRAY_TO_STREAM(p, t, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_prnt = key;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);/* ... */
#endif
p = msg;
ARRAY_TO_STREAM(p, length, 2);
ARRAY_TO_STREAM(p, a2, 7);
ARRAY_TO_STREAM(p, a1, 7);
ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
ARRAY_TO_STREAM(p, key_id, 4);
ARRAY_TO_STREAM(p, counter, 1);
#if SMP_DEBUG == TRUE
p_prnt = msg;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", msg_len);/* ... */
#endif
if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
ret = FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_prnt = cmac;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = mac;
ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
return ret;
}{ ... }
/* ... */
BOOLEAN smp_calculate_f5_key(UINT8 *w, UINT8 *t)
{
UINT8 *p = NULL;
/* ... */
BT_OCTET16 salt = {
0xBE, 0x83, 0x60, 0x5A, 0xDB, 0x0B, 0x37, 0x60,
0x38, 0xA5, 0xF5, 0xAA, 0x91, 0x83, 0x88, 0x6C
}{...};
#if SMP_DEBUG == TRUE
UINT8 *p_prnt = NULL;
#endif
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_prnt = salt;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"salt", BT_OCTET16_LEN);
p_prnt = w;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"W", BT_OCTET32_LEN);/* ... */
#endif
BT_OCTET16 key;
BT_OCTET32 msg;
p = key;
ARRAY_TO_STREAM(p, salt, BT_OCTET16_LEN);
p = msg;
ARRAY_TO_STREAM(p, w, BT_OCTET32_LEN);
#if SMP_DEBUG == TRUE
p_prnt = key;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);
p_prnt = msg;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", BT_OCTET32_LEN);/* ... */
#endif
BT_OCTET16 cmac;
BOOLEAN ret = TRUE;
if (!aes_cipher_msg_auth_code(key, msg, BT_OCTET32_LEN, BT_OCTET16_LEN, cmac)) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
ret = FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_prnt = cmac;
smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = t;
ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
return ret;
}{ ... }
/* ... */
void smp_calculate_local_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UINT8 iocap[3], a[7], b[7];
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
smp_calculate_f5_mackey_and_long_term_key(p_cb);
smp_collect_local_io_capabilities(iocap, p_cb);
smp_collect_local_ble_address(a, p_cb);
smp_collect_peer_ble_address(b, p_cb);
smp_calculate_f6(p_cb->mac_key, p_cb->rand, p_cb->rrand, p_cb->peer_random, iocap, a, b,
p_cb->dhkey_check);
SMP_TRACE_EVENT ("local DHKey check calculation is completed");
}{ ... }
/* ... */
void smp_calculate_peer_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
{
UINT8 iocap[3], a[7], b[7];
BT_OCTET16 param_buf;
BOOLEAN ret;
tSMP_KEY key;
tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
smp_collect_peer_io_capabilities(iocap, p_cb);
smp_collect_local_ble_address(a, p_cb);
smp_collect_peer_ble_address(b, p_cb);
ret = smp_calculate_f6(p_cb->mac_key, p_cb->rrand, p_cb->rand, p_cb->local_random, iocap,
b, a, param_buf);
if (ret) {
SMP_TRACE_EVENT ("peer DHKey check calculation is completed");
#if (SMP_DEBUG == TRUE)
smp_debug_print_nbyte_little_endian (param_buf, (const UINT8 *)"peer DHKey check",
BT_OCTET16_LEN);/* ... */
#endif
key.key_type = SMP_KEY_TYPE_PEER_DHK_CHCK;
key.p_data = param_buf;
smp_sm_event(p_cb, SMP_SC_KEY_READY_EVT, &key);
}{...} else {
SMP_TRACE_EVENT ("peer DHKey check calculation failed");
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
}{...}
}{ ... }
/* ... */
BOOLEAN smp_calculate_f6(UINT8 *w, UINT8 *n1, UINT8 *n2, UINT8 *r, UINT8 *iocap, UINT8 *a1,
UINT8 *a2, UINT8 *c)
{
UINT8 *p = NULL;
UINT8 msg_len = BT_OCTET16_LEN + BT_OCTET16_LEN +
BT_OCTET16_LEN + 3 + 7
+ 7 ;
UINT8 msg[BT_OCTET16_LEN + BT_OCTET16_LEN + BT_OCTET16_LEN + 3 + 7 + 7];
#if SMP_DEBUG == TRUE
UINT8 *p_print = NULL;
#endif
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_print = w;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"W", BT_OCTET16_LEN);
p_print = n1;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"N1", BT_OCTET16_LEN);
p_print = n2;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"N2", BT_OCTET16_LEN);
p_print = r;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"R", BT_OCTET16_LEN);
p_print = iocap;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"IOcap", 3);
p_print = a1;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"A1", 7);
p_print = a2;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"A2", 7);/* ... */
#endif
UINT8 cmac[BT_OCTET16_LEN];
UINT8 key[BT_OCTET16_LEN];
p = key;
ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_print = key;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"K", BT_OCTET16_LEN);/* ... */
#endif
p = msg;
ARRAY_TO_STREAM(p, a2, 7);
ARRAY_TO_STREAM(p, a1, 7);
ARRAY_TO_STREAM(p, iocap, 3);
ARRAY_TO_STREAM(p, r, BT_OCTET16_LEN);
ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_print = msg;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"M", msg_len);/* ... */
#endif
BOOLEAN ret = TRUE;
if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
ret = FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_print = cmac;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = c;
ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
return ret;
}{ ... }
/* ... */
BOOLEAN smp_calculate_link_key_from_long_term_key(tSMP_CB *p_cb)
{
tBTM_SEC_DEV_REC *p_dev_rec;
BD_ADDR bda_for_lk;
tBLE_ADDR_TYPE conn_addr_type;
SMP_TRACE_DEBUG ("%s", __func__);
if (p_cb->id_addr_rcvd && p_cb->id_addr_type == BLE_ADDR_PUBLIC) {
SMP_TRACE_DEBUG ("Use rcvd identity address as BD_ADDR of LK rcvd identity address");
memcpy(bda_for_lk, p_cb->id_addr, BD_ADDR_LEN);
}{...} else if ((BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda_for_lk, &conn_addr_type)) &&
conn_addr_type == BLE_ADDR_PUBLIC) {
SMP_TRACE_DEBUG ("Use rcvd connection address as BD_ADDR of LK");
}{...} else {
SMP_TRACE_WARNING ("Don't have peer public address to associate with LK");
return FALSE;
}{...}
if ((p_dev_rec = btm_find_dev (p_cb->pairing_bda)) == NULL) {
SMP_TRACE_ERROR("%s failed to find Security Record", __func__);
return FALSE;
}{...}
BT_OCTET16 intermediate_link_key;
BOOLEAN ret = TRUE;
ret = smp_calculate_h6(p_cb->ltk, (UINT8 *)"1pmt" , intermediate_link_key);
if (!ret) {
SMP_TRACE_ERROR("%s failed to derive intermediate_link_key", __func__);
return ret;
}{...}
BT_OCTET16 link_key;
ret = smp_calculate_h6(intermediate_link_key, (UINT8 *) "rbel" , link_key);
if (!ret) {
SMP_TRACE_ERROR("%s failed", __func__);
}{...} else {
UINT8 link_key_type;
if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
}{...} else if (controller_get_interface()->supports_secure_connections()) {
if (p_cb->sec_level == SMP_SEC_AUTHENTICATED) {
link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
}{...} else {
link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB_P_256;
}{...}
}{...} else if (btm_cb.security_mode == BTM_SEC_MODE_SP) {
if (p_cb->sec_level == SMP_SEC_AUTHENTICATED) {
link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
}{...} else {
link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
}{...}
}{...} else {
SMP_TRACE_ERROR ("%s failed to update link_key. Sec Mode = %d, sm4 = 0x%02x",
__func__, btm_cb.security_mode, p_dev_rec->sm4);
return FALSE;
}{...}
link_key_type += BTM_LTK_DERIVED_LKEY_OFFSET;
UINT8 *p;
BT_OCTET16 notif_link_key;
p = notif_link_key;
ARRAY16_TO_STREAM(p, link_key);
btm_sec_link_key_notification (bda_for_lk, notif_link_key, link_key_type);
SMP_TRACE_EVENT ("%s is completed", __func__);
}{...}
return ret;
}{ ... }
/* ... */
BOOLEAN smp_calculate_long_term_key_from_link_key(tSMP_CB *p_cb)
{
BOOLEAN ret = TRUE;
tBTM_SEC_DEV_REC *p_dev_rec;
UINT8 rev_link_key[16];
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
if ((p_dev_rec = btm_find_dev (p_cb->pairing_bda)) == NULL) {
SMP_TRACE_ERROR("%s failed to find Security Record", __FUNCTION__);
return FALSE;
}{...}
UINT8 br_link_key_type;
if ((br_link_key_type = BTM_SecGetDeviceLinkKeyType (p_cb->pairing_bda))
== BTM_LKEY_TYPE_IGNORE) {
SMP_TRACE_ERROR("%s failed to retrieve BR link type", __FUNCTION__);
return FALSE;
}{...}
if ((br_link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256) &&
(br_link_key_type != BTM_LKEY_TYPE_UNAUTH_COMB_P_256)) {
SMP_TRACE_ERROR("%s LE SC LTK can't be derived from LK %d",
__FUNCTION__, br_link_key_type);
return FALSE;
}{...}
UINT8 *p1;
UINT8 *p2;
p1 = rev_link_key;
p2 = p_dev_rec->link_key;
REVERSE_ARRAY_TO_STREAM(p1, p2, 16);
BT_OCTET16 intermediate_long_term_key;
ret = smp_calculate_h6(rev_link_key, (UINT8 *) "2pmt" ,
intermediate_long_term_key);
if (!ret) {
SMP_TRACE_ERROR("%s failed to derive intermediate_long_term_key", __FUNCTION__);
return ret;
}{...}
ret = smp_calculate_h6(intermediate_long_term_key, (UINT8 *) "elrb" ,
p_cb->ltk);
if (!ret) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
}{...} else {
p_cb->sec_level = (br_link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)
? SMP_SEC_AUTHENTICATED : SMP_SEC_UNAUTHENTICATE;
SMP_TRACE_EVENT ("%s is completed", __FUNCTION__);
}{...}
return ret;
}{ ... }
/* ... */
BOOLEAN smp_calculate_h6(UINT8 *w, UINT8 *keyid, UINT8 *c)
{
#if SMP_DEBUG == TRUE
UINT8 *p_print = NULL;
#endif
SMP_TRACE_DEBUG ("%s", __FUNCTION__);
#if SMP_DEBUG == TRUE
p_print = w;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"W", BT_OCTET16_LEN);
p_print = keyid;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"keyID", 4);/* ... */
#endif
UINT8 *p = NULL;
UINT8 key[BT_OCTET16_LEN];
p = key;
ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
#if SMP_DEBUG == TRUE
p_print = key;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"K", BT_OCTET16_LEN);/* ... */
#endif
UINT8 msg_len = 4 ;
UINT8 msg[4];
p = msg;
ARRAY_TO_STREAM(p, keyid, 4);
#if SMP_DEBUG == TRUE
p_print = msg;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *) "M", msg_len);/* ... */
#endif
BOOLEAN ret = TRUE;
UINT8 cmac[BT_OCTET16_LEN];
if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
SMP_TRACE_ERROR("%s failed", __FUNCTION__);
ret = FALSE;
}{...}
#if SMP_DEBUG == TRUE
p_print = cmac;
smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);/* ... */
#endif
p = c;
ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
return ret;
}{ ... }
/* ... */
void smp_start_nonce_generation(tSMP_CB *p_cb)
{
SMP_TRACE_DEBUG("%s", __FUNCTION__);
p_cb->rand_enc_proc_state = SMP_GEN_NONCE_0_7;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_finish_nonce_generation(tSMP_CB *p_cb)
{
SMP_TRACE_DEBUG("%s", __FUNCTION__);
p_cb->rand_enc_proc_state = SMP_GEN_NONCE_8_15;
if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
smp_rand_back(NULL);
}{...}
}{ ... }
/* ... */
void smp_process_new_nonce(tSMP_CB *p_cb)
{
SMP_TRACE_DEBUG ("%s round %d", __FUNCTION__, p_cb->round);
smp_sm_event(p_cb, SMP_HAVE_LOC_NONCE_EVT, NULL);
}{ ... }
/* ... */
static void smp_rand_back(tBTM_RAND_ENC *p)
{
tSMP_CB *p_cb = &smp_cb;
UINT8 *pp = p->param_buf;
UINT8 failure = SMP_PAIR_FAIL_UNKNOWN;
UINT8 state = p_cb->rand_enc_proc_state & ~0x80;
SMP_TRACE_DEBUG ("%s state=0x%x", __FUNCTION__, state);
if (p && p->status == HCI_SUCCESS) {
switch (state) {
case SMP_GEN_SRAND_MRAND:
memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
smp_generate_rand_cont(p_cb, NULL);
break;
...
case SMP_GEN_SRAND_MRAND_CONT:
memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
smp_generate_confirm(p_cb, NULL);
break;
...
case SMP_GEN_DIV_LTK:
STREAM_TO_UINT16(p_cb->div, pp);
smp_generate_ltk_cont(p_cb, NULL);
break;
...
case SMP_GEN_DIV_CSRK:
STREAM_TO_UINT16(p_cb->div, pp);
smp_compute_csrk(p_cb, NULL);
break;
...
case SMP_GEN_TK:
smp_proc_passkey(p_cb, p);
break;
...
case SMP_GEN_RAND_V:
memcpy(p_cb->enc_rand, p->param_buf, BT_OCTET8_LEN);
smp_generate_y(p_cb, NULL);
break;
...
case SMP_GENERATE_PRIVATE_KEY_0_7:
case SMP_GENERATE_PRIVATE_KEY_8_15:
case SMP_GENERATE_PRIVATE_KEY_16_23:
case SMP_GENERATE_PRIVATE_KEY_24_31:
smp_continue_private_key_creation(p_cb, p);
break;
...
case SMP_GEN_NONCE_0_7:
memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
smp_finish_nonce_generation(p_cb);
break;
...
case SMP_GEN_NONCE_8_15:
memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
smp_process_new_nonce(p_cb);
break;...
}{...}
return;
}{...}
SMP_TRACE_ERROR("%s key generation failed: (%d)", __FUNCTION__, p_cb->rand_enc_proc_state);
smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
}{ ... }
/* ... */#endif