hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
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
780
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
807
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
864
865
866
867
868
869
870
871
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
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
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
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
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
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
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
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
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
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
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
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
1840
1841
1842
1843
1844
1845
1846
1847
1848
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
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
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
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
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
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
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
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
/*
 *   bcmwpa.c - shared WPA-related functions
 *
 * Broadcom Proprietary and Confidential. Copyright (C) 2020,
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom;
 * the contents of this file may not be disclosed to third parties,
 * copied or duplicated in any form, in whole or in part, without
 * the prior written permission of Broadcom.
 *
 *
 * <<Broadcom-WL-IPTag/Proprietary:>>
 */
 
/* include wl driver config file if this file is compiled for driver */
#ifdef BCMDRIVER
#include <osl.h>
/* HACK: this case for external supplicant use */
#else
#include <string.h>
#if defined(BCMEXTSUP)
#include <bcm_osl.h>
#else
#ifndef ASSERT
#define ASSERT(exp)
#endif
#endif /* BCMEXTSUP */
#endif /* BCMDRIVER */
 
#include <ethernet.h>
#include <eapol.h>
#include <802.11.h>
#include <wpa.h>
#include <802.11r.h>
 
#include <bcmutils.h>
#include <bcmendian.h>
#include <bcmwpa.h>
#include <aeskeywrap.h>
 
#include <bcmstdlib_s.h>
 
#include <wlioctl.h>
 
#include <bcmutils.h>
#include <bcmwpa.h>
#ifdef WL_OCV
#include <bcm_ocv.h>
#endif /* WL_OCV */
 
#if defined(BCMSUP_PSK) || defined(WLFBT) || defined(BCMAUTH_PSK) || \
   defined(WL_OKC) || defined(WLTDLS) || defined(GTKOE) || defined(WLHOSTFBT)
#ifdef WLHOSTFBT
#include <string.h>
#endif
#endif /* defined(BCMSUP_PSK) || defined(WLFBT) || defined(BCMAUTH_PSK) ||
   * defined(WL_OKC) || defined(WLTDLS) || defined(GTKOE) || defined(WLHOSTFBT)
   */
 
/* prefix strings */
#define PMK_NAME_PFX "PMK Name"
#define FT_PTK_PFX "FT-PTK"
#define FT_R0_PFX "FT-R0"
#define FT_R0N_PFX "FT-R0N"
#define FT_R1_PFX "FT-R1"
#define FT_R1N_PFX "FT-R1N"
#define WPA_PTK_PFX "Pairwise key expansion"
#define TDLS_PMK_PFX "TDLS PMK"
/* end prefix strings */
 
#ifndef BIT
#define BIT(x) (1 << (x))
#endif
 
#define PRF_PREFIXES_NUM    5u
 
typedef struct key_length_entry {
   uint8 suite;
   uint8 len;
} key_length_entry_t;
 
/* EAPOL key(PMK/KCK/KEK/TK) length lookup tables */
static const key_length_entry_t eapol_pmk_len[] = {
   {RSN_AKM_SUITEB_SHA384_1X, EAPOL_WPA_PMK_SHA384_LEN},
   {RSN_AKM_FBT_SHA384_1X, EAPOL_WPA_PMK_SHA384_LEN},
   {RSN_AKM_FBT_SHA384_PSK, EAPOL_WPA_PMK_SHA384_LEN},
   {0u, EAPOL_WPA_PMK_DEFAULT_LEN} /* default */
};
 
static const key_length_entry_t eapol_kck_mic_len[] = {
   {RSN_AKM_SUITEB_SHA384_1X, EAPOL_WPA_KCK_MIC_SHA384_LEN},
   {RSN_AKM_FILS_SHA256, 0u},
   {RSN_AKM_FILS_SHA384, 0u},
   {RSN_AKM_FBT_SHA256_FILS, EAPOL_WPA_KCK_MIC_DEFAULT_LEN},
   {RSN_AKM_FBT_SHA384_FILS, EAPOL_WPA_KCK2_SHA384_LEN},
   {RSN_AKM_OWE, EAPOL_WPA_KCK_MIC_DEFAULT_LEN},
   {RSN_AKM_FBT_SHA384_1X, EAPOL_WPA_KCK_MIC_SHA384_LEN},
   {RSN_AKM_FBT_SHA384_PSK, EAPOL_WPA_KCK_MIC_SHA384_LEN},
   {0u, EAPOL_WPA_KCK_MIC_DEFAULT_LEN} /* default */
};
 
static const key_length_entry_t eapol_kck_len[] = {
   {RSN_AKM_SUITEB_SHA384_1X, EAPOL_WPA_KCK_SHA384_LEN},
   {RSN_AKM_FILS_SHA256, 0u},
   {RSN_AKM_FILS_SHA384, 0u},
   {RSN_AKM_FBT_SHA256_FILS, 0u},
   {RSN_AKM_FBT_SHA384_FILS, 0u},
   {RSN_AKM_OWE, EAPOL_WPA_KCK_DEFAULT_LEN},
   {RSN_AKM_FBT_SHA384_1X, EAPOL_WPA_KCK_SHA384_LEN},
   {RSN_AKM_FBT_SHA384_PSK, EAPOL_WPA_KCK_SHA384_LEN},
   {0u, EAPOL_WPA_KCK_DEFAULT_LEN} /* default */
};
 
static const key_length_entry_t eapol_kek_len[] = {
   {RSN_AKM_FILS_SHA384, EAPOL_WPA_ENCR_KEY_MAX_LEN},
   {RSN_AKM_FBT_SHA384_FILS, EAPOL_WPA_ENCR_KEY_MAX_LEN},
   {RSN_AKM_SUITEB_SHA384_1X, EAPOL_WPA_ENCR_KEY_MAX_LEN / 2},
   {RSN_AKM_FILS_SHA256, EAPOL_WPA_ENCR_KEY_MAX_LEN / 2},
   {RSN_AKM_FBT_SHA256_FILS, EAPOL_WPA_ENCR_KEY_MAX_LEN / 2},
   {RSN_AKM_OWE, EAPOL_WPA_ENCR_KEY_DEFAULT_LEN},
   {RSN_AKM_FBT_SHA384_1X, EAPOL_WPA_ENCR_KEY_MAX_LEN / 2},
   {RSN_AKM_FBT_SHA384_PSK, EAPOL_WPA_ENCR_KEY_MAX_LEN / 2},
   {0u, EAPOL_WPA_ENCR_KEY_DEFAULT_LEN} /* default */
};
 
static const key_length_entry_t eapol_tk_len[] = {
   {WPA_CIPHER_CCMP_256, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN},
   {WPA_CIPHER_AES_GCM256, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN},
   {WPA_CIPHER_BIP_GMAC_256, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN},
   {WPA_CIPHER_BIP_CMAC_256, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN},
   {WPA_CIPHER_AES_CCM, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN / 2},
   {WPA_CIPHER_AES_GCM, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN / 2},
   {WPA_CIPHER_BIP_GMAC_128, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN / 2},
   {WPA_CIPHER_TKIP, EAPOL_WPA_TEMP_ENCR_KEY_MAX_LEN},
   {0u, 0u} /* default */
};
 
#if defined(WL_FILS) && defined(WLFBT)
static const key_length_entry_t eapol_kck2_len[] = {
   {RSN_AKM_FBT_SHA256_FILS, EAPOL_WPA_KCK2_SHA256_LEN},
   {RSN_AKM_FBT_SHA384_FILS, EAPOL_WPA_KCK2_SHA384_LEN},
   {0u, 0u} /* default */
};
 
static const key_length_entry_t eapol_kek2_len[] = {
   {RSN_AKM_FBT_SHA256_FILS, EAPOL_WPA_KEK2_SHA256_LEN},
   {RSN_AKM_FBT_SHA384_FILS, EAPOL_WPA_KEK2_SHA384_LEN},
   {0u, 0u} /* default */
};
#endif /* WL_FILS && WLFBT */
 
typedef struct key_length_lookup {
   const eapol_key_type_t key;
   const key_length_entry_t *key_entry;
} key_length_lookup_t;
 
static const key_length_lookup_t eapol_key_lookup_tbl[] = {
   {EAPOL_KEY_PMK, eapol_pmk_len},
   {EAPOL_KEY_KCK_MIC, eapol_kck_mic_len},
   {EAPOL_KEY_KCK, eapol_kck_len},
   {EAPOL_KEY_KEK, eapol_kek_len},
   {EAPOL_KEY_TK, eapol_tk_len},
#if defined(WL_FILS) && defined(WLFBT)
   {EAPOL_KEY_KCK2, eapol_kck2_len},
   {EAPOL_KEY_KEK2, eapol_kek2_len},
#endif /* WL_FILS && WLFBT */
};
 
typedef struct rsn_akm_lookup_entry {
   const rsn_akm_t rsn_akm;
   const sha2_hash_type_t hash_type;
} rsn_akm_lookup_entry_t;
 
static const rsn_akm_lookup_entry_t rsn_akm_lookup_tbl[] = {
   {RSN_AKM_NONE, HASH_SHA1},
   {RSN_AKM_UNSPECIFIED, HASH_SHA1},
   {RSN_AKM_PSK, HASH_SHA1},
   {RSN_AKM_FBT_1X, HASH_SHA256},
   {RSN_AKM_FBT_PSK, HASH_SHA256},
   {RSN_AKM_MFP_1X, HASH_SHA256},
   {RSN_AKM_MFP_PSK, HASH_SHA256},
   {RSN_AKM_SHA256_1X, HASH_SHA256},
   {RSN_AKM_SHA256_PSK, HASH_SHA256},
   {RSN_AKM_TPK, HASH_SHA256},
   {RSN_AKM_SAE_PSK, HASH_SHA256},
   {RSN_AKM_SAE_FBT, HASH_SHA256},
   {RSN_AKM_SUITEB_SHA256_1X, HASH_SHA256},
   {RSN_AKM_SUITEB_SHA384_1X, HASH_SHA384},
   {RSN_AKM_FBT_SHA384_1X, HASH_SHA384},
   {RSN_AKM_FILS_SHA256, HASH_SHA256},
   {RSN_AKM_FILS_SHA384, HASH_SHA384},
   {RSN_AKM_FBT_SHA256_FILS, HASH_SHA256},
   {RSN_AKM_FBT_SHA384_FILS, HASH_SHA384},
   {RSN_AKM_OWE, HASH_SHA256},
   {RSN_AKM_FBT_SHA384_PSK, HASH_SHA384},
   {RSN_AKM_PSK_SHA384, HASH_SHA384},
};
 
typedef struct rsn_akm_cipher_match_entry {
   uint16  akm_type;
   uint32    u_cast; /* BITMAP */
   uint32    m_cast; /* BITMAP */
   uint32    g_mgmt; /* BITMAP */
} rsn_akm_cipher_match_entry_t;
 
/* list only explicit cipher restriction for given AKM (e.g SuiteB)
 * refer to 802.11 spec 9.4.2.24.3
 * If not listed here, it means no restriction in using any ciphers.
 */
static const rsn_akm_cipher_match_entry_t rsn_akm_cipher_match_table[] = {
   {RSN_AKM_SUITEB_SHA256_1X,
   BCM_BIT(WPA_CIPHER_AES_GCM),
   BCM_BIT(WPA_CIPHER_AES_GCM),
   BCM_BIT(WPA_CIPHER_BIP_GMAC_128)},
   {RSN_AKM_SUITEB_SHA384_1X,
   BCM_BIT(WPA_CIPHER_AES_GCM256) | BCM_BIT(WPA_CIPHER_CCMP_256),
   BCM_BIT(WPA_CIPHER_AES_GCM256) | BCM_BIT(WPA_CIPHER_AES_GCM256),
   BCM_BIT(WPA_CIPHER_BIP_GMAC_256) | BCM_BIT(WPA_CIPHER_BIP_CMAC_256)},
   {RSN_AKM_FBT_SHA384_1X,
   BCM_BIT(WPA_CIPHER_AES_GCM256) | BCM_BIT(WPA_CIPHER_CCMP_256),
   BCM_BIT(WPA_CIPHER_AES_GCM256) | BCM_BIT(WPA_CIPHER_AES_GCM256),
   BCM_BIT(WPA_CIPHER_BIP_GMAC_256) | BCM_BIT(WPA_CIPHER_BIP_CMAC_256)}
};
 
#if defined(WL_BAND6G)
static const rsn_akm_mask_t rsn_akm_6g_inval_mask =
   BCM_BIT(RSN_AKM_PSK) |
   BCM_BIT(RSN_AKM_FBT_PSK) |
   BCM_BIT(RSN_AKM_SHA256_PSK) |
   BCM_BIT(RSN_AKM_FBT_SHA384_PSK) |
   BCM_BIT(RSN_AKM_PSK_SHA384);
 
static const rsn_ciphers_t cipher_6g_inval_mask =
   BCM_BIT(WPA_CIPHER_NONE) |
   BCM_BIT(WPA_CIPHER_WEP_40) |
   BCM_BIT(WPA_CIPHER_TKIP) |
   BCM_BIT(WPA_CIPHER_WEP_104);
