|
root / base / usr / src / uts / common / sys / crypto
crypto Plain Text 7104 lines 228.3 KB
   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
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_SYS_CRYPTO_API_H
#define	_SYS_CRYPTO_API_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/crypto/common.h>

typedef long crypto_req_id_t;
typedef void *crypto_bc_t;
typedef void *crypto_context_t;
typedef void *crypto_ctx_template_t;

typedef uint32_t crypto_call_flag_t;

/* crypto_call_flag's values */
#define	CRYPTO_ALWAYS_QUEUE	0x00000001	/* ALWAYS queue the req. */
#define	CRYPTO_NOTIFY_OPDONE	0x00000002	/* Notify intermediate steps */
#define	CRYPTO_SKIP_REQID	0x00000004	/* Skip request ID generation */

typedef struct {
	crypto_call_flag_t	cr_flag;
	void			(*cr_callback_func)(void *, int);
	void			*cr_callback_arg;
	crypto_req_id_t		cr_reqid;
} crypto_call_req_t;

/*
 * Returns the mechanism type corresponding to a mechanism name.
 */

#define	CRYPTO_MECH_INVALID	((uint64_t)-1)
extern crypto_mech_type_t crypto_mech2id(const char *name);

/*
 * Create and destroy context templates.
 */
extern int crypto_create_ctx_template(crypto_mechanism_t *mech,
    crypto_key_t *key, crypto_ctx_template_t *tmpl, int kmflag);
extern void crypto_destroy_ctx_template(crypto_ctx_template_t tmpl);

/*
 * Single and multi-part digest operations.
 */
extern int crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
    crypto_data_t *digest, crypto_call_req_t *cr);
extern int crypto_digest_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
    crypto_call_req_t *);
extern int crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
    crypto_call_req_t *cr);
extern int crypto_digest_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_context_t *, crypto_call_req_t *);
extern int crypto_digest_update(crypto_context_t ctx, crypto_data_t *data,
    crypto_call_req_t *cr);
extern int crypto_digest_final(crypto_context_t ctx, crypto_data_t *digest,
    crypto_call_req_t *cr);

/*
 * Single and multi-part MAC operations.
 */
extern int crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
    crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
    crypto_call_req_t *cr);
extern int crypto_mac_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data,
    crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
    crypto_call_req_t *cr);
extern int crypto_mac_verify_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
extern int crypto_mac_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_context_t *, crypto_call_req_t *);
extern int crypto_mac_update(crypto_context_t ctx, crypto_data_t *data,
    crypto_call_req_t *cr);
extern int crypto_mac_final(crypto_context_t ctx, crypto_data_t *data,
    crypto_call_req_t *cr);

/*
 * Single and multi-part sign with private key operations.
 */
extern int crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_data_t *data, crypto_ctx_template_t tmpl,
    crypto_data_t *signature, crypto_call_req_t *cr);
extern int crypto_sign_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
extern int crypto_sign_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_context_t *, crypto_call_req_t *);
extern int crypto_sign_update(crypto_context_t ctx, crypto_data_t *data,
    crypto_call_req_t *cr);
extern int crypto_sign_final(crypto_context_t ctx, crypto_data_t *signature,
    crypto_call_req_t *cr);
extern int crypto_sign_recover_init_prov(crypto_provider_t,
    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
    crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *);
extern int crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
    crypto_call_req_t *cr);
extern int crypto_sign_recover_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);

/*
 * Single and multi-part verify with public key operations.
 */
extern int crypto_verify(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
    crypto_call_req_t *cr);
extern int crypto_verify_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_verify_init(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
extern int crypto_verify_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_context_t *, crypto_call_req_t *);
extern int crypto_verify_update(crypto_context_t ctx, crypto_data_t *data,
    crypto_call_req_t *cr);
extern int crypto_verify_final(crypto_context_t ctx, crypto_data_t *signature,
    crypto_call_req_t *cr);
extern int crypto_verify_recover_init_prov(crypto_provider_t,
    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
    crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *);
extern int crypto_verify_recover(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_data_t *signature, crypto_ctx_template_t tmpl, crypto_data_t *data,
    crypto_call_req_t *cr);
extern int crypto_verify_recover_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);

/*
 * Single and multi-part encryption operations.
 */
extern int crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
    crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext,
    crypto_call_req_t *cr);
extern int crypto_encrypt_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_encrypt_init(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
extern int crypto_encrypt_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_context_t *, crypto_call_req_t *);
extern int crypto_encrypt_update(crypto_context_t ctx,
    crypto_data_t *plaintext, crypto_data_t *ciphertext,
    crypto_call_req_t *cr);
extern int crypto_encrypt_final(crypto_context_t ctx,
    crypto_data_t *ciphertext, crypto_call_req_t *cr);

/*
 * Single and multi-part decryption operations.
 */
extern int crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
    crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext,
    crypto_call_req_t *cr);
extern int crypto_decrypt_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
    crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
extern int crypto_decrypt_init(crypto_mechanism_t *mech, crypto_key_t *key,
    crypto_ctx_template_t tmpl, crypto_context_t *ctxp,
    crypto_call_req_t *cr);
extern int crypto_decrypt_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_context_t *, crypto_call_req_t *);
extern int crypto_decrypt_update(crypto_context_t ctx,
    crypto_data_t *ciphertext, crypto_data_t *plaintext,
    crypto_call_req_t *cr);
extern int crypto_decrypt_final(crypto_context_t ctx, crypto_data_t *plaintext,
    crypto_call_req_t *cr);

/*
 * Single and multi-part encrypt/MAC dual operations.
 */
extern int crypto_encrypt_mac(crypto_mechanism_t *encr_mech,
    crypto_mechanism_t *mac_mech, crypto_data_t *pt,
    crypto_key_t *encr_key, crypto_key_t *mac_key,
    crypto_ctx_template_t encr_tmpl, crypto_ctx_template_t mac_tmpl,
    crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr);
extern int crypto_encrypt_mac_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_mechanism_t *, crypto_data_t *,
    crypto_key_t *, crypto_key_t *, crypto_ctx_template_t,
    crypto_ctx_template_t, crypto_dual_data_t *, crypto_data_t *,
    crypto_call_req_t *);
extern int crypto_encrypt_mac_init(crypto_mechanism_t *encr_mech,
    crypto_mechanism_t *mac_mech, crypto_key_t *encr_key,
    crypto_key_t *mac_key, crypto_ctx_template_t encr_tmpl,
    crypto_ctx_template_t mac_tmpl, crypto_context_t *ctxp,
    crypto_call_req_t *cr);
extern int crypto_encrypt_mac_init_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_mechanism_t *, crypto_key_t *, crypto_key_t *,
    crypto_ctx_template_t, crypto_ctx_template_t, crypto_context_t *,
    crypto_call_req_t *);
extern int crypto_encrypt_mac_update(crypto_context_t ctx,
    crypto_data_t *pt, crypto_dual_data_t *ct, crypto_call_req_t *cr);
extern int crypto_encrypt_mac_final(crypto_context_t ctx,
    crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr);

/*
 * Single and multi-part MAC/decrypt dual operations.
 */