#endif /* WL_BAND6G */
 
#if defined(BCMSUP_PSK) || defined(BCMSUPPL)
typedef struct group_cipher_algo_entry {
   rsn_cipher_t g_mgmt_cipher;
   uint8 bip_algo;
} group_cipher_algo_entry_t;
 
static const group_cipher_algo_entry_t group_mgmt_cipher_algo[] = {
   {WPA_CIPHER_BIP_GMAC_256, CRYPTO_ALGO_BIP_GMAC256},
   {WPA_CIPHER_BIP_CMAC_256, CRYPTO_ALGO_BIP_CMAC256},
   {WPA_CIPHER_BIP_GMAC_128, CRYPTO_ALGO_BIP_GMAC},
   {WPA_CIPHER_BIP, CRYPTO_ALGO_BIP},
};
#endif /* defined(BCMSUP_PSK) || defined(BCMSUPPL) */
 
static uint16 wlc_calc_rsn_desc_version(const rsn_ie_info_t *rsn_info);
static int bcmwpa_is_valid_akm(const rsn_akm_t akm);
#if defined(BCMSUP_PSK) || defined(BCMAUTH_PSK) || defined(WLFBT) || defined(GTKOE)
static sha2_hash_type_t bcmwpa_rsn_akm_to_hash(const rsn_akm_t akm);
#ifdef RSN_IE_INFO_STRUCT_RELOCATED
static int bcmwpa_decode_cipher_suite(rsn_ie_info_t *info, const uint8 **ptr, uint ie_len, uint
   *remain_len, uint16 *p_count);
#endif
#endif /* defined(BCMSUP_PSK) || defined(BCMAUTH_PSK) || defined(WLFBT) || defined(GTKOE) */
#if defined(BCMSUP_PSK) || defined(WLFBT) || defined(WL_OKC) || defined(WLHOSTFBT)
#include <rc4.h>
 
/* calculate wpa PMKID: HMAC-SHA1-128(PMK, "PMK Name" | AA | SPA) */
static void
wpa_calc_pmkid_impl(sha2_hash_type_t hash_type,
   const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *pmk, uint pmk_len, uint8 *pmkid)
{
   int err;
   hmac_sha2_ctx_t ctx;
 
   err = hmac_sha2_init(&ctx, hash_type, pmk, pmk_len);
   if (err != BCME_OK)
       goto done;
   hmac_sha2_update(&ctx, (const uint8 *)PMK_NAME_PFX, sizeof(PMK_NAME_PFX) - 1);
   hmac_sha2_update(&ctx, (const uint8 *)auth_ea, ETHER_ADDR_LEN);
   hmac_sha2_update(&ctx, (const uint8 *)sta_ea, ETHER_ADDR_LEN);
   hmac_sha2_final(&ctx, pmkid, WPA2_PMKID_LEN);
done:;
}
 
void
wpa_calc_pmkid(const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *pmk, uint pmk_len, uint8 *pmkid)
{
   wpa_calc_pmkid_impl(HASH_SHA1, auth_ea, sta_ea, pmk, pmk_len, pmkid);
}
 
void
kdf_calc_pmkid(const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *key, uint key_len, uint8 *pmkid, rsn_ie_info_t *rsn_info)
{
   sha2_hash_type_t hash_type;
 
   if (rsn_info->sta_akm == RSN_AKM_SUITEB_SHA384_1X) {
       hash_type = HASH_SHA384;
   } else {
       hash_type = HASH_SHA256;
   }
 
   wpa_calc_pmkid_impl(hash_type, auth_ea, sta_ea, key, key_len, pmkid);
}
 
#if defined(WLFBT) || defined(WLHOSTFBT)
void
wpa_calc_pmkR0(sha2_hash_type_t hash_type, const uint8 *ssid, uint ssid_len,
   uint16 mdid, const uint8 *r0kh, uint r0kh_len, const struct ether_addr *sta_ea,
   const uint8 *pmk, uint pmk_len, uint8 *pmkr0, uint8 *pmkr0name)
{
   uint8 out[FBT_R0KH_ID_LEN + WPA2_PMKID_LEN - 1];
   int out_len = FBT_R0KH_ID_LEN - 1;
   bcm_const_xlvp_t pfx[7];
   bcm_const_xlvp_t pfx2[2];
   int npfx = 0;
   int npfx2 = 0;
   uint8 mdid_le[2];
   uint8 pfx_ssid_len;
   uint8 pfx_r0kh_len;
 
   if (hash_type == HASH_SHA384) {
       out_len += WPA2_PMKID_LEN;
   }
 
   /* create prefixes for pmkr0 */
   pfx[npfx].len = sizeof(FT_R0_PFX) - 1;
   pfx[npfx++].data = (uint8 *)FT_R0_PFX;
 
   /* ssid length and ssid */
   pfx_ssid_len = ssid_len & 0xff;
   pfx[npfx].len = (uint16)sizeof(pfx_ssid_len);
   pfx[npfx++].data = &pfx_ssid_len;
 
   pfx[npfx].len = (uint16)(ssid_len & 0xffff);
   pfx[npfx++].data = ssid;
 
   /* mdid */
   htol16_ua_store(mdid, mdid_le);
   pfx[npfx].len = sizeof(mdid_le);
   pfx[npfx++].data = mdid_le;
 
   /* r0kh len and r0kh */
   pfx_r0kh_len = r0kh_len & 0xff;
   pfx[npfx].len = sizeof(pfx_r0kh_len);
   pfx[npfx++].data = &pfx_r0kh_len;
 
   pfx[npfx].len = (uint16)(r0kh_len & 0xffff);
   pfx[npfx++].data = r0kh;
 
   /* sta addr */
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *)sta_ea;
 
   hmac_sha2_n(hash_type, pmk, pmk_len, pfx, npfx, NULL, 0, out, out_len);
   (void)memcpy_s(pmkr0, pmk_len, out, pmk_len);
 
   /* coverity checks overflow if pfx size changes */
 
   /* create prefixes for pmkr0 name */
   pfx2[npfx2].len = sizeof(FT_R0N_PFX) - 1;
   pfx2[npfx2++].data = (uint8 *)FT_R0N_PFX;
   pfx2[npfx2].len = WPA2_PMKID_LEN;
   pfx2[npfx2++].data = &out[pmk_len];
 
   (void)sha2(hash_type, pfx2, npfx2, NULL, 0, pmkr0name, WPA2_PMKID_LEN);
}
 
void
wpa_calc_pmkR1(sha2_hash_type_t hash_type, const struct ether_addr *r1kh,
   const struct ether_addr *sta_ea, const uint8 *pmk, uint pmk_len, const uint8 *pmkr0name,
   uint8 *pmkr1, uint8 *pmkr1name)
{
   bcm_const_xlvp_t pfx[3];
   bcm_const_xlvp_t pfx2[4];
   int npfx = 0;
   int npfx2 = 0;
 
   if (!pmkr1 && !pmkr1name)
       goto done;
   else if (!pmkr1)
       goto calc_r1name;
 
   /* create prefixes for pmkr1 */
   pfx[npfx].len = sizeof(FT_R1_PFX) - 1;
   pfx[npfx++].data = (uint8 *)FT_R1_PFX;
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *)r1kh;
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *)sta_ea;
 
   hmac_sha2_n(hash_type, pmk, pmk_len, pfx, npfx, NULL, 0,
       pmkr1, sha2_digest_len(hash_type));
 
calc_r1name:
   /* create prefixes for pmkr1 name */
   pfx2[npfx2].len = sizeof(FT_R1N_PFX) - 1;
   pfx2[npfx2++].data = (uint8 *)FT_R1N_PFX;
 
   pfx2[npfx2].len = WPA2_PMKID_LEN;
   pfx2[npfx2++].data = pmkr0name;
 
   pfx2[npfx2].len = ETHER_ADDR_LEN;
   pfx2[npfx2++].data = (const uint8 *)r1kh;
 
   pfx2[npfx2].len = ETHER_ADDR_LEN;
   pfx2[npfx2++].data = (const uint8 *)sta_ea;
 
   sha2(hash_type, pfx2, npfx2, NULL, 0, pmkr1name, WPA2_PMKID_LEN);
done:;
}
 
void
wpa_calc_ft_ptk(sha2_hash_type_t hash_type,
   const struct ether_addr *bssid, const struct ether_addr *sta_ea,
   const uint8 *anonce, const uint8* snonce,
   const uint8 *pmk, uint pmk_len, uint8 *ptk, uint ptk_len)
{
   bcm_const_xlvp_t pfx[5];
   int npfx = 0;
 
   /* FT-PTK||SNONCE||ANONCE||BSSID||STA Addr */
 
   pfx[npfx].len = sizeof(FT_PTK_PFX) - 1;
   pfx[npfx++].data = (uint8 *)FT_PTK_PFX;
 
   pfx[npfx].len = EAPOL_WPA_KEY_NONCE_LEN;
   pfx[npfx++].data = snonce;
 
   pfx[npfx].len = EAPOL_WPA_KEY_NONCE_LEN;
   pfx[npfx++].data = anonce;
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *)bssid;
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *)sta_ea;
 
   hmac_sha2_n(hash_type, pmk, pmk_len, pfx, npfx, NULL, 0, ptk, ptk_len);
}
 
void
wpa_derive_pmkR1_name(sha2_hash_type_t hash_type,
   struct ether_addr *r1kh, struct ether_addr *sta_ea,
   uint8 *pmkr0name, uint8 *pmkr1name)
{
   wpa_calc_pmkR1(hash_type, r1kh, sta_ea, NULL /* pmk */, 0,
       pmkr0name, NULL /* pmkr1 */, pmkr1name);
}
#endif /* WLFBT || WLHOSTFBT */
#endif /* BCMSUP_PSK || WLFBT || WL_OKC */
 
#if defined(BCMSUP_PSK) || defined(GTKOE) || defined(BCMAUTH_PSK) || defined(WLFBT)
/* Decrypt a key data from a WPA key message */
int
wpa_decr_key_data(eapol_wpa_key_header_t *body, uint16 key_info, uint8 *ekey,
   uint8 *encrkey, rc4_ks_t *rc4key, const rsn_ie_info_t *rsn_info, uint16 *dec_len)
{
   uint16 len;
   int err = BCME_OK;
   uint8 *key_data;
 
   switch (key_info & (WPA_KEY_DESC_V1 | WPA_KEY_DESC_V2)) {
   case WPA_KEY_DESC_V1:
       err = memcpy_s(encrkey, EAPOL_WPA_KEY_IV_LEN + EAPOL_WPA_ENCR_KEY_MAX_LEN,
           body->iv, EAPOL_WPA_KEY_IV_LEN);
       if (err) {
           ASSERT(0);
           return err;
       }
       err = memcpy_s(&encrkey[EAPOL_WPA_KEY_IV_LEN], EAPOL_WPA_ENCR_KEY_MAX_LEN,
           ekey, rsn_info->kek_len);
       if (err) {
           ASSERT(0);
           return err;
       }
       /* decrypt the key data */
       prepare_key(encrkey, EAPOL_WPA_KEY_IV_LEN + rsn_info->kek_len, rc4key);
       rc4(NULL, WPA_KEY_DATA_LEN_256, rc4key); /* dump 256 bytes */
       len = ntoh16_ua(EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body, rsn_info->kck_mic_len));
       key_data = EAPOL_WPA_KEY_HDR_DATA_PTR(body, rsn_info->kck_mic_len);
       rc4(key_data, len, rc4key);
       break;
 
   case WPA_KEY_DESC_V2:
   case WPA_KEY_DESC_V3:
   case WPA_KEY_DESC_V0:
       /* fallthrough */
       len = ntoh16_ua(EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body, rsn_info->kck_mic_len));
       if (!len) {
           *dec_len = 0;
           break; /* ignore zero length */
       }
       key_data = EAPOL_WPA_KEY_HDR_DATA_PTR(body, rsn_info->kck_mic_len);
       if (aes_unwrap(rsn_info->kek_len, ekey, len, key_data, key_data)) {
           *dec_len = 0;
           err = BCME_DECERR;
           break;
       }
       *dec_len = (len > AKW_BLOCK_LEN) ? (len - AKW_BLOCK_LEN) : 0;
       break;
 
   default:
       *dec_len = 0;
       err = BCME_UNSUPPORTED; /* may need revisiting - see 802.11-2016 */
       break;
   }
 
   return err;
}
 
/* internal function - assumes enouch space allocated, retuns written number */
static int
wpa_calc_ptk_prefixes(const uint8 *prefix, uint prefix_len,
   const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *anonce, uint8 anonce_len, const uint8 *snonce, uint8 snonce_len,
   bcm_const_xlvp_t *pfx)
{
   int npfx = 0;
   const uint8 *nonce;
 
   /* prefix || min ea || max ea || min nonce || max nonce */
   pfx[npfx].len = (uint16)(prefix_len & 0xffff);
   pfx[npfx++].data = prefix;
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *) wpa_array_cmp(MIN_ARRAY,
       (const uint8 *)auth_ea, (const uint8 *)sta_ea, ETHER_ADDR_LEN);
 
   pfx[npfx].len = ETHER_ADDR_LEN;
   pfx[npfx++].data = (const uint8 *) wpa_array_cmp(MAX_ARRAY,
       (const uint8 *)auth_ea, (const uint8 *)sta_ea, ETHER_ADDR_LEN);
 
   nonce = (const uint8 *)wpa_array_cmp(MIN_ARRAY, snonce, anonce, snonce_len);
 
   if (nonce == snonce) {
       pfx[npfx].len = snonce_len;
       pfx[npfx++].data = snonce;
       pfx[npfx].len = anonce_len;
       pfx[npfx++].data = anonce;
   } else {
       pfx[npfx].len = anonce_len;
       pfx[npfx++].data = anonce;
       pfx[npfx].len = snonce_len;
       pfx[npfx++].data = snonce;
   }
 
   return npfx;
}
 
void
kdf_calc_ptk(const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *anonce, const uint8* snonce,
   const uint8 *pmk, uint pmk_len, uint8 *ptk, uint ptk_len)
{
   bcm_const_xlvp_t pfx[5];
   int npfx;
 
   /* note: kdf omits trailing NULL in prefix */
   npfx = wpa_calc_ptk_prefixes((uint8 *)WPA_PTK_PFX, sizeof(WPA_PTK_PFX) - 1,
       auth_ea, sta_ea, anonce, EAPOL_WPA_KEY_NONCE_LEN, snonce,
       EAPOL_WPA_KEY_NONCE_LEN, pfx);
   hmac_sha2_n(HASH_SHA256, pmk, pmk_len, pfx, npfx, NULL, 0, ptk, ptk_len);
}
#endif    /* BCMSUP_PSK || GTKOE || BCMAUTH_PSK || WLFBT */
 
#if defined(BCMSUP_PSK) || defined(BCMAUTH_PSK) || defined(WLFBT) || defined(GTKOE)
/* Compute Message Integrity Code (MIC) over EAPOL message */
int
wpa_make_mic(eapol_header_t *eapol, uint key_desc, uint8 *mic_key,
   rsn_ie_info_t *rsn_info, uchar *mic, uint mic_len)
{
   uint data_len;
   int err = BCME_OK;
   sha2_hash_type_t type = HASH_NONE;
 
   /* length of eapol pkt from the version field on */
   data_len = 4 + ntoh16_ua((uint8 *)&eapol->length);
 
   /* Create the MIC for the pkt */
   switch (key_desc) {
       case WPA_KEY_DESC_V1:
           type = HASH_MD5;
           break;
       case WPA_KEY_DESC_V2:
           /* note: transparent truncation to mic_len */
           type = HASH_SHA1;
           break;
       case WPA_KEY_DESC_V3:
           aes_cmac_calc(NULL, 0, &eapol->version, data_len, mic_key,
               mic_len, mic, AES_BLOCK_SZ);
           goto exit;
       case WPA_KEY_DESC_V0:
           ASSERT(rsn_info != NULL);
           if (rsn_info == NULL) {
               return BCME_BADARG;
           }
           if (IS_SAE_AKM(rsn_info->sta_akm)) {
               aes_cmac_calc(NULL, 0, &eapol->version, data_len, mic_key,
                   mic_len, mic, AES_BLOCK_SZ);
                   goto exit;
           }
           type = bcmwpa_rsn_akm_to_hash(rsn_info->sta_akm);
           break;
       default:
           /* 11mc D8.0 some AKMs use descriptor version 0 */
           err = BCME_UNSUPPORTED;
           goto exit;
   }
 
   if (type) {
       err = hmac_sha2(type, mic_key, mic_len, NULL, 0, (uint8 *)&eapol->version, data_len,
           mic, mic_len);
   }
exit:
   return err;
}
 
int
wpa_calc_ptk(rsn_akm_t akm, const struct ether_addr *auth_ea, const struct ether_addr *sta_ea,
   const uint8 *anonce, uint8 anon_len, const uint8 *snonce, uint8 snon_len, const uint8 *pmk,
   uint pmk_len, uint8 *ptk, uint ptk_len)
{
   bcm_const_xlvp_t pfx[PRF_PREFIXES_NUM];
   int npfx;
   int ret = BCME_OK;
   sha2_hash_type_t hash_type;
   uint label_len;
 
   if (RSN_AKM_USE_KDF(akm)) {
       label_len = sizeof(WPA_PTK_PFX) - 1u;
   } else { //WPA AKMS
       label_len = sizeof(WPA_PTK_PFX); /* note: wpa needs trailing NULL in prefix */
   }
 
   hash_type = bcmwpa_rsn_akm_to_hash(akm);
 
   npfx = wpa_calc_ptk_prefixes((uint8 *)WPA_PTK_PFX, label_len,
       auth_ea, sta_ea, anonce, anon_len, snonce, snon_len, pfx);
   ret = hmac_sha2_n(hash_type, pmk, pmk_len, pfx, npfx, NULL, 0, ptk, ptk_len);
   return ret;
}
 
bool
wpa_encr_key_data(eapol_wpa_key_header_t *body, uint16 key_info, uint8 *ekey,
   uint8 *gtk, uint8 *data, uint8 *encrkey, rc4_ks_t *rc4key, const rsn_ie_info_t *rsn_info)
{
   uint16 len;
   uint8 *key_data;
 
   switch (key_info & (WPA_KEY_DESC_V1 | WPA_KEY_DESC_V2)) {
       case WPA_KEY_DESC_V1:
           if (gtk) {
               len = ntoh16_ua((uint8 *)&body->key_len);
           } else {
               len = ntoh16_ua(EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body,
                   rsn_info->kck_mic_len));
           }
 
           /* create the iv/ptk key */
           if (memcpy_s(encrkey, EAPOL_WPA_KEY_IV_LEN, body->iv, sizeof(body->iv))) {
               return FALSE;
           }
           if (memcpy_s(&encrkey[EAPOL_WPA_KEY_IV_LEN], EAPOL_WPA_ENCR_KEY_DEFAULT_LEN,
               ekey, EAPOL_WPA_ENCR_KEY_DEFAULT_LEN)) {
               return FALSE;
           }
           /* encrypt the key data */
           prepare_key(encrkey, EAPOL_WPA_KEY_IV_LEN + EAPOL_WPA_ENCR_KEY_DEFAULT_LEN,
               rc4key);
           rc4(data, WPA_KEY_DATA_LEN_256, rc4key); /* dump 256 bytes */
           key_data = EAPOL_WPA_KEY_HDR_DATA_PTR(body, rsn_info->kck_mic_len);
           rc4(key_data, len, rc4key);
           break;
       case WPA_KEY_DESC_V2: /* fall through */
       case WPA_KEY_DESC_V3:
       case WPA_KEY_DESC_V0:
           len = ntoh16_ua(EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body,
                   rsn_info->kck_mic_len));
           /* FIXME: data_len is length to encrypt, but need to make sure
            * buffer is big enought
            * for expansion.  how?  problem for caller?
            */
           key_data = EAPOL_WPA_KEY_HDR_DATA_PTR(body, rsn_info->kck_mic_len);
           /* pad if needed - min. 16 bytes, 8 byte aligned */
           /* padding is 0xdd followed by 0's */
           if (len < 2u *AKW_BLOCK_LEN) {
               key_data[len] = WPA2_KEY_DATA_PAD;
               bzero(&key_data[len + 1u], 2u * AKW_BLOCK_LEN - (len + 1u));
               len = 2u *AKW_BLOCK_LEN;
           } else if (len % AKW_BLOCK_LEN) {
               key_data[len] = WPA2_KEY_DATA_PAD;
               bzero(&key_data[len + 1u],
                   AKW_BLOCK_LEN - ((len + 1u) % AKW_BLOCK_LEN));
               len += AKW_BLOCK_LEN - (len % AKW_BLOCK_LEN);
           }
           if (aes_wrap(rsn_info->kek_len, ekey, len, key_data, key_data)) {
               return FALSE;
           }
           len += AKW_BLOCK_LEN;
           hton16_ua_store(len,
               (uint8 *)EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body,
               rsn_info->kck_mic_len));
           break;
       default:
           /* 11mc D8.0 key descriptor version 0 used */
           return FALSE;
   }
 
   return TRUE;
}
 
/* Check MIC of EAPOL message */
bool
wpa_check_mic(eapol_header_t *eapol, uint key_desc, uint8 *mic_key, rsn_ie_info_t *rsn_info)
{
   eapol_wpa_key_header_t *body = NULL;
   uchar digest[SHA2_MAX_DIGEST_LEN];
   uchar mic[EAPOL_WPA_KEY_MAX_MIC_LEN];
 
   if (!mic_key || !rsn_info || !eapol) {
       return FALSE;
   }
 
   body = (eapol_wpa_key_header_t *)eapol->body;
 
#ifndef EAPOL_KEY_HDR_VER_V2
   if (rsn_info->kck_mic_len != EAPOL_WPA_KCK_DEFAULT_LEN)
#else
   if (rsn_info->kck_mic_len > EAPOL_WPA_KEY_MAX_MIC_LEN)
#endif /* EAPOL_KEY_HDR_VER_V2 */
   {
       ASSERT(0);
       return FALSE;
   }
   /* save MIC and clear its space in message */
   if (memcpy_s(mic, sizeof(mic), EAPOL_WPA_KEY_HDR_MIC_PTR(body),
       rsn_info->kck_mic_len)) {
       return FALSE;
   }
   bzero(EAPOL_WPA_KEY_HDR_MIC_PTR(body), rsn_info->kck_mic_len);
   if (wpa_make_mic(eapol, key_desc, mic_key, rsn_info, digest, rsn_info->kck_mic_len)
       != BCME_OK) {
       return FALSE;
   }
   return !memcmp(digest, mic, rsn_info->kck_mic_len);
}
 
static sha2_hash_type_t bcmwpa_rsn_akm_to_hash(const rsn_akm_t akm)
{
   uint i = 0;
   sha2_hash_type_t type = HASH_NONE;
 
   for (i = 0; i < ARRAYSIZE(rsn_akm_lookup_tbl); i++) {
       if (akm == rsn_akm_lookup_tbl[i].rsn_akm) {
           type = rsn_akm_lookup_tbl[i].hash_type;
           break;
       }
   }
   return type;
}
#endif /* BCMSUP_PSK || BCMAUTH_PSK  || WLFBT || GTKOE */
 
#ifdef WLTDLS
void
wpa_calc_tpk(const struct ether_addr *init_ea, const struct ether_addr *resp_ea,
   const struct ether_addr *bssid, const uint8 *anonce, const uint8* snonce,
   uint8 *tpk, uint tpk_len)
{
   uint8 pmk[SHA2_MAX_DIGEST_LEN];
   uint pmk_len;
   bcm_const_xlvp_t ikpfx[2];
   int nikpfx = 0;
   bcm_const_xlvp_t  tpkpfx[4];
   int ntpkpfx = 0;
 
   pmk_len = sha2_digest_len(HASH_SHA256);
 
   /* compute pmk to use - using anonce and snonce  - min and then max */
   ikpfx[nikpfx].len = EAPOL_WPA_KEY_NONCE_LEN;
   ikpfx[nikpfx++].data = wpa_array_cmp(MIN_ARRAY, snonce, anonce,
                       EAPOL_WPA_KEY_NONCE_LEN),
 
   ikpfx[nikpfx].len = EAPOL_WPA_KEY_NONCE_LEN;
   ikpfx[nikpfx++].data = wpa_array_cmp(MAX_ARRAY, snonce, anonce,
                       EAPOL_WPA_KEY_NONCE_LEN),
 
   (void)sha2(HASH_SHA256, ikpfx, nikpfx, NULL, 0, pmk, SHA2_SHA256_DIGEST_LEN);
 
   /* compute the tpk - using prefix, min ea, max ea, bssid */
   tpkpfx[ntpkpfx].len = sizeof(TDLS_PMK_PFX) - 1;
   tpkpfx[ntpkpfx++].data = (const uint8 *)TDLS_PMK_PFX;
 
   tpkpfx[ntpkpfx].len = ETHER_ADDR_LEN;
   tpkpfx[ntpkpfx++].data = wpa_array_cmp(MIN_ARRAY, (const uint8 *)init_ea,
       (const uint8 *)resp_ea, ETHER_ADDR_LEN),
 
   tpkpfx[ntpkpfx].len = ETHER_ADDR_LEN;
   tpkpfx[ntpkpfx++].data = wpa_array_cmp(MAX_ARRAY, (const uint8 *)init_ea,
       (const uint8 *)resp_ea, ETHER_ADDR_LEN),
 
   tpkpfx[ntpkpfx].len = ETHER_ADDR_LEN;
   tpkpfx[ntpkpfx++].data = (const uint8 *)bssid;
 
   (void)hmac_sha2_n(HASH_SHA256, pmk, pmk_len, tpkpfx, ntpkpfx, NULL, 0, tpk, tpk_len);
}
#endif /* WLTDLS */
 