extern int crypto_mac_decrypt(crypto_mechanism_t *mac_mech,
    crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
    crypto_key_t *mac_key, crypto_key_t *decr_key,
    crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
    crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
extern int crypto_mac_decrypt_prov(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *mac_mech, crypto_mechanism_t *decr_mech,
    crypto_dual_data_t *ct, crypto_key_t *mac_key, crypto_key_t *decr_key,
    crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
    crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
extern int crypto_mac_verify_decrypt(crypto_mechanism_t *mac_mech,
    crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
    crypto_key_t *mac_key, crypto_key_t *decr_key,
    crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
    crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
extern int crypto_mac_verify_decrypt_prov(crypto_provider_t,
    crypto_session_id_t, crypto_mechanism_t *mac_mech,
    crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
    crypto_key_t *mac_key, crypto_key_t *decr_key,
    crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
    crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
extern int crypto_mac_decrypt_init(crypto_mechanism_t *mac_mech,
    crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
    crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
    crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
    crypto_call_req_t *cr);
extern int crypto_mac_decrypt_init_prov(crypto_provider_t,
    crypto_session_id_t, crypto_mechanism_t *mac_mech,
    crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
    crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
    crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
    crypto_call_req_t *cr);
extern int crypto_mac_decrypt_update(crypto_context_t ctx,
    crypto_dual_data_t *ct, crypto_data_t *pt, crypto_call_req_t *cr);
extern int crypto_mac_decrypt_final(crypto_context_t ctx, crypto_data_t *mac,
    crypto_data_t *pt, crypto_call_req_t *cr);

/* Session Management */
extern int crypto_session_open(crypto_provider_t, crypto_session_id_t *,
    crypto_call_req_t *);
extern int crypto_session_close(crypto_provider_t, crypto_session_id_t,
    crypto_call_req_t *);
extern int crypto_session_login(crypto_provider_t, crypto_session_id_t,
    crypto_user_type_t, char *, size_t, crypto_call_req_t *);
extern int crypto_session_logout(crypto_provider_t, crypto_session_id_t,
    crypto_call_req_t *);

/* Object Management */
extern int crypto_object_copy(crypto_provider_t, crypto_session_id_t,
    crypto_object_id_t, crypto_object_attribute_t *, uint_t,
    crypto_object_id_t *, crypto_call_req_t *);
extern int crypto_object_create(crypto_provider_t, crypto_session_id_t,
    crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
    crypto_call_req_t *);
extern int crypto_object_destroy(crypto_provider_t, crypto_session_id_t,
    crypto_object_id_t, crypto_call_req_t *);
extern int crypto_object_get_attribute_value(crypto_provider_t,
    crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *,
    uint_t, crypto_call_req_t *);
extern int crypto_object_get_size(crypto_provider_t, crypto_session_id_t,
    crypto_object_id_t, size_t *, crypto_call_req_t *);
extern int crypto_object_find_final(crypto_provider_t, void *,
    crypto_call_req_t *);
extern int crypto_object_find_init(crypto_provider_t, crypto_session_id_t,
    crypto_object_attribute_t *, uint_t, void **, crypto_call_req_t *);
extern int crypto_object_find(crypto_provider_t, void *, crypto_object_id_t *,
    uint_t *, uint_t, crypto_call_req_t *);
extern int crypto_object_set_attribute_value(crypto_provider_t,
    crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *,
    uint_t, crypto_call_req_t *);

/* Key Management */
extern int crypto_key_derive(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_object_attribute_t *,
    uint_t, crypto_object_id_t *, crypto_call_req_t *);
extern int crypto_key_generate(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
    crypto_object_id_t *, crypto_call_req_t *);
extern int crypto_key_generate_pair(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
    crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
    crypto_object_id_t *, crypto_call_req_t *);
extern int crypto_key_unwrap(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, uchar_t *, size_t *,
    crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
    crypto_call_req_t *);
extern int crypto_key_wrap(crypto_provider_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_object_id_t *, uchar_t *,
    size_t *, crypto_call_req_t *);
extern int crypto_key_check_prov(crypto_provider_t, crypto_mechanism_t *mech,
    crypto_key_t *key);
extern int crypto_key_check(crypto_mechanism_t *mech, crypto_key_t *key);


/*
 * Routines to cancel a single asynchronous request or all asynchronous
 * requests associated with a particular context.
 */
extern void crypto_cancel_req(crypto_req_id_t req);
extern void crypto_cancel_ctx(crypto_context_t ctx);

/*
 * crypto_get_mech_list(9F) allocates and returns the list of currently
 * supported cryptographic mechanisms.
 */
extern crypto_mech_name_t *crypto_get_mech_list(uint_t *count, int kmflag);
extern void crypto_free_mech_list(crypto_mech_name_t *mech_names,
    uint_t count);

extern crypto_provider_t crypto_get_provider(char *, char *, char *);
extern int crypto_get_provinfo(crypto_provider_t, crypto_provider_ext_info_t *);
extern void crypto_release_provider(crypto_provider_t);

/*
 * A kernel consumer can request to be notified when some particular event
 * occurs. The valid events, callback function type, and functions to
 * be called to register or unregister for notification are defined below.
 */

#define	CRYPTO_EVENT_MECHS_CHANGED		0x00000001
#define	CRYPTO_EVENT_PROVIDER_REGISTERED	0x00000002
#define	CRYPTO_EVENT_PROVIDER_UNREGISTERED	0x00000004

typedef enum {
	CRYPTO_MECH_ADDED = 1,
	CRYPTO_MECH_REMOVED
} crypto_event_change_t;

/* The event_arg argument structure for CRYPTO_EVENT_PROVIDERS_CHANGE event */
typedef struct crypto_notify_event_change {
	crypto_mech_name_t ec_mech_name;
	crypto_provider_type_t ec_provider_type;
	crypto_event_change_t ec_change;
} crypto_notify_event_change_t;

typedef void *crypto_notify_handle_t;
typedef void (*crypto_notify_callback_t)(uint32_t event_mask, void *event_arg);

extern crypto_notify_handle_t crypto_notify_events(
    crypto_notify_callback_t nf, uint32_t event_mask);
extern void crypto_unnotify_events(crypto_notify_handle_t);

/*
 * crypto_bufcall(9F) group of routines.
 */
extern crypto_bc_t crypto_bufcall_alloc(void);
extern int crypto_bufcall_free(crypto_bc_t bc);
extern int crypto_bufcall(crypto_bc_t bc, void (*func)(void *arg), void *arg);
extern int crypto_unbufcall(crypto_bc_t bc);

/*
 * To obtain the list of key size ranges supported by a mechanism.
 */

#define	CRYPTO_MECH_USAGE_ENCRYPT	0x00000001
#define	CRYPTO_MECH_USAGE_DECRYPT	0x00000002
#define	CRYPTO_MECH_USAGE_MAC		0x00000004

typedef	uint32_t crypto_mech_usage_t;

typedef struct crypto_mechanism_info {
	size_t mi_min_key_size;
	size_t mi_max_key_size;
	crypto_keysize_unit_t mi_keysize_unit; /* for mi_xxx_key_size */
	crypto_mech_usage_t mi_usage;
} crypto_mechanism_info_t;

#ifdef	_SYSCALL32

typedef struct crypto_mechanism_info32 {
	size32_t mi_min_key_size;
	size32_t mi_max_key_size;
	crypto_keysize_unit_t mi_keysize_unit; /* for mi_xxx_key_size */
	crypto_mech_usage_t mi_usage;
} crypto_mechanism_info32_t;

#endif	/* _SYSCALL32 */

extern int crypto_get_all_mech_info(crypto_mech_type_t,
    crypto_mechanism_info_t **, uint_t *, int);
extern void crypto_free_all_mech_info(crypto_mechanism_info_t *, uint_t);

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_API_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * Copyright 2013 Saso Kiselkov.  All rights reserved.
 * Copyright 2023 RackTop Systems, Inc.
 */

#ifndef _SYS_CRYPTO_COMMON_H
#define	_SYS_CRYPTO_COMMON_H

/*
 * Header file for the common data structures of the cryptographic framework
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stream.h>
#include <sys/mutex.h>
#include <sys/condvar.h>


/* Cryptographic Mechanisms */

#define	CRYPTO_MAX_MECH_NAME 32
typedef char crypto_mech_name_t[CRYPTO_MAX_MECH_NAME];

typedef uint64_t crypto_mech_type_t;

typedef struct crypto_mechanism {
	crypto_mech_type_t	cm_type;	/* mechanism type */
	caddr_t			cm_param;	/* mech. parameter */
	size_t			cm_param_len;	/* mech. parameter len */
} crypto_mechanism_t;

#ifdef  _SYSCALL32

typedef struct crypto_mechanism32 {
	crypto_mech_type_t	cm_type;	/* mechanism type */
	caddr32_t		cm_param;	/* mech. parameter */
	size32_t		cm_param_len;   /* mech. parameter len */
} crypto_mechanism32_t;

#endif  /* _SYSCALL32 */

/*
 * New structures that have internal pointers need a custom me_copyin_param
 * and me_free_param function added to their kcf_mech_entry_t.
 * See: kcf_copyin_*_param in kcf_mech_tabs.c.
 */

#ifdef _KERNEL
/* CK_AES_CTR_PARAMS provides parameters to the CKM_AES_CTR mechanism */
typedef struct CK_AES_CTR_PARAMS {
	ulong_t	ulCounterBits;
	uint8_t cb[16];
} CK_AES_CTR_PARAMS;
#endif

/* CK_AES_CCM_PARAMS provides parameters to the CKM_AES_CCM mechanism */
typedef struct CK_AES_CCM_PARAMS {
	ulong_t ulMACSize;
	ulong_t ulNonceSize;
	ulong_t ulAuthDataSize;
	ulong_t ulDataSize; /* used for plaintext or ciphertext */
	uchar_t *nonce;
	uchar_t *authData;
} CK_AES_CCM_PARAMS;

/* CK_AES_GCM_PARAMS provides parameters to the CKM_AES_GCM mechanism */
typedef struct CK_AES_GCM_PARAMS {
	uchar_t *pIv;
	ulong_t ulIvLen;
	ulong_t ulIvBits;
	uchar_t *pAAD;
	ulong_t ulAADLen;
	ulong_t ulTagBits;
} CK_AES_GCM_PARAMS;

/* CK_AES_GMAC_PARAMS provides parameters to the CKM_AES_GMAC mechanism */
typedef struct CK_AES_GMAC_PARAMS {
	uchar_t *pIv;	/* Length is AES_GMAC_IV_LEN */
	uchar_t *pAAD;
	ulong_t ulAADLen;
} CK_AES_GMAC_PARAMS;

#ifdef _KERNEL
/*
 * CK_ECDH1_DERIVE_PARAMS provides the parameters to the
 * CKM_ECDH1_KEY_DERIVE mechanism
 */
typedef struct CK_ECDH1_DERIVE_PARAMS {
	ulong_t		kdf;
	ulong_t		ulSharedDataLen;
	uchar_t		*pSharedData;
	ulong_t		ulPublicDataLen;
	uchar_t		*pPublicData;
} CK_ECDH1_DERIVE_PARAMS;
#endif

#ifdef _KERNEL
#ifdef  _SYSCALL32

/* needed for 32-bit applications running on 64-bit kernels */
typedef struct CK_AES_CTR_PARAMS32 {
	uint32_t ulCounterBits;
	uint8_t cb[16];
} CK_AES_CTR_PARAMS32;

/* needed for 32-bit applications running on 64-bit kernels */
typedef struct CK_AES_CCM_PARAMS32 {
	uint32_t ulMACSize;
	uint32_t ulNonceSize;
	uint32_t ulAuthDataSize;
	uint32_t ulDataSize;
	caddr32_t nonce;
	caddr32_t authData;
} CK_AES_CCM_PARAMS32;

/* needed for 32-bit applications running on 64-bit kernels */
typedef struct CK_AES_GCM_PARAMS32 {
	caddr32_t pIv;
	uint32_t ulIvLen;
	uint32_t ulIvBits;
	caddr32_t pAAD;
	uint32_t ulAADLen;
	uint32_t ulTagBits;
} CK_AES_GCM_PARAMS32;

/* needed for 32-bit applications running on 64-bit kernels */
typedef struct CK_AES_GMAC_PARAMS32 {
	caddr32_t pIv;
	caddr32_t pAAD;
	uint32_t ulAADLen;
} CK_AES_GMAC_PARAMS32;

typedef struct CK_ECDH1_DERIVE_PARAMS32 {
	uint32_t	kdf;
	uint32_t	ulSharedDataLen;
	caddr32_t	pSharedData;
	uint32_t	ulPublicDataLen;
	caddr32_t	pPublicData;
} CK_ECDH1_DERIVE_PARAMS32;

#endif  /* _SYSCALL32 */
#endif /* _KERNEL */

/*
 * The measurement unit bit flag for a mechanism's minimum or maximum key size.
 * The unit are mechanism dependent.  It can be in bits or in bytes.
 */
typedef uint32_t crypto_keysize_unit_t;

/*
 * The following bit flags are valid in cm_mech_flags field in
 * the crypto_mech_info_t structure of the SPI.
 *
 * Only the first two bit flags are valid in mi_keysize_unit
 * field in the crypto_mechanism_info_t structure of the API.
 */
#define	CRYPTO_KEYSIZE_UNIT_IN_BITS	0x00000001
#define	CRYPTO_KEYSIZE_UNIT_IN_BYTES	0x00000002
#define	CRYPTO_CAN_SHARE_OPSTATE	0x00000004 /* supports sharing */


/* Mechanisms supported out-of-the-box */
#define	SUN_CKM_MD4			"CKM_MD4"
#define	SUN_CKM_MD5			"CKM_MD5"
#define	SUN_CKM_MD5_HMAC		"CKM_MD5_HMAC"
#define	SUN_CKM_MD5_HMAC_GENERAL	"CKM_MD5_HMAC_GENERAL"
#define	SUN_CKM_SHA1			"CKM_SHA_1"
#define	SUN_CKM_SHA1_HMAC		"CKM_SHA_1_HMAC"
#define	SUN_CKM_SHA1_HMAC_GENERAL	"CKM_SHA_1_HMAC_GENERAL"
#define	SUN_CKM_SHA256			"CKM_SHA256"
#define	SUN_CKM_SHA256_HMAC		"CKM_SHA256_HMAC"
#define	SUN_CKM_SHA256_HMAC_GENERAL	"CKM_SHA256_HMAC_GENERAL"
#define	SUN_CKM_SHA384			"CKM_SHA384"
#define	SUN_CKM_SHA384_HMAC		"CKM_SHA384_HMAC"
#define	SUN_CKM_SHA384_HMAC_GENERAL	"CKM_SHA384_HMAC_GENERAL"
#define	SUN_CKM_SHA512			"CKM_SHA512"
#define	SUN_CKM_SHA512_HMAC		"CKM_SHA512_HMAC"
#define	SUN_CKM_SHA512_HMAC_GENERAL	"CKM_SHA512_HMAC_GENERAL"
#define	SUN_CKM_SHA512_224		"CKM_SHA512_224"
#define	SUN_CKM_SHA512_256		"CKM_SHA512_256"
#define	SUN_CKM_DES_CBC			"CKM_DES_CBC"
#define	SUN_CKM_DES3_CBC		"CKM_DES3_CBC"
#define	SUN_CKM_DES_ECB			"CKM_DES_ECB"
#define	SUN_CKM_DES3_ECB		"CKM_DES3_ECB"
#define	SUN_CKM_BLOWFISH_CBC		"CKM_BLOWFISH_CBC"
#define	SUN_CKM_BLOWFISH_ECB		"CKM_BLOWFISH_ECB"
#define	SUN_CKM_AES_CBC			"CKM_AES_CBC"
#define	SUN_CKM_AES_CMAC		"CKM_AES_CMAC"
#define	SUN_CKM_AES_ECB			"CKM_AES_ECB"
#define	SUN_CKM_AES_CTR			"CKM_AES_CTR"
#define	SUN_CKM_AES_CCM			"CKM_AES_CCM"
#define	SUN_CKM_AES_GCM			"CKM_AES_GCM"
#define	SUN_CKM_AES_GMAC		"CKM_AES_GMAC"
#define	SUN_CKM_AES_CFB128		"CKM_AES_CFB128"
#define	SUN_CKM_RC4			"CKM_RC4"
#define	SUN_CKM_RSA_PKCS		"CKM_RSA_PKCS"
#define	SUN_CKM_RSA_X_509		"CKM_RSA_X_509"
#define	SUN_CKM_MD5_RSA_PKCS		"CKM_MD5_RSA_PKCS"
#define	SUN_CKM_SHA1_RSA_PKCS		"CKM_SHA1_RSA_PKCS"
#define	SUN_CKM_SHA256_RSA_PKCS		"CKM_SHA256_RSA_PKCS"
#define	SUN_CKM_SHA384_RSA_PKCS		"CKM_SHA384_RSA_PKCS"
#define	SUN_CKM_SHA512_RSA_PKCS		"CKM_SHA512_RSA_PKCS"
#define	SUN_CKM_EC_KEY_PAIR_GEN		"CKM_EC_KEY_PAIR_GEN"
#define	SUN_CKM_ECDH1_DERIVE		"CKM_ECDH1_DERIVE"
#define	SUN_CKM_ECDSA_SHA1		"CKM_ECDSA_SHA1"
#define	SUN_CKM_ECDSA			"CKM_ECDSA"

/* Shared operation context format for CKM_RC4 */
typedef struct {
#if defined(__amd64)
	uint32_t	i, j;
	uint32_t	arr[256];
	uint32_t	flag;
#else
	uchar_t		arr[256];
	uchar_t		i, j;
#endif /* __amd64 */
	uint64_t	pad;		/* For 64-bit alignment */
} arcfour_state_t;

/* Data arguments of cryptographic operations */

typedef enum crypto_data_format {
	CRYPTO_DATA_RAW = 1,
	CRYPTO_DATA_UIO,
	CRYPTO_DATA_MBLK
} crypto_data_format_t;

typedef struct crypto_data {
	crypto_data_format_t	cd_format;	/* Format identifier	*/
	off_t			cd_offset;	/* Offset from the beginning */
	size_t			cd_length;	/* # of bytes in use */
	caddr_t			cd_miscdata;	/* ancillary data */
	union {
		/* Raw format */
		iovec_t cdu_raw;		/* Pointer and length	    */

		/* uio scatter-gather format */
		uio_t	*cdu_uio;

		/* mblk scatter-gather format */
		mblk_t	*cdu_mp;		/* The mblk chain */

	} cdu;	/* Crypto Data Union */
} crypto_data_t;

#define	cd_raw		cdu.cdu_raw
#define	cd_uio		cdu.cdu_uio
#define	cd_mp		cdu.cdu_mp

typedef struct crypto_dual_data {
	crypto_data_t		dd_data;	/* The data */
	off_t			dd_offset2;	/* Used by dual operation */
	size_t			dd_len2;	/* # of bytes to take	*/
} crypto_dual_data_t;

#define	dd_format	dd_data.cd_format
#define	dd_offset1	dd_data.cd_offset
#define	dd_len1		dd_data.cd_length
#define	dd_miscdata	dd_data.cd_miscdata
#define	dd_raw		dd_data.cd_raw
#define	dd_uio		dd_data.cd_uio
#define	dd_mp		dd_data.cd_mp

/* The keys, and their contents */

typedef enum {
	CRYPTO_KEY_RAW = 1,	/* ck_data is a cleartext key */
	CRYPTO_KEY_REFERENCE,	/* ck_obj_id is an opaque reference */
	CRYPTO_KEY_ATTR_LIST	/* ck_attrs is a list of object attributes */
} crypto_key_format_t;

typedef uint64_t crypto_attr_type_t;

/* Attribute types to use for passing a RSA public key or a private key. */
#define	SUN_CKA_MODULUS			0x00000120
#define	SUN_CKA_MODULUS_BITS		0x00000121
#define	SUN_CKA_PUBLIC_EXPONENT		0x00000122
#define	SUN_CKA_PRIVATE_EXPONENT	0x00000123
#define	SUN_CKA_PRIME_1			0x00000124
#define	SUN_CKA_PRIME_2			0x00000125
#define	SUN_CKA_EXPONENT_1		0x00000126
#define	SUN_CKA_EXPONENT_2		0x00000127
#define	SUN_CKA_COEFFICIENT		0x00000128
#define	SUN_CKA_PRIME			0x00000130
#define	SUN_CKA_SUBPRIME		0x00000131
#define	SUN_CKA_BASE			0x00000132

#define	CKK_EC			0x00000003UL
#define	CKK_GENERIC_SECRET	0x00000010UL
#define	CKK_RC4			0x00000012UL
#define	CKK_AES			0x0000001FUL
#define	CKK_DES			0x00000013UL
#define	CKK_DES2		0x00000014UL
#define	CKK_DES3		0x00000015UL

#define	CKO_PUBLIC_KEY		0x00000002UL
#define	CKO_PRIVATE_KEY		0x00000003UL
#define	CKA_CLASS		0x00000000UL
#define	CKA_VALUE		0x00000011UL
#define	CKA_KEY_TYPE		0x00000100UL
#define	CKA_VALUE_LEN		0x00000161UL
#define	CKA_EC_PARAMS		0x00000180UL
#define	CKA_EC_POINT		0x00000181UL

typedef uint32_t	crypto_object_id_t;

typedef struct crypto_object_attribute {
	crypto_attr_type_t	oa_type;	/* attribute type */
	caddr_t			oa_value;	/* attribute value */
	ssize_t			oa_value_len;	/* length of attribute value */
} crypto_object_attribute_t;

typedef struct crypto_key {
	crypto_key_format_t	ck_format;	/* format identifier */
	union {
		/* for CRYPTO_KEY_RAW ck_format */
		struct {
			uint_t	cku_v_length;	/* # of bits in ck_data   */
			void	*cku_v_data;	/* ptr to key value */
		} cku_key_value;

		/* for CRYPTO_KEY_REFERENCE ck_format */
		crypto_object_id_t cku_key_id;	/* reference to object key */

		/* for CRYPTO_KEY_ATTR_LIST ck_format */
		struct {
			uint_t cku_a_count;	/* number of attributes */
			crypto_object_attribute_t *cku_a_oattr;
		} cku_key_attrs;
	} cku_data;				/* Crypto Key union */
} crypto_key_t;

#ifdef  _SYSCALL32

typedef struct crypto_object_attribute32 {
	uint64_t	oa_type;	/* attribute type */
	caddr32_t	oa_value;	/* attribute value */
	ssize32_t	oa_value_len;	/* length of attribute value */
} crypto_object_attribute32_t;

typedef struct crypto_key32 {
	crypto_key_format_t	ck_format;	/* format identifier */
	union {
		/* for CRYPTO_KEY_RAW ck_format */
		struct {
			uint32_t cku_v_length;	/* # of bytes in ck_data */
			caddr32_t cku_v_data;	/* ptr to key value */
		} cku_key_value;

		/* for CRYPTO_KEY_REFERENCE ck_format */
		crypto_object_id_t cku_key_id; /* reference to object key */

		/* for CRYPTO_KEY_ATTR_LIST ck_format */
		struct {
			uint32_t cku_a_count;	/* number of attributes */
			caddr32_t cku_a_oattr;
		} cku_key_attrs;
	} cku_data;				/* Crypto Key union */
} crypto_key32_t;

#endif  /* _SYSCALL32 */

#define	ck_data		cku_data.cku_key_value.cku_v_data
#define	ck_length	cku_data.cku_key_value.cku_v_length
#define	ck_obj_id	cku_data.cku_key_id
#define	ck_count	cku_data.cku_key_attrs.cku_a_count
#define	ck_attrs	cku_data.cku_key_attrs.cku_a_oattr

/*
 * Raw key lengths are expressed in number of bits.
 * The following macro returns the minimum number of
 * bytes that can contain the specified number of bits.
 * Round up without overflowing the integer type.
 */
#define	CRYPTO_BITS2BYTES(n) ((n) == 0 ? 0 : (((n) - 1) >> 3) + 1)
#define	CRYPTO_BYTES2BITS(n) ((n) << 3)

/* Providers */

typedef enum {
	CRYPTO_HW_PROVIDER = 0,
	CRYPTO_SW_PROVIDER,
	CRYPTO_LOGICAL_PROVIDER
} crypto_provider_type_t;

typedef uint32_t	crypto_provider_id_t;
#define	KCF_PROVID_INVALID	((uint32_t)-1)

typedef struct crypto_provider_entry {
	crypto_provider_id_t	pe_provider_id;
	uint_t			pe_mechanism_count;
} crypto_provider_entry_t;

typedef struct crypto_dev_list_entry {
	char			le_dev_name[MAXNAMELEN];
	uint_t			le_dev_instance;
	uint_t			le_mechanism_count;
} crypto_dev_list_entry_t;

/* User type for authentication ioctls and SPI entry points */

typedef enum crypto_user_type {
	CRYPTO_SO = 0,
	CRYPTO_USER
} crypto_user_type_t;

/* Version for provider management ioctls and SPI entry points */

typedef struct crypto_version {
	uchar_t	cv_major;
	uchar_t	cv_minor;
} crypto_version_t;

/* session data structure opaque to the consumer */
typedef void *crypto_session_t;

/* provider data structure opaque to the consumer */
typedef void *crypto_provider_t;

/* Limits used by both consumers and providers */
#define	CRYPTO_EXT_SIZE_LABEL		32
#define	CRYPTO_EXT_SIZE_MANUF		32
#define	CRYPTO_EXT_SIZE_MODEL		16
#define	CRYPTO_EXT_SIZE_SERIAL		16
#define	CRYPTO_EXT_SIZE_TIME		16

typedef struct crypto_provider_ext_info {
	uchar_t			ei_label[CRYPTO_EXT_SIZE_LABEL];
	uchar_t			ei_manufacturerID[CRYPTO_EXT_SIZE_MANUF];
	uchar_t			ei_model[CRYPTO_EXT_SIZE_MODEL];
	uchar_t			ei_serial_number[CRYPTO_EXT_SIZE_SERIAL];
	ulong_t			ei_flags;
	ulong_t			ei_max_session_count;
	ulong_t			ei_max_pin_len;
	ulong_t			ei_min_pin_len;
	ulong_t			ei_total_public_memory;
	ulong_t			ei_free_public_memory;
	ulong_t			ei_total_private_memory;
	ulong_t			ei_free_private_memory;
	crypto_version_t	ei_hardware_version;
	crypto_version_t	ei_firmware_version;
	uchar_t			ei_time[CRYPTO_EXT_SIZE_TIME];
	int			ei_hash_max_input_len;
	int			ei_hmac_max_input_len;
} crypto_provider_ext_info_t;

typedef uint_t		crypto_session_id_t;

typedef enum cmd_type {
	COPY_FROM_DATA,
	COPY_TO_DATA,
	COMPARE_TO_DATA,
	MD5_DIGEST_DATA,
	SHA1_DIGEST_DATA,
	SHA2_DIGEST_DATA,
	GHASH_DATA
} cmd_type_t;

#define	CRYPTO_DO_UPDATE	0x01
#define	CRYPTO_DO_FINAL		0x02
#define	CRYPTO_DO_MD5		0x04
#define	CRYPTO_DO_SHA1		0x08
#define	CRYPTO_DO_SIGN		0x10
#define	CRYPTO_DO_VERIFY	0x20
#define	CRYPTO_DO_SHA2		0x40

#define	PROVIDER_OWNS_KEY_SCHEDULE	0x00000001

/*
 * Common cryptographic status and error codes.
 */
#define	CRYPTO_SUCCESS				0x00000000
#define	CRYPTO_CANCEL				0x00000001
#define	CRYPTO_HOST_MEMORY			0x00000002
#define	CRYPTO_GENERAL_ERROR			0x00000003
#define	CRYPTO_FAILED				0x00000004
#define	CRYPTO_ARGUMENTS_BAD			0x00000005
#define	CRYPTO_ATTRIBUTE_READ_ONLY		0x00000006
#define	CRYPTO_ATTRIBUTE_SENSITIVE		0x00000007
#define	CRYPTO_ATTRIBUTE_TYPE_INVALID		0x00000008
#define	CRYPTO_ATTRIBUTE_VALUE_INVALID		0x00000009
#define	CRYPTO_CANCELED				0x0000000A
#define	CRYPTO_DATA_INVALID			0x0000000B
#define	CRYPTO_DATA_LEN_RANGE			0x0000000C
#define	CRYPTO_DEVICE_ERROR			0x0000000D
#define	CRYPTO_DEVICE_MEMORY			0x0000000E
#define	CRYPTO_DEVICE_REMOVED			0x0000000F
#define	CRYPTO_ENCRYPTED_DATA_INVALID		0x00000010
#define	CRYPTO_ENCRYPTED_DATA_LEN_RANGE		0x00000011
#define	CRYPTO_KEY_HANDLE_INVALID		0x00000012
#define	CRYPTO_KEY_SIZE_RANGE			0x00000013
#define	CRYPTO_KEY_TYPE_INCONSISTENT		0x00000014
#define	CRYPTO_KEY_NOT_NEEDED			0x00000015
#define	CRYPTO_KEY_CHANGED			0x00000016
#define	CRYPTO_KEY_NEEDED			0x00000017
#define	CRYPTO_KEY_INDIGESTIBLE			0x00000018
#define	CRYPTO_KEY_FUNCTION_NOT_PERMITTED	0x00000019
#define	CRYPTO_KEY_NOT_WRAPPABLE		0x0000001A
#define	CRYPTO_KEY_UNEXTRACTABLE		0x0000001B
#define	CRYPTO_MECHANISM_INVALID		0x0000001C
#define	CRYPTO_MECHANISM_PARAM_INVALID		0x0000001D
#define	CRYPTO_OBJECT_HANDLE_INVALID		0x0000001E
#define	CRYPTO_OPERATION_IS_ACTIVE		0x0000001F
#define	CRYPTO_OPERATION_NOT_INITIALIZED	0x00000020
#define	CRYPTO_PIN_INCORRECT			0x00000021
#define	CRYPTO_PIN_INVALID			0x00000022
#define	CRYPTO_PIN_LEN_RANGE			0x00000023
#define	CRYPTO_PIN_EXPIRED			0x00000024
#define	CRYPTO_PIN_LOCKED			0x00000025
#define	CRYPTO_SESSION_CLOSED			0x00000026
#define	CRYPTO_SESSION_COUNT			0x00000027
#define	CRYPTO_SESSION_HANDLE_INVALID		0x00000028
#define	CRYPTO_SESSION_READ_ONLY		0x00000029
#define	CRYPTO_SESSION_EXISTS			0x0000002A
#define	CRYPTO_SESSION_READ_ONLY_EXISTS		0x0000002B
#define	CRYPTO_SESSION_READ_WRITE_SO_EXISTS	0x0000002C
#define	CRYPTO_SIGNATURE_INVALID		0x0000002D
#define	CRYPTO_SIGNATURE_LEN_RANGE		0x0000002E
#define	CRYPTO_TEMPLATE_INCOMPLETE		0x0000002F
#define	CRYPTO_TEMPLATE_INCONSISTENT		0x00000030
#define	CRYPTO_UNWRAPPING_KEY_HANDLE_INVALID	0x00000031
#define	CRYPTO_UNWRAPPING_KEY_SIZE_RANGE	0x00000032
#define	CRYPTO_UNWRAPPING_KEY_TYPE_INCONSISTENT	0x00000033
#define	CRYPTO_USER_ALREADY_LOGGED_IN		0x00000034
#define	CRYPTO_USER_NOT_LOGGED_IN		0x00000035
#define	CRYPTO_USER_PIN_NOT_INITIALIZED		0x00000036
#define	CRYPTO_USER_TYPE_INVALID		0x00000037
#define	CRYPTO_USER_ANOTHER_ALREADY_LOGGED_IN	0x00000038
#define	CRYPTO_USER_TOO_MANY_TYPES		0x00000039
#define	CRYPTO_WRAPPED_KEY_INVALID		0x0000003A
#define	CRYPTO_WRAPPED_KEY_LEN_RANGE		0x0000003B
#define	CRYPTO_WRAPPING_KEY_HANDLE_INVALID	0x0000003C
#define	CRYPTO_WRAPPING_KEY_SIZE_RANGE		0x0000003D
#define	CRYPTO_WRAPPING_KEY_TYPE_INCONSISTENT	0x0000003E
#define	CRYPTO_RANDOM_SEED_NOT_SUPPORTED	0x0000003F
#define	CRYPTO_RANDOM_NO_RNG			0x00000040
#define	CRYPTO_DOMAIN_PARAMS_INVALID		0x00000041
#define	CRYPTO_BUFFER_TOO_SMALL			0x00000042
#define	CRYPTO_INFORMATION_SENSITIVE		0x00000043
#define	CRYPTO_NOT_SUPPORTED			0x00000044

#define	CRYPTO_QUEUED				0x00000045
#define	CRYPTO_BUFFER_TOO_BIG			0x00000046
#define	CRYPTO_INVALID_CONTEXT			0x00000047
#define	CRYPTO_INVALID_MAC			0x00000048
#define	CRYPTO_MECH_NOT_SUPPORTED		0x00000049
#define	CRYPTO_INCONSISTENT_ATTRIBUTE		0x0000004A
#define	CRYPTO_NO_PERMISSION			0x0000004B
#define	CRYPTO_INVALID_PROVIDER_ID		0x0000004C
#define	CRYPTO_VERSION_MISMATCH			0x0000004D
#define	CRYPTO_BUSY				0x0000004E
#define	CRYPTO_UNKNOWN_PROVIDER			0x0000004F
#define	CRYPTO_MODVERIFICATION_FAILED		0x00000050
#define	CRYPTO_OLD_CTX_TEMPLATE			0x00000051
#define	CRYPTO_WEAK_KEY				0x00000052
#define	CRYPTO_FIPS140_ERROR			0x00000053
/*
 * Don't forget to update CRYPTO_LAST_ERROR and the error_number_table[]
 * in kernelUtil.c when new error code is added.
 */
#define	CRYPTO_LAST_ERROR			0x00000053

/*
 * Special values that can be used to indicate that information is unavailable
 * or that there is not practical limit. These values can be used
 * by fields of the SPI crypto_provider_ext_info(9S) structure.
 * The value of CRYPTO_UNAVAILABLE_INFO should be the same as
 * CK_UNAVAILABLE_INFO in the PKCS#11 spec.
 */
#define	CRYPTO_UNAVAILABLE_INFO		((ulong_t)(-1))
#define	CRYPTO_EFFECTIVELY_INFINITE	0x0

#ifdef __cplusplus
}
#endif

#endif /* _SYS_CRYPTO_COMMON_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYS_CRYPTO_DCA_H
#define	_SYS_CRYPTO_DCA_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/varargs.h>

#include <sys/crypto/spi.h>

/*
 * Deimos - cryptographic acceleration based upon Broadcom 582x.
 *
 * Note: Everything in this file is private to the Deimos device
 *	 driver!  Do not include this in any other file.
 */

#define	DRIVER			"dca"
#define	DCA_MANUFACTURER_ID	"SUNWdca"

#ifdef _KERNEL

/*
 * Tunables.
 */
#define	MCR1LOWATER	16	/* these numbers favor overall throughput */
#define	MCR1HIWATER	24
#define	MCR1MAXREQS	8
#define	MCR2LOWATER	16
#define	MCR2HIWATER	24
#define	MCR2MAXREQS	4
#define	MAXMCR		2	/* there are 2 mcrs */
#define	MAXREQSPERMCR	16	/* there are 4 subunits serviced by MCR2 */
#define	MAXFRAGS	6	/* Limit on the number of fragments */
#define	MAXWORK		6	/* How many work structures to preallocate */

/*
 * These are constants.  Do not change them.
 */
#if defined(i386) || defined(__i386) || defined(__amd64)
#define	MAXPACKET	0xefff	/* rootnex INT_MAX_BUF hack. */
#else
#define	MAXPACKET	0xffff	/* Max size of a packet or fragment */
#endif
#define	DESBLOCK	8	/* Size of a DES or 3DES block */
#define	DSAPARTLEN	20	/* Size of fixed DSA parts (r, s, q, x, v) */
#define	DSASIGLEN	40	/* Size of a DSA signature */
#define	SHA1LEN		20	/* Size of a SHA1 hash */
#define	SECOND		1000000	/* One second in usec */
#define	MSEC		1000	/* One millisecond in usec */
#define	DES_KEYSIZE	8
#define	DES_IV_LEN	8
#define	DES3_KEYSIZE	(3 * DES_KEYSIZE)

/*
 * Mechanism info structure passed to KCF during registration.
 */

#define	MD5_HMAC_BLOCK_SIZE	64	/* MD5-HMAC block size */
#define	MD5_HMAC_MIN_KEY_LEN	1	/* MD5-HMAC min key length in bytes */
#define	MD5_HMAC_MAX_KEY_LEN	64	/* MD5-HMAC max key length in bytes */

#define	SHA1_HMAC_BLOCK_SIZE	64	/* SHA1-HMAC block size */
#define	SHA1_HMAC_MIN_KEY_LEN	1	/* SHA1-HMAC min key length in bytes */
#define	SHA1_HMAC_MAX_KEY_LEN	64	/* SHA1-HMAC max key length in bytes */

#define	DES_KEY_LEN		8	/* DES key length in bytes */
#define	DES3_MIN_KEY_LEN	16	/* 3DES min key length in bytes */
#define	DES3_MAX_KEY_LEN	24	/* 3DES max key length in bytes */

#define	DSA_MIN_KEY_LEN		64	/* DSA min key length in bytes */
#define	DSA_MAX_KEY_LEN		128	/* DSA max key length in bytes */

#define	RSA_MIN_KEY_LEN		32	/* RSA min key length in bytes */
#define	RSA_MAX_KEY_LEN		256	/* RSA max key length in bytes */

/*
 * RSA implementation.
 */

#define	DCA_RSA_ENC	0
#define	DCA_RSA_DEC	1
#define	DCA_RSA_SIGN	2
#define	DCA_RSA_VRFY	3
#define	DCA_RSA_SIGNR	4
#define	DCA_RSA_VRFYR	5

/*
 * DSA implementation.
 */

#define	DCA_DSA_SIGN	0
#define	DCA_DSA_VRFY	1

/*
 * FMA eclass index definitions. Note that this enum must be consistent
 * with the dca_fma_eclass_sca1000 and dca_fma_eclass_sca500 string arrays.
 */
typedef enum dca_fma_eclass {
	DCA_FM_ECLASS_HW_DEVICE = 0,
	DCA_FM_ECLASS_HW_TIMEOUT,
	DCA_FM_ECLASS_NONE
} dca_fma_eclass_t;

/*
 * Forward typedefs.
 */
typedef struct dca dca_t;
typedef struct dca_chain dca_chain_t;
typedef struct dca_listnode dca_listnode_t;
typedef struct dca_worklist dca_worklist_t;
typedef struct dca_work dca_work_t;
typedef struct dca_request dca_request_t;
typedef struct dca_stat dca_stat_t;
typedef struct dca_cookie dca_cookie_t;
typedef struct dca_device dca_device_t;

/*
 * This structure is used to identify a specific board.
 */
struct dca_device {
	ushort_t		dd_vendor_id;
	ushort_t		dd_device_id;
	char			*dd_model;
};

/*
 * Structure representing a node in a DMA chain.  (Broadcom calls
 * these "Data Buffer Chain Entries".)
 *
 * note, this structure must be a multiple of sizeof (intptr_t)
 */
struct dca_chain {
	/* the descriptor */
	caddr_t			dc_desc_kaddr;
	/* and the buffer to which it points */
	size_t			dc_buffer_length;
	ddi_dma_handle_t	dc_buffer_dmah;
	caddr_t			dc_buffer_kaddr;
	/* physical addresses */
	uint32_t		dc_desc_paddr;
	uint32_t		dc_buffer_paddr;
	uint32_t		dc_next_paddr;
};

/*
 * Linked-list linkage.
 */
struct dca_listnode {
	dca_listnode_t		*dl_next;
	dca_listnode_t		*dl_prev;
	dca_listnode_t		*dl_next2;
	dca_listnode_t		*dl_prev2;
};

typedef enum dca_mech_type {
	DES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_DES_CBC */
	DES3_CBC_MECH_INFO_TYPE,	/* SUN_CKM_DES3_CBC */
	DSA_MECH_INFO_TYPE,		/* SUN_CKM_DSA */
	RSA_X_509_MECH_INFO_TYPE,	/* SUN_CKM_RSA_X_509 */
	RSA_PKCS_MECH_INFO_TYPE		/* SUN_CKM_RSA_PKCS */
} dca_mech_type_t;

#define	SUN_CKM_DSA			"CKM_DSA"

struct dca_rng {
	uint32_t		dr_chunklen;
};

union dca_parameters {
	struct dca_rng		dp_rng;
};

typedef struct dca_ctx {
	/*
	 * The following are context fields for Deimos 2.0.
	 */
	crypto_mech_type_t	ctx_cm_type;	/* Mechanism type */
	int			mode;		/* Mode of operation */
	int 			atomic;		/* Boolean */

	/* Fields for RSA and DSA */
	uchar_t			*mod;		/* RSA modulus */
	unsigned		modlen;		/* RSA modulus length */
	unsigned		pqfix;		/* RSA flag */

	/* Fields for DES and 3DES */
	uint32_t		iv[2];
	uint32_t		key[6];
	int			residlen;
	uchar_t			resid[DESBLOCK];
	int			activeresidlen;
	uchar_t			activeresid[DESBLOCK];
	crypto_data_t		in_dup;		/* input data duplicate */
} dca_ctx_t;

/*
 * Work structure.  One of these per actual job submitted to an MCR.
 * Contains everything we need to submit the job, and everything we
 * need to notify caller and release resources when the completion
 * interrupt comes.
 */
struct dca_request {
	dca_listnode_t		dr_linkage;
	uint16_t		dr_pkt_length;
	crypto_req_handle_t	dr_kcf_req;
	dca_t			*dr_dca;
	dca_worklist_t		*dr_wlp;
	/*
	 * Consumer's I/O buffers.
	 */
	crypto_data_t		*dr_in;
	crypto_data_t		*dr_out;
	dca_ctx_t		dr_ctx;
	/*
	 * Chains and DMA structures.
	 */
	size_t			dr_dma_size;
	uint32_t		dr_ctx_paddr;
	caddr_t			dr_ctx_kaddr;
	ddi_acc_handle_t	dr_ctx_acch;
	ddi_dma_handle_t	dr_ctx_dmah;
	/*
	 * Scratch input buffer.
	 */
	ddi_acc_handle_t	dr_ibuf_acch;
	ddi_dma_handle_t	dr_ibuf_dmah;
	caddr_t			dr_ibuf_kaddr;
	uint32_t		dr_ibuf_paddr;

	/*
	 * Scratch output buffer.
	 */
	ddi_acc_handle_t	dr_obuf_acch;
	ddi_dma_handle_t	dr_obuf_dmah;
	caddr_t			dr_obuf_kaddr;
	uint32_t		dr_obuf_paddr;

	/*
	 * Values to program MCR with.
	 */
	uint32_t		dr_in_paddr;
	uint32_t		dr_out_paddr;
	uint32_t		dr_in_next;
	uint32_t		dr_out_next;
	uint16_t		dr_in_len;
	uint16_t		dr_out_len;
	/*
	 * Callback.
	 */
	void			(*dr_callback)(dca_request_t *, int);
	/*
	 * Other stuff.
	 */
	uint32_t		dr_flags;
	/*
	 * Algorithm specific parameters.
	 */
	void			*dr_context;
	union dca_parameters	dr_param;
	/*
	 * Statistics.
	 */
	int			dr_job_stat;
	int			dr_byte_stat;

	/* Pre-mapped input and output data buffer chain support */
	dca_chain_t		dr_ibuf_head;
	dca_chain_t		dr_obuf_head;

	/*
	 * User buffers are mapped to DMA handles dynamically. The physically
	 * contigous blocks ( >= a page) are built into a data buffer chain.
	 */
	dca_chain_t		dr_chain_in_head;
	ddi_dma_handle_t	dr_chain_in_dmah;

	dca_chain_t		dr_chain_out_head;
	ddi_dma_handle_t	dr_chain_out_dmah;

	/* Offset in the context page for storing dynamic buffer chains */
	int			dr_offset;

	/* Destroy this request if true */
	int			destroy;
};

/*
 * Request flags (dca_request_t.dr_flags).
 */
#define	DR_INPLACE		0x002
#define	DR_SCATTER		0x004
#define	DR_GATHER		0x008
#define	DR_NOCACHE		0x020
#define	DR_ENCRYPT		0x040
#define	DR_DECRYPT		0x080
#define	DR_TRIPLE		0x100	/* triple DES vs. single DES */
#define	DR_ATOMIC		0x200	/* for atomic operation */

struct dca_work {
	dca_listnode_t		dw_linkage;
	dca_worklist_t		*dw_wlp;

	/* DMA access to the MCR and context */
	ddi_acc_handle_t	dw_mcr_acch;
	ddi_dma_handle_t	dw_mcr_dmah;
	caddr_t			dw_mcr_kaddr;
	uint32_t		dw_mcr_paddr;

	dca_request_t		*dw_reqs[MAXREQSPERMCR];
	clock_t			dw_lbolt;
};

/*
 * MCRs.
 */
#define	MCR1			0x1
#define	MCR2			0x2

struct dca_worklist {
	dca_t			*dwl_dca;
	crypto_kcf_provider_handle_t	dwl_prov;
	char			dwl_name[16];
	int			dwl_mcr;
	kmutex_t		dwl_lock;
	kmutex_t		dwl_freelock;
	kmutex_t		dwl_freereqslock;
	kcondvar_t		dwl_cv;
	dca_listnode_t		dwl_freereqs;	/* available requests */
	dca_listnode_t		dwl_waitq;	/* requests arrive here */
	dca_listnode_t		dwl_freework;	/* available work structures */
	dca_listnode_t		dwl_runq;	/* work structs sent to chip */
	timeout_id_t		dwl_schedtid;
	clock_t			dwl_lastsubmit;
	int			dwl_count;
	int			dwl_busy;
	int			dwl_lowater;
	int			dwl_hiwater;
	int			dwl_reqspermcr;
	int			dwl_drain;	/* for DR (suspend) */
	/* Kstats */
	u_longlong_t		dwl_submit;
	u_longlong_t		dwl_flowctl;
};

/*
 * Operations for MCR1 (bulk stuff).
 */
#define	CMD_IPSEC		0x0	/* IPsec packet processing */
#define	CMD_SSLMAC		0x1	/* SSL HMAC processing */
#define	CMD_TLSMAC		0x2	/* TLS HMAC processing */
#define	CMD_3DES		0x3	/* SSL/TLS/raw 3DES processing */
#define	CMD_RC4			0x4	/* ARCFOUR procesing */
#define	CMD_PUREHASH		0x5	/* Pure MD5/SHA1 hash processing */

/*
 * Operations for MCR2 (key stuff).
 */
#define	CMD_DHPUBLIC		0x1	/* DH public key generation */
#define	CMD_DHSHARED		0x2	/* DH shared secret generation */
#define	CMD_RSAPUBLIC		0x3	/* RSA public key operation */
#define	CMD_RSAPRIVATE		0x4	/* RSA private key operation (CRT) */
#define	CMD_DSASIGN		0x5	/* DSA signing operation */
#define	CMD_DSAVERIFY		0x6	/* DSA verification operation */
#define	CMD_RNGDIRECT		0x41	/* Direct access to the RNG */
#define	CMD_RNGSHA1		0x42	/* RNG output processed by SHA1 */
#define	CMD_MODADD		0x43	/* Modular add */
#define	CMD_MODSUB		0x44	/* Moduler subtract */
#define	CMD_MODMUL		0x45	/* Modular multiply */
#define	CMD_MODREM		0x46	/* Modular remainder */
#define	CMD_MODEXP		0x47	/* Modular exponentiation */
#define	CMD_MODINV		0x48	/* Modular inverse */

/*
 * Kstats.
 */
#define	DS_3DESJOBS		0
#define	DS_3DESBYTES		1
#define	DS_RSAPUBLIC		2
#define	DS_RSAPRIVATE		3
#define	DS_DSASIGN		4
#define	DS_DSAVERIFY		5
#define	DS_RNGJOBS		6
#define	DS_RNGBYTES		7
#define	DS_RNGSHA1JOBS		8
#define	DS_RNGSHA1BYTES		9
#define	DS_MAX			10

#if 0
/*
 * note that when reenabling any of these stats, DS_MAX will need to
 * be adjusted.
 */
#define	DS_RC4JOBS		11
#define	DS_RC4BYTES		12
#define	DS_DHPUBLIC		13
#define	DS_DHSECRET		14
#endif

struct dca_stat {
	kstat_named_t		ds_status;
	kstat_named_t		ds_algs[DS_MAX];
	struct {
		kstat_named_t	ds_submit;
		kstat_named_t	ds_flowctl;
		kstat_named_t	ds_lowater;
		kstat_named_t	ds_hiwater;
		kstat_named_t	ds_maxreqs;
	}			ds_mcr[MAXMCR];
};

/*
 * Blocking structure for ioctls.
 */
struct dca_cookie {
	kmutex_t		dc_mx;
	kcondvar_t		dc_cv;
	int			dc_outstanding;
	int			dc_status;
};

/*
 * Per instance structure.
 */
struct dca {
	dev_info_t		*dca_dip;
	kmutex_t		dca_intrlock;
	caddr_t			dca_regs;
	ddi_acc_handle_t	dca_regs_handle;
	ddi_iblock_cookie_t	dca_icookie;
	timeout_id_t		dca_jobtid;
	ulong_t			dca_pagesize;
	unsigned		dca_flags;	/* dev state flags */

	/*
	 * Work requests.
	 */
	dca_worklist_t		dca_worklist[MAXMCR];

	/*
	 * hardware model
	 */
	char			*dca_model;
	ushort_t		dca_devid;

	/*
	 * Kstats.  There is no standard for what standards
	 * Cryptographic Providers should supply, so we're
	 * making them up for now.
	 */
	kstat_t			*dca_ksp;
	kstat_t			*dca_intrstats;
	u_longlong_t		dca_stats[DS_MAX];

	/* For the local random number pool used internally by the dca driver */
	char 			*dca_buf1;
	char 			*dca_buf2;
	char 			*dca_buf_ptr;
	int 			dca_index;
	uint32_t 		dca_random_filling;
	kmutex_t 		dca_random_lock;

	/* FMA capabilities */
	int			fm_capabilities;	/* FMA capabilities */

	kmutex_t		dca_ctx_list_lock;
	dca_listnode_t		dca_ctx_list;
};

/*
 * Device flags (dca_t.dca_flags)
 */
#define	DCA_FAILED		0x1
#define	DCA_POWERMGMT		0x4
#define	DCA_RNGSHA1		0x8

#define	KIOIP(dca)		KSTAT_INTR_PTR((dca)->dca_intrstats)

/*
 * Scatter/gather checks.
 */
typedef enum dca_sg_param {
	DCA_SG_CONTIG = 1,
	DCA_SG_WALIGN,
	DCA_SG_PALIGN
} dca_sg_param_t;

#define	FALSE		0
#define	TRUE		1

/*
 * PCI configuration registers.
 */
#define	PCI_VENID		0x00	/* vendor id, 16 bits */
#define	PCI_DEVID		0x02	/* device id, 16 bits */
#define	PCI_COMM		0x04	/* command, 16 bits */
#define	PCI_STATUS		0x06	/* status, 16 bits */
#define	PCI_REVID		0x08	/* revision id, 8 bits */
#define	PCI_PROGCLASS		0x09	/* programming class, 8 bits */
#define	PCI_SUBCLASS		0x0A	/* subclass, 8 bits */
#define	PCI_CACHELINESZ		0x0C	/* cache line size, 8 bits */
#define	PCI_LATTMR		0x0D	/* latency timer, 8 bits */
#define	PCI_BIST		0x0F	/* builtin-self-test, 8 bits */
#define	PCI_SUBVENID		0x2C	/* subsystem vendor id, 16 bits */
#define	PCI_SUBSYSID		0x2E	/* subsystem id, 16 bits */
#define	PCI_MINGNT		0x3E	/* min grant for burst, 8 bits */
#define	PCI_MAXLAT		0x3F	/* maximum grant for burst, 8 bits */
#define	PCI_TRDYTO		0x40	/* TRDY timeout, 8 bits */
#define	PCI_RETRIES		0x41	/* retries bus will perform, 8 bits */

/*
 * PCI configuration register bit values.
 */
#define	PCICOMM_FBBE		0x0200	/* fast back-to-back enable */
#define	PCICOMM_SEE		0x0100	/* system error enable */
#define	PCICOMM_PEE		0x0040	/* parity error enable */
#define	PCICOMM_MWIE		0x0010	/* memory write & invalidate enable */
#define	PCICOMM_BME		0x0004	/* bus master enable */
#define	PCICOMM_MAE		0x0002	/* memory access enable */

#define	PCISTAT_PERR		0x8000	/* parity error detected */
#define	PCISTAT_SERR		0x4000	/* system error detected */
#define	PCISTAT_MABRT		0x2000	/* master abort detected */
#define	PCISTAT_TABRT		0x1000	/* target abort detected */
#define	PCISTAT_TABRTS		0x0800	/* target abort signaled */
#define	PCISTAT_PARITY		0x0100	/* data parity error detected */

#define	PCIREVID_DOMESTIC	0x01	/* domestic version */
#define	PCIREVID_EXPORT		0xE1	/* export version */

/* Note: 5820 errata: BIST feature does not work */
#define	PCIBIST_CAP		0x80	/* BIST capable */
#define	PCIBIST_START		0x40	/* start BIST test */
#define	PCIBIST_ERRMASK		0x0F	/* mask of BIST error codes */

/*
 * Command and Status Registers.
 */
#define	CSR_MCR1		0x00	/* pointer to MCR1 (bulk) */
#define	CSR_DMACTL		0x04	/* DMA control */
#define	CSR_DMASTAT		0x08	/* DMA status */
#define	CSR_DMAEA		0x0C	/* DMA error address */
#define	CSR_MCR2		0x10	/* pointer to MCR2 (exponentiator) */

/*
 * Command and status register bits.
 */
#define	DMACTL_RESET		0x80000000U	/* reset the chip */
#define	DMACTL_MCR2IE		0x40000000U	/* MCR2 interrupt enable */
#define	DMACTL_MCR1IE		0x20000000U	/* MCR1 interrupt enable */
#define	DMACTL_OFM		0x10000000U	/* output fragment mode */
#define	DMACTL_BE32		0x08000000U	/* 32-bit big endian mode */
#define	DMACTL_BE64		0x04000000U	/* 64-bit big endian mode */
#define	DMACTL_EIE		0x02000000U	/* error interrupt enable */
#define	DMACTL_RNGMASK		0x01800000U	/* RNG mode mask */
#define	DMACTL_RNG1		0x00000000U	/* 1 RNG bit per cycle */
#define	DMACTL_RNG4		0x00800000U	/* 1 RNG bit per 4 cycles */
#define	DMACTL_RNG8		0x01000000U	/* 1 RNG bit per 8 cycles */
#define	DMACTL_RNG16		0x01800000U	/* 1 RNG bit per 16 cycles */
#define	DMACTL_MODNORM		0x00400000U	/* s/w modulus normalization */
#define	DMACTL_RD256		0x00020000U	/* 256 byte read DMA size */
#define	DMACTL_FRAGMASK		0x0000FFFFU	/* output fragment size */

#define	DMASTAT_MAIP		0x80000000U	/* master access in progress */
#define	DMASTAT_MCR1FULL	0x40000000U	/* MCR1 is full */
#define	DMASTAT_MCR1INT		0x20000000U	/* MCR1 interrupted */
#define	DMASTAT_ERRINT		0x10000000U	/* error interrupted */
#define	DMASTAT_MCR2FULL	0x08000000U	/* MCR2 is full */
#define	DMASTAT_MCR2INT		0x04000000U	/* MCR2 interrupted */
#define	DMASTAT_INTERRUPTS	0x34000000U	/* all interrupts */

/*
 * Offsets of things relative to an MCR.
 */
#define	MCR_COUNT	0	/* 16 bits */
#define	MCR_FLAGS	2	/* 16 bits */
#define	MCR_CTXADDR	4	/* 32 bits */

/*
 * Basis for size (should be optimized by constant folding):
 *	4 bytes for flags and #packets.
 *	for each packet:
 *		2 descriptors (DESC_SIZE)
 *		4 bytes for context address
 *		4 bytes for packet length and reserved
 */
#define	MCR_SIZE	(4 + MAXREQSPERMCR * ((2 * DESC_SIZE) + 8))

/*
 * MCR flags.
 */
#define	MCRFLAG_FINISHED	0x0001		/* MCR processing complete */
#define	MCRFLAG_ERROR		0x0002		/* set if an error occured */
#define	MCRFLAG_ERRORMASK	0xff00		/* error code bits */

/*
 * Fields within a descriptor (data buffer chain).
 */
#define	DESC_BUFADDR	0	/* 32 bits */
#define	DESC_NEXT	4	/* 32 bits */
#define	DESC_LENGTH	8	/* 16 bits */
#define	DESC_RSVD	10	/* 16 bits */
#define	DESC_SIZE	16	/* ROUNDUP(12, 16) - descriptor size (bytes) */

/*
 * Offsets of fields within context structures, see Broadcom spec.
 */
#define	CTX_LENGTH		0	/* 16 bits */
#define	CTX_CMD			2	/* 16 bits */
#define	CTX_MAXLENGTH		768	/* max size of ctx, fits anything */

#define	CTX_3DESDIRECTION	4	/* 16 bits */
#define	CTX_3DESKEY1HI		8	/* 32 bits */
#define	CTX_3DESKEY1LO		12	/* 32 bits */
#define	CTX_3DESKEY2HI		16	/* 32 bits */
#define	CTX_3DESKEY2LO		20	/* 32 bits */
#define	CTX_3DESKEY3HI		24	/* 32 bits */
#define	CTX_3DESKEY3LO		28	/* 32 bits */
#define	CTX_3DESIVHI		32	/* 32 bits */
#define	CTX_3DESIVLO		36	/* 32 bits */

#define	CTX_IPSECFLAGS		4	/* 16 bits */
#define	CTX_IPSECOFFSET		6	/* 16 bits */
#define	CTX_IPSECKEY1HI		8	/* 32 bits */
#define	CTX_IPSECKEY1LO		12	/* 32 bits */
#define	CTX_IPSECKEY2HI		16	/* 32 bits */
#define	CTX_IPSECKEY2LO		20	/* 32 bits */
#define	CTX_IPSECKEY3HI		24	/* 32 bits */
#define	CTX_IPSECKEY3LO		28	/* 32 bits */
#define	CTX_IPSECIVHI		32	/* 32 bits */
#define	CTX_IPSECIVLO		36	/* 32 bits */
#define	CTX_IPSECHMACINNER1	40	/* 32 bits */
#define	CTX_IPSECHMACINNER2	44	/* 32 bits */
#define	CTX_IPSECHMACINNER3	48	/* 32 bits */
#define	CTX_IPSECHMACINNER4	52	/* 32 bits */
#define	CTX_IPSECHMACINNER5	56	/* 32 bits */
#define	CTX_IPSECHMACOUTER1	60	/* 32 bits */
#define	CTX_IPSECHMACOUTER2	64	/* 32 bits */
#define	CTX_IPSECHMACOUTER3	68	/* 32 bits */
#define	CTX_IPSECHMACOUTER4	72	/* 32 bits */
#define	CTX_IPSECHMACOUTER5	76	/* 32 bits */

#define	CTX_RSAEXPLEN		4	/* 16 bits */
#define	CTX_RSAMODLEN		6	/* 16 bits */
#define	CTX_RSABIGNUMS		8	/* variable length */
#define	CTX_RSAQLEN		4	/* 16 bits */
#define	CTX_RSAPLEN		6	/* 16 bits */

#define	CTX_DSAMSGTYPE		4	/* 16 bits */
#define	CTX_DSARSVD		6	/* 16 bits */
#define	CTX_DSARNG		8	/* 16 bits */
#define	CTX_DSAPLEN		10	/* 16 bits */
#define	CTX_DSABIGNUMS		12	/* variable length */

/*
 * Values for specific operations.
 */
#define	CTX_RNG_LENGTH		64	/* context length for RNG (64 min) */
#define	CTX_3DES_LENGTH		64	/* context length for 3DES (64 min) */
#define	CTX_3DES_DECRYPT	0x4000	/* perform decryption */
#define	CTX_3DES_ENCRYPT	0x0000	/* perform encryption */
#define	CTX_IPSEC_LENGTH	80	/* context length for IPsec */
#define	CTX_IPSEC_ENCRYPT	0x8000	/* perform encryption */
#define	CTX_IPSEC_DECRYPT	0xc000	/* perform decryption */
#define	CTX_IPSEC_HMAC_MD5	0x1000	/* HMAC-MD5 authentication */
#define	CTX_IPSEC_HMAC_SHA1	0x2000	/* HMAC-MD5 authentication */
#define	CTX_DSAMSGTYPE_SHA1	0	/* Message is SHA1 */
#define	CTX_DSAMSGTYPE_TEXT	1	/* Generate SHA1 hash first */
#define	CTX_DSARNG_GEN		1	/* Generate random k */
#define	CTX_DSARNG_SUPPLY	0	/* Random k is supplied */

/*
 * Macros to access fields within the MCR.  Note that this includes the
 * context fields as well, since the context is just offset from the
 * base of the MCR.
 */

#define	PUTMCR32(work, reg, val)	\
	ddi_put32(work->dw_mcr_acch,	\
	(uint32_t *)(work->dw_mcr_kaddr + reg), val)

#define	PUTMCR16(work, reg, val)	\
	ddi_put16(work->dw_mcr_acch,	\
	(uint16_t *)(work->dw_mcr_kaddr + reg), val)

#define	GETMCR32(work, reg)	\
	ddi_get32(work->dw_mcr_acch, (uint32_t *)(work->dw_mcr_kaddr + reg))

#define	GETMCR16(work, reg)	\
	ddi_get16(work->dw_mcr_acch, (uint16_t *)(work->dw_mcr_kaddr + reg))

#define	PUTDESC32(req, dc_desc_kaddr, reg, val)	\
	ddi_put32(req->dr_ctx_acch,	\
	(uint32_t *)(dc_desc_kaddr + reg), val)

#define	PUTDESC16(req, dc_desc_kaddr, reg, val)	\
	ddi_put16(req->dr_ctx_acch,	\
	(uint16_t *)(dc_desc_kaddr + reg), val)

/* XXX: define the GET forms for descriptors only if needed */

#define	PUTCTX32(req, reg, val)	\
	ddi_put32(req->dr_ctx_acch,	\
	(uint32_t *)(req->dr_ctx_kaddr + reg), val)

#define	PUTCTX16(req, reg, val)	\
	ddi_put16(req->dr_ctx_acch,	\
	(uint16_t *)(req->dr_ctx_kaddr + reg), val)

#define	CTXBCOPY(req, src, dst, count)	\
	ddi_rep_put8(req->dr_ctx_acch, (uchar_t *)src, (uchar_t *)dst, count, \
	DDI_DEV_AUTOINCR)

/*
 * Register access.
 */
#define	GETCSR(dca, reg)	\
	ddi_get32(dca->dca_regs_handle, (uint_t *)(dca->dca_regs + reg))

#define	PUTCSR(dca, reg, val)	\
	ddi_put32(dca->dca_regs_handle, (uint_t *)(dca->dca_regs + reg), val)

#define	SETBIT(dca, reg, val)	\
	PUTCSR(dca, reg, GETCSR(dca, reg) | val)

#define	CLRBIT(dca, reg, val)	\
	PUTCSR(dca, reg, GETCSR(dca, reg) & ~val)

/*
 * Used to guarantee alignment.
 */
#define	ROUNDUP(a, n)	(((a) + ((n) - 1)) & ~((n) - 1))
#define	ROUNDDOWN(a, n)	(((a) & ~((n) - 1)))
#define	HIDBLWORD(x)	(((x) & 0xffffffff00000000ULL) >> 32)
#define	LODBLWORD(x)	((x) & 0xffffffffULL)

/*
 * Driver hardening related.
 */
#define	CHECK_REGS(dca)	ddi_check_acc_handle(dca->dca_regs_handle)

/*
 * Other utility macros.
 */
#define	QEMPTY(q)	((q)->dl_next == (q))
#define	BITS2BYTES(b)	((b) >> 3)
#define	WORKLIST(dca, mcr)	(&((dca)->dca_worklist[mcr - 1]))

/*
 * Debug stuff.
 */
#ifdef	DEBUG
#define	DWARN		0x0001
#define	DPCI		0x0002
#define	DINTR		0x0004
#define	DSTART		0x0008
#define	DRECLAIM	0x0010
#define	DCHATTY		0x0020
#define	DMOD		0x0040	/* _init/_fini/_info/attach/detach */
#define	DENTRY		0x0080	/* crypto routine entry/exit points */

void	dca_dprintf(dca_t *, int, const char *, ...);
#define	DBG	dca_dprintf
#else
#define	DBG(dca, lvl, ...)
#endif

/*
 * Some pkcs#11 defines as there are no pkcs#11 header files included.
 */
#define	CKO_PUBLIC_KEY		0x00000002UL
#define	CKO_PRIVATE_KEY		0x00000003UL

#define	CKA_CLASS		0x00000000UL
#define	CKA_VALUE		0x00000011UL
#define	CKA_KEY_TYPE		0x00000100UL
#define	CKA_MODULUS		0x00000120UL
#define	CKA_PUBLIC_EXPONENT	0x00000122UL
#define	CKA_PRIVATE_EXPONENT	0x00000123UL
#define	CKA_PRIME_1		0x00000124UL
#define	CKA_PRIME_2		0x00000125UL
#define	CKA_EXPONENT_1		0x00000126UL
#define	CKA_EXPONENT_2		0x00000127UL
#define	CKA_COEFFICIENT		0x00000128UL
#define	CKA_PRIME		0x00000130UL
#define	CKA_SUBPRIME		0x00000131UL
#define	CKA_BASE		0x00000132UL
/*
 * Driver globals.
 */
extern int	dca_mindma;
extern int	dca_hardening;

/*
 * Prototypes.
 */

/*
 * dca_debug.c
 */
void	dca_error(dca_t *, const char *, ...);
void	dca_diperror(dev_info_t *, const char *, ...);
void	dca_dipverror(dev_info_t *, const char *, va_list);
/*
 * dca_3des.c
 */
int	dca_3desctxinit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
    int, int);
int	dca_3des(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
    crypto_req_handle_t, int);
int	dca_3desupdate(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
    crypto_req_handle_t, int);
int	dca_3desfinal(crypto_ctx_t *, crypto_data_t *, int);
int	dca_3desatomic(crypto_provider_handle_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
    int, crypto_req_handle_t, int);
void	dca_3desctxfree(void *);

/*
 * dca_rsa.c
 */
int	dca_rsastart(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
    crypto_req_handle_t, int);
int	dca_rsainit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *, int);
void	dca_rsactxfree(void *);
int	dca_rsaatomic(crypto_provider_handle_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
    int, crypto_req_handle_t, int);

/*
 * dca_dsa.c
 */
int	dca_dsa_sign(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
    crypto_req_handle_t);
int	dca_dsa_verify(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
    crypto_req_handle_t);
int	dca_dsainit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *, int,
    int);