/* Convert WPA/WPA2 IE cipher suite to locally used value */
static bool
rsn_cipher(wpa_suite_t *suite, ushort *cipher, const uint8 *std_oui, bool wep_ok)
{
   bool ret = TRUE;
 
   if (!memcmp((const char *)suite->oui, std_oui, DOT11_OUI_LEN)) {
       switch (suite->type) {
       case WPA_CIPHER_TKIP:
           *cipher = CRYPTO_ALGO_TKIP;
           break;
       case WPA_CIPHER_AES_CCM:
           *cipher = CRYPTO_ALGO_AES_CCM;
           break;
       case WPA_CIPHER_AES_GCM:
           *cipher = CRYPTO_ALGO_AES_GCM;
           break;
       case WPA_CIPHER_AES_GCM256:
           *cipher = CRYPTO_ALGO_AES_GCM256;
           break;
       case WPA_CIPHER_WEP_40:
           if (wep_ok)
               *cipher = CRYPTO_ALGO_WEP1;
           else
               ret = FALSE;
           break;
       case WPA_CIPHER_WEP_104:
           if (wep_ok)
               *cipher = CRYPTO_ALGO_WEP128;
           else
               ret = FALSE;
           break;
       default:
           ret = FALSE;
           break;
       }
       return ret;
   }
 
   return FALSE;
}
 
bool
wpa_cipher(wpa_suite_t *suite, ushort *cipher, bool wep_ok)
{
   return rsn_cipher(suite, cipher, (const uchar*)WPA_OUI, wep_ok);
}
 
bool
wpa2_cipher(wpa_suite_t *suite, ushort *cipher, bool wep_ok)
{
   return rsn_cipher(suite, cipher, (const uchar*)WPA2_OUI, wep_ok);
}
 
/* Is any of the tlvs the expected entry? If
 * not update the tlvs buffer pointer/length.
 */
bool
bcm_has_ie(uint8 *ie, uint8 **tlvs, uint *tlvs_len, const uint8 *oui, uint oui_len, uint8 type)
{
   /* If the contents match the OUI and the type */
   if (ie[TLV_LEN_OFF] >= oui_len + 1 &&
       !memcmp(&ie[TLV_BODY_OFF], oui, oui_len) &&
       type == ie[TLV_BODY_OFF + oui_len]) {
       return TRUE;
   }
 
   /* point to the next ie */
   ie += ie[TLV_LEN_OFF] + TLV_HDR_LEN;
   /* calculate the length of the rest of the buffer */
   *tlvs_len -= (uint)(ie - *tlvs);
   /* update the pointer to the start of the buffer */
   *tlvs = ie;
 
   return FALSE;
}
 
wpa_ie_fixed_t *
bcm_find_wpaie(uint8 *parse, uint len)
{
   return (wpa_ie_fixed_t *) bcm_find_ie(parse, len, DOT11_MNG_VS_ID,
       WPA_OUI_LEN, (const char*) WPA_OUI, WPA_OUI_TYPE);
}
 
int
bcm_find_security_ies(uint8 *buf, uint buflen, void **wpa_ie,
       void **rsn_ie)
{
   bcm_tlv_t *tlv = NULL;
   uint totlen = 0;
   uint8 *end = NULL;
   uint len = 0;
   uint tlvs_len = 0;
   uint8 *tlvs = NULL;
 
   if ((tlv = (bcm_tlv_t*)buf) == NULL ||
           !wpa_ie || !rsn_ie || buflen == 0) {
       return BCME_BADARG;
   }
 
   totlen = buflen;
   *rsn_ie = *wpa_ie = NULL;
   end = buf;
   end += buflen;
 
   /* find rsn ie and wpa ie */
   while (totlen >= TLV_HDR_LEN) {
       len = tlv->len;
       tlvs_len = buflen;
       tlvs = buf;
 
       /* check if tlv overruns buffer */
       if (totlen < (len + TLV_HDR_LEN)) {
           return BCME_BUFTOOSHORT;
       }
 
       /* validate remaining totlen */
       if (totlen >= (len + TLV_HDR_LEN)) {
           if ((*rsn_ie == NULL) && (tlv->id == DOT11_MNG_RSN_ID)) {
               *rsn_ie = tlv;
           } else if ((*wpa_ie == NULL) && (tlv->id == DOT11_MNG_VS_ID)) {
               /* if vendor ie, check if its wpa ie */
               if (bcm_is_wpa_ie((uint8 *)tlv, &tlvs, &tlvs_len))
                   *wpa_ie = tlv;
           }
       }
 
       if (*rsn_ie && *wpa_ie)
           break;
 
       tlv = (bcm_tlv_t*)((uint8*)tlv + (len + TLV_HDR_LEN));
       totlen -= (len + TLV_HDR_LEN);
 
       if (totlen > buflen) {
           return BCME_BUFTOOLONG;
       }
 
       if ((uint8 *)tlv > end) {
           return BCME_BUFTOOSHORT;
       }
 
   }
 
   if (*wpa_ie || *rsn_ie)
       return BCME_OK;
   else
       return BCME_NOTFOUND;
}
 
bcm_tlv_t *
bcm_find_wmeie(uint8 *parse, uint len, uint8 subtype, uint8 subtype_len)
{
   bcm_tlv_t *ie;
   if ((ie = bcm_find_ie(parse, len, DOT11_MNG_VS_ID, WME_OUI_LEN,
       (const char*) WME_OUI, WME_OUI_TYPE))) {
       uint ie_len = TLV_HDR_LEN + ie->len;
       wme_ie_t *ie_data = (wme_ie_t *)ie->data;
       /* the subtype_len must include OUI+type+subtype */
       if (subtype_len > WME_OUI_LEN + 1 &&
           ie_len == (uint)TLV_HDR_LEN + subtype_len &&
           ie_data->subtype == subtype) {
           return ie;
       }
       /* move to next IE */
       len -= (uint)((uint8 *)ie + ie_len - parse);
       parse = (uint8 *)ie + ie_len;
   }
   return NULL;
}
 
wps_ie_fixed_t *
bcm_find_wpsie(const uint8 *parse, uint len)
{
   uint8 type = WPS_OUI_TYPE;
 
   return (wps_ie_fixed_t *)bcm_find_vendor_ie(parse, len, WPS_OUI, &type, sizeof(type));
}
 
/* locate the Attribute in the WPS IE */
/* assume the caller has validated the WPS IE tag and length */
wps_at_fixed_t *
bcm_wps_find_at(wps_at_fixed_t *at, uint len, uint16 id)
{
   while ((int)len >= WPS_AT_FIXED_LEN) {
       uint alen = WPS_AT_FIXED_LEN + ntoh16_ua(((wps_at_fixed_t *)at)->len);
       if (ntoh16_ua(((wps_at_fixed_t *)at)->at) == id && alen <= len)
           return at;
       at = (wps_at_fixed_t *)((uint8 *)at + alen);
       len -= alen;
   }
   return NULL;
}
 
#ifdef WLP2P
wifi_p2p_ie_t *
bcm_find_p2pie(const uint8 *parse, uint len)
{
   uint8 type = P2P_OUI_TYPE;
 
   return (wifi_p2p_ie_t *)bcm_find_vendor_ie(parse, len, P2P_OUI, &type, sizeof(type));
}
#endif
 
bcm_tlv_t *
bcm_find_hs20ie(uint8 *parse, uint len)
{
   return bcm_find_ie(parse, len, DOT11_MNG_VS_ID, WFA_OUI_LEN,
       (const char *)WFA_OUI, WFA_OUI_TYPE_HS20);
}
 
bcm_tlv_t *
bcm_find_osenie(uint8 *parse, uint len)
{
   return bcm_find_ie(parse, len, DOT11_MNG_VS_ID, WFA_OUI_LEN,
       (const char *) WFA_OUI, WFA_OUI_TYPE_OSEN);
}
 
#if defined(BCMSUP_PSK) || defined(BCMSUPPL) || defined(GTKOE) || defined(WL_FILS)
#define wpa_is_kde(ie, tlvs, len, type)    bcm_has_ie(ie, tlvs, len, \
   (const uint8 *)WPA2_OUI, WPA2_OUI_LEN, type)
 
eapol_wpa2_encap_data_t *
wpa_find_kde(const uint8 *parse, uint len, uint8 type)
{
   return (eapol_wpa2_encap_data_t *) bcm_find_ie(parse, len,
       DOT11_MNG_PROPR_ID, WPA2_OUI_LEN, (const char *) WPA2_OUI, type);
}
 
bool
wpa_is_gtk_encap(uint8 *ie, uint8 **tlvs, uint *tlvs_len)
{
   return wpa_is_kde(ie, tlvs, tlvs_len, WPA2_KEY_DATA_SUBTYPE_GTK);
}
 
eapol_wpa2_encap_data_t *
wpa_find_gtk_encap(uint8 *parse, uint len)
{
   eapol_wpa2_encap_data_t *data;
 
   /* minimum length includes kde upto gtk field in eapol_wpa2_key_gtk_encap_t */
   data = wpa_find_kde(parse, len, WPA2_KEY_DATA_SUBTYPE_GTK);
   if (data && (data->length < EAPOL_WPA2_GTK_ENCAP_MIN_LEN)) {
       data = NULL;
   }
 
   return data;
}
 
int
wpa_find_eapol_kde_data(eapol_header_t* eapol, uint8 eapol_mic_len,
   uint8 subtype, eapol_wpa2_encap_data_t **out_data)
{
   eapol_wpa_key_header_t *body;
   uint8 *parse;
   uint16 body_len;
   uint16 data_len;
 
   if (!eapol) {
       return BCME_BADARG;
   }
 
   body = (eapol_wpa_key_header_t *)eapol->body;
   body_len = ntoh16_ua(&eapol->length);
 
   data_len = ntoh16_ua(EAPOL_WPA_KEY_HDR_DATA_LEN_PTR(body,
           eapol_mic_len));
 
   parse = EAPOL_WPA_KEY_HDR_DATA_PTR(body, eapol_mic_len);
 
   if (((uint8 *)body + body_len) < ((uint8 *)parse + data_len)) {
       return BCME_BUFTOOSHORT;
   }
 
   return wpa_find_kde_data(parse, data_len, subtype, out_data);
}
 
int
wpa_find_kde_data(const uint8 *kde_buf, uint16 buf_len,
   uint8 subtype, eapol_wpa2_encap_data_t **out_data)
{
   eapol_wpa2_encap_data_t *data;
   uint8 min_len;
 
   if (!kde_buf) {
       return BCME_BADARG;
   }
 
   /* minimum length includes kde upto gtk field in eapol_wpa2_key_gtk_encap_t */
   data = wpa_find_kde(kde_buf, buf_len, subtype);
   if (!data) {
       return BCME_IE_NOTFOUND;
   }
 
   switch (subtype) {
   case WPA2_KEY_DATA_SUBTYPE_GTK:
       min_len = EAPOL_WPA2_GTK_ENCAP_MIN_LEN;
       break;
   case WPA2_KEY_DATA_SUBTYPE_IGTK:
       min_len = EAPOL_WPA2_BIGTK_ENCAP_MIN_LEN;
       break;
   case WPA2_KEY_DATA_SUBTYPE_BIGTK:
       min_len = EAPOL_WPA2_IGTK_ENCAP_MIN_LEN;
       break;
#ifdef WL_OCV
   case WPA2_KEY_DATA_SUBTYPE_OCI:
       min_len = EAPOL_WPA2_OCI_ENCAP_MIN_LEN;
       break;
#endif /* WL_OCV */
   default:
       return BCME_UNSUPPORTED;
   }
 
   if (data->length < min_len) {
       return BCME_BADLEN;
   }
 
   *out_data = data;
 
   return BCME_OK;
}
 
#ifdef WL_OCV
bool
wpa_check_ocv_caps(uint16 local_caps, uint16 peer_caps)
{
   bool ocv_enabled =
       ((local_caps & RSN_CAP_OCVC) &&
       (peer_caps & RSN_CAP_OCVC));
   bool mfp_enabled =
       ((peer_caps & RSN_CAP_MFPC) ||
       (peer_caps & RSN_CAP_MFPR));
 
   return (ocv_enabled && mfp_enabled);
}
 
int
wpa_add_oci_encap(chanspec_t chspec, uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   eapol_wpa2_encap_data_t* oci_kde;
   uint len = buf_len;
 
   if (buf_len < WPA_OCV_OCI_KDE_SIZE) {
       retval = BCME_BUFTOOSHORT;
       goto done;
   }
 
   oci_kde = (eapol_wpa2_encap_data_t*)buf;
 
   oci_kde->type = DOT11_MNG_WPA_ID;
   oci_kde->subtype = WPA2_KEY_DATA_SUBTYPE_OCI;
   oci_kde->length = (WPA_OCV_OCI_KDE_SIZE - TLV_HDR_LEN);
 
   oci_kde->oui[0u] = WPA2_OUI[0u];
   oci_kde->oui[1u] = WPA2_OUI[1u];
   oci_kde->oui[2u] = WPA2_OUI[2u];
 
   buf += EAPOL_WPA2_ENCAP_DATA_HDR_LEN;
   len -= EAPOL_WPA2_ENCAP_DATA_HDR_LEN;
 
   retval = bcm_ocv_write_oci(chspec, buf, len);
   if (retval != BCME_OK) {
       goto done;
   }
 
done:
   return retval;
}
 
int
wpa_add_oci_ie(chanspec_t chspec, uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   uint8* oci_buf = buf + BCM_TLV_EXT_HDR_SIZE;
 
   if (buf_len < (bcm_ocv_get_oci_len() + BCM_TLV_EXT_HDR_SIZE)) {
       retval = BCME_BUFTOOSHORT;
       goto done;
   }
 
   retval = bcm_ocv_write_oci(chspec, oci_buf, bcm_ocv_get_oci_len());
   if (retval != BCME_OK) {
       goto done;
   }
 
   (void)bcm_write_tlv_ext(DOT11_MNG_ID_EXT_ID,
       OCV_EXTID_MNG_OCI_ID, oci_buf, bcm_ocv_get_oci_len(), buf);
 
done:
   return retval;
}
 
int
wpa_add_oci_ft_subelem(chanspec_t chspec, uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   uint8* oci_buf = buf + BCM_TLV_HDR_SIZE;
 
   if (buf_len < (bcm_ocv_get_oci_len() + BCM_TLV_HDR_SIZE)) {
       retval = BCME_BUFTOOSHORT;
       goto done;
   }
 
   retval = bcm_ocv_write_oci(chspec, oci_buf, bcm_ocv_get_oci_len());
   if (retval != BCME_OK) {
       goto done;
   }
 
   bcm_write_tlv_safe(DOT11_FBT_SUBELEM_ID_OCI,
       oci_buf, bcm_ocv_get_oci_len(), buf, buf_len);
 
done:
   return retval;
}
 
int wpa_validate_oci_encap(chanspec_t chspec, const uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   eapol_wpa2_encap_data_t *encap = NULL;
 
   retval = wpa_find_kde_data(buf, buf_len, WPA2_KEY_DATA_SUBTYPE_OCI, &encap);
   if (retval != BCME_OK) {
       retval = BCME_NOTFOUND;
       goto done;
   }
 
   retval = bcm_ocv_validate_oci(chspec,
       encap->data, encap->length);
   if (retval != BCME_OK) {
       goto done;
   }
 
done:
   return retval;
}
 
int wpa_validate_oci_ie(chanspec_t chspec, const uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   bcm_tlv_ext_t *oci_ie;
 
   oci_ie = (bcm_tlv_ext_t *)bcm_parse_tlvs_dot11(buf, buf_len,
           OCV_EXTID_MNG_OCI_ID, TRUE);
 
   if (!oci_ie) {
       retval = BCME_NOTFOUND;
       goto done;
   }
 
   retval = bcm_ocv_validate_oci(chspec, oci_ie->data, oci_ie->len);
   if (retval != BCME_OK) {
       goto done;
   }
 
done:
   return retval;
}
 
int wpa_validate_oci_ft_subelem(chanspec_t chspec, const uint8* buf, uint buf_len)
{
   int retval = BCME_OK;
   bcm_tlv_t *oci_ie;
 
   oci_ie = (bcm_tlv_t *)bcm_parse_tlvs_dot11(buf, buf_len,
           DOT11_FBT_SUBELEM_ID_OCI, FALSE);
 
   if (!oci_ie) {
       retval = BCME_NOTFOUND;
       goto done;
   }
 
   retval = bcm_ocv_validate_oci(chspec, oci_ie->data, oci_ie->len);
   if (retval != BCME_OK) {
       goto done;
   }
 
done:
   return retval;
}
#endif /* WL_OCV */
#endif /* defined(BCMSUP_PSK) || defined(BCMSUPPL) || defined(GTKOE) || defined(WL_FILS) */
 
const uint8 *
wpa_array_cmp(int max_array, const uint8 *x, const uint8 *y, uint len)
{
   uint i;
   const uint8 *ret = x;
 
   for (i = 0; i < len; i++)
       if (x[i] != y[i])
           break;
 
   if (i == len) {
       /* returning null will cause crash, return value used for copying */
       /* return first param in this case to close security loophole */
       return x;
   }
   if (max_array && (y[i] > x[i]))
       ret = y;
   if (!max_array && (y[i] < x[i]))
       ret = y;
 
   return (ret);
}
 
void
wpa_incr_array(uint8 *array, uint len)
{
   int i;
 
   for (i = (len-1); i >= 0; i--)
       if (array[i]++ != 0xff) {
           break;
       }
}
 
bool
bcmwpa_akm2WPAauth(uint8 *akm, uint32 *auth, bool sta_iswpa)
{
   uint i;
   oui_akm_wpa_tbl_t wpa_auth_tbl_match[] = {
       {WPA2_OUI, RSN_AKM_NONE, WPA_AUTH_NONE},
       {WPA2_OUI, RSN_AKM_UNSPECIFIED, WPA2_AUTH_UNSPECIFIED},
       {WPA2_OUI, RSN_AKM_PSK, WPA2_AUTH_PSK},
       {WPA2_OUI, RSN_AKM_FBT_1X, WPA2_AUTH_UNSPECIFIED | WPA2_AUTH_FT},
       {WPA2_OUI, RSN_AKM_FBT_PSK, WPA2_AUTH_PSK | WPA2_AUTH_FT},
       {WPA2_OUI, RSN_AKM_SHA256_1X, WPA2_AUTH_1X_SHA256},
       {WPA2_OUI, RSN_AKM_SHA256_PSK, WPA2_AUTH_PSK_SHA256},
       {WPA2_OUI, RSN_AKM_FILS_SHA256, WPA2_AUTH_FILS_SHA256},
       {WPA2_OUI, RSN_AKM_FILS_SHA384, WPA2_AUTH_FILS_SHA384},
       {WPA2_OUI, RSN_AKM_FBT_SHA256_FILS, WPA2_AUTH_FILS_SHA256 | WPA2_AUTH_FT},
       {WPA2_OUI, RSN_AKM_FBT_SHA384_FILS, WPA2_AUTH_FILS_SHA384 | WPA2_AUTH_FT},
       {WPA2_OUI, RSN_AKM_SAE_PSK, WPA3_AUTH_SAE_PSK},
       {WPA2_OUI, RSN_AKM_SAE_FBT, WPA3_AUTH_SAE_PSK | WPA2_AUTH_FT},
       {WPA2_OUI, RSN_AKM_OWE, WPA3_AUTH_OWE},
       {WPA2_OUI, RSN_AKM_SUITEB_SHA256_1X, WPA3_AUTH_1X_SUITE_B_SHA256},
       {WPA2_OUI, RSN_AKM_SUITEB_SHA384_1X, WPA3_AUTH_1X_SUITE_B_SHA384},
       {WFA_OUI, OSEN_AKM_UNSPECIFIED, WPA2_AUTH_UNSPECIFIED},
       {WFA_OUI, RSN_AKM_FBT_SHA256_FILS, WPA2_AUTH_FILS_SHA256 | WPA2_AUTH_FT},
       {WFA_OUI, RSN_AKM_FBT_SHA384_FILS, WPA2_AUTH_FILS_SHA384 | WPA2_AUTH_FT},
       {WFA_OUI, RSN_AKM_DPP, WPA3_AUTH_DPP_AKM},
 
#ifdef BCMWAPI_WAI
       {WAPI_OUI, RSN_AKM_NONE, WAPI_AUTH_NONE},
       {WAPI_OUI, RSN_AKM_UNSPECIFIED, WAPI_AUTH_UNSPECIFIED},
       {WAPI_OUI, RSN_AKM_PSK, WAPI_AUTH_PSK},
#endif /* BCMWAPI_WAI */
 
       {WPA_OUI, RSN_AKM_NONE, WPA_AUTH_NONE},
       {WPA_OUI, RSN_AKM_UNSPECIFIED, WPA_AUTH_UNSPECIFIED},
       {WPA_OUI, RSN_AKM_PSK, WPA_AUTH_PSK},
   };
 
   BCM_REFERENCE(sta_iswpa);
 
   for (i = 0; i < ARRAYSIZE(wpa_auth_tbl_match); i++)  {
       if (!memcmp(akm, wpa_auth_tbl_match[i].oui, DOT11_OUI_LEN)) {
           if (wpa_auth_tbl_match[i].rsn_akm == akm[DOT11_OUI_LEN]) {
               *auth = wpa_auth_tbl_match[i].wpa_auth;
               return TRUE;
           }
       }
   }
   return FALSE;
}
 
/* map cipher suite to internal WSEC_XXXX */
/* cs points 4 byte cipher suite, and only the type is used for non CCX ciphers */
bool
bcmwpa_cipher2wsec(uint8 *cipher, uint32 *wsec)
{
 
#ifdef BCMWAPI_WAI
   if (!memcmp(cipher, WAPI_OUI, DOT11_OUI_LEN)) {
       switch (WAPI_CSE_WPI_2_CIPHER(cipher[DOT11_OUI_LEN])) {
       case WAPI_CIPHER_NONE:
           *wsec = 0;
           break;
       case WAPI_CIPHER_SMS4:
           *wsec = SMS4_ENABLED;
           break;
       default:
           return FALSE;
       }
       return TRUE;
   }
#endif /* BCMWAPI_WAI */
 
   switch (cipher[DOT11_OUI_LEN]) {
   case WPA_CIPHER_NONE:
       *wsec = 0;
       break;
   case WPA_CIPHER_WEP_40:
   case WPA_CIPHER_WEP_104:
       *wsec = WEP_ENABLED;
       break;
   case WPA_CIPHER_TKIP:
       *wsec = TKIP_ENABLED;
       break;
   case WPA_CIPHER_AES_CCM:
       /* fall through */
   case WPA_CIPHER_AES_GCM:
       /* fall through */
   case WPA_CIPHER_AES_GCM256:
       *wsec = AES_ENABLED;
       break;
 
#ifdef BCMWAPI_WAI
   case WAPI_CIPHER_SMS4:
       *wsec = SMS4_ENABLED;
       break;
#endif /* BCMWAPI_WAI */
 
   default:
       return FALSE;
   }
   return TRUE;
}
 
#ifdef RSN_IE_INFO_STRUCT_RELOCATED
/* map WPA/RSN cipher to internal WSEC */
uint32
bcmwpa_wpaciphers2wsec(uint32 wpacipher)
{
   uint32 wsec = 0;
 
   switch (wpacipher) {
   case BCM_BIT(WPA_CIPHER_WEP_40):
       case BCM_BIT(WPA_CIPHER_WEP_104):
           wsec = WEP_ENABLED;
           break;
       case BCM_BIT(WPA_CIPHER_TKIP):
           wsec = TKIP_ENABLED;
           break;
       case BCM_BIT(WPA_CIPHER_AES_OCB):
           /* fall through */
       case BCM_BIT(WPA_CIPHER_AES_CCM):
           wsec = AES_ENABLED;
           break;
       case BCM_BIT(WPA_CIPHER_AES_GCM):
           /* fall through */
       case BCM_BIT(WPA_CIPHER_AES_GCM256):
           wsec = AES_ENABLED;
       break;
 
#ifdef BCMWAPI_WAI
       case BCM_BIT(WAPI_CIPHER_SMS4):
           wsec = SMS4_ENABLED;
           break;
#endif /* BCMWAPI_WAI */
 
       default:
           break;
   }
 
   return wsec;
}
 
uint32
wlc_convert_rsn_to_wsec_bitmap(uint32 ap_cipher_mask)
{
 
   uint32 ap_wsec = 0;
   uint32 tmp_mask = ap_cipher_mask;
   uint32 c;
 
   FOREACH_BIT(c, tmp_mask) {
       ap_wsec |= bcmwpa_wpaciphers2wsec(c);
   }
 
   return ap_wsec;
}
 
#else /* Not RSN_IE_INFO_STRUCT_RELOCATED */
uint32
bcmwpa_wpaciphers2wsec(uint8 wpacipher)
{
   uint32 wsec = 0;
 
   switch (wpacipher) {
   case WPA_CIPHER_NONE:
       break;
   case WPA_CIPHER_WEP_40:
   case WPA_CIPHER_WEP_104:
       wsec = WEP_ENABLED;
       break;
   case WPA_CIPHER_TKIP:
       wsec = TKIP_ENABLED;
       break;
   case WPA_CIPHER_AES_OCB:
       /* fall through */
   case WPA_CIPHER_AES_CCM:
       wsec = AES_ENABLED;
       break;
   case WPA_CIPHER_AES_GCM:
   /* fall through */
   case WPA_CIPHER_AES_GCM256:
       wsec = AES_ENABLED;
       break;
 
#ifdef BCMWAPI_WAI
   case WAPI_CIPHER_SMS4:
       wsec = SMS4_ENABLED;
       break;
#endif /* BCMWAPI_WAI */
 
   default:
       break;
   }
 
   return wsec;
}
#endif /* RSN_IE_INFO_STRUCT_RELOCATED */
 