void	dca_dsactxfree(void *);
int	dca_dsaatomic(crypto_provider_handle_t, crypto_session_id_t,
    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
    int, crypto_req_handle_t, int);

/*
 * dca_rng.c
 */
int	dca_rng(dca_t *, uchar_t *, size_t len, crypto_req_handle_t);
int	dca_random_buffer(dca_t *dca, caddr_t buf, int len);
int	dca_random_init();
void	dca_random_fini();

/*
 * dca_kstat.c
 */
void	dca_ksinit(dca_t *);
/*
 * dca.c
 */
void	dca_rmqueue(dca_listnode_t *);
dca_request_t *dca_getreq(dca_t *, int, int);
void	dca_freereq(dca_request_t *);
int	dca_bindchains(dca_request_t *, size_t, size_t);
int	dca_unbindchains(dca_request_t *);
int	dca_start(dca_t *, dca_request_t *, int, int);
void	dca_done(dca_request_t *, int);
void	dca_destroyreq(dca_request_t *);
int	dca_length(crypto_data_t *);
int	dca_gather(crypto_data_t *, char *, int, int);
int	dca_resid_gather(crypto_data_t *, char *, int *, char *, int);
int	dca_scatter(const char *, crypto_data_t *, int, int);
int	dca_bcmp_reverse(const void *s1, const void *s2, size_t n);
int	dca_dupcrypto(crypto_data_t *, crypto_data_t *);
int	dca_verifyio(crypto_data_t *, crypto_data_t *);
int	dca_getbufbytes(crypto_data_t *, size_t, int, uchar_t *);
int	dca_sgcheck(dca_t *, crypto_data_t *, dca_sg_param_t);
crypto_object_attribute_t *
	dca_get_key_attr(crypto_key_t *);
int	dca_attr_lookup_uint32(crypto_object_attribute_t *, uint_t, uint64_t,
	    uint32_t *);
int	dca_attr_lookup_uint8_array(crypto_object_attribute_t *, uint_t,
	    uint64_t, void **, unsigned int *);
crypto_object_attribute_t *
	dca_find_attribute(crypto_object_attribute_t *, uint_t, uint64_t);
caddr_t	dca_bufdaddr(crypto_data_t *);
void	dca_rcoalesce(dca_request_t *, int);
void	dca_runcoalesce(dca_request_t *);
int	dca_bitlen(unsigned char *, int);
uint16_t dca_padhalf(int);
uint16_t dca_padfull(int);
void	dca_reverse(void *, void *, int, int);
int	dca_numcmp(caddr_t, int, caddr_t, int);
int dca_check_dma_handle(dca_t *dca, ddi_dma_handle_t handle,
	dca_fma_eclass_t eclass_index);
int dca_free_context(crypto_ctx_t *ctx);

#endif	/* _KERNEL */

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_DCA_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
 */

#ifndef _SYS_CRYPTO_ELFSIGN_H
#define	_SYS_CRYPTO_ELFSIGN_H

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Consolidation Private Interface for elfsign/libpkcs11/kcfd
 */

#include <sys/types.h>
#include <sys/param.h>

/*
 * Project Private structures and types used for communication between kcfd
 * and KCF over the door.
 */

typedef enum ELFsign_status_e {
	ELFSIGN_UNKNOWN,
	ELFSIGN_SUCCESS,
	ELFSIGN_FAILED,
	ELFSIGN_NOTSIGNED,
	ELFSIGN_INVALID_CERTPATH,
	ELFSIGN_INVALID_ELFOBJ,
	ELFSIGN_UNAVAILABLE
} ELFsign_status_t;


#define	SIG_MAX_LENGTH		1024

#define	ELF_SIGNATURE_SECTION	".SUNW_signature"

typedef uint32_t	filesig_vers_t;

/*
 * File Signature Structure
 *	Applicable to ELF and other file formats
 */
struct filesignatures {
	uint32_t	filesig_cnt;	/* count of signatures */
	uint32_t	filesig_pad;	/* unused */
	union {
		char	filesig_data[1];
		struct filesig {	/* one of these for each signature */
			uint32_t	filesig_size;
			filesig_vers_t	filesig_version;
			union {
				struct filesig_version1 {
					uint32_t	filesig_v1_dnsize;
					uint32_t	filesig_v1_sigsize;
					uint32_t	filesig_v1_oidsize;
					char	filesig_v1_data[1];
				} filesig_v1;
				struct filesig_version3 {
					uint64_t	filesig_v3_time;
					uint32_t	filesig_v3_dnsize;
					uint32_t	filesig_v3_sigsize;
					uint32_t	filesig_v3_oidsize;
					char	filesig_v3_data[1];
				} filesig_v3;
			} _u2;
		} filesig_sig;
		uint64_t filesig_align;
	} _u1;
};
#define	filesig_sig		_u1.filesig_sig

#define	filesig_v1_dnsize	_u2.filesig_v1.filesig_v1_dnsize
#define	filesig_v1_sigsize	_u2.filesig_v1.filesig_v1_sigsize
#define	filesig_v1_oidsize	_u2.filesig_v1.filesig_v1_oidsize
#define	filesig_v1_data		_u2.filesig_v1.filesig_v1_data

#define	filesig_v3_time		_u2.filesig_v3.filesig_v3_time
#define	filesig_v3_dnsize	_u2.filesig_v3.filesig_v3_dnsize
#define	filesig_v3_sigsize	_u2.filesig_v3.filesig_v3_sigsize
#define	filesig_v3_oidsize	_u2.filesig_v3.filesig_v3_oidsize
#define	filesig_v3_data		_u2.filesig_v3.filesig_v3_data

#define	filesig_ALIGN(s)	(((s) + sizeof (uint64_t) - 1) & \
				    (-sizeof (uint64_t)))
#define	filesig_next(ptr)	(struct filesig *)((void *)((char *)(ptr) + \
				    filesig_ALIGN((ptr)->filesig_size)))

#define	FILESIG_UNKNOWN		0	/* unrecognized version */
#define	FILESIG_VERSION1	1	/* version1, all but sig section */
#define	FILESIG_VERSION2	2	/* version1 format, SHF_ALLOC only */
#define	FILESIG_VERSION3	3	/* version3, all but sig section */
#define	FILESIG_VERSION4	4	/* version3 format, SHF_ALLOC only */

#ifndef	_KERNEL

#endif	/* _KERNEL */

#ifdef __cplusplus
}
#endif

#endif /* _SYS_CRYPTO_ELFSIGN_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2023-2026 RackTop Systems, Inc.
 */

#ifndef	_SYS_CRYPTO_IMPL_H
#define	_SYS_CRYPTO_IMPL_H

/*
 * Kernel Cryptographic Framework private implementation definitions.
 */

#include <sys/types.h>
#include <sys/param.h>

#ifdef _KERNEL
#include <sys/crypto/common.h>
#include <sys/crypto/api.h>
#include <sys/crypto/spi.h>
#include <sys/crypto/ioctl.h>
#include <sys/atomic.h>
#include <sys/project.h>
#include <sys/taskq.h>
#include <sys/rctl.h>
#include <sys/cpuvar.h>
#endif /* _KERNEL */

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef _KERNEL

/*
 * Prefixes convention: structures internal to the kernel cryptographic
 * framework start with 'kcf_'. Exposed structure start with 'crypto_'.
 */

/* Provider stats. Not protected. */
typedef	struct kcf_prov_stats {
	kstat_named_t	ps_ops_total;
	kstat_named_t	ps_ops_passed;
	kstat_named_t	ps_ops_failed;
	kstat_named_t	ps_ops_busy_rval;
} kcf_prov_stats_t;

/* Various kcf stats. Not protected. */
typedef	struct kcf_stats {
	kstat_named_t	ks_thrs_in_pool;
	kstat_named_t	ks_idle_thrs;
	kstat_named_t	ks_minthrs;
	kstat_named_t	ks_maxthrs;
	kstat_named_t	ks_swq_njobs;
	kstat_named_t	ks_swq_maxjobs;
	kstat_named_t	ks_taskq_threads;
	kstat_named_t	ks_taskq_minalloc;
	kstat_named_t	ks_taskq_maxalloc;
} kcf_stats_t;

#define	CPU_SEQID	(CPU->cpu_seqid)

typedef struct kcf_lock_withpad {
	kmutex_t	kl_lock;
	uint8_t		kl_pad[64 - sizeof (kmutex_t)];
} kcf_lock_withpad_t;

/*
 * Per-CPU structure used by a provider to keep track of
 * various counters.
 */
typedef struct kcf_prov_cpu {
	kmutex_t	kp_lock;
	int		kp_holdcnt;	/* can go negative! */
	uint_t		kp_jobcnt;

	uint64_t	kp_ndispatches;
	uint64_t	kp_nfails;
	uint64_t	kp_nbusy_rval;
	kcondvar_t	kp_cv;

	uint8_t		kp_pad[64 - sizeof (kmutex_t) - 2 * sizeof (int) -
	    3 * sizeof (uint64_t) - sizeof (kcondvar_t)];
} kcf_prov_cpu_t;

/*
 * kcf_get_refcnt(pd) is the number of inflight requests to the
 * provider. So, it is a good measure of the load on a provider when
 * it is not in a busy state. Once a provider notifies it is busy, requests
 * backup in the taskq. So, we use tq_nalloc in that case which gives
 * the number of task entries in the task queue. Note that we do not
 * acquire any locks here as it is not critical to get the exact number
 * and the lock contention is too costly for this code path.
 */
#define	KCF_PROV_LOAD(pd)	((pd)->pd_state != KCF_PROV_BUSY ?	\
	kcf_get_refcnt(pd, B_FALSE) : (pd)->pd_taskq->tq_nalloc)


/*
 * The following two macros should be
 * #define KCF_OPS_CLASSSIZE (KCF_LAST_OPSCLASS - KCF_FIRST_OPSCLASS + 2)
 * #define KCF_MAXMECHTAB KCF_MAXCIPHER
 *
 * However, doing that would involve reorganizing the header file a bit.
 * When impl.h is broken up (bug# 4703218), this will be done. For now,
 * we hardcode these values.
 */
#define	KCF_OPS_CLASSSIZE	8
#define	KCF_MAXMECHTAB		32

/*
 * Valid values for the state of a provider. The order of
 * the elements is important.
 *
 * Routines which get a provider or the list of providers
 * should pick only those that are either in KCF_PROV_READY state
 * or in KCF_PROV_BUSY state.
 */
typedef enum {
	KCF_PROV_ALLOCATED = 1,
	KCF_PROV_UNVERIFIED,
	KCF_PROV_UNVERIFIED_FIPS140,
	KCF_PROV_VERIFICATION_FAILED,
	/*
	 * state < KCF_PROV_READY means the provider can not
	 * be used at all.
	 */
	KCF_PROV_READY,
	KCF_PROV_BUSY,
	/*
	 * state > KCF_PROV_BUSY means the provider can not
	 * be used for new requests.
	 */
	KCF_PROV_FAILED,
	/*
	 * Threads setting the following two states should do so only
	 * if the current state < KCF_PROV_DISABLED.
	 */
	KCF_PROV_DISABLED,
	KCF_PROV_UNREGISTERING,
	KCF_PROV_UNREGISTERED
} kcf_prov_state_t;

#define	KCF_IS_PROV_UNVERIFIED(pd) ((pd)->pd_state == KCF_PROV_UNVERIFIED)
#define	KCF_IS_PROV_USABLE(pd) ((pd)->pd_state == KCF_PROV_READY || \
	(pd)->pd_state == KCF_PROV_BUSY)
#define	KCF_IS_PROV_REMOVED(pd)	((pd)->pd_state >= KCF_PROV_UNREGISTERING)

/* Internal flags valid for pd_flags field */
#define	KCF_LPROV_MEMBER	0x80000000 /* is member of a logical provider */

/*
 * A provider descriptor structure. There is one such structure per
 * provider. It is allocated and initialized at registration time and
 * freed when the provider unregisters.
 *
 * pd_prov_type:	Provider type, hardware or software
 * pd_sid:		Session ID of the provider used by kernel clients.
 *			This is valid only for session-oriented providers.
 * pd_taskq:		taskq used to dispatch crypto requests
 * pd_nbins:		number of bins in pd_percpu_bins
 * pd_percpu_bins:	Pointer to an array of per-CPU structures
 *			containing a lock, a cv and various counters.
 * pd_lock:		lock protects pd_state and pd_provider_list
 * pd_state:		State value of the provider
 * pd_provider_list:	Used to cross-reference logical providers and their
 *			members. Not used for software providers.
 * pd_resume_cv:	cv to wait for state to change from KCF_PROV_BUSY
 * pd_prov_handle:	Provider handle specified by provider
 * pd_ops_vector:	The ops vector specified by Provider
 * pd_mech_indx:	Lookup table which maps a core framework mechanism
 *			number to an index in pd_mechanisms array
 * pd_mechanisms:	Array of mechanisms supported by the provider, specified
 *			by the provider during registration
 * pd_mech_list_count:	The number of entries in pi_mechanisms, specified
 *			by the provider during registration
 * pd_name:		Device name or module name
 * pd_instance:		Device instance
 * pd_module_id:	Module ID returned by modload
 * pd_mctlp:		Pointer to modctl structure for this provider
 * pd_description:	Provider description string
 * pd_flags:		bitwise OR of pi_flags from crypto_provider_info_t
 *			and other internal flags defined above.
 * pd_hash_limit:	Maximum data size that hash mechanisms of this provider
 *			can support.
 * pd_hmac_limit:	Maximum data size that HMAC mechanisms of this provider
 *			can support.
 * pd_kcf_prov_handle:	KCF-private handle assigned by KCF
 * pd_prov_id:		Identification # assigned by KCF to provider
 * pd_kstat:		kstat associated with the provider
 * pd_ks_data:		kstat data
 */
typedef struct kcf_provider_desc {
	crypto_provider_type_t		pd_prov_type;
	crypto_session_id_t		pd_sid;
	taskq_t				*pd_taskq;
	uint_t				pd_nbins;
	kcf_prov_cpu_t			*pd_percpu_bins;
	kmutex_t			pd_lock;
	kcf_prov_state_t		pd_state;
	struct kcf_provider_list	*pd_provider_list;
	kcondvar_t			pd_resume_cv;
	crypto_provider_handle_t	pd_prov_handle;
	crypto_ops_t			*pd_ops_vector;
	ushort_t			pd_mech_indx[KCF_OPS_CLASSSIZE]\
					    [KCF_MAXMECHTAB];
	crypto_mech_info_t		*pd_mechanisms;
	uint_t				pd_mech_list_count;
	char				*pd_name;
	uint_t				pd_instance;
	int				pd_module_id;
	struct modctl			*pd_mctlp;
	char				*pd_description;
	uint_t				pd_flags;
	uint_t				pd_hash_limit;
	uint_t				pd_hmac_limit;
	crypto_kcf_provider_handle_t	pd_kcf_prov_handle;
	crypto_provider_id_t		pd_prov_id;
	kstat_t				*pd_kstat;
	kcf_prov_stats_t		pd_ks_data;
} kcf_provider_desc_t;

/* useful for making a list of providers */
typedef struct kcf_provider_list {
	struct kcf_provider_list *pl_next;
	struct kcf_provider_desc *pl_provider;
} kcf_provider_list_t;

/*
 * If a component has a reference to a kcf_provider_desc_t,
 * it REFHOLD()s. A new provider descriptor which is referenced only
 * by the providers table has a reference counter of one.
 */
#define	KCF_PROV_REFHOLD(desc) {			\
	kcf_prov_cpu_t	*mp;				\
							\
	mp = &((desc)->pd_percpu_bins[CPU_SEQID]);	\
	mutex_enter(&mp->kp_lock);			\
	mp->kp_holdcnt++;				\
	mutex_exit(&mp->kp_lock);			\
}

#define	KCF_PROV_REFRELE(desc) {			\
	kcf_prov_cpu_t	*mp;				\
							\
	mp = &((desc)->pd_percpu_bins[CPU_SEQID]);	\
	mutex_enter(&mp->kp_lock);			\
	mp->kp_holdcnt--;				\
	mutex_exit(&mp->kp_lock);			\
}

#define	KCF_PROV_REFHELD(desc)	(kcf_get_refcnt(desc, B_TRUE) >= 1)

/*
 * The JOB macros are used only for a hardware provider.
 * Hardware providers can have holds that stay forever.
 * So, the job counter is used to check if it is safe to
 * unregister a provider.
 */
#define	KCF_PROV_JOB_HOLD(mp) {			\
	mutex_enter(&(mp)->kp_lock);		\
	(mp)->kp_jobcnt++;			\
	mutex_exit(&(mp)->kp_lock);		\
}

#define	KCF_PROV_JOB_RELE(mp) {			\
	mutex_enter(&(mp)->kp_lock);		\
	(mp)->kp_jobcnt--;			\
	if ((mp)->kp_jobcnt == 0)		\
		cv_signal(&(mp)->kp_cv);	\
	mutex_exit(&(mp)->kp_lock);		\
}

#define	KCF_PROV_JOB_RELE_STAT(mp, doincr) {	\
	if (doincr)				\
		(mp)->kp_nfails++;		\
	KCF_PROV_JOB_RELE(mp);			\
}

#define	KCF_PROV_INCRSTATS(pd, error)	{				\
	kcf_prov_cpu_t	*mp;						\
									\
	mp = &((pd)->pd_percpu_bins[CPU_SEQID]);			\
	mp->kp_ndispatches++;						\
	if ((error) == CRYPTO_BUSY)					\
		mp->kp_nbusy_rval++;					\
	else if ((error) != CRYPTO_SUCCESS && (error) != CRYPTO_QUEUED)	\
		mp->kp_nfails++;					\
}

/* list of crypto_mech_info_t valid as the second mech in a dual operation */

typedef	struct crypto_mech_info_list {
	struct crypto_mech_info_list	*ml_next;
	crypto_mech_type_t		ml_kcf_mechid;	/* KCF's id */
	crypto_mech_info_t		ml_mech_info;
} crypto_mech_info_list_t;

/*
 * An element in a mechanism provider descriptors chain.
 * The kcf_prov_mech_desc_t is duplicated in every chain the provider belongs
 * to. This is a small tradeoff memory vs mutex spinning time to access the
 * common provider field.
 */

typedef struct kcf_prov_mech_desc {
	struct kcf_mech_entry		*pm_me;		/* Back to the head */
	struct kcf_prov_mech_desc	*pm_next;	/* Next in the chain */
	crypto_mech_info_t		pm_mech_info;	/* Provider mech info */
	crypto_mech_info_list_t		*pm_mi_list;	/* list for duals */
	kcf_provider_desc_t		*pm_prov_desc;	/* Common desc. */
} kcf_prov_mech_desc_t;

/* and the notation shortcuts ... */
#define	pm_provider_type	pm_prov_desc.pd_provider_type
#define	pm_provider_handle	pm_prov_desc.pd_provider_handle
#define	pm_ops_vector		pm_prov_desc.pd_ops_vector

extern kcf_lock_withpad_t *me_mutexes;

typedef int (*kcf_copyin_param_func_t) (caddr_t, size_t, crypto_mechanism_t *,
    int, int);

#define	KCF_CPU_PAD (128 - sizeof (crypto_mech_name_t) - \
    sizeof (crypto_mech_type_t) - \
    2 * sizeof (kcf_prov_mech_desc_t *) - \
    sizeof (int) - sizeof (uint32_t) - sizeof (size_t) - \
    sizeof (kcf_copyin_param_func_t))

CTASSERT(KCF_CPU_PAD > 0);

/*
 * A mechanism entry in an xxx_mech_tab[]. KCF_CPU_PAD needs
 * to be adjusted if this structure is changed.
 */
typedef	struct kcf_mech_entry {
	crypto_mech_name_t	me_name;	/* mechanism name */
	crypto_mech_type_t	me_mechid;	/* Internal id for mechanism */
	kcf_prov_mech_desc_t	*me_hw_prov_chain;  /* list of HW providers */
	kcf_prov_mech_desc_t	*me_sw_prov;    /* SW provider */
	/*
	 * Number of HW providers in the chain. There is only one
	 * SW provider. So, we need only a count of HW providers.
	 */
	int			me_num_hwprov;
	/*
	 * When a SW provider is present, this is the generation number that
	 * ensures no objects from old SW providers are used in the new one
	 */
	uint32_t		me_gen_swprov;
	/*
	 *  threshold for using hardware providers for this mech
	 */
	size_t			me_threshold;
	kcf_copyin_param_func_t	me_copyin_param;
	uint8_t			me_pad[KCF_CPU_PAD];
} kcf_mech_entry_t;

/*
 * A policy descriptor structure. It is allocated and initialized
 * when administrative ioctls load disabled mechanisms.
 *
 * pd_prov_type:	Provider type, hardware or software
 * pd_name:		Device name or module name.
 * pd_instance:		Device instance.
 * pd_refcnt:		Reference counter for this policy descriptor
 * pd_mutex:		Protects array and count of disabled mechanisms.
 * pd_disabled_count:	Count of disabled mechanisms.
 * pd_disabled_mechs:	Array of disabled mechanisms.
 */
typedef struct kcf_policy_desc {
	crypto_provider_type_t	pd_prov_type;
	char			*pd_name;
	uint_t			pd_instance;
	uint_t			pd_refcnt;
	kmutex_t		pd_mutex;
	uint_t			pd_disabled_count;
	crypto_mech_name_t	*pd_disabled_mechs;
} kcf_policy_desc_t;

/*
 * If a component has a reference to a kcf_policy_desc_t,
 * it REFHOLD()s. A new policy descriptor which is referenced only
 * by the policy table has a reference count of one.
 */
#define	KCF_POLICY_REFHOLD(desc) {		\
	atomic_inc_32(&(desc)->pd_refcnt);	\
	ASSERT((desc)->pd_refcnt != 0);		\
}

/*
 * Releases a reference to a policy descriptor. When the last
 * reference is released, the descriptor is freed.
 */
#define	KCF_POLICY_REFRELE(desc) {				\
	ASSERT((desc)->pd_refcnt != 0);				\
	membar_exit();						\
	if (atomic_dec_32_nv(&(desc)->pd_refcnt) == 0)	\
		kcf_policy_free_desc(desc);			\
}

/*
 * This entry stores the name of a software module and its
 * mechanisms.  The mechanisms are 'hints' that are used to
 * trigger loading of the module.
 */
typedef struct kcf_soft_conf_entry {
	struct kcf_soft_conf_entry	*ce_next;
	char				*ce_name;
	crypto_mech_name_t		*ce_mechs;
	uint_t				ce_count;
} kcf_soft_conf_entry_t;

extern kmutex_t soft_config_mutex;
extern kcf_soft_conf_entry_t *soft_config_list;

/*
 * Global tables. The sizes are from the predefined PKCS#11 v2.20 mechanisms,
 * with a margin of few extra empty entry points
 */

#define	KCF_MAXDIGEST		16	/* Digests */
#define	KCF_MAXCIPHER		64	/* Ciphers */
#define	KCF_MAXMAC		40	/* Message authentication codes */
#define	KCF_MAXSIGN		24	/* Sign/Verify */
#define	KCF_MAXKEYOPS		116	/* Key generation and derivation */
#define	KCF_MAXMISC		16	/* Others ... */