bool
bcmwpa_is_wpa_auth(uint32 auth)
{
   if ((auth == WPA_AUTH_NONE) ||
      (auth == WPA_AUTH_UNSPECIFIED) ||
      (auth == WPA_AUTH_PSK))
       return TRUE;
   else
       return FALSE;
}
 
bool
bcmwpa_includes_wpa_auth(uint32 auth)
{
   if (auth & (WPA_AUTH_NONE |
       WPA_AUTH_UNSPECIFIED |
       WPA_AUTH_PSK))
       return TRUE;
   else
       return FALSE;
}
 
bool
bcmwpa_is_rsn_auth(uint32 auth)
{
   auth = auth & ~WPA2_AUTH_FT;
 
   if ((auth == WPA2_AUTH_UNSPECIFIED) ||
       (auth == WPA2_AUTH_PSK) ||
       (auth == BRCM_AUTH_PSK) ||
       (auth == WPA2_AUTH_1X_SHA256) ||
       (auth == WPA2_AUTH_PSK_SHA256) ||
       (auth == WPA3_AUTH_SAE_PSK) ||
       (auth == WPA3_AUTH_OWE) ||
       WPA2_AUTH_IS_FILS(auth) ||
       (auth == WPA3_AUTH_1X_SUITE_B_SHA256) ||
       (auth == WPA3_AUTH_1X_SUITE_B_SHA384) ||
       (auth == WPA3_AUTH_PSK_SHA384) ||
       (auth == WPA3_AUTH_DPP_AKM)) {
       return TRUE;
   } else {
       return FALSE;
   }
}
 
bool
bcmwpa_includes_rsn_auth(uint32 auth)
{
   if (auth & (WPA2_AUTH_UNSPECIFIED |
               WPA2_AUTH_PSK |
               BRCM_AUTH_PSK | WPA2_AUTH_1X_SHA256 | WPA2_AUTH_PSK_SHA256 |
               WPA2_AUTH_IS_FILS(auth) | WPA3_AUTH_SAE_PSK | WPA3_AUTH_OWE |
               WPA3_AUTH_1X_SUITE_B_SHA256 | WPA3_AUTH_1X_SUITE_B_SHA384 |
           WPA3_AUTH_PSK_SHA384 | WPA3_AUTH_DPP_AKM))
       return TRUE;
   else
       return FALSE;
}
 
#ifdef RSN_IE_INFO_STRUCT_RELOCATED
/* decode unicast/multicast cipher in RSNIE */
static int
bcmwpa_decode_cipher_suite(rsn_ie_info_t *info, const uint8 **ptr_inc, uint ie_len, uint
   *remain_len, uint16 *p_count)
{
   const wpa_suite_ucast_t *ucast;
   const wpa_suite_mcast_t *mcast;
   uint i;
 
   if (!(*remain_len)) {
       info->g_cipher = WPA_CIPHER_UNSPECIFIED;
       info->p_ciphers = WPA_P_CIPHERS_UNSPECIFIED;
       goto done; /* only have upto ver */
   }
   *ptr_inc += ie_len - *remain_len;
 
   if (*remain_len < sizeof(wpa_suite_mcast_t)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
   mcast = (const wpa_suite_mcast_t *)*ptr_inc;
 
   if (IS_WPA_CIPHER(mcast->type)) {
       info->g_cipher = mcast->type;
   } else {
       info->parse_status = BCME_BAD_IE_DATA;
       goto done;
   }
 
   /* for rsn pairwise cipher suite */
   *ptr_inc += sizeof(wpa_suite_mcast_t);
   *remain_len -= sizeof(wpa_suite_mcast_t);
 
   if (!(*remain_len)) {
       info->p_ciphers = WPA_P_CIPHERS_UNSPECIFIED;
       info->sta_akm = WPA_CIPHER_UNSPECIFIED;
       goto done;
   }
 
   ucast = (const wpa_suite_ucast_t *)*ptr_inc;
 
   if ((*remain_len) < sizeof(ucast->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (!ucast->count.low && !ucast->count.high) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   *p_count = ltoh16_ua(&ucast->count);
   if (info->dev_type == DEV_STA && *p_count != 1u) {
       info->parse_status = BCME_BAD_IE_DATA;
       goto done;
   }
   if ((*remain_len) < (*p_count * WPA_SUITE_LEN + sizeof(ucast->count))) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (info->dev_type == DEV_STA) {
       if (IS_WPA_CIPHER(ucast->list[0].type)) {
           /* update the pairwise cipher */
           info->sta_cipher = ucast->list[0].type;
       } else {
           info->parse_status = BCME_BAD_IE_DATA;
           goto done;
       }
   } else {
       for (i = 0; i < *p_count; i++) {
           if (IS_WPA_CIPHER(ucast->list[i].type)) {
               info->p_ciphers |= BIT(ucast->list[i].type);
               info->rsn_p_ciphers = info->p_ciphers;
           } else {
               info->parse_status = BCME_BAD_IE_DATA;
               goto done;
           }
       }
   }
 
   /* update buffer ptr and remaining length */
   *ptr_inc += (*p_count * WPA_SUITE_LEN) + sizeof(ucast->count);
   *remain_len -= (*p_count * WPA_SUITE_LEN) + sizeof(ucast->count);
 
done:
 
   if (info->parse_status == BCME_OK) {
       if (info->g_cipher == WPA_CIPHER_UNSPECIFIED) {
           info->g_cipher = WPA_CIPHER_AES_CCM;
       }
       if (info->p_ciphers == WPA_P_CIPHERS_UNSPECIFIED) {
           info->p_ciphers = BIT(WPA_CIPHER_AES_CCM);
           info->rsn_p_ciphers = info->p_ciphers;
       }
   }
 
   return info->parse_status;
}
/* sta_akm/sta_cipher must be set before this call */
int
bcmwpa_rsnie_eapol_key_len(rsn_ie_info_t *info)
{
   info->pmk_len = bcmwpa_eapol_key_length(EAPOL_KEY_PMK, info->sta_akm, 0);
   info->kck_mic_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK_MIC, info->sta_akm, 0);
   info->kck_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK, info->sta_akm, 0);
   info->kek_len = bcmwpa_eapol_key_length(EAPOL_KEY_KEK, info->sta_akm, 0);
   info->tk_len = bcmwpa_eapol_key_length(EAPOL_KEY_TK, 0, info->sta_cipher);
   info->ptk_len = info->kck_len + info->kek_len + info->tk_len;
#if defined(WL_FILS) && defined(WLFBT)
   info->kck2_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK2, info->sta_akm, 0);
   info->kek2_len = bcmwpa_eapol_key_length(EAPOL_KEY_KEK2, info->sta_akm, 0);
   if (WPA_IS_FILS_FT_AKM(info->sta_akm)) {
       info->ptk_len += (info->kck2_len + info->kek2_len);
   }
#endif /* WL_FILS && WLFBT */
   return BCME_OK;
}
/* Extract and store information from WPA or RSN IEs
 *
 * called after either
 *  -an association request has been built (STA),
 * - an association was received (AP)
 * - a probe request has been built (AP)
 * - a probe response was received (STA)
 *
 * All available information is extracted to be used for subsequent
 * bss pruning, association request validation, key descriptor compuation etc.
 *
 * To be expanded as needed.
 *
 * ie: RSN IE input
 * rsn_info:  parsed information. Placed in either bsscfg for self, or scb for peer.
 * dev_type: STA_RSN or AP_RSN
 *
 * Return : parse status.
 * NOTE: the parse status is also saved in the the parse_status field.
 * NOTE 2 : the IE itself is copied at the end of the structure. Since there is
 * no reference to the osh available here, the allocation has to happen outside
 * and so the structure cannot be zeroed in this function.
 * For the STA, it should happen everytime.
 * For the AP, it should happen right after a new beacon/probe has been acquired.
 */
 
int
bcmwpa_parse_rsnie(const bcm_tlv_t *ie, rsn_ie_info_t *info, device_type_t dev_type)
{
 
   const uint8 *ptr_inc = NULL;
   const wpa_suite_mcast_t *mcast;
   const wpa_suite_auth_key_mgmt_t *mgmt;
   const wpa_pmkid_list_t *pmkid_list;
   uint32 remain_len = 0, i;
   uint8 auth_ie_type;
   uint16 p_count = 0;
   uint16 akm_count;
 
   ASSERT(info != NULL);
 
   /* this function might be called from place where there
    * is no error detection.
    * e.g. fron the iem callback. Store status here.
    */
 
   info->parse_status = BCME_OK;
 
   if (!ie) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
 
   /* For AP, do not zero this structure since there could be multiple
    * IEs. In that case, add to the existing
    * bits in field (ciphers, akms) as necessary.
    */
   if (dev_type == DEV_AP) {
       /* if already created, check device type */
       if (info->dev_type != DEV_NONE) {
           if (info->dev_type != DEV_AP) {
               info->parse_status = BCME_BADARG;
               goto done;
           }
       }
   }
   info->dev_type = dev_type;
   ptr_inc = ie->data;
 
   /* decode auth IE (WPA vs RSN). Fill in the auth_ie_type and version.
    * Modify remain_len to indicate the position of the pointer.
    */
   /* NOTE the status field will be updated in this call */
   if (bcmwpa_decode_ie_type(ie, info, &remain_len, &auth_ie_type) != BCME_OK) {
       goto done;
   }
 
   /* decode multicast, unicast ciphers */
   if (bcmwpa_decode_cipher_suite(info, &ptr_inc, ie->len, &remain_len, &p_count) != BCME_OK) {
       goto done;
   }
 
   if (!(remain_len)) {
       info->akms = BIT(RSN_AKM_UNSPECIFIED);
       goto done;
   }
 
   mgmt = (const wpa_suite_auth_key_mgmt_t *)ptr_inc;
 
   if (remain_len < sizeof(mgmt->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   akm_count = ltoh16_ua(&mgmt->count);
 
   if (!akm_count) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
 
   if (dev_type == DEV_STA && akm_count != 1) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
 
   if ((remain_len) < (akm_count * WPA_SUITE_LEN + sizeof(mgmt->count))) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (dev_type == DEV_STA) {
       info->sta_akm = mgmt->list[0].type;
   }
   for (i = 0; i < akm_count; i++) {
       if (bcmwpa_is_valid_akm(mgmt->list[i].type) == BCME_OK) {
           ASSERT((mgmt->list[i].type) <
               (sizeof(info->akms) * NBBY));
           info->akms |= BIT(mgmt->list[i].type);
       }
   }
 
   /* save IE dependent values in their respective fields */
   if (dev_type == DEV_AP) {
       if (auth_ie_type == RSN_AUTH_IE) {
           info->rsn_akms = info->akms;
       } else if (auth_ie_type == WPA_AUTH_IE) {
           info->wpa_akms = info->akms;
           info->wpa_p_ciphers = info->p_ciphers;
       }
   }
 
   /* as a STA,  at this point, we can compute the key descriptor version */
   if (dev_type == DEV_STA) {
       info->key_desc = wlc_calc_rsn_desc_version(info);
       /* For STA, we can set the auth ie */
       if (auth_ie_type == RSN_AUTH_IE) {
           info->auth_ie = info->rsn_ie;
           info->auth_ie_len = info->rsn_ie_len;
       } else {
           info->auth_ie = info->wpa_ie;
           info->auth_ie_len = info->wpa_ie_len;
       }
   }
 
   /* RSN AKM/cipher suite related EAPOL key length update */
   bcmwpa_rsnie_eapol_key_len(info);
 
   /* for rsn capabilities */
   ptr_inc += akm_count * WPA_SUITE_LEN + sizeof(mgmt->count);
   remain_len -=  akm_count * WPA_SUITE_LEN + sizeof(mgmt->count);
 
   if (!(remain_len)) {
       goto done;
   }
   if (remain_len < RSN_CAP_LEN) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (ie->id == DOT11_MNG_RSN_ID) {
       info->caps = ltoh16_ua(ptr_inc);
   }
 
   /* check if AKMs require MFP capable to be set */
   if ((info->akms & RSN_MFPC_AKM_MASK) && !(info->caps & RSN_CAP_MFPC)) {
       /* NOTE: Acting as WPA3 CTT testbed device, it requires to send assoc request frame
       with user provided mfp value as is. So should not return error here.
       */
#ifndef WPA3_CTT
       info->parse_status = BCME_EPERM;
       goto done;
#endif /* WPA3_CTT */
   }
 
   /* for rsn PMKID */
   ptr_inc += RSN_CAP_LEN;
   remain_len -= RSN_CAP_LEN;
 
   if (!(remain_len)) {
       goto done;
   }
 
   /* here's possible cases after RSN_CAP parsed
    * a) pmkid_count 2B(00 00)
    * b) pmkid_count 2B(00 00) + BIP 4B
    * c) pmkid_count 2B(non zero) + pmkid_count * 16B
    * d) pmkid_count 2B(non zero) + pmkid_count * 16B + BIP 4B
    */
 
   /* pmkids_offset set to
    * 1) if pmkid_count field(2B) present, point to first PMKID offset in the RSN ID
    *    no matter what pmkid_count value is. (true, even if pmkid_count == 00 00)
    * 2) if pmkid_count field(2B) not present, it shall be zero.
    */
 
   pmkid_list = (const wpa_pmkid_list_t*)ptr_inc;
 
   if ((remain_len) < sizeof(pmkid_list->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   info->pmkid_count = (uint8)ltoh16_ua(&pmkid_list->count);
   ptr_inc += sizeof(pmkid_list->count);
   remain_len -= sizeof(pmkid_list->count);
 
   if (remain_len < (uint32)(info->pmkid_count * WPA2_PMKID_LEN)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   info->pmkids_offset = ie->len + TLV_HDR_LEN - remain_len;
   /* for rsn group management cipher suite */
   ptr_inc += info->pmkid_count * WPA2_PMKID_LEN;
   remain_len -= info->pmkid_count * WPA2_PMKID_LEN;
 
   if (!(remain_len)) {
       goto done;
   }
   /*
    * from WPA2_Security_Improvements_Test_Plan_v1.0
    * 4.2.4 APUT RSNE bounds verification using WPA2-PSK
    * May content RSNE extensibile element ay this point
    */
   if (remain_len < sizeof(wpa_suite_mcast_t)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   mcast = (const wpa_suite_mcast_t *)ptr_inc;
   if (IS_VALID_BIP_CIPHER((rsn_cipher_t)mcast->type)) {
       info->g_mgmt_cipher = (rsn_cipher_t)mcast->type;
   }
 
done:
   return info->parse_status;
}
 
/* Determine if the IE is of WPA or RSN type. Decode
 * up to version field. Modify the remaining len parameter to
 * indicate where the next field is.
 * Store and return error status.
 */
 
int
bcmwpa_decode_ie_type(const bcm_tlv_t *ie, rsn_ie_info_t *info, uint32 *remaining,
    uint8 *type)
{
   const uint8 * ptr_inc = (const uint8 *)ie->data;
   uint32 remain_len = ie->len;
   uint8 version, version_len;
 
   if (ie->id == DOT11_MNG_WPA_ID) {
       /* min len check */
       if (remain_len < WPA_IE_FIXED_LEN) {
           info->parse_status = BCME_BADLEN;
           goto done;
       }
       /* WPA IE */
       if (memcmp(WPA_OUI, ie->data, WPA_OUI_LEN)) {
           /* bad OUI */
           info->parse_status = BCME_BADARG;
           goto done;
       }
       ptr_inc += WPA_OUI_LEN;
       if (*ptr_inc == WPA_OUI_TYPE) {
           *type = WPA_AUTH_IE;
       } else if  (*ptr_inc == WFA_OUI_TYPE_OSEN) {
           *type = OSEN_AUTH_IE;
       }
       else {
           /* wrong type */
           info->parse_status = BCME_BADARG;
           goto done;
       }
 
       ptr_inc ++;
       remain_len -= WPA_OUI_LEN + 1u;
       version_len = WPA_VERSION_LEN;
   }
   else if  (ie->id == DOT11_MNG_RSN_ID) {
       if (remain_len < WPA2_VERSION_LEN) {
           info->parse_status = BCME_BADLEN;
           goto done;
       }
       /* RSN IE */
       *type = RSN_AUTH_IE;
       version_len = WPA2_VERSION_LEN;
   } else {
       printf("IE ID %d\n", ie->id);
       /* TODO : add support for CCX, WAPI ? */
       info->parse_status = BCME_UNSUPPORTED;
       goto done;
   }
   info->auth_ie_type |= *type;
   /* mask down to uint8 for Windows build */
   version = 0xff & ltoh16_ua(ptr_inc);
   if (version > MAX_RSNE_SUPPORTED_VERSION) {
       info->parse_status = BCME_UNSUPPORTED;
       goto done;
   }
 
   info->version = (uint8)version;
   *remaining = remain_len - version_len;
done:
   return info->parse_status;
}
 
/* rsn info allocation management.
 *
 * In some cases, the rsn ie info structures are embedded in the scan results
 * which can be shared by different lists.
 * To keep track of their allocation, we use a reference counter.
 * The counter is incremented on demand by rsn_ie_info_add_ref()
 * at the time the reference is shared.
 * It is decremented in rsn_ie_info_rel_ref
 * When ref_count gets to 0, bcmwpa_rsn_ie_info_free_mem
 * is called to free the whole structure.
 */
 
/* free rsn_ie and wpa_ie, if any, and zero the rsn_info */
void
bcmwpa_rsn_ie_info_reset(rsn_ie_info_t *rsn_info, osl_t *osh)
{
   uint8 ref_count;
   if (rsn_info == NULL) {
       return;
   }
   ref_count = rsn_info->ref_count;
   MFREE(osh, rsn_info->rsn_ie, rsn_info->rsn_ie_len);
   MFREE(osh, rsn_info->wpa_ie, rsn_info->wpa_ie_len);
   MFREE(osh, rsn_info->rsnxe, rsn_info->rsnxe_len);
   bzero(rsn_info, sizeof(*rsn_info));
   rsn_info->ref_count = ref_count;
 
}
 
static
void bcmwpa_rsn_ie_info_free_mem(rsn_ie_info_t **rsn_info, osl_t *osh)
{
   bcmwpa_rsn_ie_info_reset(*rsn_info, osh);
   MFREE(osh, *rsn_info, sizeof(**rsn_info));
   *rsn_info = NULL;
}
 
void bcmwpa_rsn_ie_info_rel_ref(rsn_ie_info_t **rsn_info, osl_t *osh)
{
 
   if (rsn_info == NULL || *rsn_info == NULL) {
       return;
   }
 
   /* already freed ? */
   if ((*rsn_info)->ref_count == 0) {
       ASSERT(0);
       return;
   }
   /* decrement ref count */
   (*rsn_info)->ref_count -= 1;
   /* clear reference. */
   if ((*rsn_info)->ref_count > 0) {
       *rsn_info = NULL;
       return;
   }
   /* free memory and clear reference */
   bcmwpa_rsn_ie_info_free_mem(rsn_info, osh);
}
 
int
bcmwpa_rsn_ie_info_add_ref(rsn_ie_info_t *rsn_info)
{
   int status = BCME_OK;
   if (rsn_info == NULL) {
       goto done;
   }
   if (rsn_info->ref_count == 0) {
       /* don't increase from 0, which means this structure has been freed earlier.
        * That reference should not exist anymore.
        */
       ASSERT(0);
       status = BCME_BADARG;
       goto  done;
   }
   rsn_info->ref_count++;
done:
   return status;
}
 
#else /* Not RSN_IE_INFO_STRUCT_RELOCATED */
 
int
bcmwpa_parse_rsnie(const bcm_tlv_t *ie, rsn_ie_info_t *info, device_type_t dev_type)
{
 
   const uint8 *ptr_inc = NULL;
   const wpa_suite_ucast_t *ucast;
   const wpa_suite_mcast_t *mcast;
   const wpa_suite_auth_key_mgmt_t *mgmt;
   const wpa_pmkid_list_t *pmkid_list;
   uint32 remain_len = 0, i;
 
   ASSERT(info != NULL);
 
   /* this function might be called from place where there
    * is no error detection.
    * e.g. fron the iem callback. Store status here.
    */
 
   info->parse_status = BCME_OK;
 
   if (!ie) {
           info->parse_status = BCME_BADARG;
           goto done;
   }
 
   /* For AP, do not zero this structure since there could be multiple
    * IEs. In that case, add to the existing
    * bits in field (ciphers, akms) as necessary.
    */
   if (dev_type != DEV_AP) {
       bzero(info, sizeof(*info));
   } else {
       /* if already created, check device type */
       if (info->dev_type != DEV_NONE) {
           if (info->dev_type != DEV_AP) {
               info->parse_status = BCME_BADARG;
               goto done;
           }
       }
   }
   info->dev_type = dev_type;
   ptr_inc = ie->data;
 
   /* decode auth IE (WPA vs RSN). Fill in the auth_ie_type and version.
    * Modify remain_len to indicate the position of the pointer.
    */
   /* NOTE the status field will be updated in this call */
   if (bcmwpa_decode_ie_type(ie, info, &remain_len) != BCME_OK) {
       goto done;
   }
 
   if (!(remain_len)) {
       info->g_cipher = WPA_CIPHER_NONE;
       goto done; /* only have upto ver */
   }
   ptr_inc += ie->len - remain_len;
 
   if (remain_len < sizeof(wpa_suite_mcast_t)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
   mcast = (const wpa_suite_mcast_t *)ptr_inc;
 
   if (IS_WPA_CIPHER(mcast->type)) {
       info->g_cipher = mcast->type;
   }
 
   /* for rsn pairwise cipher suite */
   ptr_inc += sizeof(wpa_suite_mcast_t);
   remain_len -= sizeof(wpa_suite_mcast_t);
 
   if (!(remain_len)) {
       goto done;
   }
 
   ucast = (const wpa_suite_ucast_t *)ptr_inc;
 
   if ((remain_len) < sizeof(ucast->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (!ucast->count.low && !ucast->count.high) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   info->p_count = (uint8)ltoh16_ua(&ucast->count);
 
   if (dev_type == DEV_STA && info->p_count != 1) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
   if ((remain_len) < (info->p_count * WPA_SUITE_LEN + sizeof(ucast->count))) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (IS_WPA_CIPHER(ucast->list[0].type)) {
       /* update the pairwise cipher */
       /* set cipher to invald value */
       if (dev_type == DEV_STA) {
           info->sta_cipher = ucast->list[0].type;
       } else {
           for (i = 0; i < info->p_count; i++) {
               if (IS_WPA_CIPHER(ucast->list[i].type)) {
                   info->p_ciphers |= BIT(ucast->list[i].type);
               } else {
                   info->parse_status = BCME_BAD_IE_DATA;
                   goto done;
               }
           }
       }
   } else {
       info->parse_status = BCME_BAD_IE_DATA;
       goto done;
   }
 
   /* for rsn AKM authentication */
   ptr_inc += info->p_count * WPA_SUITE_LEN + sizeof(ucast->count);
   remain_len -= (info->p_count * WPA_SUITE_LEN + sizeof(ucast->count));
 
   mgmt = (const wpa_suite_auth_key_mgmt_t *)ptr_inc;
 
   if (remain_len < sizeof(mgmt->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   info->akm_count = (uint8)ltoh16_ua(&mgmt->count);
 
   if (!info->akm_count) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
 
   if (dev_type == DEV_STA && info->akm_count != 1) {
       info->parse_status = BCME_BADARG;
       goto done;
   }
 
   if ((remain_len) < (info->akm_count * WPA_SUITE_LEN + sizeof(mgmt->count))) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (dev_type == DEV_STA) {
       info->sta_akm = mgmt->list[0].type;
   }
   for (i = 0; i < info->akm_count; i++) {
       if (bcmwpa_is_valid_akm(mgmt->list[i].type) == BCME_OK) {
           ASSERT((mgmt->list[i].type) <
               (sizeof(info->akms) * NBBY));
           info->akms |= BIT(mgmt->list[i].type);
       }
   }
 
   /* RSN AKM/cipher suite related EAPOL key length update */
   info->pmk_len = bcmwpa_eapol_key_length(EAPOL_KEY_PMK, info->sta_akm, 0);
   info->kck_mic_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK_MIC, info->sta_akm, 0);
   info->kck_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK, info->sta_akm, 0);
   info->kek_len = bcmwpa_eapol_key_length(EAPOL_KEY_KEK, info->sta_akm, 0);
   info->tk_len = bcmwpa_eapol_key_length(EAPOL_KEY_TK, 0, info->sta_cipher);
   info->ptk_len = info->kck_mic_len + info->kek_len + info->tk_len;
#if defined(WL_FILS) && defined(WLFBT)
   info->kck2_len = bcmwpa_eapol_key_length(EAPOL_KEY_KCK2, info->sta_akm, 0);
   info->kek2_len = bcmwpa_eapol_key_length(EAPOL_KEY_KEK2, info->sta_akm, 0);
#endif /* WL_FILS && WLFBT */
 
   /* for rsn capabilities */
   ptr_inc += info->akm_count * WPA_SUITE_LEN + sizeof(mgmt->count);
   remain_len -=  info->akm_count * WPA_SUITE_LEN + sizeof(mgmt->count);
 
   /* as a STA,  at this point, we can compute the key descriptor version */
   if (dev_type == DEV_STA) {
       info->key_desc = wlc_calc_rsn_desc_version(info);
   }
 
   if (!(remain_len)) {
       goto done;
   }
   if (remain_len < RSN_CAP_LEN) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   if (ie->id == DOT11_MNG_RSN_ID) {
       info->caps = ltoh16_ua(ptr_inc);
   }
 
   /* for WFA If MFP required, check that we are using a SHA256 AKM
    * or higher  and nothing else.
    * In case MFP Required and MFP Capable do not enforce check of AKM.
    */
   if ((info->caps & RSN_CAP_MFPR) && !(info->akms & (1u << RSN_AKM_PSK))) {
       if ((info->akms & (AKM_SHA256_MASK | AKM_SHA384_MASK)) == 0 ||
           (info->akms & ~(AKM_SHA256_MASK | AKM_SHA384_MASK))) {
           info->parse_status = BCME_EPERM;
           goto done;
       }
   }
 
   /* check if AKMs require MFP capable to be set */
   if ((info->akms & RSN_MFPC_AKM_MASK) && !(info->caps & RSN_CAP_MFPC)) {
       info->parse_status = BCME_EPERM;
       goto done;
   }
 
   /* for rsn PMKID */
   ptr_inc += RSN_CAP_LEN;
   remain_len -= RSN_CAP_LEN;
 
   if (!(remain_len)) {
       goto done;
   }
 
   pmkid_list = (const wpa_pmkid_list_t*)ptr_inc;
 
   if ((remain_len) < sizeof(pmkid_list->count)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   info->pmkid_count = (uint8)ltoh16_ua(&pmkid_list->count);
   ptr_inc += sizeof(pmkid_list->count);
   remain_len -= sizeof(pmkid_list->count);
 
   if (info->pmkid_count) {
       if (remain_len < (uint32)(info->pmkid_count * WPA2_PMKID_LEN)) {
           info->parse_status = BCME_BADLEN;
           goto done;
       }
       info->pmkids_offset = ie->len + TLV_HDR_LEN - remain_len;
       /* for rsn group management cipher suite */
       ptr_inc += info->pmkid_count * WPA2_PMKID_LEN;
       remain_len -= info->pmkid_count * WPA2_PMKID_LEN;
   }
 
   if (!(remain_len)) {
       goto done;
   }
   /*
    * from WPA2_Security_Improvements_Test_Plan_v1.0
    * 4.2.4 APUT RSNE bounds verification using WPA2-PSK
    * May content RSNE extensibile element ay this point
    */
   if (remain_len < sizeof(wpa_suite_mcast_t)) {
       info->parse_status = BCME_BADLEN;
       goto done;
   }
 
   mcast = (const wpa_suite_mcast_t *)ptr_inc;
   if (IS_VALID_BIP_CIPHER((rsn_cipher_t)mcast->type)) {
       info->g_mgmt_cipher = (rsn_cipher_t)mcast->type;
   }
 
done:
   return info->parse_status;
}
 
/* Determine if the IE is of WPA or RSN type. Decode
 * up to version field. Modify the remaining len parameter to
 * indicate where the next field is.
 * Store and return error status.
 */
 
int
bcmwpa_decode_ie_type(const bcm_tlv_t *ie, rsn_ie_info_t *info, uint32 *remaining)
{
   const uint8 * ptr_inc = (const uint8 *)ie->data;
   uint32 remain_len = ie->len;
   uint8 version, version_len;
 
   if (ie->id == DOT11_MNG_WPA_ID) {
       /* min len check */
       if (remain_len < WPA_IE_FIXED_LEN) {
           info->parse_status = BCME_BADLEN;
           goto done;
       }
       /* WPA IE */
       if (memcmp(WPA_OUI, ie->data, WPA_OUI_LEN)) {
           /* bad OUI */
           info->parse_status = BCME_BADARG;
           goto done;
       }
       ptr_inc += WPA_OUI_LEN;
       if (*ptr_inc != WPA_OUI_TYPE) {
           /* wrong type */
           info->parse_status = BCME_BADARG;
           goto done;
       }
       ptr_inc ++;
       remain_len -= WPA_OUI_LEN + 1u;
       info->auth_ie_type |= WPA_AUTH_IE;
       version_len = WPA_VERSION_LEN;
   }
   else if  (ie->id == DOT11_MNG_RSN_ID) {
       if (remain_len < WPA2_VERSION_LEN) {
           info->parse_status = BCME_BADLEN;
           goto done;
       }
       /* RSN IE */
       info->auth_ie_type |= RSN_AUTH_IE;
       version_len = WPA2_VERSION_LEN;
   } else {
       /* TODO : add support for CCX, WAPI ? */
       info->parse_status = BCME_UNSUPPORTED;
       goto done;
   }
 
   /* mask down to uint8 for Windows build */
   version = 0xff & ltoh16_ua(ptr_inc);
   if (version > MAX_RSNE_SUPPORTED_VERSION) {
       info->parse_status = BCME_UNSUPPORTED;
       goto done;
   }
 
   info->version = (uint8)version;
   *remaining = remain_len - version_len;
done:
   return info->parse_status;
}
 
#endif /* RSN_IE_INFO_STRUCT_RELOCATED */
 
/* return the key descriptor version based on the AKM suite
 * applicable only for STA with RSN
 */
static uint16
wlc_calc_rsn_desc_version(const rsn_ie_info_t *rsn_info)
{
   uint16 key_desc_ver = WPA_KEY_DESC_V0;
   uint8 akm;
 
   ASSERT(rsn_info != NULL);
   ASSERT(rsn_info->dev_type == DEV_STA);
   akm = rsn_info->sta_akm;
 
   /* Refer Draft 802.11REVmd_D1.0.pdf  Section 12.7.2 */
   if ((akm == RSN_AKM_UNSPECIFIED) ||
       (akm == RSN_AKM_PSK)) {
       if ((rsn_info->sta_cipher == WPA_CIPHER_TKIP) ||
           (rsn_info->sta_cipher == WPA_CIPHER_NONE)) {
           key_desc_ver = WPA_KEY_DESC_V1;
       } else if ((rsn_info->sta_cipher != WPA_CIPHER_TKIP) ||
           (rsn_info->g_cipher != WPA_CIPHER_TKIP)) {
           key_desc_ver = WPA_KEY_DESC_V2;
       }
   } else if ((akm == RSN_AKM_FBT_1X) ||
       (akm == RSN_AKM_FBT_PSK) ||
       (akm == RSN_AKM_SHA256_1X) ||
       (akm == RSN_AKM_SHA256_PSK)) {
           key_desc_ver = WPA_KEY_DESC_V3;
   }
   return key_desc_ver;
}
 
/* get EAPOL key length based on RSN IE AKM/Cipher(unicast) suite
 * key: EAPOL key type
 * akm: RSN AKM suite selector
 * cipher: RSN unicast cipher suite selector
 * return: key length found in matching key_length_entry table
 */
uint8
bcmwpa_eapol_key_length(eapol_key_type_t key, rsn_akm_t akm, rsn_cipher_t cipher)
{
   uint i;
   uint8 key_length = 0;
   uint8 suite;
   const key_length_entry_t *key_entry = NULL;
 
   if (key == EAPOL_KEY_TK) {
       suite = cipher;
   } else {
       suite = akm;
   }
   for (i = 0; i < ARRAYSIZE(eapol_key_lookup_tbl); i++) {
       if (eapol_key_lookup_tbl[i].key == key) {
           key_entry = eapol_key_lookup_tbl[i].key_entry;
           break;
       }
   }
 
   if (key_entry) {
       i = 0;
       do {
           if (key_entry[i].suite == suite || key_entry[i].suite == 0) {
               key_length = key_entry[i].len;
               break;
           }
           i++;
       } while (i > 0);
   }
 
   return key_length;
}
 
/* check if RSM AKM suite is valid */
static int bcmwpa_is_valid_akm(const rsn_akm_t akm)
{
   uint i = 0;
   for (i = 0; i < ARRAYSIZE(rsn_akm_lookup_tbl); i++) {
       if (akm == rsn_akm_lookup_tbl[i].rsn_akm) {
           return BCME_OK;
       }
   }
   return BCME_ERROR;
}
 
/* checking cipher suite selector restriction based on AKM */
int
bcmwpa_rsn_akm_cipher_match(rsn_ie_info_t *rsn_info)
{
   uint i;
   const rsn_akm_cipher_match_entry_t *p_entry = NULL;
 
   for (i = 0; i < ARRAYSIZE(rsn_akm_cipher_match_table); i++) {
       /* akm match */
       if (rsn_info->sta_akm == rsn_akm_cipher_match_table[i].akm_type) {
           p_entry = &rsn_akm_cipher_match_table[i];
           break;
       }
   }
 
   if (p_entry) {
       /* unicast cipher match */
       if (!(rsn_info->p_ciphers & p_entry->u_cast)) {
           return BCME_UNSUPPORTED;
       }
       /* multicast cipher match */
       if (!(BCM_BIT(rsn_info->g_cipher) & p_entry->m_cast)) {
           return BCME_UNSUPPORTED;
       }
       /* group management cipher match */
       if (!(BCM_BIT(rsn_info->g_mgmt_cipher) & p_entry->g_mgmt)) {
           return BCME_UNSUPPORTED;
       }
   }
   return BCME_OK;
}
 
#if defined(BCMSUP_PSK) || defined(BCMSUPPL)
uint8 bcmwpa_find_group_mgmt_algo(rsn_cipher_t g_mgmt_cipher)
{
   uint8 i;
   uint8 algo = CRYPTO_ALGO_BIP;
 
   for (i = 0; i < ARRAYSIZE(group_mgmt_cipher_algo); i++) {
       if ((group_mgmt_cipher_algo[i].g_mgmt_cipher == g_mgmt_cipher)) {
           algo = group_mgmt_cipher_algo[i].bip_algo;
           break;
       }
   }
 
   return algo;
}
#endif /* defined(BCMSUP_PSK) || defined(BCMSUPPL) */
 
#if defined(WL_BAND6G)
bool
bcmwpa_is_invalid_6g_akm(const rsn_akm_mask_t akms_bmp)
{
   if (akms_bmp & rsn_akm_6g_inval_mask) {
       return TRUE;
   }
   return FALSE;
}
 
bool
bcmwpa_is_invalid_6g_cipher(const rsn_ciphers_t ciphers_bmp)
{
   if (ciphers_bmp & cipher_6g_inval_mask) {
       return TRUE;
   }
   return FALSE;
}
#endif /* WL_BAND6G */
 
/*
 * bcmwpa_get_algo_key_len returns the key_length for the algorithm.
 * API : bcm_get_algorithm key length
 * input: algo: Get the crypto algorithm.
 *        km: Keymgmt information.
 * output: returns the key length and error status.
 *         BCME_OK is valid else BCME_UNSUPPORTED if not supported
 */
int
bcmwpa_get_algo_key_len(uint8 algo, uint16 *key_len)
{
   int err = BCME_OK;
 
   if (key_len == NULL) {
       return BCME_BADARG;
   }
 
   switch (algo) {
       case CRYPTO_ALGO_WEP1:
           *key_len = WEP1_KEY_SIZE;
           break;
 
       case CRYPTO_ALGO_TKIP:
           *key_len = TKIP_KEY_SIZE;
           break;
 
       case CRYPTO_ALGO_WEP128:
           *key_len = WEP128_KEY_SIZE;
           break;
 
       case CRYPTO_ALGO_AES_CCM:       /* fall through */
       case CRYPTO_ALGO_AES_GCM:       /* fall through */
       case CRYPTO_ALGO_AES_OCB_MSDU : /* fall through */
       case CRYPTO_ALGO_AES_OCB_MPDU:
           *key_len = AES_KEY_SIZE;
           break;
 
#ifdef BCMWAPI_WPI
       /* TODO: Need to double check */
       case CRYPTO_ALGO_SMS4:
           *key_len = SMS4_KEY_LEN + SMS4_WPI_CBC_MAC_LEN;
           break;
#endif /* BCMWAPI_WPI */
 
       case CRYPTO_ALGO_BIP: /* fall through */
       case CRYPTO_ALGO_BIP_GMAC:
           *key_len = BIP_KEY_SIZE;
           break;
 
       case CRYPTO_ALGO_AES_CCM256:  /* fall through */
       case CRYPTO_ALGO_AES_GCM256:  /* fall through */
       case CRYPTO_ALGO_BIP_CMAC256: /* fall through */
       case CRYPTO_ALGO_BIP_GMAC256:
           *key_len = AES256_KEY_SIZE;
           break;
 
       case CRYPTO_ALGO_OFF:
           *key_len = 0;
           break;
 
#if !defined(BCMCCX) && !defined(BCMEXTCCX)
       case CRYPTO_ALGO_NALG:     /* fall through */
#else
       case CRYPTO_ALGO_CKIP:     /* fall through */
       case CRYPTO_ALGO_CKIP_MMH: /* fall through */
       case CRYPTO_ALGO_WEP_MMH:  /* fall through */
       case CRYPTO_ALGO_PMK:      /* fall through default */
#endif /* !defined(BCMCCX) && !defined(BCMEXTCCX) */
       default:
           *key_len = 0;
           err = BCME_UNSUPPORTED;
           break;
   }
   return err;
}