#define	KCF_MAXMECHS		KCF_MAXDIGEST + KCF_MAXCIPHER + KCF_MAXMAC + \
				KCF_MAXSIGN + KCF_MAXKEYOPS + \
				KCF_MAXMISC

extern kcf_mech_entry_t kcf_digest_mechs_tab[];
extern kcf_mech_entry_t kcf_cipher_mechs_tab[];
extern kcf_mech_entry_t kcf_mac_mechs_tab[];
extern kcf_mech_entry_t kcf_sign_mechs_tab[];
extern kcf_mech_entry_t kcf_keyops_mechs_tab[];
extern kcf_mech_entry_t kcf_misc_mechs_tab[];

extern kmutex_t kcf_mech_tabs_lock;

typedef	enum {
	KCF_DIGEST_CLASS = 1,
	KCF_CIPHER_CLASS,
	KCF_MAC_CLASS,
	KCF_SIGN_CLASS,
	KCF_KEYOPS_CLASS,
	KCF_MISC_CLASS
} kcf_ops_class_t;

#define	KCF_FIRST_OPSCLASS	KCF_DIGEST_CLASS
#define	KCF_LAST_OPSCLASS	KCF_MISC_CLASS

/* The table of all the kcf_xxx_mech_tab[]s, indexed by kcf_ops_class */

typedef	struct kcf_mech_entry_tab {
	int			met_size;	/* Size of the met_tab[] */
	kcf_mech_entry_t	*met_tab;	/* the table		 */
} kcf_mech_entry_tab_t;

extern kcf_mech_entry_tab_t kcf_mech_tabs_tab[];

#define	KCF_MECHID(class, index)				\
	(((crypto_mech_type_t)(class) << 32) | (crypto_mech_type_t)(index))

#define	KCF_MECH2CLASS(mech_type) ((kcf_ops_class_t)((mech_type) >> 32))

#define	KCF_MECH2INDEX(mech_type) ((int)(mech_type))

#define	KCF_TO_PROV_MECH_INDX(pd, mech_type)			\
	((pd)->pd_mech_indx[KCF_MECH2CLASS(mech_type)]		\
	[KCF_MECH2INDEX(mech_type)])

#define	KCF_TO_PROV_MECHINFO(pd, mech_type)			\
	((pd)->pd_mechanisms[KCF_TO_PROV_MECH_INDX(pd, mech_type)])

#define	KCF_TO_PROV_MECHNUM(pd, mech_type)			\
	(KCF_TO_PROV_MECHINFO(pd, mech_type).cm_mech_number)

#define	KCF_CAN_SHARE_OPSTATE(pd, mech_type)			\
	((KCF_TO_PROV_MECHINFO(pd, mech_type).cm_mech_flags) &	\
	CRYPTO_CAN_SHARE_OPSTATE)

/* ps_refcnt is protected by cm_lock in the crypto_minor structure */
typedef struct crypto_provider_session {
	struct crypto_provider_session *ps_next;
	crypto_session_id_t		ps_session;
	kcf_provider_desc_t		*ps_provider;
	kcf_provider_desc_t		*ps_real_provider;
	uint_t				ps_refcnt;
} crypto_provider_session_t;

typedef struct crypto_session_data {
	kmutex_t			sd_lock;
	kcondvar_t			sd_cv;
	uint32_t			sd_flags;
	int				sd_pre_approved_amount;
	crypto_ctx_t			*sd_digest_ctx;
	crypto_ctx_t			*sd_encr_ctx;
	crypto_ctx_t			*sd_decr_ctx;
	crypto_ctx_t			*sd_mac_ctx;
	crypto_ctx_t			*sd_sign_ctx;
	crypto_ctx_t			*sd_verify_ctx;
	crypto_ctx_t			*sd_sign_recover_ctx;
	crypto_ctx_t			*sd_verify_recover_ctx;
	kcf_provider_desc_t		*sd_provider;
	void				*sd_find_init_cookie;
	crypto_provider_session_t	*sd_provider_session;
} crypto_session_data_t;

#define	CRYPTO_SESSION_IN_USE		0x00000001
#define	CRYPTO_SESSION_IS_BUSY		0x00000002
#define	CRYPTO_SESSION_IS_CLOSED	0x00000004

#define	KCF_MAX_PIN_LEN			1024

/*
 * Per-minor info.
 *
 * cm_lock protects everything in this structure except for cm_refcnt.
 */
typedef struct crypto_minor {
	uint_t				cm_refcnt;
	kmutex_t			cm_lock;
	kcondvar_t			cm_cv;
	crypto_session_data_t		**cm_session_table;
	uint_t				cm_session_table_count;
	kcf_provider_desc_t		**cm_provider_array;
	uint_t				cm_provider_count;
	crypto_provider_session_t	*cm_provider_session;
} crypto_minor_t;

/* resource control framework handle used by /dev/crypto */
extern rctl_hndl_t rc_project_crypto_mem;
/*
 * Return codes for internal functions
 */
#define	KCF_SUCCESS		0x0	/* Successful call */
#define	KCF_INVALID_MECH_NUMBER	0x1	/* invalid mechanism number */
#define	KCF_INVALID_MECH_NAME	0x2	/* invalid mechanism name */
#define	KCF_INVALID_MECH_CLASS	0x3	/* invalid mechanism class */
#define	KCF_MECH_TAB_FULL	0x4	/* Need more room in the mech tabs. */
#define	KCF_INVALID_INDX	((ushort_t)-1)

/*
 * kCF internal mechanism and function group for tracking RNG providers.
 */
#define	SUN_RANDOM		"random"
#define	CRYPTO_FG_RANDOM	0x80000000	/* generate_random() */

/*
 * Wrappers for ops vectors. In the wrapper definitions below, the pd
 * argument always corresponds to a pointer to a provider descriptor
 * of type kcf_prov_desc_t.
 */

#define	KCF_PROV_CONTROL_OPS(pd)	((pd)->pd_ops_vector->co_control_ops)
#define	KCF_PROV_CTX_OPS(pd)		((pd)->pd_ops_vector->co_ctx_ops)
#define	KCF_PROV_DIGEST_OPS(pd)		((pd)->pd_ops_vector->co_digest_ops)
#define	KCF_PROV_CIPHER_OPS(pd)		((pd)->pd_ops_vector->co_cipher_ops)
#define	KCF_PROV_MAC_OPS(pd)		((pd)->pd_ops_vector->co_mac_ops)
#define	KCF_PROV_SIGN_OPS(pd)		((pd)->pd_ops_vector->co_sign_ops)
#define	KCF_PROV_VERIFY_OPS(pd)		((pd)->pd_ops_vector->co_verify_ops)
#define	KCF_PROV_DUAL_OPS(pd)		((pd)->pd_ops_vector->co_dual_ops)
#define	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) \
	((pd)->pd_ops_vector->co_dual_cipher_mac_ops)
#define	KCF_PROV_RANDOM_OPS(pd)		((pd)->pd_ops_vector->co_random_ops)
#define	KCF_PROV_SESSION_OPS(pd)	((pd)->pd_ops_vector->co_session_ops)
#define	KCF_PROV_OBJECT_OPS(pd)		((pd)->pd_ops_vector->co_object_ops)
#define	KCF_PROV_KEY_OPS(pd)		((pd)->pd_ops_vector->co_key_ops)
#define	KCF_PROV_PROVIDER_OPS(pd)	((pd)->pd_ops_vector->co_provider_ops)
#define	KCF_PROV_MECH_OPS(pd)		((pd)->pd_ops_vector->co_mech_ops)
#define	KCF_PROV_NOSTORE_KEY_OPS(pd)	\
	((pd)->pd_ops_vector->co_nostore_key_ops)
#define	KCF_PROV_FIPS140_OPS(pd)	((pd)->pd_ops_vector->co_fips140_ops)
#define	KCF_PROV_PROVMGMT_OPS(pd)	((pd)->pd_ops_vector->co_provider_ops)

/*
 * Wrappers for crypto_control_ops(9S) entry points.
 */

#define	KCF_PROV_STATUS(pd, status) ( \
	(KCF_PROV_CONTROL_OPS(pd) && \
	KCF_PROV_CONTROL_OPS(pd)->provider_status) ? \
	KCF_PROV_CONTROL_OPS(pd)->provider_status( \
	    (pd)->pd_prov_handle, status) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_ctx_ops(9S) entry points.
 */

#define	KCF_PROV_CREATE_CTX_TEMPLATE(pd, mech, key, template, size, req) ( \
	(KCF_PROV_CTX_OPS(pd) && KCF_PROV_CTX_OPS(pd)->create_ctx_template) ? \
	KCF_PROV_CTX_OPS(pd)->create_ctx_template( \
	    (pd)->pd_prov_handle, mech, key, template, size, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_FREE_CONTEXT(pd, ctx) ( \
	(KCF_PROV_CTX_OPS(pd) && KCF_PROV_CTX_OPS(pd)->free_context) ? \
	KCF_PROV_CTX_OPS(pd)->free_context(ctx) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_COPYIN_MECH(pd, umech, kmech, errorp, mode) ( \
	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->copyin_mechanism) ? \
	KCF_PROV_MECH_OPS(pd)->copyin_mechanism( \
	    (pd)->pd_prov_handle, umech, kmech, errorp, mode) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_COPYOUT_MECH(pd, kmech, umech, errorp, mode) ( \
	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->copyout_mechanism) ? \
	KCF_PROV_MECH_OPS(pd)->copyout_mechanism( \
	    (pd)->pd_prov_handle, kmech, umech, errorp, mode) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_FREE_MECH(pd, prov_mech) ( \
	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->free_mechanism) ? \
	KCF_PROV_MECH_OPS(pd)->free_mechanism( \
	    (pd)->pd_prov_handle, prov_mech) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_digest_ops(9S) entry points.
 */

#define	KCF_PROV_DIGEST_INIT(pd, ctx, mech, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_init) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest_init(ctx, mech, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * The _ (underscore) in _digest is needed to avoid replacing the
 * function digest().
 */
#define	KCF_PROV_DIGEST(pd, ctx, data, _digest, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest(ctx, data, _digest, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DIGEST_UPDATE(pd, ctx, data, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_update) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest_update(ctx, data, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DIGEST_KEY(pd, ctx, key, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_key) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest_key(ctx, key, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DIGEST_FINAL(pd, ctx, digest, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_final) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest_final(ctx, digest, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DIGEST_ATOMIC(pd, session, mech, data, digest, req) ( \
	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_atomic) ? \
	KCF_PROV_DIGEST_OPS(pd)->digest_atomic( \
	    (pd)->pd_prov_handle, session, mech, data, digest, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_cipher_ops(9S) entry points.
 */

#define	KCF_PROV_ENCRYPT_INIT(pd, ctx, mech, key, template, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_init) ? \
	KCF_PROV_CIPHER_OPS(pd)->encrypt_init(ctx, mech, key, template, \
	    req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT(pd, ctx, plaintext, ciphertext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt) ? \
	KCF_PROV_CIPHER_OPS(pd)->encrypt(ctx, plaintext, ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_UPDATE(pd, ctx, plaintext, ciphertext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_update) ? \
	KCF_PROV_CIPHER_OPS(pd)->encrypt_update(ctx, plaintext, \
	    ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_FINAL(pd, ctx, ciphertext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_final) ? \
	KCF_PROV_CIPHER_OPS(pd)->encrypt_final(ctx, ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_ATOMIC(pd, session, mech, key, plaintext, ciphertext, \
	    template, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_atomic) ? \
	KCF_PROV_CIPHER_OPS(pd)->encrypt_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, plaintext, ciphertext, \
	    template, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_INIT(pd, ctx, mech, key, template, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_init) ? \
	KCF_PROV_CIPHER_OPS(pd)->decrypt_init(ctx, mech, key, template, \
	    req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT(pd, ctx, ciphertext, plaintext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt) ? \
	KCF_PROV_CIPHER_OPS(pd)->decrypt(ctx, ciphertext, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_UPDATE(pd, ctx, ciphertext, plaintext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_update) ? \
	KCF_PROV_CIPHER_OPS(pd)->decrypt_update(ctx, ciphertext, \
	    plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_FINAL(pd, ctx, plaintext, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_final) ? \
	KCF_PROV_CIPHER_OPS(pd)->decrypt_final(ctx, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_ATOMIC(pd, session, mech, key, ciphertext, plaintext, \
	    template, req) ( \
	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_atomic) ? \
	KCF_PROV_CIPHER_OPS(pd)->decrypt_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, ciphertext, plaintext, \
	    template, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_mac_ops(9S) entry points.
 */

#define	KCF_PROV_MAC_INIT(pd, ctx, mech, key, template, req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_init) ? \
	KCF_PROV_MAC_OPS(pd)->mac_init(ctx, mech, key, template, req) \
	: CRYPTO_NOT_SUPPORTED)

/*
 * The _ (underscore) in _mac is needed to avoid replacing the
 * function mac().
 */
#define	KCF_PROV_MAC(pd, ctx, data, _mac, req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac) ? \
	KCF_PROV_MAC_OPS(pd)->mac(ctx, data, _mac, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_UPDATE(pd, ctx, data, req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_update) ? \
	KCF_PROV_MAC_OPS(pd)->mac_update(ctx, data, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_FINAL(pd, ctx, mac, req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_final) ? \
	KCF_PROV_MAC_OPS(pd)->mac_final(ctx, mac, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_ATOMIC(pd, session, mech, key, data, mac, template, \
	    req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_atomic) ? \
	KCF_PROV_MAC_OPS(pd)->mac_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, data, mac, template, \
	    req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_VERIFY_ATOMIC(pd, session, mech, key, data, mac, \
	    template, req) ( \
	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_verify_atomic) ? \
	KCF_PROV_MAC_OPS(pd)->mac_verify_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, data, mac, template, \
	    req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_sign_ops(9S) entry points.
 */

#define	KCF_PROV_SIGN_INIT(pd, ctx, mech, key, template, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_init) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_init( \
	    ctx, mech, key, template, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN(pd, ctx, data, sig, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign) ? \
	KCF_PROV_SIGN_OPS(pd)->sign(ctx, data, sig, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_UPDATE(pd, ctx, data, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_update) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_update(ctx, data, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_FINAL(pd, ctx, sig, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_final) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_final(ctx, sig, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_ATOMIC(pd, session, mech, key, data, template, \
	    sig, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_atomic) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_RECOVER_INIT(pd, ctx, mech, key, template, \
	    req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_recover_init) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_recover_init(ctx, mech, key, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_RECOVER(pd, ctx, data, sig, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_recover) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_recover(ctx, data, sig, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_RECOVER_ATOMIC(pd, session, mech, key, data, template, \
	    sig, req) ( \
	(KCF_PROV_SIGN_OPS(pd) && \
	KCF_PROV_SIGN_OPS(pd)->sign_recover_atomic) ? \
	KCF_PROV_SIGN_OPS(pd)->sign_recover_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_verify_ops(9S) entry points.
 */

#define	KCF_PROV_VERIFY_INIT(pd, ctx, mech, key, template, req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_init) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_init(ctx, mech, key, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_VERIFY(pd, ctx, data, sig, req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify(ctx, data, sig, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_VERIFY_UPDATE(pd, ctx, data, req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_update) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_update(ctx, data, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_VERIFY_FINAL(pd, ctx, sig, req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_final) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_final(ctx, sig, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_VERIFY_ATOMIC(pd, session, mech, key, data, template, sig, \
	    req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_atomic) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_VERIFY_RECOVER_INIT(pd, ctx, mech, key, template, \
	    req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && \
	KCF_PROV_VERIFY_OPS(pd)->verify_recover_init) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_recover_init(ctx, mech, key, \
	    template, req) : CRYPTO_NOT_SUPPORTED)

/* verify_recover() CSPI routine has different argument order than verify() */
#define	KCF_PROV_VERIFY_RECOVER(pd, ctx, sig, data, req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_recover) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_recover(ctx, sig, data, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * verify_recover_atomic() CSPI routine has different argument order
 * than verify_atomic().
 */
#define	KCF_PROV_VERIFY_RECOVER_ATOMIC(pd, session, mech, key, sig, \
	    template, data,  req) ( \
	(KCF_PROV_VERIFY_OPS(pd) && \
	KCF_PROV_VERIFY_OPS(pd)->verify_recover_atomic) ? \
	KCF_PROV_VERIFY_OPS(pd)->verify_recover_atomic( \
	    (pd)->pd_prov_handle, session, mech, key, sig, data, template, \
	    req) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_dual_ops(9S) entry points.
 */

#define	KCF_PROV_DIGEST_ENCRYPT_UPDATE(digest_ctx, encrypt_ctx, plaintext, \
	    ciphertext, req) ( \
	(KCF_PROV_DUAL_OPS(pd) && \
	KCF_PROV_DUAL_OPS(pd)->digest_encrypt_update) ? \
	KCF_PROV_DUAL_OPS(pd)->digest_encrypt_update( \
	    digest_ctx, encrypt_ctx, plaintext, ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_DIGEST_UPDATE(decrypt_ctx, digest_ctx, ciphertext, \
	    plaintext, req) ( \
	(KCF_PROV_DUAL_OPS(pd) && \
	KCF_PROV_DUAL_OPS(pd)->decrypt_digest_update) ? \
	KCF_PROV_DUAL_OPS(pd)->decrypt_digest_update( \
	    decrypt_ctx, digest_ctx, ciphertext, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SIGN_ENCRYPT_UPDATE(sign_ctx, encrypt_ctx, plaintext, \
	    ciphertext, req) ( \
	(KCF_PROV_DUAL_OPS(pd) && \
	KCF_PROV_DUAL_OPS(pd)->sign_encrypt_update) ? \
	KCF_PROV_DUAL_OPS(pd)->sign_encrypt_update( \
	    sign_ctx, encrypt_ctx, plaintext, ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_DECRYPT_VERIFY_UPDATE(decrypt_ctx, verify_ctx, ciphertext, \
	    plaintext, req) ( \
	(KCF_PROV_DUAL_OPS(pd) && \
	KCF_PROV_DUAL_OPS(pd)->decrypt_verify_update) ? \
	KCF_PROV_DUAL_OPS(pd)->decrypt_verify_update( \
	    decrypt_ctx, verify_ctx, ciphertext, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_dual_cipher_mac_ops(9S) entry points.
 */

#define	KCF_PROV_ENCRYPT_MAC_INIT(pd, ctx, encr_mech, encr_key, mac_mech, \
	    mac_key, encr_ctx_template, mac_ctx_template, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_init) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_init( \
	    ctx, encr_mech, encr_key, mac_mech, mac_key, encr_ctx_template, \
	    mac_ctx_template, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_MAC(pd, ctx, plaintext, ciphertext, mac, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac( \
	    ctx, plaintext, ciphertext, mac, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_MAC_UPDATE(pd, ctx, plaintext, ciphertext, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_update) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_update( \
	    ctx, plaintext, ciphertext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_MAC_FINAL(pd, ctx, ciphertext, mac, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_final) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_final( \
	    ctx, ciphertext, mac, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_ENCRYPT_MAC_ATOMIC(pd, session, encr_mech, encr_key, \
	    mac_mech, mac_key, plaintext, ciphertext, mac, \
	    encr_ctx_template, mac_ctx_template, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_atomic) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_atomic( \
	    (pd)->pd_prov_handle, session, encr_mech, encr_key, \
	    mac_mech, mac_key, plaintext, ciphertext, mac, \
	    encr_ctx_template, mac_ctx_template, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_DECRYPT_INIT(pd, ctx, mac_mech, mac_key, decr_mech, \
	    decr_key, mac_ctx_template, decr_ctx_template, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_init) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_init( \
	    ctx, mac_mech, mac_key, decr_mech, decr_key, mac_ctx_template, \
	    decr_ctx_template, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_DECRYPT(pd, ctx, ciphertext, mac, plaintext, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt( \
	    ctx, ciphertext, mac, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_DECRYPT_UPDATE(pd, ctx, ciphertext, plaintext, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_update) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_update( \
	    ctx, ciphertext, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_DECRYPT_FINAL(pd, ctx, mac, plaintext, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_final) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_final( \
	    ctx, mac, plaintext, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_DECRYPT_ATOMIC(pd, session, mac_mech, mac_key, \
	    decr_mech, decr_key, ciphertext, mac, plaintext, \
	    mac_ctx_template, decr_ctx_template, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_atomic) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_atomic( \
	    (pd)->pd_prov_handle, session, mac_mech, mac_key, \
	    decr_mech, decr_key, ciphertext, mac, plaintext, \
	    mac_ctx_template, decr_ctx_template, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_MAC_VERIFY_DECRYPT_ATOMIC(pd, session, mac_mech, mac_key, \
	    decr_mech, decr_key, ciphertext, mac, plaintext, \
	    mac_ctx_template, decr_ctx_template, req) ( \
	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_verify_decrypt_atomic \
	    != NULL) ? \
	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_verify_decrypt_atomic( \
	    (pd)->pd_prov_handle, session, mac_mech, mac_key, \
	    decr_mech, decr_key, ciphertext, mac, plaintext, \
	    mac_ctx_template, decr_ctx_template, req) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_random_number_ops(9S) entry points.
 */

#define	KCF_PROV_SEED_RANDOM(pd, session, buf, len, est, flags, req) ( \
	(KCF_PROV_RANDOM_OPS(pd) && KCF_PROV_RANDOM_OPS(pd)->seed_random) ? \
	KCF_PROV_RANDOM_OPS(pd)->seed_random((pd)->pd_prov_handle, \
	    session, buf, len, est, flags, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_GENERATE_RANDOM(pd, session, buf, len, req) ( \
	(KCF_PROV_RANDOM_OPS(pd) && \
	KCF_PROV_RANDOM_OPS(pd)->generate_random) ? \
	KCF_PROV_RANDOM_OPS(pd)->generate_random((pd)->pd_prov_handle, \
	    session, buf, len, req) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_session_ops(9S) entry points.
 *
 * ops_pd is the provider descriptor that supplies the ops_vector.
 * pd is the descriptor that supplies the provider handle.
 * Only session open/close needs two handles.
 */

#define	KCF_PROV_SESSION_OPEN(ops_pd, session, req, pd) ( \
	(KCF_PROV_SESSION_OPS(ops_pd) && \
	KCF_PROV_SESSION_OPS(ops_pd)->session_open) ? \
	KCF_PROV_SESSION_OPS(ops_pd)->session_open((pd)->pd_prov_handle, \
	    session, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SESSION_CLOSE(ops_pd, session, req, pd) ( \
	(KCF_PROV_SESSION_OPS(ops_pd) && \
	KCF_PROV_SESSION_OPS(ops_pd)->session_close) ? \
	KCF_PROV_SESSION_OPS(ops_pd)->session_close((pd)->pd_prov_handle, \
	    session, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SESSION_LOGIN(pd, session, user_type, pin, len, req) ( \
	(KCF_PROV_SESSION_OPS(pd) && \
	KCF_PROV_SESSION_OPS(pd)->session_login) ? \
	KCF_PROV_SESSION_OPS(pd)->session_login((pd)->pd_prov_handle, \
	    session, user_type, pin, len, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SESSION_LOGOUT(pd, session, req) ( \
	(KCF_PROV_SESSION_OPS(pd) && \
	KCF_PROV_SESSION_OPS(pd)->session_logout) ? \
	KCF_PROV_SESSION_OPS(pd)->session_logout((pd)->pd_prov_handle, \
	    session, req) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_object_ops(9S) entry points.
 */

#define	KCF_PROV_OBJECT_CREATE(pd, session, template, count, object, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_create) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_create((pd)->pd_prov_handle, \
	    session, template, count, object, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_COPY(pd, session, object, template, count, \
	    new_object, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_copy) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_copy((pd)->pd_prov_handle, \
	session, object, template, count, new_object, req) : \
	    CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_DESTROY(pd, session, object, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_destroy) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_destroy((pd)->pd_prov_handle, \
	    session, object, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_GET_SIZE(pd, session, object, size, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && \
	KCF_PROV_OBJECT_OPS(pd)->object_get_size) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_get_size((pd)->pd_prov_handle, \
	    session, object, size, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_GET_ATTRIBUTE_VALUE(pd, session, object, template, \
	    count, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && \
	KCF_PROV_OBJECT_OPS(pd)->object_get_attribute_value) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_get_attribute_value( \
	(pd)->pd_prov_handle, session, object, template, count, req) : \
	    CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_SET_ATTRIBUTE_VALUE(pd, session, object, template, \
	    count, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && \
	KCF_PROV_OBJECT_OPS(pd)->object_set_attribute_value) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_set_attribute_value( \
	(pd)->pd_prov_handle, session, object, template, count, req) : \
	    CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_FIND_INIT(pd, session, template, count, ppriv, \
	    req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && \
	KCF_PROV_OBJECT_OPS(pd)->object_find_init) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_find_init((pd)->pd_prov_handle, \
	session, template, count, ppriv, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_FIND(pd, ppriv, objects, max_objects, object_count, \
	    req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_find) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_find( \
	(pd)->pd_prov_handle, ppriv, objects, max_objects, object_count, \
	req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_OBJECT_FIND_FINAL(pd, ppriv, req) ( \
	(KCF_PROV_OBJECT_OPS(pd) && \
	KCF_PROV_OBJECT_OPS(pd)->object_find_final) ? \
	KCF_PROV_OBJECT_OPS(pd)->object_find_final( \
	    (pd)->pd_prov_handle, ppriv, req) : CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_key_ops(9S) entry points.
 */

#define	KCF_PROV_KEY_GENERATE(pd, session, mech, template, count, object, \
	    req) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_generate) ? \
	KCF_PROV_KEY_OPS(pd)->key_generate((pd)->pd_prov_handle, \
	    session, mech, template, count, object, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_KEY_GENERATE_PAIR(pd, session, mech, pub_template, \
	    pub_count, priv_template, priv_count, pub_key, priv_key, req) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_generate_pair) ? \
	KCF_PROV_KEY_OPS(pd)->key_generate_pair((pd)->pd_prov_handle, \
	    session, mech, pub_template, pub_count, priv_template, \
	    priv_count, pub_key, priv_key, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_KEY_WRAP(pd, session, mech, wrapping_key, key, wrapped_key, \
	    wrapped_key_len, req) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_wrap) ? \
	KCF_PROV_KEY_OPS(pd)->key_wrap((pd)->pd_prov_handle, \
	    session, mech, wrapping_key, key, wrapped_key, wrapped_key_len, \
	    req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_KEY_UNWRAP(pd, session, mech, unwrapping_key, wrapped_key, \
	    wrapped_key_len, template, count, key, req) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_unwrap) ? \
	KCF_PROV_KEY_OPS(pd)->key_unwrap((pd)->pd_prov_handle, \
	    session, mech, unwrapping_key, wrapped_key, wrapped_key_len, \
	    template, count, key, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_KEY_DERIVE(pd, session, mech, base_key, template, count, \
	    key, req) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_derive) ? \
	KCF_PROV_KEY_OPS(pd)->key_derive((pd)->pd_prov_handle, \
	    session, mech, base_key, template, count, key, req) : \
	CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_KEY_CHECK(pd, mech, key) ( \
	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_check) ? \
	KCF_PROV_KEY_OPS(pd)->key_check((pd)->pd_prov_handle, mech, key) : \
	CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_provider_management_ops(9S) entry points.
 *
 * ops_pd is the provider descriptor that supplies the ops_vector.
 * pd is the descriptor that supplies the provider handle.
 * Only ext_info needs two handles.
 */

#define	KCF_PROV_EXT_INFO(ops_pd, provext_info, req, pd) ( \
	(KCF_PROV_PROVIDER_OPS(ops_pd) && \
	KCF_PROV_PROVIDER_OPS(ops_pd)->ext_info) ? \
	KCF_PROV_PROVIDER_OPS(ops_pd)->ext_info((pd)->pd_prov_handle, \
	    provext_info, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_INIT_TOKEN(pd, pin, pin_len, label, req) ( \
	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->init_token) ? \
	KCF_PROV_PROVIDER_OPS(pd)->init_token((pd)->pd_prov_handle, \
	    pin, pin_len, label, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_INIT_PIN(pd, session, pin, pin_len, req) ( \
	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->init_pin) ? \
	KCF_PROV_PROVIDER_OPS(pd)->init_pin((pd)->pd_prov_handle, \
	    session, pin, pin_len, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_SET_PIN(pd, session, old_pin, old_len, new_pin, new_len, \
	    req) ( \
	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->set_pin) ? \
	KCF_PROV_PROVIDER_OPS(pd)->set_pin((pd)->pd_prov_handle, \
	session, old_pin, old_len, new_pin, new_len, req) : \
	    CRYPTO_NOT_SUPPORTED)

/*
 * Wrappers for crypto_nostore_key_ops(9S) entry points.
 */

#define	KCF_PROV_NOSTORE_KEY_GENERATE(pd, session, mech, template, count, \
	    out_template, out_count, req) ( \
	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate) ? \
	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate( \
	    (pd)->pd_prov_handle, session, mech, template, count, \
	    out_template, out_count, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_NOSTORE_KEY_GENERATE_PAIR(pd, session, mech, pub_template, \
	    pub_count, priv_template, priv_count, out_pub_template, \
	    out_pub_count, out_priv_template, out_priv_count, req) ( \
	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate_pair) ? \
	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate_pair( \
	    (pd)->pd_prov_handle, session, mech, pub_template, pub_count, \
	    priv_template, priv_count, out_pub_template, out_pub_count, \
	    out_priv_template, out_priv_count, req) : CRYPTO_NOT_SUPPORTED)

#define	KCF_PROV_NOSTORE_KEY_DERIVE(pd, session, mech, base_key, template, \
	    count, out_template, out_count, req) ( \
	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_derive) ? \
	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_derive( \
	    (pd)->pd_prov_handle, session, mech, base_key, template, count, \
	    out_template, out_count, req) : CRYPTO_NOT_SUPPORTED)

/*
 * The following routines are exported by the kcf module (/kernel/misc/kcf)
 * to the crypto and cryptoadmin modules.
 */

/* Digest/mac/cipher entry points that take a provider descriptor and session */
extern int crypto_digest_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

extern int crypto_mac_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

extern int crypto_encrypt_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

extern int crypto_decrypt_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);


/* Other private digest/mac/cipher entry points not exported through k-API */
extern int crypto_digest_key_prov(crypto_context_t, crypto_key_t *,
    crypto_call_req_t *);

/* Private sign entry points exported by KCF */
extern int crypto_sign_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

extern int crypto_sign_recover_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

/* Private verify entry points exported by KCF */
extern int crypto_verify_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

extern int crypto_verify_recover_single(crypto_context_t, crypto_data_t *,
    crypto_data_t *, crypto_call_req_t *);

/* Private dual operations entry points exported by KCF */
extern int crypto_digest_encrypt_update(crypto_context_t, crypto_context_t,
    crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
extern int crypto_decrypt_digest_update(crypto_context_t, crypto_context_t,
    crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
extern int crypto_sign_encrypt_update(crypto_context_t, crypto_context_t,
    crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
extern int crypto_decrypt_verify_update(crypto_context_t, crypto_context_t,
    crypto_data_t *, crypto_data_t *, crypto_call_req_t *);

/* Random Number Generation */
int crypto_seed_random(crypto_provider_handle_t provider, uchar_t *buf,
    size_t len, crypto_call_req_t *req);
int crypto_generate_random(crypto_provider_handle_t provider, uchar_t *buf,
    size_t len, crypto_call_req_t *req);

/* Provider Management */
int crypto_get_provider_info(crypto_provider_id_t id,
    crypto_provider_info_t **info, crypto_call_req_t *req);
int crypto_get_provider_mechanisms(crypto_minor_t *, crypto_provider_id_t id,
    uint_t *count, crypto_mech_name_t **list);
int crypto_init_token(crypto_provider_handle_t provider, char *pin,
    size_t pin_len, char *label, crypto_call_req_t *);
int crypto_init_pin(crypto_provider_handle_t provider, char *pin,
    size_t pin_len, crypto_call_req_t *req);
int crypto_set_pin(crypto_provider_handle_t provider, char *old_pin,
    size_t old_len, char *new_pin, size_t new_len, crypto_call_req_t *req);
void crypto_free_provider_list(crypto_provider_entry_t *list, uint_t count);
void crypto_free_provider_info(crypto_provider_info_t *info);

/* Administrative */
int crypto_get_dev_list(uint_t *count, crypto_dev_list_entry_t **list);
int crypto_get_soft_list(uint_t *count, char **list, size_t *len);
int crypto_get_dev_info(char *name, uint_t instance, uint_t *count,
    crypto_mech_name_t **list);
int crypto_get_soft_info(caddr_t name, uint_t *count,
    crypto_mech_name_t **list);
int crypto_load_dev_disabled(char *name, uint_t instance, uint_t count,
    crypto_mech_name_t *list);
int crypto_load_soft_disabled(caddr_t name, uint_t count,
    crypto_mech_name_t *list);
int crypto_unload_soft_module(caddr_t path);
int crypto_load_soft_config(caddr_t name, uint_t count,
    crypto_mech_name_t *list);
int crypto_load_door(uint_t did);
void crypto_free_mech_list(crypto_mech_name_t *list, uint_t count);
void crypto_free_dev_list(crypto_dev_list_entry_t *list, uint_t count);
extern void kcf_activate();

/* Miscellaneous */
int crypto_get_mechanism_number(caddr_t name, crypto_mech_type_t *number);
int crypto_get_function_list(crypto_provider_id_t id,
    crypto_function_list_t **list, int kmflag);
void crypto_free_function_list(crypto_function_list_t *list);
int crypto_build_permitted_mech_names(kcf_provider_desc_t *,
    crypto_mech_name_t **, uint_t *, int);
extern void kcf_init_mech_tabs(void);
extern int kcf_add_mech_provider(short, kcf_provider_desc_t *,
    kcf_prov_mech_desc_t **);
extern void kcf_remove_mech_provider(char *, kcf_provider_desc_t *);
extern int kcf_get_mech_entry(crypto_mech_type_t, kcf_mech_entry_t **);
extern kcf_provider_desc_t *kcf_alloc_provider_desc(crypto_provider_info_t *);
extern void kcf_free_provider_desc(kcf_provider_desc_t *);
extern void kcf_soft_config_init(void);
extern int get_sw_provider_for_mech(crypto_mech_name_t, char **);
extern crypto_mech_type_t crypto_mech2id_common(char *, boolean_t);
extern void undo_register_provider(kcf_provider_desc_t *, boolean_t);
extern void redo_register_provider(kcf_provider_desc_t *);
extern void kcf_rnd_init();
extern boolean_t kcf_rngprov_check(void);
extern int kcf_rnd_get_pseudo_bytes(uint8_t *, size_t);
extern int kcf_rnd_get_bytes(uint8_t *, size_t, boolean_t);
extern int random_add_pseudo_entropy(uint8_t *, size_t, uint_t);
extern void kcf_rnd_chpoll(short, int, short *, struct pollhead **);
extern int crypto_uio_data(crypto_data_t *, uchar_t *, int, cmd_type_t,
    void *, void (*update)());
extern int crypto_mblk_data(crypto_data_t *, uchar_t *, int, cmd_type_t,
    void *, void (*update)());
extern int crypto_put_output_data(uchar_t *, crypto_data_t *, int);
extern int crypto_get_input_data(crypto_data_t *, uchar_t **, uchar_t *);
extern int crypto_compare_data(crypto_data_t *, uchar_t *, size_t);
extern int crypto_copy_key_to_ctx(crypto_key_t *, crypto_key_t **, size_t *,
    int kmflag);
extern int crypto_digest_data(crypto_data_t *, void *, uchar_t *,
    void (*update)(), void (*final)(), uchar_t);
extern int crypto_update_iov(void *, crypto_data_t *, crypto_data_t *,
    int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
    void (*copy_block)(uint8_t *, uint64_t *));
extern int crypto_update_uio(void *, crypto_data_t *, crypto_data_t *,
    int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
    void (*copy_block)(uint8_t *, uint64_t *));
extern int crypto_update_mp(void *, crypto_data_t *, crypto_data_t *,
    int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
    void (*copy_block)(uint8_t *, uint64_t *));
extern int crypto_get_key_attr(crypto_key_t *, crypto_attr_type_t, uchar_t **,
    ssize_t *);

/* param copyin helpers used in kcf_mech_tabs */
int kcf_copyin_aes_ccm_param(caddr_t, size_t, crypto_mechanism_t *, int, int);
int kcf_copyin_aes_gcm_param(caddr_t, size_t, crypto_mechanism_t *, int, int);
int kcf_copyin_aes_gmac_param(caddr_t, size_t, crypto_mechanism_t *, int, int);
int kcf_copyin_ecdh1_param(caddr_t, size_t, crypto_mechanism_t *, int, int);

/* Access to the provider's table */
extern void kcf_prov_tab_init(void);
extern int kcf_prov_tab_add_provider(kcf_provider_desc_t *);
extern int kcf_prov_tab_rem_provider(crypto_provider_id_t);
extern kcf_provider_desc_t *kcf_prov_tab_lookup_by_name(char *);
extern kcf_provider_desc_t *kcf_prov_tab_lookup_by_dev(char *, uint_t);
extern int kcf_get_hw_prov_tab(uint_t *, kcf_provider_desc_t ***, int,
    char *, uint_t, boolean_t);
extern int kcf_get_slot_list(uint_t *, kcf_provider_desc_t ***, boolean_t);
extern void kcf_free_provider_tab(uint_t, kcf_provider_desc_t **);
extern kcf_provider_desc_t *kcf_prov_tab_lookup(crypto_provider_id_t);
extern int kcf_get_sw_prov(crypto_mech_type_t, kcf_provider_desc_t **,
    kcf_mech_entry_t **, boolean_t);

extern kmutex_t prov_tab_mutex;
extern boolean_t kcf_need_provtab_walk;
extern int kcf_get_refcnt(kcf_provider_desc_t *, boolean_t);

/* Access to the policy table */
extern boolean_t is_mech_disabled(kcf_provider_desc_t *, crypto_mech_name_t);
extern boolean_t is_mech_disabled_byname(crypto_provider_type_t, char *,
    uint_t, crypto_mech_name_t);
extern void kcf_policy_tab_init(void);
extern void kcf_policy_free_desc(kcf_policy_desc_t *);
extern void kcf_policy_remove_by_name(char *, uint_t *, crypto_mech_name_t **);
extern void kcf_policy_remove_by_dev(char *, uint_t, uint_t *,
    crypto_mech_name_t **);
extern kcf_policy_desc_t *kcf_policy_lookup_by_name(char *);
extern kcf_policy_desc_t *kcf_policy_lookup_by_dev(char *, uint_t);
extern int kcf_policy_load_soft_disabled(char *, uint_t, crypto_mech_name_t *,
    uint_t *, crypto_mech_name_t **);
extern int kcf_policy_load_dev_disabled(char *, uint_t, uint_t,
    crypto_mech_name_t *, uint_t *, crypto_mech_name_t **);
extern void remove_soft_config(char *);

#endif	/* _KERNEL */

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_IMPL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYS_CRYPTO_IOCTL_H
#define	_SYS_CRYPTO_IOCTL_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/crypto/api.h>
#include <sys/crypto/spi.h>
#include <sys/crypto/common.h>

#define	CRYPTO_MAX_ATTRIBUTE_COUNT	128

#define	CRYPTO_IOFLAGS_RW_SESSION	0x00000001

#define	CRYPTO(x)		(('y' << 8) | (x))

#define	MAX_NUM_THRESHOLD	7

/* the PKCS11 Mechanisms */
#define	CKM_RC4			0x00000111UL
#define	CKM_DES3_ECB		0x00000132UL
#define	CKM_DES3_CBC		0x00000133UL
#define	CKM_MD5			0x00000210UL
#define	CKM_SHA_1		0x00000220UL
#define	CKM_AES_ECB		0x00001081UL
#define	CKM_AES_CBC		0x00001082UL

/*
 * General Purpose Ioctls
 */

typedef struct fl_mechs_threshold {
	int		mech_type;
	uint32_t	mech_threshold;
} fl_mechs_threshold_t;

typedef struct crypto_function_list {
	boolean_t fl_digest_init;
	boolean_t fl_digest;
	boolean_t fl_digest_update;
	boolean_t fl_digest_key;
	boolean_t fl_digest_final;

	boolean_t fl_encrypt_init;
	boolean_t fl_encrypt;
	boolean_t fl_encrypt_update;
	boolean_t fl_encrypt_final;

	boolean_t fl_decrypt_init;
	boolean_t fl_decrypt;
	boolean_t fl_decrypt_update;
	boolean_t fl_decrypt_final;

	boolean_t fl_mac_init;
	boolean_t fl_mac;
	boolean_t fl_mac_update;
	boolean_t fl_mac_final;

	boolean_t fl_sign_init;
	boolean_t fl_sign;
	boolean_t fl_sign_update;
	boolean_t fl_sign_final;
	boolean_t fl_sign_recover_init;
	boolean_t fl_sign_recover;

	boolean_t fl_verify_init;
	boolean_t fl_verify;
	boolean_t fl_verify_update;
	boolean_t fl_verify_final;
	boolean_t fl_verify_recover_init;
	boolean_t fl_verify_recover;

	boolean_t fl_digest_encrypt_update;
	boolean_t fl_decrypt_digest_update;
	boolean_t fl_sign_encrypt_update;
	boolean_t fl_decrypt_verify_update;

	boolean_t fl_seed_random;
	boolean_t fl_generate_random;

	boolean_t fl_session_open;
	boolean_t fl_session_close;
	boolean_t fl_session_login;
	boolean_t fl_session_logout;

	boolean_t fl_object_create;
	boolean_t fl_object_copy;
	boolean_t fl_object_destroy;
	boolean_t fl_object_get_size;
	boolean_t fl_object_get_attribute_value;
	boolean_t fl_object_set_attribute_value;
	boolean_t fl_object_find_init;
	boolean_t fl_object_find;
	boolean_t fl_object_find_final;

	boolean_t fl_key_generate;
	boolean_t fl_key_generate_pair;
	boolean_t fl_key_wrap;
	boolean_t fl_key_unwrap;
	boolean_t fl_key_derive;

	boolean_t fl_init_token;
	boolean_t fl_init_pin;
	boolean_t fl_set_pin;

	boolean_t prov_is_hash_limited;
	uint32_t prov_hash_threshold;
	uint32_t prov_hash_limit;

	boolean_t prov_is_hmac_limited;
	uint32_t prov_hmac_limit;

	int total_threshold_count;
	fl_mechs_threshold_t	fl_threshold[MAX_NUM_THRESHOLD];
} crypto_function_list_t;

typedef struct crypto_get_function_list {
	uint_t			fl_return_value;
	crypto_provider_id_t	fl_provider_id;
	crypto_function_list_t	fl_list;
} crypto_get_function_list_t;

typedef struct crypto_get_mechanism_number {
	uint_t			pn_return_value;
	caddr_t			pn_mechanism_string;
	size_t			pn_mechanism_len;
	crypto_mech_type_t	pn_internal_number;
} crypto_get_mechanism_number_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_get_mechanism_number32 {
	uint32_t		pn_return_value;
	caddr32_t		pn_mechanism_string;
	size32_t		pn_mechanism_len;
	crypto_mech_type_t	pn_internal_number;
} crypto_get_mechanism_number32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_GET_FUNCTION_LIST	CRYPTO(20)
#define	CRYPTO_GET_MECHANISM_NUMBER	CRYPTO(21)

/*
 * Session Ioctls
 */

typedef uint32_t	crypto_flags_t;

typedef struct crypto_open_session {
	uint_t			os_return_value;
	crypto_session_id_t	os_session;
	crypto_flags_t		os_flags;
	crypto_provider_id_t	os_provider_id;
} crypto_open_session_t;

typedef struct crypto_close_session {
	uint_t			cs_return_value;
	crypto_session_id_t	cs_session;
} crypto_close_session_t;

typedef struct crypto_close_all_sessions {
	uint_t			as_return_value;
	crypto_provider_id_t	as_provider_id;
} crypto_close_all_sessions_t;

#define	CRYPTO_OPEN_SESSION		CRYPTO(30)
#define	CRYPTO_CLOSE_SESSION		CRYPTO(31)
#define	CRYPTO_CLOSE_ALL_SESSIONS	CRYPTO(32)

/*
 * Login Ioctls
 */
typedef struct crypto_login {
	uint_t			co_return_value;
	crypto_session_id_t	co_session;
	uint_t			co_user_type;
	uint_t			co_pin_len;
	caddr_t			co_pin;
} crypto_login_t;

typedef struct crypto_logout {
	uint_t			cl_return_value;
	crypto_session_id_t	cl_session;
} crypto_logout_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_login32 {
	uint32_t		co_return_value;
	crypto_session_id_t	co_session;
	uint32_t		co_user_type;
	uint32_t		co_pin_len;
	caddr32_t		co_pin;
} crypto_login32_t;

typedef struct crypto_logout32 {
	uint32_t		cl_return_value;
	crypto_session_id_t	cl_session;
} crypto_logout32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_LOGIN			CRYPTO(40)
#define	CRYPTO_LOGOUT			CRYPTO(41)

/* flag for encrypt and decrypt operations */
#define	CRYPTO_INPLACE_OPERATION	0x00000001

/*
 * Cryptographic Ioctls
 */
typedef struct crypto_encrypt {
	uint_t			ce_return_value;
	crypto_session_id_t	ce_session;
	size_t			ce_datalen;
	caddr_t			ce_databuf;
	size_t			ce_encrlen;
	caddr_t			ce_encrbuf;
	uint_t			ce_flags;
} crypto_encrypt_t;

typedef struct crypto_encrypt_init {
	uint_t			ei_return_value;
	crypto_session_id_t	ei_session;
	crypto_mechanism_t	ei_mech;
	crypto_key_t		ei_key;
} crypto_encrypt_init_t;

typedef struct crypto_encrypt_update {
	uint_t			eu_return_value;
	crypto_session_id_t	eu_session;
	size_t			eu_datalen;
	caddr_t			eu_databuf;
	size_t			eu_encrlen;
	caddr_t			eu_encrbuf;
	uint_t			eu_flags;
} crypto_encrypt_update_t;

typedef struct crypto_encrypt_final {
	uint_t			ef_return_value;
	crypto_session_id_t	ef_session;
	size_t			ef_encrlen;
	caddr_t			ef_encrbuf;
} crypto_encrypt_final_t;

typedef struct crypto_decrypt {
	uint_t			cd_return_value;
	crypto_session_id_t	cd_session;
	size_t			cd_encrlen;
	caddr_t			cd_encrbuf;
	size_t			cd_datalen;
	caddr_t			cd_databuf;
	uint_t			cd_flags;
} crypto_decrypt_t;

typedef struct crypto_decrypt_init {
	uint_t			di_return_value;
	crypto_session_id_t	di_session;
	crypto_mechanism_t	di_mech;
	crypto_key_t		di_key;
} crypto_decrypt_init_t;

typedef struct crypto_decrypt_update {
	uint_t			du_return_value;
	crypto_session_id_t	du_session;
	size_t			du_encrlen;
	caddr_t			du_encrbuf;
	size_t			du_datalen;
	caddr_t			du_databuf;
	uint_t			du_flags;
} crypto_decrypt_update_t;

typedef struct crypto_decrypt_final {
	uint_t			df_return_value;
	crypto_session_id_t	df_session;
	size_t			df_datalen;
	caddr_t			df_databuf;
} crypto_decrypt_final_t;

typedef struct crypto_digest {
	uint_t			cd_return_value;
	crypto_session_id_t	cd_session;
	size_t			cd_datalen;
	caddr_t			cd_databuf;
	size_t			cd_digestlen;
	caddr_t			cd_digestbuf;
} crypto_digest_t;

typedef struct crypto_digest_init {
	uint_t			di_return_value;
	crypto_session_id_t	di_session;
	crypto_mechanism_t	di_mech;
} crypto_digest_init_t;

typedef struct crypto_digest_update {
	uint_t			du_return_value;
	crypto_session_id_t	du_session;
	size_t			du_datalen;
	caddr_t			du_databuf;
} crypto_digest_update_t;

typedef struct crypto_digest_key {
	uint_t			dk_return_value;
	crypto_session_id_t	dk_session;
	crypto_key_t		dk_key;
} crypto_digest_key_t;

typedef struct crypto_digest_final {
	uint_t			df_return_value;
	crypto_session_id_t	df_session;
	size_t			df_digestlen;
	caddr_t			df_digestbuf;
} crypto_digest_final_t;

typedef struct crypto_mac {
	uint_t			cm_return_value;
	crypto_session_id_t	cm_session;
	size_t			cm_datalen;
	caddr_t			cm_databuf;
	size_t			cm_maclen;
	caddr_t			cm_macbuf;
} crypto_mac_t;

typedef struct crypto_mac_init {
	uint_t			mi_return_value;
	crypto_session_id_t	mi_session;
	crypto_mechanism_t	mi_mech;
	crypto_key_t		mi_key;
} crypto_mac_init_t;

typedef struct crypto_mac_update {
	uint_t			mu_return_value;
	crypto_session_id_t	mu_session;
	size_t			mu_datalen;
	caddr_t			mu_databuf;
} crypto_mac_update_t;

typedef struct crypto_mac_final {
	uint_t			mf_return_value;
	crypto_session_id_t	mf_session;
	size_t			mf_maclen;
	caddr_t			mf_macbuf;
} crypto_mac_final_t;

typedef struct crypto_sign {
	uint_t			cs_return_value;
	crypto_session_id_t	cs_session;
	size_t			cs_datalen;
	caddr_t			cs_databuf;
	size_t			cs_signlen;
	caddr_t			cs_signbuf;
} crypto_sign_t;

typedef struct crypto_sign_init {
	uint_t			si_return_value;
	crypto_session_id_t	si_session;
	crypto_mechanism_t	si_mech;
	crypto_key_t		si_key;
} crypto_sign_init_t;

typedef struct crypto_sign_update {
	uint_t			su_return_value;
	crypto_session_id_t	su_session;
	size_t			su_datalen;
	caddr_t			su_databuf;
} crypto_sign_update_t;

typedef struct crypto_sign_final {
	uint_t			sf_return_value;
	crypto_session_id_t	sf_session;
	size_t			sf_signlen;
	caddr_t			sf_signbuf;
} crypto_sign_final_t;

typedef struct crypto_sign_recover_init {
	uint_t			ri_return_value;
	crypto_session_id_t	ri_session;
	crypto_mechanism_t	ri_mech;
	crypto_key_t		ri_key;
} crypto_sign_recover_init_t;

typedef struct crypto_sign_recover {
	uint_t			sr_return_value;
	crypto_session_id_t	sr_session;
	size_t			sr_datalen;
	caddr_t			sr_databuf;
	size_t			sr_signlen;
	caddr_t			sr_signbuf;
} crypto_sign_recover_t;

typedef struct crypto_verify {
	uint_t			cv_return_value;
	crypto_session_id_t	cv_session;
	size_t			cv_datalen;
	caddr_t			cv_databuf;
	size_t			cv_signlen;
	caddr_t			cv_signbuf;
} crypto_verify_t;

typedef struct crypto_verify_init {
	uint_t			vi_return_value;
	crypto_session_id_t	vi_session;
	crypto_mechanism_t	vi_mech;
	crypto_key_t		vi_key;
} crypto_verify_init_t;

typedef struct crypto_verify_update {
	uint_t			vu_return_value;
	crypto_session_id_t	vu_session;
	size_t			vu_datalen;
	caddr_t			vu_databuf;
} crypto_verify_update_t;

typedef struct crypto_verify_final {
	uint_t			vf_return_value;
	crypto_session_id_t	vf_session;
	size_t			vf_signlen;
	caddr_t			vf_signbuf;
} crypto_verify_final_t;

typedef struct crypto_verify_recover_init {
	uint_t			ri_return_value;
	crypto_session_id_t	ri_session;
	crypto_mechanism_t	ri_mech;
	crypto_key_t		ri_key;
} crypto_verify_recover_init_t;

typedef struct crypto_verify_recover {
	uint_t			vr_return_value;
	crypto_session_id_t	vr_session;
	size_t			vr_signlen;
	caddr_t			vr_signbuf;
	size_t			vr_datalen;
	caddr_t			vr_databuf;
} crypto_verify_recover_t;

typedef struct crypto_digest_encrypt_update {
	uint_t			eu_return_value;
	crypto_session_id_t	eu_session;
	size_t			eu_datalen;
	caddr_t			eu_databuf;
	size_t			eu_encrlen;
	caddr_t			eu_encrbuf;
} crypto_digest_encrypt_update_t;

typedef struct crypto_decrypt_digest_update {
	uint_t			du_return_value;
	crypto_session_id_t	du_session;
	size_t			du_encrlen;
	caddr_t			du_encrbuf;
	size_t			du_datalen;
	caddr_t			du_databuf;
} crypto_decrypt_digest_update_t;

typedef struct crypto_sign_encrypt_update {
	uint_t			eu_return_value;
	crypto_session_id_t	eu_session;
	size_t			eu_datalen;
	caddr_t			eu_databuf;
	size_t			eu_encrlen;
	caddr_t			eu_encrbuf;
} crypto_sign_encrypt_update_t;

typedef struct crypto_decrypt_verify_update {
	uint_t			vu_return_value;
	crypto_session_id_t	vu_session;
	size_t			vu_encrlen;
	caddr_t			vu_encrbuf;
	size_t			vu_datalen;
	caddr_t			vu_databuf;
} crypto_decrypt_verify_update_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_encrypt32 {
	uint32_t		ce_return_value;
	crypto_session_id_t	ce_session;
	size32_t		ce_datalen;
	caddr32_t		ce_databuf;
	size32_t		ce_encrlen;
	caddr32_t		ce_encrbuf;
	uint32_t		ce_flags;
} crypto_encrypt32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_encrypt_init32 {
	uint32_t		ei_return_value;
	crypto_session_id_t	ei_session;
	crypto_mechanism32_t	ei_mech;
	crypto_key32_t		ei_key;
} crypto_encrypt_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_encrypt_update32 {
	uint32_t		eu_return_value;
	crypto_session_id_t	eu_session;
	size32_t		eu_datalen;
	caddr32_t		eu_databuf;
	size32_t		eu_encrlen;
	caddr32_t		eu_encrbuf;
	uint_t			eu_flags;
} crypto_encrypt_update32_t;

typedef struct crypto_encrypt_final32 {
	uint32_t		ef_return_value;
	crypto_session_id_t	ef_session;
	size32_t		ef_encrlen;
	caddr32_t		ef_encrbuf;
} crypto_encrypt_final32_t;

typedef struct crypto_decrypt32 {
	uint32_t		cd_return_value;
	crypto_session_id_t	cd_session;
	size32_t		cd_encrlen;
	caddr32_t		cd_encrbuf;
	size32_t		cd_datalen;
	caddr32_t		cd_databuf;
	uint32_t		cd_flags;
} crypto_decrypt32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_decrypt_init32 {
	uint32_t		di_return_value;
	crypto_session_id_t	di_session;
	crypto_mechanism32_t	di_mech;
	crypto_key32_t		di_key;
} crypto_decrypt_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_decrypt_update32 {
	uint32_t		du_return_value;
	crypto_session_id_t	du_session;
	size32_t		du_encrlen;
	caddr32_t		du_encrbuf;
	size32_t		du_datalen;
	caddr32_t		du_databuf;
	uint_t			du_flags;
} crypto_decrypt_update32_t;

typedef struct crypto_decrypt_final32 {
	uint32_t		df_return_value;
	crypto_session_id_t	df_session;
	size32_t		df_datalen;
	caddr32_t		df_databuf;
} crypto_decrypt_final32_t;

typedef struct crypto_digest32 {
	uint32_t		cd_return_value;
	crypto_session_id_t	cd_session;
	size32_t		cd_datalen;
	caddr32_t		cd_databuf;
	size32_t		cd_digestlen;
	caddr32_t		cd_digestbuf;
} crypto_digest32_t;

typedef struct crypto_digest_init32 {
	uint32_t		di_return_value;
	crypto_session_id_t	di_session;
	crypto_mechanism32_t	di_mech;
} crypto_digest_init32_t;

typedef struct crypto_digest_update32 {
	uint32_t		du_return_value;
	crypto_session_id_t	du_session;
	size32_t		du_datalen;
	caddr32_t		du_databuf;
} crypto_digest_update32_t;

typedef struct crypto_digest_key32 {
	uint32_t		dk_return_value;
	crypto_session_id_t	dk_session;
	crypto_key32_t		dk_key;
} crypto_digest_key32_t;

typedef struct crypto_digest_final32 {
	uint32_t		df_return_value;
	crypto_session_id_t	df_session;
	size32_t		df_digestlen;
	caddr32_t		df_digestbuf;
} crypto_digest_final32_t;

typedef struct crypto_mac32 {
	uint32_t		cm_return_value;
	crypto_session_id_t	cm_session;
	size32_t		cm_datalen;
	caddr32_t		cm_databuf;
	size32_t		cm_maclen;
	caddr32_t		cm_macbuf;
} crypto_mac32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_mac_init32 {
	uint32_t		mi_return_value;
	crypto_session_id_t	mi_session;
	crypto_mechanism32_t	mi_mech;
	crypto_key32_t		mi_key;
} crypto_mac_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_mac_update32 {
	uint32_t		mu_return_value;
	crypto_session_id_t	mu_session;
	size32_t		mu_datalen;
	caddr32_t		mu_databuf;
} crypto_mac_update32_t;

typedef struct crypto_mac_final32 {
	uint32_t		mf_return_value;
	crypto_session_id_t	mf_session;
	size32_t		mf_maclen;
	caddr32_t		mf_macbuf;
} crypto_mac_final32_t;

typedef struct crypto_sign32 {
	uint32_t		cs_return_value;
	crypto_session_id_t	cs_session;
	size32_t		cs_datalen;
	caddr32_t		cs_databuf;
	size32_t		cs_signlen;
	caddr32_t		cs_signbuf;
} crypto_sign32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_sign_init32 {
	uint32_t		si_return_value;
	crypto_session_id_t	si_session;
	crypto_mechanism32_t	si_mech;
	crypto_key32_t		si_key;
} crypto_sign_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_sign_update32 {
	uint32_t		su_return_value;
	crypto_session_id_t	su_session;
	size32_t		su_datalen;
	caddr32_t		su_databuf;
} crypto_sign_update32_t;

typedef struct crypto_sign_final32 {
	uint32_t		sf_return_value;
	crypto_session_id_t	sf_session;
	size32_t		sf_signlen;
	caddr32_t		sf_signbuf;
} crypto_sign_final32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_sign_recover_init32 {
	uint32_t		ri_return_value;
	crypto_session_id_t	ri_session;
	crypto_mechanism32_t	ri_mech;
	crypto_key32_t		ri_key;
} crypto_sign_recover_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_sign_recover32 {
	uint32_t		sr_return_value;
	crypto_session_id_t	sr_session;
	size32_t		sr_datalen;
	caddr32_t		sr_databuf;
	size32_t		sr_signlen;
	caddr32_t		sr_signbuf;
} crypto_sign_recover32_t;

typedef struct crypto_verify32 {
	uint32_t		cv_return_value;
	crypto_session_id_t	cv_session;
	size32_t		cv_datalen;
	caddr32_t		cv_databuf;
	size32_t		cv_signlen;
	caddr32_t		cv_signbuf;
} crypto_verify32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_verify_init32 {
	uint32_t		vi_return_value;
	crypto_session_id_t	vi_session;
	crypto_mechanism32_t	vi_mech;
	crypto_key32_t		vi_key;
} crypto_verify_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_verify_update32 {
	uint32_t		vu_return_value;
	crypto_session_id_t	vu_session;
	size32_t		vu_datalen;
	caddr32_t		vu_databuf;
} crypto_verify_update32_t;

typedef struct crypto_verify_final32 {
	uint32_t		vf_return_value;
	crypto_session_id_t	vf_session;
	size32_t		vf_signlen;
	caddr32_t		vf_signbuf;
} crypto_verify_final32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_verify_recover_init32 {
	uint32_t		ri_return_value;
	crypto_session_id_t	ri_session;
	crypto_mechanism32_t	ri_mech;
	crypto_key32_t		ri_key;
} crypto_verify_recover_init32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_verify_recover32 {
	uint32_t		vr_return_value;
	crypto_session_id_t	vr_session;
	size32_t		vr_signlen;
	caddr32_t		vr_signbuf;
	size32_t		vr_datalen;
	caddr32_t		vr_databuf;
} crypto_verify_recover32_t;

typedef struct crypto_digest_encrypt_update32 {
	uint32_t		eu_return_value;
	crypto_session_id_t	eu_session;
	size32_t		eu_datalen;
	caddr32_t		eu_databuf;
	size32_t		eu_encrlen;
	caddr32_t		eu_encrbuf;
} crypto_digest_encrypt_update32_t;

typedef struct crypto_decrypt_digest_update32 {
	uint32_t		du_return_value;
	crypto_session_id_t	du_session;
	size32_t		du_encrlen;
	caddr32_t		du_encrbuf;
	size32_t		du_datalen;
	caddr32_t		du_databuf;
} crypto_decrypt_digest_update32_t;

typedef struct crypto_sign_encrypt_update32 {
	uint32_t		eu_return_value;
	crypto_session_id_t	eu_session;
	size32_t		eu_datalen;
	caddr32_t		eu_databuf;
	size32_t		eu_encrlen;
	caddr32_t		eu_encrbuf;
} crypto_sign_encrypt_update32_t;

typedef struct crypto_decrypt_verify_update32 {
	uint32_t		vu_return_value;
	crypto_session_id_t	vu_session;
	size32_t		vu_encrlen;
	caddr32_t		vu_encrbuf;
	size32_t		vu_datalen;
	caddr32_t		vu_databuf;
} crypto_decrypt_verify_update32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_ENCRYPT			CRYPTO(50)
#define	CRYPTO_ENCRYPT_INIT		CRYPTO(51)
#define	CRYPTO_ENCRYPT_UPDATE		CRYPTO(52)
#define	CRYPTO_ENCRYPT_FINAL		CRYPTO(53)
#define	CRYPTO_DECRYPT			CRYPTO(54)
#define	CRYPTO_DECRYPT_INIT		CRYPTO(55)
#define	CRYPTO_DECRYPT_UPDATE		CRYPTO(56)
#define	CRYPTO_DECRYPT_FINAL		CRYPTO(57)

#define	CRYPTO_DIGEST			CRYPTO(58)
#define	CRYPTO_DIGEST_INIT		CRYPTO(59)
#define	CRYPTO_DIGEST_UPDATE		CRYPTO(60)
#define	CRYPTO_DIGEST_KEY		CRYPTO(61)
#define	CRYPTO_DIGEST_FINAL		CRYPTO(62)
#define	CRYPTO_MAC			CRYPTO(63)
#define	CRYPTO_MAC_INIT			CRYPTO(64)
#define	CRYPTO_MAC_UPDATE		CRYPTO(65)
#define	CRYPTO_MAC_FINAL		CRYPTO(66)

#define	CRYPTO_SIGN			CRYPTO(67)
#define	CRYPTO_SIGN_INIT		CRYPTO(68)
#define	CRYPTO_SIGN_UPDATE		CRYPTO(69)
#define	CRYPTO_SIGN_FINAL		CRYPTO(70)
#define	CRYPTO_SIGN_RECOVER_INIT	CRYPTO(71)
#define	CRYPTO_SIGN_RECOVER		CRYPTO(72)
#define	CRYPTO_VERIFY			CRYPTO(73)
#define	CRYPTO_VERIFY_INIT		CRYPTO(74)
#define	CRYPTO_VERIFY_UPDATE		CRYPTO(75)
#define	CRYPTO_VERIFY_FINAL		CRYPTO(76)
#define	CRYPTO_VERIFY_RECOVER_INIT	CRYPTO(77)
#define	CRYPTO_VERIFY_RECOVER		CRYPTO(78)

#define	CRYPTO_DIGEST_ENCRYPT_UPDATE	CRYPTO(79)
#define	CRYPTO_DECRYPT_DIGEST_UPDATE	CRYPTO(80)
#define	CRYPTO_SIGN_ENCRYPT_UPDATE	CRYPTO(81)
#define	CRYPTO_DECRYPT_VERIFY_UPDATE	CRYPTO(82)

/*
 * Random Number Ioctls
 */
typedef struct crypto_seed_random {
	uint_t			sr_return_value;
	crypto_session_id_t	sr_session;
	size_t			sr_seedlen;
	caddr_t			sr_seedbuf;
} crypto_seed_random_t;

typedef struct crypto_generate_random {
	uint_t			gr_return_value;
	crypto_session_id_t	gr_session;
	caddr_t			gr_buf;
	size_t			gr_buflen;
} crypto_generate_random_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_seed_random32 {
	uint32_t		sr_return_value;
	crypto_session_id_t	sr_session;
	size32_t		sr_seedlen;
	caddr32_t		sr_seedbuf;
} crypto_seed_random32_t;

typedef struct crypto_generate_random32 {
	uint32_t		gr_return_value;
	crypto_session_id_t	gr_session;
	caddr32_t		gr_buf;
	size32_t		gr_buflen;
} crypto_generate_random32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_SEED_RANDOM		CRYPTO(90)
#define	CRYPTO_GENERATE_RANDOM		CRYPTO(91)

/*
 * Object Management Ioctls
 */
typedef struct crypto_object_create {
	uint_t			oc_return_value;
	crypto_session_id_t	oc_session;
	crypto_object_id_t	oc_handle;
	uint_t			oc_count;
	caddr_t			oc_attributes;
} crypto_object_create_t;

typedef struct crypto_object_copy {
	uint_t			oc_return_value;
	crypto_session_id_t	oc_session;
	crypto_object_id_t	oc_handle;
	crypto_object_id_t	oc_new_handle;
	uint_t			oc_count;
	caddr_t			oc_new_attributes;
} crypto_object_copy_t;

typedef struct crypto_object_destroy {
	uint_t			od_return_value;
	crypto_session_id_t	od_session;
	crypto_object_id_t	od_handle;
} crypto_object_destroy_t;

typedef struct crypto_object_get_attribute_value {
	uint_t			og_return_value;
	crypto_session_id_t	og_session;
	crypto_object_id_t	og_handle;
	uint_t			og_count;
	caddr_t			og_attributes;
} crypto_object_get_attribute_value_t;

typedef struct crypto_object_get_size {
	uint_t			gs_return_value;
	crypto_session_id_t	gs_session;
	crypto_object_id_t	gs_handle;
	size_t			gs_size;
} crypto_object_get_size_t;

typedef struct crypto_object_set_attribute_value {
	uint_t			sa_return_value;
	crypto_session_id_t	sa_session;
	crypto_object_id_t	sa_handle;
	uint_t			sa_count;
	caddr_t			sa_attributes;
} crypto_object_set_attribute_value_t;

typedef struct crypto_object_find_init {
	uint_t			fi_return_value;
	crypto_session_id_t	fi_session;
	uint_t			fi_count;
	caddr_t			fi_attributes;
} crypto_object_find_init_t;

typedef struct crypto_object_find_update {
	uint_t			fu_return_value;
	crypto_session_id_t	fu_session;
	uint_t			fu_max_count;
	uint_t			fu_count;
	caddr_t			fu_handles;
} crypto_object_find_update_t;

typedef struct crypto_object_find_final {
	uint_t			ff_return_value;
	crypto_session_id_t	ff_session;
} crypto_object_find_final_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_object_create32 {
	uint32_t		oc_return_value;
	crypto_session_id_t	oc_session;
	crypto_object_id_t	oc_handle;
	uint32_t		oc_count;
	caddr32_t		oc_attributes;
} crypto_object_create32_t;

typedef struct crypto_object_copy32 {
	uint32_t		oc_return_value;
	crypto_session_id_t	oc_session;
	crypto_object_id_t	oc_handle;
	crypto_object_id_t	oc_new_handle;
	uint32_t		oc_count;
	caddr32_t		oc_new_attributes;
} crypto_object_copy32_t;

typedef struct crypto_object_destroy32 {
	uint32_t		od_return_value;
	crypto_session_id_t	od_session;
	crypto_object_id_t	od_handle;
} crypto_object_destroy32_t;

typedef struct crypto_object_get_attribute_value32 {
	uint32_t		og_return_value;
	crypto_session_id_t	og_session;
	crypto_object_id_t	og_handle;
	uint32_t		og_count;
	caddr32_t		og_attributes;
} crypto_object_get_attribute_value32_t;

typedef struct crypto_object_get_size32 {
	uint32_t		gs_return_value;
	crypto_session_id_t	gs_session;
	crypto_object_id_t	gs_handle;
	size32_t		gs_size;
} crypto_object_get_size32_t;

typedef struct crypto_object_set_attribute_value32 {
	uint32_t		sa_return_value;
	crypto_session_id_t	sa_session;
	crypto_object_id_t	sa_handle;
	uint32_t		sa_count;
	caddr32_t		sa_attributes;
} crypto_object_set_attribute_value32_t;

typedef struct crypto_object_find_init32 {
	uint32_t		fi_return_value;
	crypto_session_id_t	fi_session;
	uint32_t		fi_count;
	caddr32_t		fi_attributes;
} crypto_object_find_init32_t;

typedef struct crypto_object_find_update32 {
	uint32_t		fu_return_value;
	crypto_session_id_t	fu_session;
	uint32_t		fu_max_count;
	uint32_t		fu_count;
	caddr32_t		fu_handles;
} crypto_object_find_update32_t;

typedef struct crypto_object_find_final32 {
	uint32_t		ff_return_value;
	crypto_session_id_t	ff_session;
} crypto_object_find_final32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_OBJECT_CREATE			CRYPTO(100)
#define	CRYPTO_OBJECT_COPY			CRYPTO(101)
#define	CRYPTO_OBJECT_DESTROY			CRYPTO(102)
#define	CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE	CRYPTO(103)
#define	CRYPTO_OBJECT_GET_SIZE			CRYPTO(104)
#define	CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE	CRYPTO(105)
#define	CRYPTO_OBJECT_FIND_INIT			CRYPTO(106)
#define	CRYPTO_OBJECT_FIND_UPDATE		CRYPTO(107)
#define	CRYPTO_OBJECT_FIND_FINAL		CRYPTO(108)

/*
 * Key Generation Ioctls
 */
typedef struct crypto_object_generate_key {
	uint_t			gk_return_value;
	crypto_session_id_t	gk_session;
	crypto_object_id_t	gk_handle;
	crypto_mechanism_t	gk_mechanism;
	uint_t			gk_count;
	caddr_t			gk_attributes;
} crypto_object_generate_key_t;

typedef struct crypto_object_generate_key_pair {
	uint_t			kp_return_value;
	crypto_session_id_t	kp_session;
	crypto_object_id_t	kp_public_handle;
	crypto_object_id_t	kp_private_handle;
	uint_t			kp_public_count;
	uint_t			kp_private_count;
	caddr_t			kp_public_attributes;
	caddr_t			kp_private_attributes;
	crypto_mechanism_t	kp_mechanism;
} crypto_object_generate_key_pair_t;

typedef struct crypto_object_wrap_key {
	uint_t			wk_return_value;
	crypto_session_id_t	wk_session;
	crypto_mechanism_t	wk_mechanism;
	crypto_key_t		wk_wrapping_key;
	crypto_object_id_t	wk_object_handle;
	size_t			wk_wrapped_key_len;
	caddr_t			wk_wrapped_key;
} crypto_object_wrap_key_t;

typedef struct crypto_object_unwrap_key {
	uint_t			uk_return_value;
	crypto_session_id_t	uk_session;
	crypto_mechanism_t	uk_mechanism;
	crypto_key_t		uk_unwrapping_key;
	crypto_object_id_t	uk_object_handle;
	size_t			uk_wrapped_key_len;
	caddr_t			uk_wrapped_key;
	uint_t			uk_count;
	caddr_t			uk_attributes;
} crypto_object_unwrap_key_t;

typedef struct crypto_derive_key {
	uint_t			dk_return_value;
	crypto_session_id_t	dk_session;
	crypto_mechanism_t	dk_mechanism;
	crypto_key_t		dk_base_key;
	crypto_object_id_t	dk_object_handle;
	uint_t			dk_count;
	caddr_t			dk_attributes;
} crypto_derive_key_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_object_generate_key32 {
	uint32_t		gk_return_value;
	crypto_session_id_t	gk_session;
	crypto_object_id_t	gk_handle;
	crypto_mechanism32_t	gk_mechanism;
	uint32_t		gk_count;
	caddr32_t		gk_attributes;
} crypto_object_generate_key32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

typedef struct crypto_object_generate_key_pair32 {
	uint32_t		kp_return_value;
	crypto_session_id_t	kp_session;
	crypto_object_id_t	kp_public_handle;
	crypto_object_id_t	kp_private_handle;
	uint32_t		kp_public_count;
	uint32_t		kp_private_count;
	caddr32_t		kp_public_attributes;
	caddr32_t		kp_private_attributes;
	crypto_mechanism32_t	kp_mechanism;
} crypto_object_generate_key_pair32_t;

typedef struct crypto_object_wrap_key32 {
	uint32_t		wk_return_value;
	crypto_session_id_t	wk_session;
	crypto_mechanism32_t	wk_mechanism;
	crypto_key32_t		wk_wrapping_key;
	crypto_object_id_t	wk_object_handle;
	size32_t		wk_wrapped_key_len;
	caddr32_t		wk_wrapped_key;
} crypto_object_wrap_key32_t;

typedef struct crypto_object_unwrap_key32 {
	uint32_t		uk_return_value;
	crypto_session_id_t	uk_session;
	crypto_mechanism32_t	uk_mechanism;
	crypto_key32_t		uk_unwrapping_key;
	crypto_object_id_t	uk_object_handle;
	size32_t		uk_wrapped_key_len;
	caddr32_t		uk_wrapped_key;
	uint32_t		uk_count;
	caddr32_t		uk_attributes;
} crypto_object_unwrap_key32_t;

typedef struct crypto_derive_key32 {
	uint32_t		dk_return_value;
	crypto_session_id_t	dk_session;
	crypto_mechanism32_t	dk_mechanism;
	crypto_key32_t		dk_base_key;
	crypto_object_id_t	dk_object_handle;
	uint32_t		dk_count;
	caddr32_t		dk_attributes;
} crypto_derive_key32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_GENERATE_KEY		CRYPTO(110)
#define	CRYPTO_GENERATE_KEY_PAIR	CRYPTO(111)
#define	CRYPTO_WRAP_KEY			CRYPTO(112)
#define	CRYPTO_UNWRAP_KEY		CRYPTO(113)
#define	CRYPTO_DERIVE_KEY		CRYPTO(114)

/*
 * Provider Management Ioctls
 */

typedef struct crypto_get_provider_list {
	uint_t			pl_return_value;
	uint_t			pl_count;
	crypto_provider_entry_t	pl_list[1];
} crypto_get_provider_list_t;

typedef struct crypto_provider_data {
	uchar_t			pd_prov_desc[CRYPTO_PROVIDER_DESCR_MAX_LEN];
	uchar_t			pd_label[CRYPTO_EXT_SIZE_LABEL];
	uchar_t			pd_manufacturerID[CRYPTO_EXT_SIZE_MANUF];
	uchar_t			pd_model[CRYPTO_EXT_SIZE_MODEL];
	uchar_t			pd_serial_number[CRYPTO_EXT_SIZE_SERIAL];
	ulong_t			pd_flags;
	ulong_t			pd_max_session_count;
	ulong_t			pd_session_count;
	ulong_t			pd_max_rw_session_count;
	ulong_t			pd_rw_session_count;
	ulong_t			pd_max_pin_len;
	ulong_t			pd_min_pin_len;
	ulong_t			pd_total_public_memory;
	ulong_t			pd_free_public_memory;
	ulong_t			pd_total_private_memory;
	ulong_t			pd_free_private_memory;
	crypto_version_t	pd_hardware_version;
	crypto_version_t	pd_firmware_version;
	uchar_t			pd_time[CRYPTO_EXT_SIZE_TIME];
} crypto_provider_data_t;

typedef struct crypto_get_provider_info {
	uint_t			gi_return_value;
	crypto_provider_id_t	gi_provider_id;
	crypto_provider_data_t	gi_provider_data;
} crypto_get_provider_info_t;

typedef struct crypto_get_provider_mechanisms {
	uint_t			pm_return_value;
	crypto_provider_id_t	pm_provider_id;
	uint_t			pm_count;
	crypto_mech_name_t	pm_list[1];
} crypto_get_provider_mechanisms_t;

typedef struct crypto_get_provider_mechanism_info {
	uint_t			mi_return_value;
	crypto_provider_id_t	mi_provider_id;
	crypto_mech_name_t	mi_mechanism_name;
	uint32_t		mi_min_key_size;
	uint32_t		mi_max_key_size;
	uint32_t		mi_flags;
} crypto_get_provider_mechanism_info_t;

typedef struct crypto_init_token {
	uint_t			it_return_value;
	crypto_provider_id_t	it_provider_id;
	caddr_t			it_pin;
	size_t			it_pin_len;
	caddr_t			it_label;
} crypto_init_token_t;

typedef struct crypto_init_pin {
	uint_t			ip_return_value;
	crypto_session_id_t	ip_session;
	caddr_t			ip_pin;
	size_t			ip_pin_len;
} crypto_init_pin_t;

typedef struct crypto_set_pin {
	uint_t			sp_return_value;
	crypto_session_id_t	sp_session;
	caddr_t			sp_old_pin;
	size_t			sp_old_len;
	caddr_t			sp_new_pin;
	size_t			sp_new_len;
} crypto_set_pin_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_get_provider_list32 {
	uint32_t		pl_return_value;
	uint32_t		pl_count;
	crypto_provider_entry_t pl_list[1];
} crypto_get_provider_list32_t;

typedef struct crypto_version32 {
	uchar_t	cv_major;
	uchar_t	cv_minor;
} crypto_version32_t;

typedef struct crypto_provider_data32 {
	uchar_t			pd_prov_desc[CRYPTO_PROVIDER_DESCR_MAX_LEN];
	uchar_t			pd_label[CRYPTO_EXT_SIZE_LABEL];
	uchar_t			pd_manufacturerID[CRYPTO_EXT_SIZE_MANUF];
	uchar_t			pd_model[CRYPTO_EXT_SIZE_MODEL];
	uchar_t			pd_serial_number[CRYPTO_EXT_SIZE_SERIAL];
	uint32_t		pd_flags;
	uint32_t		pd_max_session_count;
	uint32_t		pd_session_count;
	uint32_t		pd_max_rw_session_count;
	uint32_t		pd_rw_session_count;
	uint32_t		pd_max_pin_len;
	uint32_t		pd_min_pin_len;
	uint32_t		pd_total_public_memory;
	uint32_t		pd_free_public_memory;
	uint32_t		pd_total_private_memory;
	uint32_t		pd_free_private_memory;
	crypto_version32_t	pd_hardware_version;
	crypto_version32_t	pd_firmware_version;
	uchar_t			pd_time[CRYPTO_EXT_SIZE_TIME];
} crypto_provider_data32_t;

typedef struct crypto_get_provider_info32 {
	uint32_t		gi_return_value;
	crypto_provider_id_t	gi_provider_id;
	crypto_provider_data32_t gi_provider_data;
} crypto_get_provider_info32_t;

typedef struct crypto_get_provider_mechanisms32 {
	uint32_t		pm_return_value;
	crypto_provider_id_t	pm_provider_id;
	uint32_t		pm_count;
	crypto_mech_name_t	pm_list[1];
} crypto_get_provider_mechanisms32_t;

typedef struct crypto_init_token32 {
	uint32_t		it_return_value;
	crypto_provider_id_t	it_provider_id;
	caddr32_t		it_pin;
	size32_t		it_pin_len;
	caddr32_t		it_label;
} crypto_init_token32_t;

typedef struct crypto_init_pin32 {
	uint32_t		ip_return_value;
	crypto_session_id_t	ip_session;
	caddr32_t		ip_pin;
	size32_t		ip_pin_len;
} crypto_init_pin32_t;

typedef struct crypto_set_pin32 {
	uint32_t		sp_return_value;
	crypto_session_id_t	sp_session;
	caddr32_t		sp_old_pin;
	size32_t		sp_old_len;
	caddr32_t		sp_new_pin;
	size32_t		sp_new_len;
} crypto_set_pin32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_GET_PROVIDER_LIST		CRYPTO(120)
#define	CRYPTO_GET_PROVIDER_INFO		CRYPTO(121)
#define	CRYPTO_GET_PROVIDER_MECHANISMS		CRYPTO(122)
#define	CRYPTO_GET_PROVIDER_MECHANISM_INFO	CRYPTO(123)
#define	CRYPTO_INIT_TOKEN			CRYPTO(124)
#define	CRYPTO_INIT_PIN				CRYPTO(125)
#define	CRYPTO_SET_PIN				CRYPTO(126)

/*
 * No (Key) Store Key Generation Ioctls
 */
typedef struct crypto_nostore_generate_key {
	uint_t			ngk_return_value;
	crypto_session_id_t	ngk_session;
	crypto_mechanism_t	ngk_mechanism;
	uint_t			ngk_in_count;
	uint_t			ngk_out_count;
	caddr_t			ngk_in_attributes;
	caddr_t			ngk_out_attributes;
} crypto_nostore_generate_key_t;

typedef struct crypto_nostore_generate_key_pair {
	uint_t			nkp_return_value;
	crypto_session_id_t	nkp_session;
	uint_t			nkp_in_public_count;
	uint_t			nkp_in_private_count;
	uint_t			nkp_out_public_count;
	uint_t			nkp_out_private_count;
	caddr_t			nkp_in_public_attributes;
	caddr_t			nkp_in_private_attributes;
	caddr_t			nkp_out_public_attributes;
	caddr_t			nkp_out_private_attributes;
	crypto_mechanism_t	nkp_mechanism;
} crypto_nostore_generate_key_pair_t;

typedef struct crypto_nostore_derive_key {
	uint_t			ndk_return_value;
	crypto_session_id_t	ndk_session;
	crypto_mechanism_t	ndk_mechanism;
	crypto_key_t		ndk_base_key;
	uint_t			ndk_in_count;
	uint_t			ndk_out_count;
	caddr_t			ndk_in_attributes;
	caddr_t			ndk_out_attributes;
} crypto_nostore_derive_key_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_nostore_generate_key32 {
	uint32_t		ngk_return_value;
	crypto_session_id_t	ngk_session;
	crypto_mechanism32_t	ngk_mechanism;
	uint32_t		ngk_in_count;
	uint32_t		ngk_out_count;
	caddr32_t		ngk_in_attributes;
	caddr32_t		ngk_out_attributes;
} crypto_nostore_generate_key32_t;

typedef struct crypto_nostore_generate_key_pair32 {
	uint32_t		nkp_return_value;
	crypto_session_id_t	nkp_session;
	uint32_t		nkp_in_public_count;
	uint32_t		nkp_in_private_count;
	uint32_t		nkp_out_public_count;
	uint32_t		nkp_out_private_count;
	caddr32_t		nkp_in_public_attributes;
	caddr32_t		nkp_in_private_attributes;
	caddr32_t		nkp_out_public_attributes;
	caddr32_t		nkp_out_private_attributes;
	crypto_mechanism32_t	nkp_mechanism;
} crypto_nostore_generate_key_pair32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack(4)
#endif

typedef struct crypto_nostore_derive_key32 {
	uint32_t		ndk_return_value;
	crypto_session_id_t	ndk_session;
	crypto_mechanism32_t	ndk_mechanism;
	crypto_key32_t		ndk_base_key;
	uint32_t		ndk_in_count;
	uint32_t		ndk_out_count;
	caddr32_t		ndk_in_attributes;
	caddr32_t		ndk_out_attributes;
} crypto_nostore_derive_key32_t;

#if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
#pragma pack()
#endif

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_NOSTORE_GENERATE_KEY		CRYPTO(127)
#define	CRYPTO_NOSTORE_GENERATE_KEY_PAIR	CRYPTO(128)
#define	CRYPTO_NOSTORE_DERIVE_KEY		CRYPTO(129)

/*
 * Mechanism Ioctls
 */

typedef struct crypto_get_mechanism_list {
	uint_t			ml_return_value;
	uint_t			ml_count;
	crypto_mech_name_t	ml_list[1];
} crypto_get_mechanism_list_t;

typedef struct crypto_get_all_mechanism_info {
	uint_t			mi_return_value;
	crypto_mech_name_t	mi_mechanism_name;
	uint_t			mi_count;
	crypto_mechanism_info_t	mi_list[1];
} crypto_get_all_mechanism_info_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_get_mechanism_list32 {
	uint32_t		ml_return_value;
	uint32_t		ml_count;
	crypto_mech_name_t	ml_list[1];
} crypto_get_mechanism_list32_t;

typedef struct crypto_get_all_mechanism_info32 {
	uint32_t		mi_return_value;
	crypto_mech_name_t	mi_mechanism_name;
	uint32_t		mi_count;
	crypto_mechanism_info32_t mi_list[1];
} crypto_get_all_mechanism_info32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#define	CRYPTO_GET_MECHANISM_LIST		CRYPTO(140)
#define	CRYPTO_GET_ALL_MECHANISM_INFO		CRYPTO(141)

#define	CRYPTO_GET_PROVIDER_BY_MECH		CRYPTO(142)

typedef struct crypto_by_mech {
	int rv;
	int res;
	crypto_mech_type_t mech_type;
	uint_t mech_keylen;
	crypto_func_group_t mech_fg;
	crypto_session_id_t session_id;
} crypto_by_mech_t;

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_IOCTL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYS_CRYPTO_IOCTLADMIN_H
#define	_SYS_CRYPTO_IOCTLADMIN_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/crypto/common.h>

#define	ADMIN_IOCTL_DEVICE	"/dev/cryptoadm"

#define	CRYPTOADMIN(x)		(('y' << 8) | (x))

/*
 * Administrative IOCTLs
 */

typedef struct crypto_get_dev_list {
	uint_t			dl_return_value;
	uint_t			dl_dev_count;
	crypto_dev_list_entry_t	dl_devs[1];
} crypto_get_dev_list_t;

typedef struct crypto_get_soft_list {
	uint_t			sl_return_value;
	uint_t			sl_soft_count;
	size_t			sl_soft_len;
	caddr_t			sl_soft_names;
} crypto_get_soft_list_t;

typedef struct crypto_get_dev_info {
	uint_t			di_return_value;
	char			di_dev_name[MAXNAMELEN];
	uint_t			di_dev_instance;
	uint_t			di_count;
	crypto_mech_name_t	di_list[1];
} crypto_get_dev_info_t;

typedef struct crypto_get_soft_info {
	uint_t			si_return_value;
	char			si_name[MAXNAMELEN];
	uint_t			si_count;
	crypto_mech_name_t	si_list[1];
} crypto_get_soft_info_t;

typedef struct crypto_load_dev_disabled {
	uint_t			dd_return_value;
	char			dd_dev_name[MAXNAMELEN];
	uint_t			dd_dev_instance;
	uint_t			dd_count;
	crypto_mech_name_t	dd_list[1];
} crypto_load_dev_disabled_t;

typedef struct crypto_load_soft_disabled {
	uint_t			sd_return_value;
	char			sd_name[MAXNAMELEN];
	uint_t			sd_count;
	crypto_mech_name_t	sd_list[1];
} crypto_load_soft_disabled_t;

typedef struct crypto_unload_soft_module {
	uint_t			sm_return_value;
	char			sm_name[MAXNAMELEN];
} crypto_unload_soft_module_t;

typedef struct crypto_load_soft_config {
	uint_t			sc_return_value;
	char			sc_name[MAXNAMELEN];
	uint_t			sc_count;
	crypto_mech_name_t	sc_list[1];
} crypto_load_soft_config_t;

typedef struct crypto_load_door {
	uint_t			ld_return_value;
	uint_t			ld_did;
} crypto_load_door_t;

typedef struct crypto_fips140 {
	uint_t			fips140_return_value;
	uint_t			fips140_op;
	uint_t			fips140_status;
} crypto_fips140_t;

#ifdef	_KERNEL
#ifdef	_SYSCALL32

typedef struct crypto_get_soft_list32 {
	uint32_t		sl_return_value;
	uint32_t		sl_soft_count;
	size32_t		sl_soft_len;
	caddr32_t		sl_soft_names;
} crypto_get_soft_list32_t;

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

/*
 * FIPS140 ioctl operations
 */
enum {
	FIPS140_STATUS,		/* get current fips140 mode */
	FIPS140_ENABLE,		/* enable fips140 mode */
	FIPS140_DISABLE		/* disable fips140 mode */


};

/*
 * FIPS140 Mode Status
 */
enum {
	FIPS140_MODE_UNSET,	 /* userland has not told us the mode */
	FIPS140_MODE_VALIDATING, /* In the process of validation to enable */
	FIPS140_MODE_SHUTDOWN,	 /* Failure has occurred, shutdown framework */
	FIPS140_MODE_ENABLED,	 /* Validation is complete and we are running */
	FIPS140_MODE_DISABLED	 /* Not running in FIPS 140 mode */
};

#define	CRYPTO_GET_VERSION		CRYPTOADMIN(1)
#define	CRYPTO_GET_DEV_LIST		CRYPTOADMIN(2)
#define	CRYPTO_GET_SOFT_LIST		CRYPTOADMIN(3)
#define	CRYPTO_GET_DEV_INFO		CRYPTOADMIN(4)
#define	CRYPTO_GET_SOFT_INFO		CRYPTOADMIN(5)
#define	CRYPTO_LOAD_DEV_DISABLED	CRYPTOADMIN(8)
#define	CRYPTO_LOAD_SOFT_DISABLED	CRYPTOADMIN(9)
#define	CRYPTO_UNLOAD_SOFT_MODULE	CRYPTOADMIN(10)
#define	CRYPTO_LOAD_SOFT_CONFIG		CRYPTOADMIN(11)
#define	CRYPTO_POOL_CREATE		CRYPTOADMIN(12)
#define	CRYPTO_POOL_WAIT		CRYPTOADMIN(13)
#define	CRYPTO_POOL_RUN			CRYPTOADMIN(14)
#define	CRYPTO_LOAD_DOOR		CRYPTOADMIN(15)
#define	CRYPTO_FIPS140_STATUS		CRYPTOADMIN(16)
#define	CRYPTO_FIPS140_SET		CRYPTOADMIN(17)

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_IOCTLADMIN_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _SYS_CRYPTO_OPS_IMPL_H
#define	_SYS_CRYPTO_OPS_IMPL_H

/*
 * Scheduler internal structures.
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/crypto/api.h>
#include <sys/crypto/spi.h>
#include <sys/crypto/impl.h>
#include <sys/crypto/common.h>

/*
 * The parameters needed for each function group are batched
 * in one structure. This is much simpler than having a
 * separate structure for each function.
 *
 * In some cases, a field is generically named to keep the
 * structure small. The comments indicate these cases.
 */
typedef struct kcf_digest_ops_params {
	crypto_session_id_t	do_sid;
	crypto_mech_type_t	do_framework_mechtype;
	crypto_mechanism_t	do_mech;
	crypto_data_t		*do_data;
	crypto_data_t		*do_digest;
	crypto_key_t		*do_digest_key;	/* Argument for digest_key() */
} kcf_digest_ops_params_t;

typedef struct kcf_mac_ops_params {
	crypto_session_id_t		mo_sid;
	crypto_mech_type_t		mo_framework_mechtype;
	crypto_mechanism_t		mo_mech;
	crypto_key_t			*mo_key;
	crypto_data_t			*mo_data;
	crypto_data_t			*mo_mac;
	crypto_spi_ctx_template_t	mo_templ;
} kcf_mac_ops_params_t;

typedef struct kcf_encrypt_ops_params {
	crypto_session_id_t		eo_sid;
	crypto_mech_type_t		eo_framework_mechtype;
	crypto_mechanism_t		eo_mech;
	crypto_key_t			*eo_key;
	crypto_data_t			*eo_plaintext;
	crypto_data_t			*eo_ciphertext;
	crypto_spi_ctx_template_t	eo_templ;
} kcf_encrypt_ops_params_t;

typedef struct kcf_decrypt_ops_params {
	crypto_session_id_t		dop_sid;
	crypto_mech_type_t		dop_framework_mechtype;
	crypto_mechanism_t		dop_mech;
	crypto_key_t			*dop_key;
	crypto_data_t			*dop_ciphertext;
	crypto_data_t			*dop_plaintext;
	crypto_spi_ctx_template_t	dop_templ;
} kcf_decrypt_ops_params_t;

typedef struct kcf_sign_ops_params {
	crypto_session_id_t		so_sid;
	crypto_mech_type_t		so_framework_mechtype;
	crypto_mechanism_t		so_mech;
	crypto_key_t			*so_key;
	crypto_data_t			*so_data;
	crypto_data_t			*so_signature;
	crypto_spi_ctx_template_t	so_templ;
} kcf_sign_ops_params_t;

typedef struct kcf_verify_ops_params {
	crypto_session_id_t		vo_sid;
	crypto_mech_type_t		vo_framework_mechtype;
	crypto_mechanism_t		vo_mech;
	crypto_key_t			*vo_key;
	crypto_data_t			*vo_data;
	crypto_data_t			*vo_signature;
	crypto_spi_ctx_template_t	vo_templ;
} kcf_verify_ops_params_t;

typedef struct kcf_encrypt_mac_ops_params {
	crypto_session_id_t 		em_sid;
	crypto_mech_type_t		em_framework_encr_mechtype;
	crypto_mechanism_t		em_encr_mech;
	crypto_key_t			*em_encr_key;
	crypto_mech_type_t		em_framework_mac_mechtype;
	crypto_mechanism_t		em_mac_mech;
	crypto_key_t			*em_mac_key;
	crypto_data_t			*em_plaintext;
	crypto_dual_data_t		*em_ciphertext;
	crypto_data_t			*em_mac;
	crypto_spi_ctx_template_t	em_encr_templ;
	crypto_spi_ctx_template_t	em_mac_templ;
} kcf_encrypt_mac_ops_params_t;

typedef struct kcf_mac_decrypt_ops_params {
	crypto_session_id_t 		md_sid;
	crypto_mech_type_t		md_framework_mac_mechtype;
	crypto_mechanism_t		md_mac_mech;
	crypto_key_t			*md_mac_key;
	crypto_mech_type_t		md_framework_decr_mechtype;
	crypto_mechanism_t		md_decr_mech;
	crypto_key_t			*md_decr_key;
	crypto_dual_data_t		*md_ciphertext;
	crypto_data_t			*md_mac;
	crypto_data_t			*md_plaintext;
	crypto_spi_ctx_template_t	md_mac_templ;
	crypto_spi_ctx_template_t	md_decr_templ;
} kcf_mac_decrypt_ops_params_t;

typedef struct kcf_random_number_ops_params {
	crypto_session_id_t	rn_sid;
	uchar_t			*rn_buf;
	size_t			rn_buflen;
	uint_t			rn_entropy_est;
	uint32_t		rn_flags;
} kcf_random_number_ops_params_t;

/*
 * so_pd is useful when the provider descriptor (pd) supplying the
 * provider handle is different from the pd supplying the ops vector.
 * This is the case for session open/close where so_pd can be the pd
 * of a logical provider. The pd supplying the ops vector is passed
 * as an argument to kcf_submit_request().
 */
typedef struct kcf_session_ops_params {
	crypto_session_id_t	*so_sid_ptr;
	crypto_session_id_t	so_sid;
	crypto_user_type_t	so_user_type;
	char			*so_pin;
	size_t			so_pin_len;
	kcf_provider_desc_t	*so_pd;
} kcf_session_ops_params_t;

typedef struct kcf_object_ops_params {
	crypto_session_id_t		oo_sid;
	crypto_object_id_t		oo_object_id;
	crypto_object_attribute_t	*oo_template;
	uint_t 				oo_attribute_count;
	crypto_object_id_t		*oo_object_id_ptr;
	size_t				*oo_object_size;
	void				**oo_find_init_pp_ptr;
	void				*oo_find_pp;
	uint_t				oo_max_object_count;
	uint_t				*oo_object_count_ptr;
} kcf_object_ops_params_t;

/*
 * ko_key is used to encode wrapping key in key_wrap() and
 * unwrapping key in key_unwrap(). ko_key_template and
 * ko_key_attribute_count are used to encode public template
 * and public template attr count in key_generate_pair().
 * kops->ko_key_object_id_ptr is used to encode public key
 * in key_generate_pair().
 */
typedef struct kcf_key_ops_params {
	crypto_session_id_t		ko_sid;
	crypto_mech_type_t		ko_framework_mechtype;
	crypto_mechanism_t		ko_mech;
	crypto_object_attribute_t	*ko_key_template;
	uint_t				ko_key_attribute_count;
	crypto_object_id_t		*ko_key_object_id_ptr;
	crypto_object_attribute_t	*ko_private_key_template;
	uint_t				ko_private_key_attribute_count;
	crypto_object_id_t		*ko_private_key_object_id_ptr;
	crypto_key_t			*ko_key;
	uchar_t				*ko_wrapped_key;
	size_t				*ko_wrapped_key_len_ptr;
	crypto_object_attribute_t	*ko_out_template1;
	crypto_object_attribute_t	*ko_out_template2;
	uint_t				ko_out_attribute_count1;
	uint_t				ko_out_attribute_count2;
} kcf_key_ops_params_t;

/*
 * po_pin and po_pin_len are used to encode new_pin and new_pin_len
 * when wrapping set_pin() function parameters.
 *
 * po_pd is useful when the provider descriptor (pd) supplying the
 * provider handle is different from the pd supplying the ops vector.
 * This is true for the ext_info provider entry point where po_pd
 * can be the pd of a logical provider. The pd supplying the ops vector
 * is passed as an argument to kcf_submit_request().
 */
typedef struct kcf_provmgmt_ops_params {
	crypto_session_id_t 		po_sid;
	char				*po_pin;
	size_t				po_pin_len;
	char				*po_old_pin;
	size_t				po_old_pin_len;
	char				*po_label;
	crypto_provider_ext_info_t	*po_ext_info;
	kcf_provider_desc_t		*po_pd;
} kcf_provmgmt_ops_params_t;

/*
 * The operation type within a function group.
 */
typedef enum kcf_op_type {
	/* common ops for all mechanisms */
	KCF_OP_INIT = 1,
	KCF_OP_SINGLE,	/* pkcs11 sense. So, INIT is already done */
	KCF_OP_UPDATE,
	KCF_OP_FINAL,
	KCF_OP_ATOMIC,

	/* digest_key op */
	KCF_OP_DIGEST_KEY,

	/* mac specific op */
	KCF_OP_MAC_VERIFY_ATOMIC,

	/* mac/cipher specific op */
	KCF_OP_MAC_VERIFY_DECRYPT_ATOMIC,

	/* sign_recover ops */
	KCF_OP_SIGN_RECOVER_INIT,
	KCF_OP_SIGN_RECOVER,
	KCF_OP_SIGN_RECOVER_ATOMIC,

	/* verify_recover ops */
	KCF_OP_VERIFY_RECOVER_INIT,
	KCF_OP_VERIFY_RECOVER,
	KCF_OP_VERIFY_RECOVER_ATOMIC,

	/* random number ops */
	KCF_OP_RANDOM_SEED,
	KCF_OP_RANDOM_GENERATE,

	/* session management ops */
	KCF_OP_SESSION_OPEN,
	KCF_OP_SESSION_CLOSE,
	KCF_OP_SESSION_LOGIN,
	KCF_OP_SESSION_LOGOUT,

	/* object management ops */
	KCF_OP_OBJECT_CREATE,
	KCF_OP_OBJECT_COPY,
	KCF_OP_OBJECT_DESTROY,
	KCF_OP_OBJECT_GET_SIZE,
	KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
	KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
	KCF_OP_OBJECT_FIND_INIT,
	KCF_OP_OBJECT_FIND,
	KCF_OP_OBJECT_FIND_FINAL,

	/* key management ops */
	KCF_OP_KEY_GENERATE,
	KCF_OP_KEY_GENERATE_PAIR,
	KCF_OP_KEY_WRAP,
	KCF_OP_KEY_UNWRAP,
	KCF_OP_KEY_DERIVE,
	KCF_OP_KEY_CHECK,

	/* provider management ops */
	KCF_OP_MGMT_EXTINFO,
	KCF_OP_MGMT_INITTOKEN,
	KCF_OP_MGMT_INITPIN,
	KCF_OP_MGMT_SETPIN
} kcf_op_type_t;

/*
 * The operation groups that need wrapping of parameters. This is somewhat
 * similar to the function group type in spi.h except that this also includes
 * all the functions that don't have a mechanism.
 *
 * The wrapper macros should never take these enum values as an argument.
 * Rather, they are assigned in the macro itself since they are known
 * from the macro name.
 */
typedef enum kcf_op_group {
	KCF_OG_DIGEST = 1,
	KCF_OG_MAC,
	KCF_OG_ENCRYPT,
	KCF_OG_DECRYPT,
	KCF_OG_SIGN,
	KCF_OG_VERIFY,
	KCF_OG_ENCRYPT_MAC,
	KCF_OG_MAC_DECRYPT,
	KCF_OG_RANDOM,
	KCF_OG_SESSION,
	KCF_OG_OBJECT,
	KCF_OG_KEY,
	KCF_OG_PROVMGMT,
	KCF_OG_NOSTORE_KEY
} kcf_op_group_t;

/*
 * The kcf_op_type_t enum values used here should be only for those
 * operations for which there is a k-api routine in sys/crypto/api.h.
 */
#define	IS_INIT_OP(ftype)	((ftype) == KCF_OP_INIT)
#define	IS_SINGLE_OP(ftype)	((ftype) == KCF_OP_SINGLE)
#define	IS_UPDATE_OP(ftype)	((ftype) == KCF_OP_UPDATE)
#define	IS_FINAL_OP(ftype)	((ftype) == KCF_OP_FINAL)
#define	IS_ATOMIC_OP(ftype)	( \
	(ftype) == KCF_OP_ATOMIC || (ftype) == KCF_OP_MAC_VERIFY_ATOMIC || \
	(ftype) == KCF_OP_MAC_VERIFY_DECRYPT_ATOMIC || \
	(ftype) == KCF_OP_SIGN_RECOVER_ATOMIC || \
	(ftype) == KCF_OP_VERIFY_RECOVER_ATOMIC)

/*
 * Keep the parameters associated with a request around.
 * We need to pass them to the SPI.
 */
typedef struct kcf_req_params {
	kcf_op_group_t		rp_opgrp;
	kcf_op_type_t		rp_optype;

	union {
		kcf_digest_ops_params_t		digest_params;
		kcf_mac_ops_params_t		mac_params;
		kcf_encrypt_ops_params_t	encrypt_params;
		kcf_decrypt_ops_params_t	decrypt_params;
		kcf_sign_ops_params_t		sign_params;
		kcf_verify_ops_params_t		verify_params;
		kcf_encrypt_mac_ops_params_t	encrypt_mac_params;
		kcf_mac_decrypt_ops_params_t	mac_decrypt_params;
		kcf_random_number_ops_params_t	random_number_params;
		kcf_session_ops_params_t	session_params;
		kcf_object_ops_params_t		object_params;
		kcf_key_ops_params_t		key_params;
		kcf_provmgmt_ops_params_t	provmgmt_params;
	} rp_u;
} kcf_req_params_t;


/*
 * The ioctl/k-api code should bundle the parameters into a kcf_req_params_t
 * structure before calling a scheduler routine. The following macros are
 * available for that purpose.
 *
 * For the most part, the macro arguments closely correspond to the
 * function parameters. In some cases, we use generic names. The comments
 * for the structure should indicate these cases.
 */
#define	KCF_WRAP_DIGEST_OPS_PARAMS(req, ftype, _sid, _mech, _key,	\
	_data, _digest) {						\
	kcf_digest_ops_params_t *dops = &(req)->rp_u.digest_params;	\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_DIGEST;				\
	(req)->rp_optype = ftype;					\
	dops->do_sid = _sid;						\
	if (mechp != NULL) {						\
		dops->do_mech = *mechp;					\
		dops->do_framework_mechtype = mechp->cm_type;		\
	}								\
	dops->do_digest_key = _key;					\
	dops->do_data = _data;						\
	dops->do_digest = _digest;					\
}

#define	KCF_WRAP_MAC_OPS_PARAMS(req, ftype, _sid, _mech, _key,		\
	_data, _mac, _templ) {						\
	kcf_mac_ops_params_t *mops = &(req)->rp_u.mac_params;		\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_MAC;					\
	(req)->rp_optype = ftype;					\
	mops->mo_sid = _sid;						\
	if (mechp != NULL) {						\
		mops->mo_mech = *mechp;					\
		mops->mo_framework_mechtype = mechp->cm_type;		\
	}								\
	mops->mo_key = _key;						\
	mops->mo_data = _data;						\
	mops->mo_mac = _mac;						\
	mops->mo_templ = _templ;					\
}

#define	KCF_WRAP_ENCRYPT_OPS_PARAMS(req, ftype, _sid, _mech, _key,	\
	_plaintext, _ciphertext, _templ) {				\
	kcf_encrypt_ops_params_t *cops = &(req)->rp_u.encrypt_params;	\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_ENCRYPT;				\
	(req)->rp_optype = ftype;					\
	cops->eo_sid = _sid;						\
	if (mechp != NULL) {						\
		cops->eo_mech = *mechp;					\
		cops->eo_framework_mechtype = mechp->cm_type;		\
	}								\
	cops->eo_key = _key;						\
	cops->eo_plaintext = _plaintext;				\
	cops->eo_ciphertext = _ciphertext;				\
	cops->eo_templ = _templ;					\
}

#define	KCF_WRAP_DECRYPT_OPS_PARAMS(req, ftype, _sid, _mech, _key,	\
	_ciphertext, _plaintext, _templ) {				\
	kcf_decrypt_ops_params_t *cops = &(req)->rp_u.decrypt_params;	\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_DECRYPT;				\
	(req)->rp_optype = ftype;					\
	cops->dop_sid = _sid;						\
	if (mechp != NULL) {						\
		cops->dop_mech = *mechp;				\
		cops->dop_framework_mechtype = mechp->cm_type;		\
	}								\
	cops->dop_key = _key;						\
	cops->dop_ciphertext = _ciphertext;				\
	cops->dop_plaintext = _plaintext;				\
	cops->dop_templ = _templ;					\
}

#define	KCF_WRAP_SIGN_OPS_PARAMS(req, ftype, _sid, _mech, _key,		\
	_data, _signature, _templ) {					\
	kcf_sign_ops_params_t *sops = &(req)->rp_u.sign_params;		\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_SIGN;					\
	(req)->rp_optype = ftype;					\
	sops->so_sid = _sid;						\
	if (mechp != NULL) {						\
		sops->so_mech = *mechp;					\
		sops->so_framework_mechtype = mechp->cm_type;		\
	}								\
	sops->so_key = _key;						\
	sops->so_data = _data;						\
	sops->so_signature = _signature;				\
	sops->so_templ = _templ;					\
}

#define	KCF_WRAP_VERIFY_OPS_PARAMS(req, ftype, _sid, _mech, _key,	\
	_data, _signature, _templ) {					\
	kcf_verify_ops_params_t *vops = &(req)->rp_u.verify_params;	\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_VERIFY;				\
	(req)->rp_optype = ftype;					\
	vops->vo_sid = _sid;						\
	if (mechp != NULL) {						\
		vops->vo_mech = *mechp;					\
		vops->vo_framework_mechtype = mechp->cm_type;		\
	}								\
	vops->vo_key = _key;						\
	vops->vo_data = _data;						\
	vops->vo_signature = _signature;				\
	vops->vo_templ = _templ;					\
}

#define	KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(req, ftype, _sid, _encr_key,	\
	_mac_key, _plaintext, _ciphertext, _mac, _encr_templ, _mac_templ) { \
	kcf_encrypt_mac_ops_params_t *cmops = &(req)->rp_u.encrypt_mac_params; \
									\
	(req)->rp_opgrp = KCF_OG_ENCRYPT_MAC;				\
	(req)->rp_optype = ftype;					\
	cmops->em_sid = _sid;						\
	cmops->em_encr_key = _encr_key;					\
	cmops->em_mac_key = _mac_key;					\
	cmops->em_plaintext = _plaintext;				\
	cmops->em_ciphertext = _ciphertext;				\
	cmops->em_mac = _mac;						\
	cmops->em_encr_templ = _encr_templ;				\
	cmops->em_mac_templ = _mac_templ;				\
}

#define	KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(req, ftype, _sid, _mac_key,	\
	_decr_key, _ciphertext, _mac, _plaintext, _mac_templ, _decr_templ) { \
	kcf_mac_decrypt_ops_params_t *cmops = &(req)->rp_u.mac_decrypt_params; \
									\
	(req)->rp_opgrp = KCF_OG_MAC_DECRYPT;				\
	(req)->rp_optype = ftype;					\
	cmops->md_sid = _sid;						\
	cmops->md_mac_key = _mac_key;					\
	cmops->md_decr_key = _decr_key;					\
	cmops->md_ciphertext = _ciphertext;				\
	cmops->md_mac = _mac;						\
	cmops->md_plaintext = _plaintext;				\
	cmops->md_mac_templ = _mac_templ;				\
	cmops->md_decr_templ = _decr_templ;				\
}

#define	KCF_WRAP_RANDOM_OPS_PARAMS(req, ftype, _sid, _buf, _buflen,	\
	_est, _flags) {							\
	kcf_random_number_ops_params_t *rops =				\
		&(req)->rp_u.random_number_params;			\
									\
	(req)->rp_opgrp = KCF_OG_RANDOM;				\
	(req)->rp_optype = ftype;					\
	rops->rn_sid = _sid;						\
	rops->rn_buf = _buf;						\
	rops->rn_buflen = _buflen;					\
	rops->rn_entropy_est = _est;					\
	rops->rn_flags = _flags;					\
}

#define	KCF_WRAP_SESSION_OPS_PARAMS(req, ftype, _sid_ptr, _sid,		\
	_user_type, _pin, _pin_len, _pd) {				\
	kcf_session_ops_params_t *sops = &(req)->rp_u.session_params;	\
									\
	(req)->rp_opgrp = KCF_OG_SESSION;				\
	(req)->rp_optype = ftype;					\
	sops->so_sid_ptr = _sid_ptr;					\
	sops->so_sid = _sid;						\
	sops->so_user_type = _user_type;				\
	sops->so_pin = _pin;						\
	sops->so_pin_len = _pin_len;					\
	sops->so_pd = _pd;						\
}

#define	KCF_WRAP_OBJECT_OPS_PARAMS(req, ftype, _sid, _object_id,	\
	_template, _attribute_count, _object_id_ptr, _object_size,	\
	_find_init_pp_ptr, _find_pp, _max_object_count, _object_count_ptr) { \
	kcf_object_ops_params_t *jops = &(req)->rp_u.object_params;	\
									\
	(req)->rp_opgrp = KCF_OG_OBJECT;				\
	(req)->rp_optype = ftype;					\
	jops->oo_sid = _sid;						\
	jops->oo_object_id = _object_id;				\
	jops->oo_template = _template;					\
	jops->oo_attribute_count = _attribute_count;			\
	jops->oo_object_id_ptr = _object_id_ptr;			\
	jops->oo_object_size = _object_size;				\
	jops->oo_find_init_pp_ptr = _find_init_pp_ptr;			\
	jops->oo_find_pp = _find_pp;					\
	jops->oo_max_object_count = _max_object_count;			\
	jops->oo_object_count_ptr = _object_count_ptr;			\
}

#define	KCF_WRAP_KEY_OPS_PARAMS(req, ftype, _sid, _mech, _key_template, \
	_key_attribute_count, _key_object_id_ptr, _private_key_template, \
	_private_key_attribute_count, _private_key_object_id_ptr,	\
	_key, _wrapped_key, _wrapped_key_len_ptr) {			\
	kcf_key_ops_params_t *kops = &(req)->rp_u.key_params;		\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_KEY;					\
	(req)->rp_optype = ftype;					\
	kops->ko_sid = _sid;						\
	if (mechp != NULL) {						\
		kops->ko_mech = *mechp;					\
		kops->ko_framework_mechtype = mechp->cm_type;		\
	}								\
	kops->ko_key_template = _key_template;				\
	kops->ko_key_attribute_count = _key_attribute_count;		\
	kops->ko_key_object_id_ptr = _key_object_id_ptr;		\
	kops->ko_private_key_template = _private_key_template;		\
	kops->ko_private_key_attribute_count = _private_key_attribute_count; \
	kops->ko_private_key_object_id_ptr = _private_key_object_id_ptr; \
	kops->ko_key = _key;						\
	kops->ko_wrapped_key = _wrapped_key;				\
	kops->ko_wrapped_key_len_ptr = _wrapped_key_len_ptr;		\
}

#define	KCF_WRAP_PROVMGMT_OPS_PARAMS(req, ftype, _sid, _old_pin,	\
	_old_pin_len, _pin, _pin_len, _label, _ext_info, _pd) {		\
	kcf_provmgmt_ops_params_t *pops = &(req)->rp_u.provmgmt_params;	\
									\
	(req)->rp_opgrp = KCF_OG_PROVMGMT;				\
	(req)->rp_optype = ftype;					\
	pops->po_sid = _sid;						\
	pops->po_pin = _pin;						\
	pops->po_pin_len = _pin_len;					\
	pops->po_old_pin = _old_pin;					\
	pops->po_old_pin_len = _old_pin_len;				\
	pops->po_label = _label;					\
	pops->po_ext_info = _ext_info;					\
	pops->po_pd = _pd;						\
}

#define	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(req, ftype, _sid, _mech,	\
	_key_template, _key_attribute_count, _private_key_template,	\
	_private_key_attribute_count, _key, _out_template1,		\
	_out_attribute_count1, _out_template2, _out_attribute_count2) {	\
	kcf_key_ops_params_t *kops = &(req)->rp_u.key_params;		\
	crypto_mechanism_t *mechp = _mech;				\
									\
	(req)->rp_opgrp = KCF_OG_NOSTORE_KEY;				\
	(req)->rp_optype = ftype;					\
	kops->ko_sid = _sid;						\
	if (mechp != NULL) {						\
		kops->ko_mech = *mechp;					\
		kops->ko_framework_mechtype = mechp->cm_type;		\
	}								\
	kops->ko_key_template = _key_template;				\
	kops->ko_key_attribute_count = _key_attribute_count;		\
	kops->ko_key_object_id_ptr = NULL;				\
	kops->ko_private_key_template = _private_key_template;		\
	kops->ko_private_key_attribute_count = _private_key_attribute_count; \
	kops->ko_private_key_object_id_ptr = NULL;			\
	kops->ko_key = _key;						\
	kops->ko_wrapped_key = NULL;					\
	kops->ko_wrapped_key_len_ptr = 0;				\
	kops->ko_out_template1 = _out_template1;			\
	kops->ko_out_template2 = _out_template2;			\
	kops->ko_out_attribute_count1 = _out_attribute_count1;		\
	kops->ko_out_attribute_count2 = _out_attribute_count2;		\
}

#define	KCF_SET_PROVIDER_MECHNUM(fmtype, pd, mechp)			\
	(mechp)->cm_type =						\
	    KCF_TO_PROV_MECHNUM(pd, fmtype);

#ifdef __cplusplus
}
#endif

#endif /* _SYS_CRYPTO_OPS_IMPL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
 */

#ifndef _SYS_CRYPTO_SCHED_IMPL_H
#define	_SYS_CRYPTO_SCHED_IMPL_H

/*
 * Scheduler internal structures.
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/door.h>
#include <sys/crypto/api.h>
#include <sys/crypto/spi.h>
#include <sys/crypto/impl.h>
#include <sys/crypto/common.h>
#include <sys/crypto/ops_impl.h>

typedef void (kcf_func_t)(void *, int);

typedef enum kcf_req_status {
	REQ_ALLOCATED = 1,
	REQ_WAITING,		/* At the framework level */
	REQ_INPROGRESS,		/* At the provider level */
	REQ_DONE,
	REQ_CANCELED
} kcf_req_status_t;

typedef enum kcf_call_type {
	CRYPTO_SYNCH = 1,
	CRYPTO_ASYNCH
} kcf_call_type_t;

#define	CHECK_FASTPATH(crq, pd) ((crq) == NULL ||	\
	!((crq)->cr_flag & CRYPTO_ALWAYS_QUEUE)) &&	\
	(pd)->pd_prov_type == CRYPTO_SW_PROVIDER

#define	KCF_KMFLAG(crq)	(((crq) == NULL) ? KM_SLEEP : KM_NOSLEEP)

/*
 * The framework keeps an internal handle to use in the adaptive
 * asynchronous case. This is the case when a client has the
 * CRYPTO_ALWAYS_QUEUE bit clear and a software provider is used for
 * the request. The request is completed in the context of the calling
 * thread and kernel memory must be allocated with KM_NOSLEEP.
 *
 * The framework passes a pointer to the handle in crypto_req_handle_t
 * argument when it calls the SPI of the software provider. The macros
 * KCF_RHNDL() and KCF_SWFP_RHNDL() are used to do this.
 *
 * When a provider asks the framework for kmflag value via
 * crypto_kmflag(9S) we use REQHNDL2_KMFLAG() macro.
 */
extern ulong_t kcf_swprov_hndl;
#define	KCF_RHNDL(kmflag) (((kmflag) == KM_SLEEP) ? NULL : &kcf_swprov_hndl)
#define	KCF_SWFP_RHNDL(crq) (((crq) == NULL) ? NULL : &kcf_swprov_hndl)
#define	REQHNDL2_KMFLAG(rhndl) \
	((rhndl == &kcf_swprov_hndl) ? KM_NOSLEEP : KM_SLEEP)

/* Internal call_req flags. They start after the public ones in api.h */

#define	CRYPTO_SETDUAL	0x00001000	/* Set the 'cont' boolean before */
					/* submitting the request */
#define	KCF_ISDUALREQ(crq)	\
	(((crq) == NULL) ? B_FALSE : (crq->cr_flag & CRYPTO_SETDUAL))

typedef struct kcf_prov_tried {
	kcf_provider_desc_t	*pt_pd;
	struct kcf_prov_tried	*pt_next;
} kcf_prov_tried_t;

/* Must be different from KM_SLEEP and KM_NOSLEEP */
#define	KCF_HOLD_PROV	0x1000

#define	IS_FG_SUPPORTED(mdesc, fg)		\
	(((mdesc)->pm_mech_info.cm_func_group_mask & (fg)) != 0)

#define	IS_PROVIDER_TRIED(pd, tlist)		\
	(tlist != NULL && is_in_triedlist(pd, tlist))

#define	IS_RECOVERABLE(error)			\
	(error == CRYPTO_BUFFER_TOO_BIG ||	\
	error == CRYPTO_BUSY ||			\
	error == CRYPTO_DEVICE_ERROR ||		\
	error == CRYPTO_DEVICE_MEMORY ||	\
	error == CRYPTO_KEY_SIZE_RANGE ||	\
	error == CRYPTO_NO_PERMISSION)

#define	KCF_ATOMIC_INCR(x)	atomic_inc_32(&(x))
#define	KCF_ATOMIC_DECR(x)	atomic_dec_32(&(x))

/*
 * Node structure for synchronous requests.
 */
typedef struct kcf_sreq_node {
	/* Should always be the first field in this structure */
	kcf_call_type_t		sn_type;
	/*
	 * sn_cv and sr_lock are used to wait for the
	 * operation to complete. sn_lock also protects
	 * the sn_state field.
	 */
	kcondvar_t		sn_cv;
	kmutex_t		sn_lock;
	kcf_req_status_t	sn_state;

	/*
	 * Return value from the operation. This will be
	 * one of the CRYPTO_* errors defined in common.h.
	 */
	int			sn_rv;

	/*
	 * parameters to call the SPI with. This can be
	 * a pointer as we know the caller context/stack stays.
	 */
	struct kcf_req_params	*sn_params;

	/* Internal context for this request */
	struct kcf_context	*sn_context;

	/* Provider handling this request */
	kcf_provider_desc_t	*sn_provider;

	kcf_prov_cpu_t		*sn_mp;
} kcf_sreq_node_t;

/*
 * Node structure for asynchronous requests. A node can be on
 * on a chain of requests hanging of the internal context
 * structure and can be in the global software provider queue.
 */
typedef struct kcf_areq_node {
	/* Should always be the first field in this structure */
	kcf_call_type_t		an_type;

	/* an_lock protects the field an_state  */
	kmutex_t		an_lock;
	kcf_req_status_t	an_state;
	crypto_call_req_t	an_reqarg;

	/*
	 * parameters to call the SPI with. We need to
	 * save the params since the caller stack can go away.
	 */
	struct kcf_req_params	an_params;

	/*
	 * The next two fields should be NULL for operations that
	 * don't need a context.
	 */
	/* Internal context for this request */
	struct kcf_context	*an_context;

	/* next in chain of requests for context */
	struct kcf_areq_node	*an_ctxchain_next;

	kcondvar_t		an_turn_cv;
	boolean_t		an_is_my_turn;
	boolean_t		an_isdual;	/* for internal reuse */

	/*
	 * Next and previous nodes in the global software
	 * queue. These fields are NULL for a hardware
	 * provider since we use a taskq there.
	 */
	struct kcf_areq_node	*an_next;
	struct kcf_areq_node	*an_prev;

	/* Provider handling this request */
	kcf_provider_desc_t	*an_provider;
	kcf_prov_cpu_t		*an_mp;
	kcf_prov_tried_t	*an_tried_plist;

	struct kcf_areq_node	*an_idnext;	/* Next in ID hash */
	struct kcf_areq_node	*an_idprev;	/* Prev in ID hash */
	kcondvar_t		an_done;	/* Signal request completion */
	uint_t			an_refcnt;
} kcf_areq_node_t;

#define	KCF_AREQ_REFHOLD(areq) {		\
	atomic_inc_32(&(areq)->an_refcnt);	\
	ASSERT((areq)->an_refcnt != 0);		\
}

#define	KCF_AREQ_REFRELE(areq) {				\
	ASSERT((areq)->an_refcnt != 0);				\
	membar_exit();						\
	if (atomic_dec_32_nv(&(areq)->an_refcnt) == 0)	\
		kcf_free_req(areq);				\
}

#define	GET_REQ_TYPE(arg) *((kcf_call_type_t *)(arg))

#define	NOTIFY_CLIENT(areq, err) (*(areq)->an_reqarg.cr_callback_func)(\
	(areq)->an_reqarg.cr_callback_arg, err);

/* For internally generated call requests for dual operations */
typedef	struct kcf_call_req {
	crypto_call_req_t	kr_callreq;	/* external client call req */
	kcf_req_params_t	kr_params;	/* Params saved for next call */
	kcf_areq_node_t		*kr_areq;	/* Use this areq */
	off_t			kr_saveoffset;
	size_t			kr_savelen;
} kcf_dual_req_t;

/*
 * The following are some what similar to macros in callo.h, which implement
 * callout tables.
 *
 * The lower four bits of the ID are used to encode the table ID to
 * index in to. The REQID_COUNTER_HIGH bit is used to avoid any check for
 * wrap around when generating ID. We assume that there won't be a request
 * which takes more time than 2^^(sizeof (long) - 5) other requests submitted
 * after it. This ensures there won't be any ID collision.
 */
#define	REQID_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 1))
#define	REQID_COUNTER_SHIFT	4
#define	REQID_COUNTER_LOW	(1 << REQID_COUNTER_SHIFT)
#define	REQID_TABLES		16
#define	REQID_TABLE_MASK	(REQID_TABLES - 1)

#define	REQID_BUCKETS		512
#define	REQID_BUCKET_MASK	(REQID_BUCKETS - 1)
#define	REQID_HASH(id)	(((id) >> REQID_COUNTER_SHIFT) & REQID_BUCKET_MASK)

#define	GET_REQID(areq) (areq)->an_reqarg.cr_reqid
#define	SET_REQID(areq, val)	GET_REQID(areq) = val

/*
 * Hash table for async requests.
 */
typedef struct kcf_reqid_table {
	kmutex_t		rt_lock;
	crypto_req_id_t		rt_curid;
	kcf_areq_node_t		*rt_idhash[REQID_BUCKETS];
} kcf_reqid_table_t;

/*
 * Global software provider queue structure. Requests to be
 * handled by a SW provider and have the ALWAYS_QUEUE flag set
 * get queued here.
 */
typedef struct kcf_global_swq {
	/*
	 * gs_cv and gs_lock are used to wait for new requests.
	 * gs_lock protects the changes to the queue.
	 */
	kcondvar_t		gs_cv;
	kmutex_t		gs_lock;
	uint_t			gs_njobs;
	uint_t			gs_maxjobs;
	kcf_areq_node_t		*gs_first;
	kcf_areq_node_t		*gs_last;
} kcf_global_swq_t;


/*
 * Internal representation of a canonical context. We contain crypto_ctx_t
 * structure in order to have just one memory allocation. The SPI
 * ((crypto_ctx_t *)ctx)->cc_framework_private maps to this structure.
 */
typedef struct kcf_context {
	crypto_ctx_t		kc_glbl_ctx;
	uint_t			kc_refcnt;
	kmutex_t		kc_in_use_lock;
	/*
	 * kc_req_chain_first and kc_req_chain_last are used to chain
	 * multiple async requests using the same context. They should be
	 * NULL for sync requests.
	 */
	kcf_areq_node_t		*kc_req_chain_first;
	kcf_areq_node_t		*kc_req_chain_last;
	kcf_provider_desc_t	*kc_prov_desc;	/* Prov. descriptor */
	kcf_provider_desc_t	*kc_sw_prov_desc;	/* Prov. descriptor */
	kcf_mech_entry_t	*kc_mech;
	struct kcf_context	*kc_secondctx;	/* for dual contexts */
} kcf_context_t;

/*
 * Bump up the reference count on the framework private context. A
 * global context or a request that references this structure should
 * do a hold.
 */
#define	KCF_CONTEXT_REFHOLD(ictx) {		\
	atomic_inc_32(&(ictx)->kc_refcnt);	\
	ASSERT((ictx)->kc_refcnt != 0);		\
}

/*
 * Decrement the reference count on the framework private context.
 * When the last reference is released, the framework private
 * context structure is freed along with the global context.
 */
#define	KCF_CONTEXT_REFRELE(ictx) {				\
	ASSERT((ictx)->kc_refcnt != 0);				\
	membar_exit();						\
	if (atomic_dec_32_nv(&(ictx)->kc_refcnt) == 0)	\
		kcf_free_context(ictx);				\
}

/*
 * Check if we can release the context now. In case of CRYPTO_QUEUED
 * we do not release it as we can do it only after the provider notified
 * us. In case of CRYPTO_BUSY, the client can retry the request using
 * the context, so we do not release the context.
 *
 * This macro should be called only from the final routine in
 * an init/update/final sequence. We do not release the context in case
 * of update operations. We require the consumer to free it
 * explicitly, in case it wants to abandon the operation. This is done
 * as there may be mechanisms in ECB mode that can continue even if
 * an operation on a block fails.
 */
#define	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx) {			\
	if (KCF_CONTEXT_DONE(rv))				\
		KCF_CONTEXT_REFRELE(kcf_ctx);			\
}

/*
 * This macro determines whether we're done with a context.
 */
#define	KCF_CONTEXT_DONE(rv)					\
	((rv) != CRYPTO_QUEUED && (rv) != CRYPTO_BUSY &&	\
	    (rv) != CRYPTO_BUFFER_TOO_SMALL)

/*
 * A crypto_ctx_template_t is internally a pointer to this struct
 */
typedef	struct kcf_ctx_template {
	crypto_kcf_provider_handle_t	ct_prov_handle;	/* provider handle */
	uint_t				ct_generation;	/* generation # */
	size_t				ct_size;	/* for freeing */
	crypto_spi_ctx_template_t	ct_prov_tmpl;	/* context template */
							/* from the SW prov */
} kcf_ctx_template_t;

/*
 * Structure for pool of threads working on global software queue.
 */
typedef struct kcf_pool {
	uint32_t	kp_threads;		/* Number of threads in pool */
	uint32_t	kp_idlethreads;		/* Idle threads in pool */
	uint32_t	kp_blockedthreads;	/* Blocked threads in pool */

	/*
	 * cv & lock for the condition where more threads need to be created.
	 */
	kcondvar_t	kp_cv;		/* Creator cond. variable */
	kmutex_t	kp_lock;		/* Creator lock */

} kcf_pool_t;


/*
 * State of a crypto bufcall element.
 */
typedef enum cbuf_state {
	CBUF_FREE = 1,
	CBUF_WAITING,
	CBUF_RUNNING
} cbuf_state_t;

/*
 * Structure of a crypto bufcall element.
 */
typedef struct kcf_cbuf_elem {
	/*
	 * lock and cv to wait for CBUF_RUNNING to be done
	 * kc_lock also protects kc_state.
	 */
	kmutex_t		kc_lock;
	kcondvar_t		kc_cv;
	cbuf_state_t		kc_state;

	struct kcf_cbuf_elem	*kc_next;
	struct kcf_cbuf_elem	*kc_prev;

	void			(*kc_func)(void *arg);
	void			*kc_arg;
} kcf_cbuf_elem_t;

/*
 * State of a notify element.
 */
typedef enum ntfy_elem_state {
	NTFY_WAITING = 1,
	NTFY_RUNNING
} ntfy_elem_state_t;

/*
 * Structure of a notify list element.
 */
typedef struct kcf_ntfy_elem {
	/*
	 * lock and cv to wait for NTFY_RUNNING to be done.
	 * kn_lock also protects kn_state.
	 */
	kmutex_t			kn_lock;
	kcondvar_t			kn_cv;
	ntfy_elem_state_t		kn_state;

	struct kcf_ntfy_elem		*kn_next;
	struct kcf_ntfy_elem		*kn_prev;

	crypto_notify_callback_t	kn_func;
	uint32_t			kn_event_mask;
} kcf_ntfy_elem_t;


/*
 * The following values are based on the assumption that it would
 * take around eight cpus to load a hardware provider (This is true for
 * at least one product) and a kernel client may come from different
 * low-priority interrupt levels. We will have CYRPTO_TASKQ_MIN number
 * of cached taskq entries. The CRYPTO_TASKQ_MAX number is based on
 * a throughput of 1GB/s using 512-byte buffers. These are just
 * reasonable estimates and might need to change in future.
 */
#define	CRYPTO_TASKQ_THREADS	8
#define	CYRPTO_TASKQ_MIN	64
#define	CRYPTO_TASKQ_MAX	2 * 1024 * 1024

extern int crypto_taskq_threads;
extern int crypto_taskq_minalloc;
extern int crypto_taskq_maxalloc;
extern kcf_global_swq_t *gswq;
extern int kcf_maxthreads;
extern int kcf_minthreads;

/*
 * All pending crypto bufcalls are put on a list. cbuf_list_lock
 * protects changes to this list.
 */
extern kmutex_t cbuf_list_lock;
extern kcondvar_t cbuf_list_cv;

/*
 * All event subscribers are put on a list. kcf_notify_list_lock
 * protects changes to this list.
 */
extern kmutex_t ntfy_list_lock;
extern kcondvar_t ntfy_list_cv;

boolean_t kcf_get_next_logical_provider_member(kcf_provider_desc_t *,
    kcf_provider_desc_t *, kcf_provider_desc_t **);
extern int kcf_get_hardware_provider(crypto_mech_type_t, crypto_key_t *,
    crypto_mech_type_t, crypto_key_t *,
    kcf_provider_desc_t *, kcf_provider_desc_t **,
    crypto_func_group_t);
extern int kcf_get_hardware_provider_nomech(offset_t, offset_t,
    kcf_provider_desc_t *, kcf_provider_desc_t **);
extern void kcf_free_triedlist(kcf_prov_tried_t *);
extern kcf_prov_tried_t *kcf_insert_triedlist(kcf_prov_tried_t **,
    kcf_provider_desc_t *, int);
extern kcf_provider_desc_t *kcf_get_mech_provider(crypto_mech_type_t,
    crypto_key_t *, kcf_mech_entry_t **, int *, kcf_prov_tried_t *,
    crypto_func_group_t, size_t);
extern kcf_provider_desc_t *kcf_get_dual_provider(crypto_mechanism_t *,
    crypto_key_t *, crypto_mechanism_t *, crypto_key_t *,
    kcf_mech_entry_t **, crypto_mech_type_t *,
    crypto_mech_type_t *, int *, kcf_prov_tried_t *,
    crypto_func_group_t, crypto_func_group_t, size_t);
extern crypto_ctx_t *kcf_new_ctx(crypto_call_req_t  *, kcf_provider_desc_t *,
    crypto_session_id_t);
extern int kcf_submit_request(kcf_provider_desc_t *, crypto_ctx_t *,
    crypto_call_req_t *, kcf_req_params_t *, boolean_t);
extern void kcf_sched_init(void);
extern void kcf_sched_start(void);
extern void kcf_sop_done(kcf_sreq_node_t *, int);
extern void kcf_aop_done(kcf_areq_node_t *, int);
extern int common_submit_request(kcf_provider_desc_t *,
    crypto_ctx_t *, kcf_req_params_t *, crypto_req_handle_t);
extern void kcf_free_context(kcf_context_t *);

extern struct modctl *kcf_get_modctl(crypto_provider_info_t *);
extern void kcf_free_req(kcf_areq_node_t *areq);
extern void crypto_bufcall_service(void);

extern void kcf_walk_ntfylist(uint32_t, void *);
extern void kcf_do_notify(kcf_provider_desc_t *, boolean_t);

extern kcf_dual_req_t *kcf_alloc_req(crypto_call_req_t *);
extern void kcf_next_req(void *, int);
extern void kcf_last_req(void *, int);

#ifdef __cplusplus
}
#endif

#endif /* _SYS_CRYPTO_SCHED_IMPL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYS_CRYPTO_SPI_H
#define	_SYS_CRYPTO_SPI_H

/*
 * CSPI: Cryptographic Service Provider Interface.
 */

#include <sys/types.h>
#include <sys/dditypes.h>
#include <sys/ddi.h>
#include <sys/kmem.h>
#include <sys/crypto/common.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef	_KERNEL

#define	CRYPTO_SPI_VERSION_1	1
#define	CRYPTO_SPI_VERSION_2	2
#define	CRYPTO_SPI_VERSION_3	3
#define	CRYPTO_SPI_VERSION_4	4

/*
 * Provider-private handle. This handle is specified by a provider
 * when it registers by means of the pi_provider_handle field of
 * the crypto_provider_info structure, and passed to the provider
 * when its entry points are invoked.
 */
typedef void *crypto_provider_handle_t;

/*
 * Context templates can be used to by software providers to pre-process
 * keying material, such as key schedules. They are allocated by
 * a software provider create_ctx_template(9E) entry point, and passed
 * as argument to initialization and atomic provider entry points.
 */
typedef void *crypto_spi_ctx_template_t;

/*
 * Request handles are used by the kernel to identify an asynchronous
 * request being processed by a provider. It is passed by the kernel
 * to a hardware provider when submitting a request, and must be
 * specified by a provider when calling crypto_op_notification(9F)
 */
typedef void *crypto_req_handle_t;

/* Values for cc_flags field */
#define	CRYPTO_INIT_OPSTATE	0x00000001 /* allocate and init cc_opstate */
#define	CRYPTO_USE_OPSTATE	0x00000002 /* .. start using it as context */

/*
 * The context structure is passed from the kernel to a provider.
 * It contains the information needed to process a multi-part or
 * single part operation. The context structure is not used
 * by atomic operations.
 *
 * Parameters needed to perform a cryptographic operation, such
 * as keys, mechanisms, input and output buffers, are passed
 * as separate arguments to Provider routines.
 */
typedef struct crypto_ctx {
	crypto_provider_handle_t cc_provider;
	crypto_session_id_t	cc_session;
	void			*cc_provider_private;	/* owned by provider */
	void			*cc_framework_private;	/* owned by framework */
	uint32_t		cc_flags;		/* flags */
	void			*cc_opstate;		/* state */
} crypto_ctx_t;

/*
 * Extended provider information.
 */

/*
 * valid values for ei_flags field of extended info structure
 * They match the RSA Security, Inc PKCS#11 tokenInfo flags.
 */
#define	CRYPTO_EXTF_RNG					0x00000001
#define	CRYPTO_EXTF_WRITE_PROTECTED			0x00000002
#define	CRYPTO_EXTF_LOGIN_REQUIRED			0x00000004
#define	CRYPTO_EXTF_USER_PIN_INITIALIZED		0x00000008
#define	CRYPTO_EXTF_CLOCK_ON_TOKEN			0x00000040
#define	CRYPTO_EXTF_PROTECTED_AUTHENTICATION_PATH	0x00000100
#define	CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS		0x00000200
#define	CRYPTO_EXTF_TOKEN_INITIALIZED			0x00000400
#define	CRYPTO_EXTF_USER_PIN_COUNT_LOW			0x00010000
#define	CRYPTO_EXTF_USER_PIN_FINAL_TRY			0x00020000
#define	CRYPTO_EXTF_USER_PIN_LOCKED			0x00040000
#define	CRYPTO_EXTF_USER_PIN_TO_BE_CHANGED		0x00080000
#define	CRYPTO_EXTF_SO_PIN_COUNT_LOW			0x00100000
#define	CRYPTO_EXTF_SO_PIN_FINAL_TRY			0x00200000
#define	CRYPTO_EXTF_SO_PIN_LOCKED			0x00400000
#define	CRYPTO_EXTF_SO_PIN_TO_BE_CHANGED		0x00800000

/*
 * The crypto_control_ops structure contains pointers to control
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_control_ops {
	void (*provider_status)(crypto_provider_handle_t, uint_t *);
} crypto_control_ops_t;

/*
 * The crypto_ctx_ops structure contains points to context and context
 * templates management operations for cryptographic providers. It is
 * passed through the crypto_ops(9S) structure when providers register
 * with the kernel using crypto_register_provider(9F).
 */
typedef struct crypto_ctx_ops {
	int (*create_ctx_template)(crypto_provider_handle_t,
	    crypto_mechanism_t *, crypto_key_t *,
	    crypto_spi_ctx_template_t *, size_t *, crypto_req_handle_t);
	int (*free_context)(crypto_ctx_t *);
} crypto_ctx_ops_t;

/*
 * The crypto_digest_ops structure contains pointers to digest
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_digest_ops {
	int (*digest_init)(crypto_ctx_t *, crypto_mechanism_t *,
	    crypto_req_handle_t);
	int (*digest)(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
	    crypto_req_handle_t);
	int (*digest_update)(crypto_ctx_t *, crypto_data_t *,
	    crypto_req_handle_t);
	int (*digest_key)(crypto_ctx_t *, crypto_key_t *, crypto_req_handle_t);
	int (*digest_final)(crypto_ctx_t *, crypto_data_t *,
	    crypto_req_handle_t);
	int (*digest_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_data_t *,
	    crypto_data_t *, crypto_req_handle_t);
} crypto_digest_ops_t;

/*
 * The crypto_cipher_ops structure contains pointers to encryption
 * and decryption operations for cryptographic providers.  It is
 * passed through the crypto_ops(9S) structure when providers register
 * with the kernel using crypto_register_provider(9F).
 */
typedef struct crypto_cipher_ops {
	int (*encrypt_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*encrypt)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*encrypt_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*encrypt_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*encrypt_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);

	int (*decrypt_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*decrypt)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*decrypt_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*decrypt_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*decrypt_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);
} crypto_cipher_ops_t;

/*
 * The crypto_mac_ops structure contains pointers to MAC
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_mac_ops {
	int (*mac_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*mac)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*mac_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*mac_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*mac_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*mac_verify_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
} crypto_mac_ops_t;

/*
 * The crypto_sign_ops structure contains pointers to signing
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_sign_ops {
	int (*sign_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*sign)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*sign_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*sign_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*sign_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*sign_recover_init)(crypto_ctx_t *, crypto_mechanism_t *,
	    crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*sign_recover)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*sign_recover_atomic)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
	    crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
} crypto_sign_ops_t;

/*
 * The crypto_verify_ops structure contains pointers to verify
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_verify_ops {
	int (*verify_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*verify)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*verify_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*verify_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*verify_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*verify_recover_init)(crypto_ctx_t *, crypto_mechanism_t *,
	    crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
	int (*verify_recover)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*verify_recover_atomic)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
	    crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_req_handle_t);
} crypto_verify_ops_t;

/*
 * The crypto_dual_ops structure contains pointers to dual
 * cipher and sign/verify operations for cryptographic providers.
 * It is passed through the crypto_ops(9S) structure when
 * providers register with the kernel using
 * crypto_register_provider(9F).
 */
typedef struct crypto_dual_ops {
	int (*digest_encrypt_update)(
	    crypto_ctx_t *, crypto_ctx_t *, crypto_data_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*decrypt_digest_update)(
	    crypto_ctx_t *, crypto_ctx_t *, crypto_data_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*sign_encrypt_update)(
	    crypto_ctx_t *, crypto_ctx_t *, crypto_data_t *,
	    crypto_data_t *, crypto_req_handle_t);
	int (*decrypt_verify_update)(
	    crypto_ctx_t *, crypto_ctx_t *, crypto_data_t *,
	    crypto_data_t *, crypto_req_handle_t);
} crypto_dual_ops_t;

/*
 * The crypto_dual_cipher_mac_ops structure contains pointers to dual
 * cipher and MAC operations for cryptographic providers.
 * It is passed through the crypto_ops(9S) structure when
 * providers register with the kernel using
 * crypto_register_provider(9F).
 */
typedef struct crypto_dual_cipher_mac_ops {
	int (*encrypt_mac_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_mechanism_t *,
	    crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*encrypt_mac)(crypto_ctx_t *,
	    crypto_data_t *, crypto_dual_data_t *, crypto_data_t *,
	    crypto_req_handle_t);
	int (*encrypt_mac_update)(crypto_ctx_t *,
	    crypto_data_t *, crypto_dual_data_t *, crypto_req_handle_t);
	int (*encrypt_mac_final)(crypto_ctx_t *,
	    crypto_dual_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*encrypt_mac_atomic)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_mechanism_t *,
	    crypto_key_t *, crypto_data_t *, crypto_dual_data_t *,
	    crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);

	int (*mac_decrypt_init)(crypto_ctx_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_mechanism_t *,
	    crypto_key_t *, crypto_spi_ctx_template_t,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*mac_decrypt)(crypto_ctx_t *,
	    crypto_dual_data_t *, crypto_data_t *, crypto_data_t *,
	    crypto_req_handle_t);
	int (*mac_decrypt_update)(crypto_ctx_t *,
	    crypto_dual_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*mac_decrypt_final)(crypto_ctx_t *,
	    crypto_data_t *, crypto_data_t *, crypto_req_handle_t);
	int (*mac_decrypt_atomic)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_dual_data_t *,
	    crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
	int (*mac_verify_decrypt_atomic)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
	    crypto_mechanism_t *, crypto_key_t *, crypto_dual_data_t *,
	    crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t,
	    crypto_spi_ctx_template_t, crypto_req_handle_t);
} crypto_dual_cipher_mac_ops_t;

/*
 * The crypto_random_number_ops structure contains pointers to random
 * number operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_random_number_ops {
	int (*seed_random)(crypto_provider_handle_t, crypto_session_id_t,
	    uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t);
	int (*generate_random)(crypto_provider_handle_t, crypto_session_id_t,
	    uchar_t *, size_t, crypto_req_handle_t);
} crypto_random_number_ops_t;

/*
 * Flag values for seed_random.
 */
#define	CRYPTO_SEED_NOW		0x00000001

/*
 * The crypto_session_ops structure contains pointers to session
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_session_ops {
	int (*session_open)(crypto_provider_handle_t, crypto_session_id_t *,
	    crypto_req_handle_t);
	int (*session_close)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_req_handle_t);
	int (*session_login)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_user_type_t, char *, size_t, crypto_req_handle_t);
	int (*session_logout)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_req_handle_t);
} crypto_session_ops_t;

/*
 * The crypto_object_ops structure contains pointers to object
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_object_ops {
	int (*object_create)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
	    crypto_req_handle_t);
	int (*object_copy)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_object_id_t, crypto_object_attribute_t *, uint_t,
	    crypto_object_id_t *, crypto_req_handle_t);
	int (*object_destroy)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_object_id_t, crypto_req_handle_t);
	int (*object_get_size)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_object_id_t, size_t *, crypto_req_handle_t);
	int (*object_get_attribute_value)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_object_id_t,
	    crypto_object_attribute_t *, uint_t, crypto_req_handle_t);
	int (*object_set_attribute_value)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_object_id_t,
	    crypto_object_attribute_t *,  uint_t, crypto_req_handle_t);
	int (*object_find_init)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_object_attribute_t *, uint_t, void **,
	    crypto_req_handle_t);
	int (*object_find)(crypto_provider_handle_t, void *,
	    crypto_object_id_t *, uint_t, uint_t *, crypto_req_handle_t);
	int (*object_find_final)(crypto_provider_handle_t, void *,
	    crypto_req_handle_t);
} crypto_object_ops_t;

/*
 * The crypto_key_ops structure contains pointers to key
 * operations for cryptographic providers.  It is passed through
 * the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_key_ops {
	int (*key_generate)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
	    crypto_object_id_t *, crypto_req_handle_t);
	int (*key_generate_pair)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
	    crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
	    crypto_object_id_t *, crypto_req_handle_t);
	int (*key_wrap)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_object_id_t *,
	    uchar_t *, size_t *, crypto_req_handle_t);
	int (*key_unwrap)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, uchar_t *, size_t *,
	    crypto_object_attribute_t *, uint_t,
	    crypto_object_id_t *, crypto_req_handle_t);
	int (*key_derive)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_object_attribute_t *,
	    uint_t, crypto_object_id_t *, crypto_req_handle_t);
	int (*key_check)(crypto_provider_handle_t, crypto_mechanism_t *,
	    crypto_key_t *);
} crypto_key_ops_t;

/*
 * The crypto_provider_management_ops structure contains pointers
 * to management operations for cryptographic providers.  It is passed
 * through the crypto_ops(9S) structure when providers register with the
 * kernel using crypto_register_provider(9F).
 */
typedef struct crypto_provider_management_ops {
	int (*ext_info)(crypto_provider_handle_t,
	    crypto_provider_ext_info_t *, crypto_req_handle_t);
	int (*init_token)(crypto_provider_handle_t, char *, size_t,
	    char *, crypto_req_handle_t);
	int (*init_pin)(crypto_provider_handle_t, crypto_session_id_t,
	    char *, size_t, crypto_req_handle_t);
	int (*set_pin)(crypto_provider_handle_t, crypto_session_id_t,
	    char *, size_t, char *, size_t, crypto_req_handle_t);
} crypto_provider_management_ops_t;

typedef struct crypto_mech_ops {
	int (*copyin_mechanism)(crypto_provider_handle_t,
	    crypto_mechanism_t *, crypto_mechanism_t *, int *, int);
	int (*copyout_mechanism)(crypto_provider_handle_t,
	    crypto_mechanism_t *, crypto_mechanism_t *, int *, int);
	int (*free_mechanism)(crypto_provider_handle_t, crypto_mechanism_t *);
} crypto_mech_ops_t;

typedef struct crypto_nostore_key_ops {
	int (*nostore_key_generate)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *,
	    crypto_object_attribute_t *, uint_t, crypto_object_attribute_t *,
	    uint_t, crypto_req_handle_t);
	int (*nostore_key_generate_pair)(crypto_provider_handle_t,
	    crypto_session_id_t, crypto_mechanism_t *,
	    crypto_object_attribute_t *, uint_t, crypto_object_attribute_t *,
	    uint_t, crypto_object_attribute_t *, uint_t,
	    crypto_object_attribute_t *, uint_t, crypto_req_handle_t);
	int (*nostore_key_derive)(crypto_provider_handle_t, crypto_session_id_t,
	    crypto_mechanism_t *, crypto_key_t *, crypto_object_attribute_t *,
	    uint_t, crypto_object_attribute_t *, uint_t, crypto_req_handle_t);
} crypto_nostore_key_ops_t;

/*
 * crypto_fips140_ops provides a function for FIPS 140 Power-On Self Test for
 * those providers that are part of the Cryptographic Framework bounday.  See
 * crypto_fips140_ops(9s) for details.
 */
typedef struct crypto_fips140_ops {
	void (*fips140_post)(int *);
} crypto_fips140_ops_t;

/*
 * The crypto_ops(9S) structure contains the structures containing
 * the pointers to functions implemented by cryptographic providers.
 * It is specified as part of the crypto_provider_info(9S)
 * supplied by a provider when it registers with the kernel
 * by calling crypto_register_provider(9F).
 */
typedef struct crypto_ops_v1 {
	crypto_control_ops_t			*co_control_ops;
	crypto_digest_ops_t			*co_digest_ops;
	crypto_cipher_ops_t			*co_cipher_ops;
	crypto_mac_ops_t			*co_mac_ops;
	crypto_sign_ops_t			*co_sign_ops;
	crypto_verify_ops_t			*co_verify_ops;
	crypto_dual_ops_t			*co_dual_ops;
	crypto_dual_cipher_mac_ops_t		*co_dual_cipher_mac_ops;
	crypto_random_number_ops_t		*co_random_ops;
	crypto_session_ops_t			*co_session_ops;
	crypto_object_ops_t			*co_object_ops;
	crypto_key_ops_t			*co_key_ops;
	crypto_provider_management_ops_t	*co_provider_ops;
	crypto_ctx_ops_t			*co_ctx_ops;
} crypto_ops_v1_t;

typedef struct crypto_ops_v2 {
	crypto_ops_v1_t				v1_ops;
	crypto_mech_ops_t			*co_mech_ops;
} crypto_ops_v2_t;

typedef struct crypto_ops_v3 {
	crypto_ops_v2_t				v2_ops;
	crypto_nostore_key_ops_t		*co_nostore_key_ops;
} crypto_ops_v3_t;

typedef struct crypto_ops_v4 {
	crypto_ops_v3_t				v3_ops;
	crypto_fips140_ops_t			*co_fips140_ops;
} crypto_ops_v4_t;

typedef struct crypto_ops {
	union {
		crypto_ops_v4_t	cou_v4;
		crypto_ops_v3_t	cou_v3;
		crypto_ops_v2_t	cou_v2;
		crypto_ops_v1_t	cou_v1;
	} cou;
} crypto_ops_t;

#define	co_control_ops			cou.cou_v1.co_control_ops
#define	co_digest_ops			cou.cou_v1.co_digest_ops
#define	co_cipher_ops			cou.cou_v1.co_cipher_ops
#define	co_mac_ops			cou.cou_v1.co_mac_ops
#define	co_sign_ops			cou.cou_v1.co_sign_ops
#define	co_verify_ops			cou.cou_v1.co_verify_ops
#define	co_dual_ops			cou.cou_v1.co_dual_ops
#define	co_dual_cipher_mac_ops		cou.cou_v1.co_dual_cipher_mac_ops
#define	co_random_ops			cou.cou_v1.co_random_ops
#define	co_session_ops			cou.cou_v1.co_session_ops
#define	co_object_ops			cou.cou_v1.co_object_ops
#define	co_key_ops			cou.cou_v1.co_key_ops
#define	co_provider_ops			cou.cou_v1.co_provider_ops
#define	co_ctx_ops			cou.cou_v1.co_ctx_ops
#define	co_mech_ops			cou.cou_v2.co_mech_ops
#define	co_nostore_key_ops		cou.cou_v3.co_nostore_key_ops
#define	co_fips140_ops			cou.cou_v4.co_fips140_ops

/*
 * Provider device specification passed during registration.
 *
 * Software providers set the pi_provider_type field of provider_info_t
 * to CRYPTO_SW_PROVIDER, and set the pd_sw field of
 * crypto_provider_dev_t to the address of their modlinkage.
 *
 * Hardware providers set the pi_provider_type field of provider_info_t
 * to CRYPTO_HW_PROVIDER, and set the pd_hw field of
 * crypto_provider_dev_t to the dev_info structure corresponding
 * to the device instance being registered.
 *
 * Logical providers set the pi_provider_type field of provider_info_t
 * to CRYPTO_LOGICAL_PROVIDER, and set the pd_hw field of
 * crypto_provider_dev_t to the dev_info structure corresponding
 * to the device instance being registered.
 */

typedef union crypto_provider_dev {
	struct modlinkage	*pd_sw; /* for CRYPTO_SW_PROVIDER */
	dev_info_t		*pd_hw; /* for CRYPTO_HW_PROVIDER */
} crypto_provider_dev_t;

#endif /* _KERNEL */

/*
 * The mechanism info structure crypto_mech_info_t contains a function group
 * bit mask cm_func_group_mask. This field, of type crypto_func_group_t,
 * specifies the provider entry point that can be used a particular
 * mechanism. The function group mask is a combination of the following values.
 */

typedef uint32_t crypto_func_group_t;

#define	CRYPTO_FG_ENCRYPT		0x00000001 /* encrypt_init() */
#define	CRYPTO_FG_DECRYPT		0x00000002 /* decrypt_init() */
#define	CRYPTO_FG_DIGEST		0x00000004 /* digest_init() */
#define	CRYPTO_FG_SIGN			0x00000008 /* sign_init() */
#define	CRYPTO_FG_SIGN_RECOVER		0x00000010 /* sign_recover_init() */
#define	CRYPTO_FG_VERIFY		0x00000020 /* verify_init() */
#define	CRYPTO_FG_VERIFY_RECOVER	0x00000040 /* verify_recover_init() */
#define	CRYPTO_FG_GENERATE		0x00000080 /* key_generate() */
#define	CRYPTO_FG_GENERATE_KEY_PAIR	0x00000100 /* key_generate_pair() */
#define	CRYPTO_FG_WRAP			0x00000200 /* key_wrap() */
#define	CRYPTO_FG_UNWRAP		0x00000400 /* key_unwrap() */
#define	CRYPTO_FG_DERIVE		0x00000800 /* key_derive() */
#define	CRYPTO_FG_MAC			0x00001000 /* mac_init() */
#define	CRYPTO_FG_ENCRYPT_MAC		0x00002000 /* encrypt_mac_init() */
#define	CRYPTO_FG_MAC_DECRYPT		0x00004000 /* decrypt_mac_init() */
#define	CRYPTO_FG_ENCRYPT_ATOMIC	0x00008000 /* encrypt_atomic() */
#define	CRYPTO_FG_DECRYPT_ATOMIC	0x00010000 /* decrypt_atomic() */
#define	CRYPTO_FG_MAC_ATOMIC		0x00020000 /* mac_atomic() */
#define	CRYPTO_FG_DIGEST_ATOMIC		0x00040000 /* digest_atomic() */
#define	CRYPTO_FG_SIGN_ATOMIC		0x00080000 /* sign_atomic() */
#define	CRYPTO_FG_SIGN_RECOVER_ATOMIC   0x00100000 /* sign_recover_atomic() */
#define	CRYPTO_FG_VERIFY_ATOMIC		0x00200000 /* verify_atomic() */
#define	CRYPTO_FG_VERIFY_RECOVER_ATOMIC	0x00400000 /* verify_recover_atomic() */
#define	CRYPTO_FG_ENCRYPT_MAC_ATOMIC	0x00800000 /* encrypt_mac_atomic() */
#define	CRYPTO_FG_MAC_DECRYPT_ATOMIC	0x01000000 /* mac_decrypt_atomic() */
#define	CRYPTO_FG_RESERVED		0x80000000

/*
 * Maximum length of the pi_provider_description field of the
 * crypto_provider_info structure.
 */
#define	CRYPTO_PROVIDER_DESCR_MAX_LEN	64

#ifdef _KERNEL

/* Bit mask for all the simple operations */
#define	CRYPTO_FG_SIMPLEOP_MASK	(CRYPTO_FG_ENCRYPT | CRYPTO_FG_DECRYPT | \
    CRYPTO_FG_DIGEST | CRYPTO_FG_SIGN | CRYPTO_FG_VERIFY | CRYPTO_FG_MAC | \
    CRYPTO_FG_ENCRYPT_ATOMIC | CRYPTO_FG_DECRYPT_ATOMIC |		\
    CRYPTO_FG_MAC_ATOMIC | CRYPTO_FG_DIGEST_ATOMIC | CRYPTO_FG_SIGN_ATOMIC | \
    CRYPTO_FG_VERIFY_ATOMIC)

/* Bit mask for all the dual operations */
#define	CRYPTO_FG_MAC_CIPHER_MASK	(CRYPTO_FG_ENCRYPT_MAC |	\
    CRYPTO_FG_MAC_DECRYPT | CRYPTO_FG_ENCRYPT_MAC_ATOMIC |		\
    CRYPTO_FG_MAC_DECRYPT_ATOMIC)

/* Add other combos to CRYPTO_FG_DUAL_MASK */
#define	CRYPTO_FG_DUAL_MASK	CRYPTO_FG_MAC_CIPHER_MASK

/*
 * The crypto_mech_info structure specifies one of the mechanisms
 * supported by a cryptographic provider. The pi_mechanisms field of
 * the crypto_provider_info structure contains a pointer to an array
 * of crypto_mech_info's.
 */
typedef struct crypto_mech_info {
	crypto_mech_name_t	cm_mech_name;
	crypto_mech_type_t	cm_mech_number;
	crypto_func_group_t	cm_func_group_mask;
	ssize_t			cm_min_key_length;
	ssize_t			cm_max_key_length;
	uint32_t		cm_mech_flags;
} crypto_mech_info_t;

/* Alias the old name to the new name for compatibility. */
#define	cm_keysize_unit	cm_mech_flags

/*
 * crypto_kcf_provider_handle_t is a handle allocated by the kernel.
 * It is returned after the provider registers with
 * crypto_register_provider(), and must be specified by the provider
 * when calling crypto_unregister_provider(), and
 * crypto_provider_notification().
 */
typedef uint_t crypto_kcf_provider_handle_t;

/*
 * Provider information. Passed as argument to crypto_register_provider(9F).
 * Describes the provider and its capabilities. Multiple providers can
 * register for the same device instance. In this case, the same
 * pi_provider_dev must be specified with a different pi_provider_handle.
 */
typedef struct crypto_provider_info_v1 {
	uint_t				pi_interface_version;
	char				*pi_provider_description;
	crypto_provider_type_t		pi_provider_type;
	crypto_provider_dev_t		pi_provider_dev;
	crypto_provider_handle_t	pi_provider_handle;
	crypto_ops_t			*pi_ops_vector;
	uint_t				pi_mech_list_count;
	crypto_mech_info_t		*pi_mechanisms;
	uint_t				pi_logical_provider_count;
	crypto_kcf_provider_handle_t	*pi_logical_providers;
} crypto_provider_info_v1_t;

typedef struct crypto_provider_info_v2 {
	crypto_provider_info_v1_t	v1_info;
	uint_t				pi_flags;
} crypto_provider_info_v2_t;

typedef struct crypto_provider_info {
	union {
		crypto_provider_info_v2_t piu_v2;
		crypto_provider_info_v1_t piu_v1;
	} piu;
} crypto_provider_info_t;

#define	pi_interface_version		piu.piu_v1.pi_interface_version
#define	pi_provider_description		piu.piu_v1.pi_provider_description
#define	pi_provider_type		piu.piu_v1.pi_provider_type
#define	pi_provider_dev			piu.piu_v1.pi_provider_dev
#define	pi_provider_handle		piu.piu_v1.pi_provider_handle
#define	pi_ops_vector			piu.piu_v1.pi_ops_vector
#define	pi_mech_list_count		piu.piu_v1.pi_mech_list_count
#define	pi_mechanisms			piu.piu_v1.pi_mechanisms
#define	pi_logical_provider_count	piu.piu_v1.pi_logical_provider_count
#define	pi_logical_providers		piu.piu_v1.pi_logical_providers
#define	pi_flags			piu.piu_v2.pi_flags

/* hidden providers can only be accessed via a logical provider */
#define	CRYPTO_HIDE_PROVIDER		0x00000001
/*
 * provider can not do multi-part digest (updates) and has a limit
 * on maximum input data that it can digest. The provider sets
 * this value in crypto_provider_ext_info_t by implementing
 * the ext_info entry point in the co_provider_ops vector.
 */
#define	CRYPTO_HASH_NO_UPDATE		0x00000002
/*
 * provider can not do multi-part HMAC (updates) and has a limit
 * on maximum input data that it can hmac. The provider sets
 * this value in crypto_provider_ext_info_t by implementing
 * the ext_info entry point in the co_provider_ops vector.
 */
#define	CRYPTO_HMAC_NO_UPDATE		0x00000008

/* provider can handle the request without returning a CRYPTO_QUEUED */
#define	CRYPTO_SYNCHRONOUS		0x00000004

#define	CRYPTO_PIFLAGS_RESERVED2	0x40000000
#define	CRYPTO_PIFLAGS_RESERVED1	0x80000000

/*
 * Provider status passed by a provider to crypto_provider_notification(9F)
 * and returned by the provider_status(9E) entry point.
 */
#define	CRYPTO_PROVIDER_READY		0
#define	CRYPTO_PROVIDER_BUSY		1
#define	CRYPTO_PROVIDER_FAILED		2

/*
 * Functions exported by Solaris to cryptographic providers. Providers
 * call these functions to register and unregister, notify the kernel
 * of state changes, and notify the kernel when a asynchronous request
 * completed.
 */
extern int crypto_register_provider(crypto_provider_info_t *,
		crypto_kcf_provider_handle_t *);
extern int crypto_unregister_provider(crypto_kcf_provider_handle_t);
extern void crypto_provider_notification(crypto_kcf_provider_handle_t, uint_t);
extern void crypto_op_notification(crypto_req_handle_t, int);
extern int crypto_kmflag(crypto_req_handle_t);

#endif	/* _KERNEL */

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_CRYPTO_SPI_H */